diff options
| -rw-r--r-- | include/kpl.h | 3 | ||||
| -rw-r--r-- | include/kpl/ast.h | 187 | ||||
| -rw-r--r-- | include/kpl/class.h | 6 | ||||
| -rw-r--r-- | include/kpl/env.h | 19 | ||||
| -rw-r--r-- | include/kpl/memory.h | 2 | ||||
| -rw-r--r-- | include/kpl/object.h | 8 | ||||
| -rw-r--r-- | include/kpl/operator.h | 13 | ||||
| -rw-r--r-- | include/kpl/storage.h | 6 | ||||
| -rw-r--r-- | include/kpl/task.h | 5 | ||||
| -rw-r--r-- | include/kpl/type.h | 40 | ||||
| -rw-r--r-- | source/ast.c | 6 | ||||
| -rw-r--r-- | source/config.c | 6 | ||||
| -rw-r--r-- | source/env.c | 77 | ||||
| -rw-r--r-- | source/error.c | 3 | ||||
| -rw-r--r-- | source/string.c | 3 | ||||
| -rw-r--r-- | source/task.c | 10 | ||||
| -rw-r--r-- | test/task.c | 24 |
17 files changed, 369 insertions, 49 deletions
diff --git a/include/kpl.h b/include/kpl.h index 467196e..ede4fdd 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -67,4 +67,7 @@ typedef struct _kpl_env kpl_env; #include "kpl/array.h" #include "kpl/error.h" #include "kpl/task.h" +#include "kpl/type.h" +#include "kpl/operator.h" +#include "kpl/ast.h" #include "kpl/env.h" diff --git a/include/kpl/ast.h b/include/kpl/ast.h index b9ba591..427ce1b 100644 --- a/include/kpl/ast.h +++ b/include/kpl/ast.h @@ -1,6 +1,193 @@ /* * KPL AST * An AST is the definition of code and data + * AST nodes cannot be shared across threads */ #pragma once + +/* + * Must fit within 4 bits, 8 kinds total, 0 invalid + */ +typedef enum : uint8_t { + KPL_AST_KIND_LIST = 1, + KPL_AST_KIND_FUNCTION = 2, + KPL_AST_KIND_COROUTINE = 3, + KPL_AST_KIND_DATA = 4, + KPL_AST_KIND_IDENTIFIER = 5, + KPL_AST_KIND_TYPE = 6, + KPL_AST_KIND_OP = 7, + KPL_AST_KIND_ACTION = 8 +} kpl_ast_kind; + +// Ast nodes can be placed in a linked list +#define KPL_AST_HEADER(STRUCT_NAME) \ + KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \ + struct STRUCT_NAME *ast_prev, *ast_next + +/* + * All ast nodes inherit this, use meta to cast to node type + */ +typedef struct _kpl_ast_base { + KPL_AST_HEADER(_kpl_ast_base); +} kpl_ast_base; + +/* + * Use object_flags field because the ast does not use the garbage collector + */ +typedef struct { + kpl_ast_kind kind : 4; + uint16_t info : 12; // meaning of this changes based on kind +} kpl_ast_meta; + +#define KPL_AST_META_KIND_BIT_WIDTH 4 + +#define KPL_AST_META_KIND_BIT_MASK 15 + +/* + * Load metadata stored in the header object_flags + */ +inline kpl_ast_meta kpl_ast_meta_get(void *ast) { + kpl_ast_base *ast_base = ast; + uint16_t object_flags = ast_base->object_flags; + kpl_ast_meta ast_meta = {}; + ast_meta.kind = object_flags & KPL_AST_META_KIND_BIT_MASK; + ast_meta.info = object_flags >> KPL_AST_META_KIND_BIT_WIDTH; + return ast_meta; +} + +/* + * Store metadata in the header object_flags + */ +inline void kpl_ast_meta_set(void *ast, kpl_ast_meta ast_meta) { + kpl_ast_base *ast_base = ast; + ast_base->object_flags = 0; + ast_base->object_flags = ast_meta.info << KPL_AST_META_KIND_BIT_WIDTH; + ast_base->object_flags |= ast_meta.kind; +} + +/* + * An identifier without its specification (var, arg, ref) in a lists scope + */ +typedef struct _kpl_ast_scope { + KPL_TREE_HEADER(_kpl_ast_scope); + uint32_t id; // unique id + kpl_type_base *type; + kpl_class class; + kpl_identifier identifier; +} kpl_ast_scope; + +/* + * Stored on ast_meta.info + */ +typedef enum : uint8_t { + KPL_AST_LIST_KIND_BLOCK = 1, + KPL_AST_LIST_KIND_COLLECTION = 2, + KPL_AST_LIST_KIND_MODIFIERS = 3 +} kpl_ast_list_kind; + +typedef struct _kpl_ast_function kpl_ast_function; + +/* + * A "list" of statements + * Only block lists have their own scope + */ +typedef struct _kpl_ast_list { + KPL_AST_HEADER(_kpl_ast_list); + struct _kpl_ast_list *parent_list; + kpl_ast_function *function_parent; // owns this list + kpl_ast_base *list_head, *list_tail; + kpl_tree scope_tree; +} kpl_ast_list; + +typedef struct _kpl_ast_coroutine kpl_ast_coroutine; + +/* + * Synchronous code + */ +typedef struct _kpl_ast_function { + KPL_AST_HEADER(_kpl_ast_function); + kpl_ast_coroutine *coroutine_parent; // owns this function + kpl_ast_list *statements; +} kpl_ast_function; + +/* + * A "list" of functions + * Coroutine is synchronous if function_head == function_tail + * Synchronous coroutines can be called asynchronously, but not vice versa + */ +typedef struct _kpl_ast_coroutine { + KPL_AST_HEADER(_kpl_ast_coroutine); + kpl_ast_coroutine *parent_coroutine; + kpl_type_coroutine *type; + kpl_ast_function *function_head, *function_tail; +} kpl_ast_coroutine; + +/* + * Value or collection + */ +typedef struct _kpl_ast_data { + KPL_AST_HEADER(_kpl_ast_data); + kpl_type_base *type; + kpl_class class; +} kpl_ast_data; + +/* + * Stored on ast_meta.info + */ +typedef enum : uint16_t { // 12 bit limit + KPL_AST_IDENTIFIER_KIND_FLAG_EMPTY = 0, + KPL_AST_IDENTIFIER_KIND_VARIABLE = 1 << 0, + KPL_AST_IDENTIFIER_KIND_ARGUMENT = 1 << 1, + KPL_AST_IDENTIFIER_KIND_REFERENCE = 1 << 2, + KPL_AST_IDENTIFIER_FLAG_NOT_NULL = 1 << 3 // Flow typing nullable type has been checked +} kpl_ast_identifier_kind_flags; + +/* + * Represents a variable, argument or ref + */ +typedef struct _kpl_ast_identifier { + KPL_AST_HEADER(_kpl_ast_identifier); + kpl_ast_scope *scope; +} kpl_ast_identifier; + +/* + * Represents a type + * Types are left associative operators, they cast data and define arguments + * Cannot define variables + */ +typedef struct _kpl_ast_type { + KPL_AST_HEADER(_kpl_ast_type); + kpl_type_base *type; + kpl_ast_base *ast_right; // the ast node this type is casting/defining +} kpl_ast_type; + +/* + * Represents an operator + * All operators return a Result + * The operator name is stored on ast_meta.info + */ +typedef struct _kpl_ast_operator { + KPL_AST_HEADER(_kpl_ast_operator); + kpl_type_result *type; + kpl_ast_base *ast_children[2]; // left 0, right 1 +} kpl_ast_operator; + +/* + * Stored on ast_meta.info + */ +typedef enum : uint8_t { + KPL_AST_ACTION_KIND_IF = 1, + KPL_AST_ACTION_KIND_LOOP = 2, + KPL_AST_ACTION_KIND_MATCH = 3 +} kpl_ast_action_kind; + +/* + * Action, if, loop, match + * All actions return a Result + */ +typedef struct _kpl_ast_action { + KPL_AST_HEADER(_kpl_ast_action); + kpl_type_result *type; + kpl_ast_list *condition_list, *body_list; +} kpl_ast_action; diff --git a/include/kpl/class.h b/include/kpl/class.h index d1c836e..8cb05da 100644 --- a/include/kpl/class.h +++ b/include/kpl/class.h @@ -19,11 +19,17 @@ typedef void kpl_interface_native_free_fn(kpl_object_native *object_native); */ typedef void kpl_interface_log_fn(kpl_thread *thread, void *object); +/* + * `copy operator + */ +typedef kpl_value64 kpl_interface_copy_fn(kpl_thread *thread_from, kpl_thread *thread_to, kpl_value64 object); + typedef struct { // TODO kpl_interface_gc_mark_object_fn *gc_mark_object_fn; kpl_interface_native_free_fn *native_free_fn; kpl_interface_log_fn *log_fn; + kpl_interface_copy_fn *copy_fn; } kpl_interface; typedef struct { diff --git a/include/kpl/env.h b/include/kpl/env.h index 11f90b3..c4237fa 100644 --- a/include/kpl/env.h +++ b/include/kpl/env.h @@ -34,7 +34,8 @@ typedef enum : uint32_t { KPL_ENV_FLAG_HAS_ERROR = 1 << 1, KPL_ENV_FLAG_RUN_MODULE = 1 << 2, KPL_ENV_FLAG_RUN_EVAL = 1 << 3, - KPL_ENV_FLAG_RUN_REPL = 1 << 4 + KPL_ENV_FLAG_RUN_REPL = 1 << 4, + KPL_ENV_FLAG_RUN_FUNCTION = 1 << 5 } kpl_env_flags; /* @@ -50,7 +51,10 @@ typedef struct _kpl_env { _Atomic uint32_t flags; // see kpl_env_flags for values, any thread can modify or check needs atomic int32_t io_uring_queue_size_pow2; // the power of two to calculate the entries in the io_uring queue int32_t thread_count; // -1 updates this value to max possible, 0 invalid, >= 1 for specific number - const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace + union { + const char *kpl_c_string_entry; // file or string, not used for repl, pointer used in kpl args namespace + kpl_task_fn *kpl_fn_entry; // a task function, for a task with no variables, placed on main thread + }; // only one entry point can be defined _Atomic ssize_t total_priority; // the sum of the threads priority fields, application stops on zero kpl_thread *threads; // 0 is main thread, >= 1 are spawned pthreads kpl_tree namespace_tree, module_tree; // Trees of kpl_storage @@ -71,11 +75,6 @@ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]); */ kpl_exit_code kpl_env_init(kpl_env *env); -typedef enum : uint8_t { - KPL_ENV_QUEUE_MODE_ASYNC = 0, - KPL_ENV_QUEUE_MODE_SYNC = 1 -} kpl_env_queue_mode; - /* * Increase priority by one */ @@ -100,13 +99,17 @@ inline void kpl_env_priority_decrement(kpl_thread *thread) { thread->env->total_priority--; } +typedef enum : uint8_t { + KPL_ENV_QUEUE_MODE_ASYNC = 0, + KPL_ENV_QUEUE_MODE_SYNC = 1 +} kpl_env_queue_mode; + /* * queue_mode ASYNC queues thread with lowest priority * queue_mode SYNC queues thread on threads[task->thread_id] */ void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode); - /* * Run once, not re-enetrant * TODO RUN TASK AND IO ORDERING diff --git a/include/kpl/memory.h b/include/kpl/memory.h index 3bb4871..5056483 100644 --- a/include/kpl/memory.h +++ b/include/kpl/memory.h @@ -60,7 +60,7 @@ void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int3 /* * Only called though gc_new - * All allocations must extend KPL_OBJECT_GC_HEADER + * All allocations must extend KPL_OBJECT_ALLOCATOR_HEADER */ void *kpl_memory_allocator_allocate_gc(kpl_memory_allocator *memory_allocator, uint16_t thread_id); diff --git a/include/kpl/object.h b/include/kpl/object.h index 933b459..5566b00 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -13,14 +13,14 @@ typedef enum : uint16_t { // 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_GC_HEADER(STRUCT_NAME) \ +#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_GC_HEADER(STRUCT_NAME); \ + KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \ kpl_object_gc *object_gc_parent; /* @@ -71,7 +71,7 @@ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue); * Any thread can mark */ typedef struct _kpl_object_gc { - KPL_OBJECT_GC_HEADER(_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; @@ -160,7 +160,7 @@ typedef struct _kpl_object_error { int32_t error_line; } kpl_object_error; -typedef kpl_object_error *kpl_task_fn(kpl_thread *thread, kpl_object_task *task); +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 diff --git a/include/kpl/operator.h b/include/kpl/operator.h new file mode 100644 index 0000000..7912ff7 --- /dev/null +++ b/include/kpl/operator.h @@ -0,0 +1,13 @@ +/* + * KPL Operator names + * AST and IR use same enum + */ + +#pragma once + +/* + * Limited to 12 bits for AST, 0 is invalid + */ +typedef enum : uint16_t { + KPL_OPERATOR_NAME_INVALID, +} kpl_operator_name; diff --git a/include/kpl/storage.h b/include/kpl/storage.h index 8758624..5b0e13a 100644 --- a/include/kpl/storage.h +++ b/include/kpl/storage.h @@ -23,14 +23,14 @@ typedef struct _kpl_storgae_object { KPL_TREE_HEADER(STRUCT_NAME); \ bool import_ready; \ kpl_tree object_tree; \ - const char *c_string_ptr; + const char *c_string_ptr /* * Uses const c string keys * The namespace is c_string_ptr */ typedef struct _kpl_storage_namespace { - KPL_TREE_HEADER(_kpl_storage_namespace); + KPL_STORAGE_HEADER(_kpl_storage_namespace); } kpl_stoage_namespace; /* @@ -44,6 +44,6 @@ typedef kpl_stoage_namespace kpl_stoage_search; * This tree can only be accessed through a mutex */ typedef struct _kpl_storage_module { - KPL_TREE_HEADER(_kpl_storage_module); + KPL_STORAGE_HEADER(_kpl_storage_module); char c_string_array[]; // null term, c_string_ptr points to this } kpl_storage_module; diff --git a/include/kpl/task.h b/include/kpl/task.h index bfdbb4a..ce183da 100644 --- a/include/kpl/task.h +++ b/include/kpl/task.h @@ -32,3 +32,8 @@ inline void kpl_object_task_join(kpl_thread *thread, kpl_object_task *restrict o object_task_complete->join_task = object_task_join; kpl_object_task_done(thread, object_task_complete); } + +/* + * An error has occurred in the task + */ +void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error); diff --git a/include/kpl/type.h b/include/kpl/type.h index 7b93e6f..91c3bd4 100644 --- a/include/kpl/type.h +++ b/include/kpl/type.h @@ -1,6 +1,7 @@ /* * KPL Types * A Type is a declaration of code and data + * Types can be shared across threads */ #pragma once @@ -58,6 +59,7 @@ typedef enum : uint8_t { KPL_TYPE_KIND_NATIVE, // keeps interface pointer KPL_TYPE_KIND_ARRAY, // type wraps an unknown number of types KPL_TYPE_KIND_TREE, // tree of type/type or identifier/type + KPL_TYPE_KIND_RESULT, // union of array and tree for tags KPL_TYPE_KIND_COROUTINE // coroutine type, tree + array } kpl_type_kind; @@ -71,6 +73,7 @@ typedef enum : uint16_t { } kpl_type_flags; // type_kind is for struct polymorphism +// types are in the gc cannot use object_flags for meta #define KPL_TYPE_HEADER(STRUCT_NAME) \ KPL_OBJECT_HEADER(STRUCT_NAME); \ kpl_type_name type_name; \ @@ -129,17 +132,48 @@ typedef struct _kpl_type_tree_item_identifier { typedef struct _kpl_type_tree { KPL_TYPE_HEADER(_kpl_type_tree); uint16_t tag_counter; - kpl_tree tree; + kpl_tree tag_tree; } kpl_type_tree; /* + * Reserved ids for result types + * For result_types tag_counter has to be initialized to 3 + * New tags cannot use these ids + */ +typedef enum : uint8_t { + KPL_TYPE_RESULT_ID_DEFAULT = 0, // .default can be any type + KPL_TYPE_RESULT_ID_ERROR = 1, // .error must be error type + KPL_TYPE_RESULT_ID_DONE = 2 // .done must be void +} kpL_type_result_ids; + +/* + * Most of the time we can use default_error and map all .default and .error tags to this array + * .done does not need a type it is always void + * If more tags are added move .default, .error (.done if applicable) to the tree and add additional tags to tree + */ +typedef union { + kpl_type_base *default_error[2]; // used if tag counter == 3 + kpl_tree tree; // tag counter > 3 +} kpl_type_result_union; + +/* + * KPL result type + */ +typedef struct _kpl_type_result { + KPL_TYPE_HEADER(_kpl_type_result); + uint16_t tag_counter; + kpl_type_result_union result; +} kpl_type_result; + +/* * Coroutine Type * Arguments size is known at creation and does not grow * Arguments size can be zero + * Can be cased to kpl_type_result when working with results */ typedef struct _kpl_type_coroutine { KPL_TYPE_HEADER(_kpl_type_coroutine); - uint16_t arguments_size, tag_counter; // unique id generator - kpl_tree result_tree; + uint16_t tag_counter, arguments_size; // unique id generator + kpl_type_result_union result; kpl_type_base *arguments[]; } kpl_type_coroutine; diff --git a/source/ast.c b/source/ast.c new file mode 100644 index 0000000..491724a --- /dev/null +++ b/source/ast.c @@ -0,0 +1,6 @@ + +#include "kpl.h" + +extern inline kpl_ast_meta kpl_ast_meta_get(void *ast); + +extern inline void kpl_ast_meta_set(void *ast, kpl_ast_meta ast_meta); diff --git a/source/config.c b/source/config.c index 6e213cb..a3f895d 100644 --- a/source/config.c +++ b/source/config.c @@ -17,7 +17,7 @@ typedef struct { #define KPL_CONFIG_MATCH(CHAR) (CHAR - 'a') static kpl_exit_code kpl_config_option_parse_eval(kpl_env *env, int32_t arg_index, char *argv[]) { - env->kpl_entry = argv[arg_index]; + env->kpl_c_string_entry = argv[arg_index]; env->flags |= KPL_ENV_FLAG_RUN_EVAL; return KPL_EXIT_CODE_SUCCESS; } @@ -132,7 +132,7 @@ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) { int32_t arg_index = 1, exit_code = KPL_EXIT_CODE_SUCCESS; while (arg_index < argc) { if (argv[arg_index][0] != '-') { - env->kpl_entry = argv[arg_index]; + env->kpl_c_string_entry = argv[arg_index]; return KPL_EXIT_CODE_SUCCESS; } if (argv[arg_index][1] && argv[arg_index][2]) { @@ -145,7 +145,7 @@ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) { printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, no arguments after --\n" KPL_COLOR(RESET)); return KPL_EXIT_CODE_ERROR; } - env->kpl_entry = argv[arg_index]; + env->kpl_c_string_entry = argv[arg_index]; return KPL_EXIT_CODE_SUCCESS; } const kpl_config_option *option = &config_options[KPL_CONFIG_MATCH(argv[arg_index][1])]; diff --git a/source/env.c b/source/env.c index 6629e7e..22b87b1 100644 --- a/source/env.c +++ b/source/env.c @@ -1,15 +1,20 @@ #include "kpl.h" +static uint32_t kpl_env_run_mode(kpl_env *env) { + return env->flags & (KPL_ENV_FLAG_RUN_MODULE | KPL_ENV_FLAG_RUN_EVAL | + KPL_ENV_FLAG_RUN_REPL | KPL_ENV_FLAG_RUN_FUNCTION); +} + kpl_exit_code kpl_env_init(kpl_env *env) { if (!env->thread_count || env->thread_count < -1) return KPL_EXIT_CODE_ERROR; if (env->io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN || env->io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) return KPL_EXIT_CODE_ERROR; - uint32_t run_mode = env->flags & (KPL_ENV_FLAG_RUN_MODULE | KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL); + uint32_t run_mode = kpl_env_run_mode(env); if (run_mode != KPL_ENV_FLAG_RUN_MODULE && run_mode != KPL_ENV_FLAG_RUN_EVAL && - run_mode != KPL_ENV_FLAG_RUN_REPL) + run_mode != KPL_ENV_FLAG_RUN_REPL && run_mode != KPL_ENV_FLAG_RUN_FUNCTION) return KPL_EXIT_CODE_ERROR; if (env->thread_count == -1) { cpu_set_t cpus; @@ -26,17 +31,6 @@ kpl_exit_code kpl_env_init(kpl_env *env) { } // TODO NAMESPACE AND MODULE TREES // TODO MODULE TREE MUTEX - switch (run_mode) { - case KPL_ENV_FLAG_RUN_MODULE: - // TODO - break; - case KPL_ENV_FLAG_RUN_EVAL: - // TODO - break; - case KPL_ENV_FLAG_RUN_REPL: - // TODO - break; - } return KPL_EXIT_CODE_SUCCESS; } @@ -67,6 +61,19 @@ void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode kpl_env_thread_queue_task(&env->threads[thread_id], task); } +static void kpl_env_thread_init_eval(kpl_thread *thread) { + kpl_object_string *string = kpl_object_string_from_c_string(thread, thread->env->kpl_c_string_entry); + (void) string; + // TODO +} + +static void kpl_env_thread_init_function(kpl_thread *thread) { + kpl_object_task *object_task = kpl_object_task_create(thread, 0, 0); + object_task->task_fn = thread->env->kpl_fn_entry; + kpl_env_queue_task(thread->env, object_task, KPL_ENV_QUEUE_MODE_SYNC); + +} + static void kpl_env_thread_init(kpl_thread *thread) { kpl_memory_allocator_init(&thread->memory_allocator); kpl_gc_init(&thread->gc); @@ -75,6 +82,29 @@ static void kpl_env_thread_init(kpl_thread *thread) { kpl_object_queue_init(&thread->task_queue); kpl_object_queue_init(&thread->error_queue); sem_init(&thread->sem, 0, 0); + if (thread->thread_id != KPL_THREAD_MAIN_ID) + return; + switch (kpl_env_run_mode(thread->env)) { + case KPL_ENV_FLAG_RUN_MODULE: + // TODO + break; + case KPL_ENV_FLAG_RUN_EVAL: + kpl_env_thread_init_eval(thread); + break; + case KPL_ENV_FLAG_RUN_REPL: + // TODO + break; + case KPL_ENV_FLAG_RUN_FUNCTION: + kpl_env_thread_init_function(thread); + break; + } +} + +static kpl_object_task *kpl_thread_dequeue_task(kpl_thread *thread) { + kpl_object_task *object_task = kpl_object_queue_dequeue(&thread->task_queue); + if (object_task) + kpl_env_priority_decrement(thread); + return object_task; } static void kpl_env_thread_free(kpl_thread *thread) { @@ -103,20 +133,19 @@ void *kpl_env_run_loop(void *arg) { exit(KPL_EXIT_CODE_THREAD_ERROR); kpl_env_thread_init(thread); for (;;) { - // TODO RUN GC - kpl_object_task *task = kpl_object_queue_dequeue(&thread->task_queue); - if (!task) - task = kpl_io_uring_get_completed(thread); - if (task) { - kpl_object_error *error = task->task_fn(thread, task); - kpl_object_task_destroy(thread, task); - if (!error) - continue; - kpl_object_queue_enqueue(&thread->error_queue, error); - thread->env->flags |= KPL_ENV_FLAG_HAS_ERROR; + if (thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) { kpl_env_wake_all_threads(thread->env); break; } + // TODO RUN GC + kpl_object_task *object_task = kpl_thread_dequeue_task(thread); + if (!object_task) + object_task = kpl_io_uring_get_completed(thread); + if (object_task) { + object_task->task_fn(thread, object_task); + kpl_object_task_destroy(thread, object_task); + continue; + } kpl_env_priority_decrement(thread); if (!thread->env->total_priority) { kpl_env_wake_all_threads(thread->env); diff --git a/source/error.c b/source/error.c index 1ea9297..650bcce 100644 --- a/source/error.c +++ b/source/error.c @@ -10,7 +10,8 @@ static void kpl_object_error_log(kpl_thread *thread, void *object) { static const kpl_interface object_error_interface = { .gc_mark_object_fn = NULL, .native_free_fn = NULL, - .log_fn = kpl_object_error_log + .log_fn = kpl_object_error_log, + .copy_fn = NULL }; kpl_object_error *kpl_object_error_create(kpl_thread *thread, kpl_class error_file, kpl_class error_data, diff --git a/source/string.c b/source/string.c index e2d19ed..10c0820 100644 --- a/source/string.c +++ b/source/string.c @@ -4,7 +4,8 @@ static const kpl_interface object_string_interface = { .gc_mark_object_fn = NULL, .native_free_fn = NULL, - .log_fn = NULL + .log_fn = NULL, + .copy_fn = NULL }; kpl_object_string *kpl_object_string_create(kpl_thread *thread, int32_t bytes_size) { diff --git a/source/task.c b/source/task.c index add3363..0766f62 100644 --- a/source/task.c +++ b/source/task.c @@ -9,7 +9,8 @@ static void kpl_interface_object_task_gc_mark_object(void *object) { static const kpl_interface object_task_interface = { .gc_mark_object_fn = kpl_interface_object_task_gc_mark_object, .native_free_fn = NULL, - .log_fn = NULL + .log_fn = NULL, + .copy_fn = NULL }; kpl_object_task *kpl_object_task_create(kpl_thread *thread, int32_t variables_size, uint64_t variables_gc_mask) { @@ -42,3 +43,10 @@ void kpl_object_task_done(kpl_thread *thread, kpl_object_task *object_task) { extern inline void kpl_object_task_join(kpl_thread *thread, kpl_object_task *restrict object_task_complete, kpl_object_task *restrict object_task_join); + +extern inline void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error); + +void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error) { + kpl_object_queue_enqueue(&thread->error_queue, object_error); + thread->env->flags |= KPL_ENV_FLAG_HAS_ERROR; +} diff --git a/test/task.c b/test/task.c new file mode 100644 index 0000000..8374989 --- /dev/null +++ b/test/task.c @@ -0,0 +1,24 @@ + +#include "kpl_test.h" + +static int32_t run_task_value = 0; + +#define KPL_RUN_TASK_SET_VALUE 1234567 + +void run_task_set_value(kpl_thread *thread, kpl_object_task *object_task) { + run_task_value = KPL_RUN_TASK_SET_VALUE; + kpl_object_task_done(thread, object_task); +} + +KPL_TEST(run_task) { + kpl_env env = {}; + kpl_env_config(&env, -1, NULL); + env.thread_count = 1; + env.io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_MIN; + env.flags |= KPL_ENV_FLAG_RUN_FUNCTION; + env.kpl_fn_entry = run_task_set_value; + KPL_ASSERT(kpl_env_init(&env) == KPL_EXIT_CODE_SUCCESS, "env should init"); + KPL_ASSERT(kpl_env_run(&env) == KPL_EXIT_CODE_SUCCESS, "env should run"); + kpl_env_free(&env); + KPL_ASSERT(run_task_value == KPL_RUN_TASK_SET_VALUE, "task should have set value"); +} |
