/* * 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_ALLOCATOR_HEADER(STRUCT_NAME) \ struct STRUCT_NAME *_Atomic queue_next; \ int32_t object_size; \ uint16_t thread_id, object_flags; // Each object can access the object_gc it is a child of #define KPL_OBJECT_HEADER(STRUCT_NAME) \ KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \ kpl_object_gc *object_gc_parent; /* * All kpl objects allocated by the kpl memory allocator extend this header * The kpl_object_gc extends is partially, so it can still be used with the memory allocator */ typedef struct _kpl_object { KPL_OBJECT_HEADER(_kpl_object); } kpl_object; /* * 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_ALLOCATOR_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; /* * UTF-8 Strings * Strings must be null termed */ typedef struct _kpl_object_string { KPL_OBJECT_HEADER(_kpl_object_string); int32_t bytes_length, bytes_size; // length does not include null term, size includes null term uint8_t bytes[]; } kpl_object_string; #define KPL_ARRAY_MAX_VALUE_SIZE ((1 << 30) - 1) // for array polymorphism // values_length and values_size refers to the number of values, to get in bytes multiply by VALUE_BIT_WIDTH #define KPL_OBJECT_ARRAY_HEADER(STRUCT_NAME) \ KPL_OBJECT_HEADER(STRUCT_NAME); \ kpl_value_bit_width value_bit_width : 3; \ uint32_t values_length : 29, values_size : 29 /* * KPL arrays * Use this object as the base for all arrays * Array methods will cast to different value sizes */ typedef struct _kpl_object_array { KPL_OBJECT_ARRAY_HEADER(_kpl_object_array); } kpl_object_array; // One container for each internal value bit width #define _KPL_OBJECT_ARRAY_DEFINE(VALUE_BIT_WIDTH) \ typedef struct _kpl_object_array ## VALUE_BIT_WIDTH { \ KPL_OBJECT_ARRAY_HEADER(_kpl_object_array ## VALUE_BIT_WIDTH); \ kpl_value ## VALUE_BIT_WIDTH values[]; \ } kpl_object_array ## VALUE_BIT_WIDTH; /* * kpl_object_array8 */ _KPL_OBJECT_ARRAY_DEFINE(8); /* * kpl_object_array16 */ _KPL_OBJECT_ARRAY_DEFINE(16); /* * kpl_object_array32 */ _KPL_OBJECT_ARRAY_DEFINE(32); /* * kpl_object_array64 */ _KPL_OBJECT_ARRAY_DEFINE(64); #define KPL_TUPLE_ITEMS_MAX 64 /* * Tuple, array of different types * Can hold up TO KPL_TUPLE_ITEMS_MAX items, if (items_gc_mask & 1UL << index) then items[index] is a kpl_object_gc */ typedef struct _kpl_object_tuple { KPL_OBJECT_HEADER(_kpl_object_tuple); uint64_t items_gc_mask; kpl_value64 items[]; } kpl_object_tuple; /* * Wraps C objects * The c object is freed with the kpl_interface_native_free_fn */ typedef struct _kpl_object_native { KPL_OBJECT_HEADER(_kpl_object_native); void *c_object; } kpl_object_native; /* * Class data is read only, cannot modify or race conditions can occur * queue_next from the object header is used to create an error stack */ typedef struct _kpl_object_error { KPL_OBJECT_HEADER(_kpl_object_error); kpl_class error_file, error_data; // Can be a reference to C code or KPL code int32_t error_line; } kpl_object_error; typedef void kpl_task_fn(kpl_thread *thread, kpl_object_task *object_task); /* * Max variables per task, a bit mask used to check if the variable can be garbage collected */ #define KPL_TASK_VARIABLES_MAX 56 /* * Coroutine state object * Can have up to KPL_TASK_VARIABLES_MAX 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; // shares cache line with variables_gc_mask uint64_t variables_gc_mask : KPL_TASK_VARIABLES_MAX; // set once then only read int32_t io_res; // io_res and io_flags are set by io_uring complete uint32_t io_flags; kpl_object_task *join_task; 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;