/* * KPL specific objects */ #pragma once typedef enum : uint16_t { KPL_OBJECT_FLAGS_EMPTY = 0, KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0, // When io pending tasks will not be reachable by GC KPL_OBJECT_FLAGS_GC_PERSIST = 1 << 1 // Object is never swept by GC until program end } kpl_object_flags; // for atomic linked list queue // size in bytes, index on the kpl_threads array // Flags should only be set by owner thread #define KPL_OBJECT_HEADER(STRUCT_NAME) \ struct STRUCT_NAME *_Atomic queue_next; \ int32_t object_size; \ uint16_t thread_id, object_flags; typedef struct _kpl_object { KPL_OBJECT_HEADER(_kpl_object); } kpl_object; // all kpl objects allocated by the kpl memory allocator extend this header /* * Atomic linked list queue any thread can add, only owner thread can remove */ typedef struct { kpl_object *_Atomic head, *tail; // head is modified with atomic exchange struct { kpl_object *_Atomic queue_next; } dummy; // head and tail are never null will always point to this } kpl_object_queue; /* * Only owner thread can call this */ inline void kpl_object_queue_init(kpl_object_queue *object_queue) { __atomic_store_n(&object_queue->dummy.queue_next, NULL, __ATOMIC_RELAXED); __atomic_store_n(&object_queue->head, &object_queue->dummy, __ATOMIC_RELAXED); object_queue->tail = (kpl_object*) &object_queue->dummy; } /* * Always succeeds, any thread can call this */ inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *object) { kpl_object *obj = object; __atomic_store_n(&obj->queue_next, NULL, __ATOMIC_RELAXED); kpl_object *head = __atomic_exchange_n(&object_queue->head, obj, __ATOMIC_SEQ_CST); __atomic_store_n(&head->queue_next, obj, __ATOMIC_RELEASE); } /* * Null for not found, only owner thread can call this */ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue); /* * All runtime objects are wrapped in the object_gc * Only owner thread can use gc_prev and gc_next * Any thread can mark */ typedef struct _kpl_object_gc { KPL_OBJECT_HEADER(_kpl_object_gc); struct _kpl_object_gc *gc_prev, *gc_next; _Atomic bool gc_mark __attribute((aligned(8))); kpl_class class; } kpl_object_gc; /* * Class data is read only, cannot modify or race conditions can occur * The error stack can be modified across threads */ typedef struct _kpl_object_error { KPL_OBJECT_HEADER(_kpl_object_error); kpl_object_queue error_stack; kpl_class error_file, error_data; // Can be a reference to C code or KPL code int32_t error_line; } kpl_object_error; typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_thread *thread); /* * Coroutine state object * Can have up to 32 arguments/locals * The variables_gc_mask is a bitmask, the set bit index represents the variable at the index is a kpl_object_gc */ typedef struct _kpl_object_task { KPL_OBJECT_HEADER(_kpl_object_task); _Atomic bool join_ready __attribute__((aligned(4))); // shares cache line with variables_gc_mask uint32_t variables_gc_mask, io_flags; // set once then only read int32_t io_res; // io_res and io_flags are set by io_uring complete kpl_object_task *join; kpl_task_fn *task_fn; kpl_result result; kpl_value64 variables[]; // a task has no distinction between an argument or a local, both live here } kpl_object_task;