Runner

ceramic.Runner (Class)

Static Members

currentIsMainThread(): Bool

Checks if the current thread is the main thread.

This method is useful for determining execution context and ensuring thread-safe operations. On single-threaded platforms, this always returns true.

Returns Description
Bool true if executing on the main thread, false otherwise

isEmulatingBackgroundWithMain(): Bool

Checks if background execution is emulated on the current platform.

Some platforms (like JavaScript/Web) don't support true threading, so background execution is emulated using deferred callbacks on the main thread. This method helps code adapt to platform capabilities.

Returns Description
Bool true if background threads are emulated (JS/Web), false if real threads are available (C++/C#)

runInMain(_fn: Function): Void

Schedules a function to run on the main thread.

This method queues the given function for execution on the main thread. The function will be executed during the next tick() call. This is particularly useful for updating UI or accessing main-thread-only resources from background threads.

The call is non-blocking and doesn't wait for the function to complete.

Example:

Runner.runInMain(() -> {
    myVisual.alpha = 0.5; // Safe UI update
});
Name Type Description
_fn Function The function to execute on the main thread

runInBackground(fn: Function): Void

Executes a function on a background thread.

On platforms with threading support (C++/C#), this creates a new thread to execute the function. On single-threaded platforms (JS/Web), the function is scheduled for deferred execution on the main thread.

This is useful for offloading heavy computations or I/O operations without blocking the main thread.

Example:

Runner.runInBackground(() -> {
    // Heavy computation
    var data = processLargeDataset();
    // Return result to main thread
    Runner.runInMain(() -> {
        handleProcessedData(data);
    });
});
Name Type Description
fn Function The function to execute in the background

setThreadAffinity(affinity: ThreadAffinity): Bool

Makes the calling thread run on the given kind of cores (see ThreadAffinity), on devices whose cores are not all the same. On Android and Linux, the thread is restricted to these cores; on iOS and macOS, it gets a quality of service class that leads the system to them. Returns true if the choice was applied, false where it has no effect (other platforms, cores all the same, cores not allowed to the app right now).

Android may reset the cores of the threads of the app when it goes to the background and comes back: a long-lived thread should apply its choice again from time to time, for instance before each piece of work (the call is cheap).

Name Type
affinity ThreadAffinity
Returns
Bool

Private Members

mainThread: sys.thread.Thread

Reference to the main thread. Set during initialization to identify the primary thread for callback execution.


queue: sys.thread.Deque<Function>

Thread-safe queue for storing callbacks to be executed on the main thread. Background threads push callbacks here, and the main thread processes them during tick().

Metadata

Name Parameters
:cppFileCode "\n#include <pthread.h>\n#include <pthread/qos.h>\n\nnamespace ceramic_thread_affinity {\n\n // No affinity on Apple platforms: a quality of service class leads the scheduler to\n // the performance or efficiency cores\n static bool apply(int affinity, bool* first) {\n first = false;\n qos_class_t qos = affinity == 1 ? QOS_CLASS_BACKGROUND : affinity == 0 ? QOS_CLASS_DEFAULT : QOS_CLASS_USER_INTERACTIVE;\n return pthread_set_qos_class_self_np(qos, 0) == 0;\n }\n\n static const char describe() {\n return "quality of service";\n }\n\n}\n"