All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gnome.glib.MainContext Maven / Gradle / Ivy

// Java-GI - Java language bindings for GObject-Introspection-based libraries
// Copyright (C) 2022-2024 Jan-Willem Harmannij
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see .
//
// This file has been generated with Java-GI.
// Do not edit this file directly!
// Visit  for more information.
//
package org.gnome.glib;

import io.github.jwharm.javagi.base.ManagedInstance;
import io.github.jwharm.javagi.base.Out;
import io.github.jwharm.javagi.interop.Interop;
import io.github.jwharm.javagi.interop.MemoryCleaner;
import java.lang.Deprecated;
import java.lang.Integer;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import javax.annotation.processing.Generated;
import org.jetbrains.annotations.Nullable;

/**
 * The {@code GMainContext} struct is an opaque data
 * type representing a set of sources to be handled in a main loop.
 */
@Generated("io.github.jwharm.JavaGI")
public class MainContext extends ManagedInstance {
    static {
        GLib.javagi$ensureInitialized();
    }

    /**
     * Create a MainContext proxy instance for the provided memory address.
     *
     * @param address the memory address of the native object
     */
    public MainContext(MemorySegment address) {
        super(address);
    }

    /**
     * Creates a new {@code GMainContext} structure.
     */
    public MainContext() {
        super(constructNew());
    }

    /**
     * Get the GType of the MainContext class
     *
     * @return the GType
     */
    public static Type getType() {
        return Interop.getType("g_main_context_get_type");
    }

    /**
     * Creates a new {@code GMainContext} structure.
     */
    private static MemorySegment constructNew() {
        MemorySegment _result;
        try {
            FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS);
            _result = (MemorySegment) Interop.downcallHandle("g_main_context_new", _fdesc, false)
                    .invokeExact();
        } catch (Throwable _err) {
            throw new AssertionError("Unexpected exception occurred: ", _err);
        }
        return _result;
    }

    /**
     * Creates a new {@code GMainContext} structure.
     * @param flags a bitwise-OR combination of {@code GMainContextFlags} flags that can only be
     *         set at creation time.
     * @return the new {@code GMainContext}
     */
    public static MainContext withFlags(MainContextFlags flags) {
        var _result = constructWithFlags(flags);
        var _instance = MemorySegment.NULL.equals(_result) ? null : new MainContext(_result);
        if (_instance != null) {
            MemoryCleaner.takeOwnership(_instance.handle());
            MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free");
            MemoryCleaner.setBoxedType(_instance.handle(), MainContext.getType());
        }
        return (MainContext) _instance;
    }

    /**
     * Creates a new {@code GMainContext} structure.
     * @param flags a bitwise-OR combination of {@code GMainContextFlags} flags that can only be
     *         set at creation time.
     * @return the new {@code GMainContext}
     */
    private static MemorySegment constructWithFlags(MainContextFlags flags) {
        MemorySegment _result;
        try {
            FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS,
                    ValueLayout.JAVA_INT);
            _result = (MemorySegment) Interop.downcallHandle("g_main_context_new_with_flags", _fdesc, false)
                    .invokeExact(flags.getValue());
        } catch (Throwable _err) {
            throw new AssertionError("Unexpected exception occurred: ", _err);
        }
        return _result;
    }

    /**
     * Returns the global-default main context. This is the main context
     * used for main loop functions when a main loop is not explicitly
     * specified, and corresponds to the "main" main loop. See also
     * g_main_context_get_thread_default().
     * @return the global-default main context.
     */
    public static MainContext default_() {
        MemorySegment _result;
        try {
            FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS);
            _result = (MemorySegment) Interop.downcallHandle("g_main_context_default", _fdesc, false)
                    .invokeExact();
        } catch (Throwable _err) {
            throw new AssertionError("Unexpected exception occurred: ", _err);
        }
        var _instance = MemorySegment.NULL.equals(_result) ? null : new MainContext(_result);
        if (_instance != null) {
            MemoryCleaner.takeOwnership(_instance.handle());
            MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free");
            MemoryCleaner.setBoxedType(_instance.handle(), MainContext.getType());
        }
        return _instance;
    }

    /**
     * Gets the thread-default {@code GMainContext} for this thread. Asynchronous
     * operations that want to be able to be run in contexts other than
     * the default one should call this method or
     * g_main_context_ref_thread_default() to get a {@code GMainContext} to add
     * their {@code GSources} to. (Note that even in single-threaded
     * programs applications may sometimes want to temporarily push a
     * non-default context, so it is not safe to assume that this will
     * always return {@code null} if you are running in the default thread.)
     * 

* If you need to hold a reference on the context, use * g_main_context_ref_thread_default() instead. * @return the thread-default {@code GMainContext}, or * {@code null} if the thread-default context is the global-default main context. */ public static MainContext getThreadDefault() { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_get_thread_default", _fdesc, false) .invokeExact(); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new MainContext(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), MainContext.getType()); } return _instance; } /** * Gets the thread-default {@code GMainContext} for this thread, as with * g_main_context_get_thread_default(), but also adds a reference to * it with g_main_context_ref(). In addition, unlike * g_main_context_get_thread_default(), if the thread-default context * is the global-default context, this will return that {@code GMainContext} * (with a ref added to it) rather than returning {@code null}. * @return the thread-default {@code GMainContext}. Unref * with g_main_context_unref() when you are done with it. */ public static MainContext refThreadDefault() { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_ref_thread_default", _fdesc, false) .invokeExact(); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new MainContext(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), MainContext.getType()); } return _instance; } /** * Tries to become the owner of the specified context. * If some other thread is the owner of the context, * returns {@code false} immediately. Ownership is properly * recursive: the owner can require ownership again * and will release ownership when g_main_context_release() * is called as many times as g_main_context_acquire(). *

* You must be the owner of a context before you * can call g_main_context_prepare(), g_main_context_query(), * g_main_context_check(), g_main_context_dispatch(), g_main_context_release(). *

* Since 2.76 {@code context} can be {@code null} to use the global-default * main context. * @return {@code true} if the operation succeeded, and * this thread is now the owner of {@code context}. */ public boolean acquire() { int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS); _result = (int) Interop.downcallHandle("g_main_context_acquire", _fdesc, false) .invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return _result != 0; } /** * Adds a file descriptor to the set of file descriptors polled for * this context. This will very seldom be used directly. Instead * a typical event source will use g_source_add_unix_fd() instead. * @param fd a {@code GPollFD} structure holding information about a file * descriptor to watch. * @param priority the priority for this file descriptor which should be * the same as the priority used for g_source_attach() to ensure that the * file descriptor is polled whenever the results may be needed. */ public void addPoll(PollFD fd, int priority) { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT); Interop.downcallHandle("g_main_context_add_poll", _fdesc, false).invokeExact(handle(), (MemorySegment) (fd == null ? MemorySegment.NULL : fd.handle()), priority); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Dispatches all pending sources. *

* You must have successfully acquired the context with * g_main_context_acquire() before you may call this function. *

* Since 2.76 {@code context} can be {@code null} to use the global-default * main context. */ public void dispatch() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_dispatch", _fdesc, false).invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Finds a source with the given source functions and user data. If * multiple sources exist with the same source function and user data, * the first one found will be returned. * @param funcs the {@code source_funcs} passed to g_source_new(). * @param userData the user data from the callback. * @return the source, if one was found, otherwise {@code null} */ public Source findSourceByFuncsUserData(SourceFuncs funcs, @Nullable MemorySegment userData) { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_find_source_by_funcs_user_data", _fdesc, false) .invokeExact(handle(), (MemorySegment) (funcs == null ? MemorySegment.NULL : funcs.handle()), (MemorySegment) (userData == null ? MemorySegment.NULL : userData)); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new Source(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), Source.getType()); } return _instance; } /** * Finds a {@code GSource} given a pair of context and ID. *

* It is a programmer error to attempt to look up a non-existent source. *

* More specifically: source IDs can be reissued after a source has been * destroyed and therefore it is never valid to use this function with a * source ID which may have already been removed. An example is when * scheduling an idle to run in another thread with g_idle_add(): the * idle may already have run and been removed by the time this function * is called on its (now invalid) source ID. This source ID may have * been reissued, leading to the operation being performed against the * wrong source. * @param sourceId the source ID, as returned by g_source_get_id(). * @return the {@code GSource} */ public Source findSourceById(int sourceId) { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.JAVA_INT); _result = (MemorySegment) Interop.downcallHandle("g_main_context_find_source_by_id", _fdesc, false) .invokeExact(handle(), sourceId); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new Source(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), Source.getType()); } return _instance; } /** * Finds a source with the given user data for the callback. If * multiple sources exist with the same user data, the first * one found will be returned. * @param userData the user_data for the callback. * @return the source, if one was found, otherwise {@code null} */ public Source findSourceByUserData(@Nullable MemorySegment userData) { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_find_source_by_user_data", _fdesc, false) .invokeExact(handle(), (MemorySegment) (userData == null ? MemorySegment.NULL : userData)); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new Source(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), Source.getType()); } return _instance; } /** * Gets the poll function set by g_main_context_set_poll_func(). * @return the poll function */ public PollFunc getPollFunc() { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_get_poll_func", _fdesc, false) .invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return null /* Unsupported parameter type */; } /** * Invokes a function in such a way that {@code context} is owned during the * invocation of {@code function}. *

* If {@code context} is {@code null} then the global-default main context — as * returned by g_main_context_default() — is used. *

* If {@code context} is owned by the current thread, {@code function} is called * directly. Otherwise, if {@code context} is the thread-default main context * of the current thread and g_main_context_acquire() succeeds, then * {@code function} is called and g_main_context_release() is called * afterwards. *

* In any other case, an idle source is created to call {@code function} and * that source is attached to {@code context} (presumably to be run in another * thread). The idle source is attached with {@code G_PRIORITY_DEFAULT} * priority. If you want a different priority, use * g_main_context_invoke_full(). *

* Note that, as with normal idle functions, {@code function} should probably * return {@code false}. If it returns {@code true}, it will be continuously run in a * loop (and may prevent this call from returning). * @param function function to call */ public void invoke(SourceFunc function) { try (var _arena = Arena.ofConfined()) { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_invoke", _fdesc, false).invokeExact( handle(), (MemorySegment) (function == null ? MemorySegment.NULL : function.toCallback(Interop.attachArena(Arena.ofConfined(), this))), MemorySegment.NULL); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } } /** * Invokes a function in such a way that {@code context} is owned during the * invocation of {@code function}. *

* This function is the same as g_main_context_invoke() except that it * lets you specify the priority in case {@code function} ends up being * scheduled as an idle and also lets you give a {@code GDestroyNotify} for {@code data}. *

* {@code notify} should not assume that it is called from any particular * thread or with any particular context acquired. * @param priority the priority at which to run {@code function} * @param function function to call */ public void invokeFull(int priority, SourceFunc function) { try (var _arena = Arena.ofConfined()) { final Arena _functionScope = Arena.ofConfined(); final DestroyNotify _functionDestroyNotify = $ -> _functionScope.close(); try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_invoke_full", _fdesc, false).invokeExact( handle(), priority, (MemorySegment) (function == null ? MemorySegment.NULL : function.toCallback(_functionScope)), MemorySegment.NULL, _functionDestroyNotify.toCallback(_functionScope)); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } } /** * Determines whether this thread holds the (recursive) * ownership of this {@code GMainContext}. This is useful to * know before waiting on another thread that may be * blocking to get ownership of {@code context}. * @return {@code true} if current thread is owner of {@code context}. */ public boolean isOwner() { int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS); _result = (int) Interop.downcallHandle("g_main_context_is_owner", _fdesc, false) .invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return _result != 0; } /** * Runs a single iteration for the given main loop. This involves * checking to see if any event sources are ready to be processed, * then if no events sources are ready and {@code may_block} is {@code true}, waiting * for a source to become ready, then dispatching the highest priority * events sources that are ready. Otherwise, if {@code may_block} is {@code false} * sources are not waited to become ready, only those highest priority * events sources will be dispatched (if any), that are ready at this * given moment without further waiting. *

* Note that even when {@code may_block} is {@code true}, it is still possible for * g_main_context_iteration() to return {@code false}, since the wait may * be interrupted for other reasons than an event source becoming ready. * @param mayBlock whether the call may block. * @return {@code true} if events were dispatched. */ public boolean iteration(boolean mayBlock) { int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_INT); _result = (int) Interop.downcallHandle("g_main_context_iteration", _fdesc, false) .invokeExact(handle(), mayBlock ? 1 : 0); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return _result != 0; } /** * Checks if any sources have pending events for the given context. * @return {@code true} if events are pending. */ public boolean pending() { int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS); _result = (int) Interop.downcallHandle("g_main_context_pending", _fdesc, false) .invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return _result != 0; } /** * Pops {@code context} off the thread-default context stack (verifying that * it was on the top of the stack). */ public void popThreadDefault() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_pop_thread_default", _fdesc, false).invokeExact( handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Prepares to poll sources within a main loop. The resulting information * for polling is determined by calling g_main_context_query (). *

* You must have successfully acquired the context with * g_main_context_acquire() before you may call this function. * @param priority location to store priority of highest priority * source already ready. * @return {@code true} if some source is ready to be dispatched * prior to polling. */ public boolean prepare(@Nullable Out priority) { try (var _arena = Arena.ofConfined()) { MemorySegment _priorityPointer = _arena.allocate(ValueLayout.JAVA_INT); int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (int) Interop.downcallHandle("g_main_context_prepare", _fdesc, false) .invokeExact(handle(), (MemorySegment) (priority == null ? MemorySegment.NULL : _priorityPointer)); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } if (priority != null) { priority.set(_priorityPointer.get(ValueLayout.JAVA_INT, 0)); } return _result != 0; } } /** * Acquires {@code context} and sets it as the thread-default context for the * current thread. This will cause certain asynchronous operations * (such as most [gio][gio]-based I/O) which are * started in this thread to run under {@code context} and deliver their * results to its main loop, rather than running under the global * default main context in the main thread. Note that calling this function * changes the context returned by g_main_context_get_thread_default(), * not the one returned by g_main_context_default(), so it does not affect * the context used by functions like g_idle_add(). *

* Normally you would call this function shortly after creating a new * thread, passing it a {@code GMainContext} which will be run by a * {@code GMainLoop} in that thread, to set a new default context for all * async operations in that thread. In this case you may not need to * ever call g_main_context_pop_thread_default(), assuming you want the * new {@code GMainContext} to be the default for the whole lifecycle of the * thread. *

* If you don't have control over how the new thread was created (e.g. * in the new thread isn't newly created, or if the thread life * cycle is managed by a {@code GThreadPool}), it is always suggested to wrap * the logic that needs to use the new {@code GMainContext} inside a * g_main_context_push_thread_default() / g_main_context_pop_thread_default() * pair, otherwise threads that are re-used will end up never explicitly * releasing the {@code GMainContext} reference they hold. *

* In some cases you may want to schedule a single operation in a * non-default context, or temporarily use a non-default context in * the main thread. In that case, you can wrap the call to the * asynchronous operation inside a * g_main_context_push_thread_default() / * g_main_context_pop_thread_default() pair, but it is up to you to * ensure that no other asynchronous operations accidentally get * started while the non-default context is active. *

* Beware that libraries that predate this function may not correctly * handle being used from a thread with a thread-default context. Eg, * see g_file_supports_thread_contexts(). */ public void pushThreadDefault() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_push_thread_default", _fdesc, false).invokeExact( handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Increases the reference count on a {@code GMainContext} object by one. * @return the {@code context} that was passed in (since 2.6) */ public MainContext ref() { MemorySegment _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (MemorySegment) Interop.downcallHandle("g_main_context_ref", _fdesc, false) .invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } var _instance = MemorySegment.NULL.equals(_result) ? null : new MainContext(_result); if (_instance != null) { MemoryCleaner.takeOwnership(_instance.handle()); MemoryCleaner.setFreeFunc(_instance.handle(), "g_boxed_free"); MemoryCleaner.setBoxedType(_instance.handle(), MainContext.getType()); } return _instance; } /** * Releases ownership of a context previously acquired by this thread * with g_main_context_acquire(). If the context was acquired multiple * times, the ownership will be released only when g_main_context_release() * is called as many times as it was acquired. *

* You must have successfully acquired the context with * g_main_context_acquire() before you may call this function. */ public void release() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_release", _fdesc, false).invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Removes file descriptor from the set of file descriptors to be * polled for a particular context. * @param fd a {@code GPollFD} descriptor previously added with g_main_context_add_poll() */ public void removePoll(PollFD fd) { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_remove_poll", _fdesc, false).invokeExact( handle(), (MemorySegment) (fd == null ? MemorySegment.NULL : fd.handle())); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Sets the function to use to handle polling of file descriptors. It * will be used instead of the poll() system call * (or GLib's replacement function, which is used where * poll() isn't available). *

* This function could possibly be used to integrate the GLib event * loop with an external event loop. * @param func the function to call to poll all file descriptors */ public void setPollFunc(PollFunc func) { try (var _arena = Arena.ofConfined()) { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_set_poll_func", _fdesc, false).invokeExact( handle(), (MemorySegment) (func == null ? MemorySegment.NULL : func.toCallback(Interop.attachArena(Arena.ofConfined(), this)))); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } } /** * Decreases the reference count on a {@code GMainContext} object by one. If * the result is zero, free the context and free all associated memory. */ public void unref() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_unref", _fdesc, false).invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } /** * Tries to become the owner of the specified context, * as with g_main_context_acquire(). But if another thread * is the owner, atomically drop {@code mutex} and wait on {@code cond} until * that owner releases ownership or until {@code cond} is signaled, then * try again (once) to become the owner. * @param cond a condition variable * @param mutex a mutex, currently held * @return {@code true} if the operation succeeded, and * this thread is now the owner of {@code context}. * @deprecated Use g_main_context_is_owner() and separate locking instead. */ @Deprecated public boolean wait_(Cond cond, Mutex mutex) { int _result; try { FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS); _result = (int) Interop.downcallHandle("g_main_context_wait", _fdesc, false) .invokeExact(handle(), (MemorySegment) (cond == null ? MemorySegment.NULL : cond.handle()), (MemorySegment) (mutex == null ? MemorySegment.NULL : mutex.handle())); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } return _result != 0; } /** * If {@code context} is currently blocking in g_main_context_iteration() * waiting for a source to become ready, cause it to stop blocking * and return. Otherwise, cause the next invocation of * g_main_context_iteration() to return without blocking. *

* This API is useful for low-level control over {@code GMainContext}; for * example, integrating it with main loop implementations such as * {@code GMainLoop}. *

* Another related use for this function is when implementing a main * loop with a termination condition, computed from multiple threads: *

{@code 
     *   #define NUM_TASKS 10
     *   static gint tasks_remaining = NUM_TASKS;  // (atomic)
     *   ...
     *  
     *   while (g_atomic_int_get (&tasks_remaining) != 0)
     *     g_main_context_iteration (NULL, TRUE);
     * }
* * Then in a thread: *
{@code 
     *   perform_work();
     *
     *   if (g_atomic_int_dec_and_test (&tasks_remaining))
     *     g_main_context_wakeup (NULL);
     * }
*/ public void wakeup() { try { FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); Interop.downcallHandle("g_main_context_wakeup", _fdesc, false).invokeExact(handle()); } catch (Throwable _err) { throw new AssertionError("Unexpected exception occurred: ", _err); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy