diff options
| -rw-r--r-- | include/kpl/env.h | 25 | ||||
| -rw-r--r-- | include/kpl/gc.h | 1 | ||||
| -rw-r--r-- | include/kpl/identifier.h | 13 | ||||
| -rw-r--r-- | include/kpl/io.h | 6 | ||||
| -rw-r--r-- | include/kpl/object.h | 2 | ||||
| -rw-r--r-- | source/env.c | 51 | ||||
| -rw-r--r-- | source/gc.c | 10 | ||||
| -rw-r--r-- | source/identifier.c | 2 | ||||
| -rw-r--r-- | source/io.c | 8 | ||||
| -rw-r--r-- | source/memory.c | 1 | ||||
| -rw-r--r-- | test/README | 2 | ||||
| -rw-r--r-- | test/identifier.c | 1 | ||||
| -rw-r--r-- | test/io.c | 40 |
13 files changed, 127 insertions, 35 deletions
diff --git a/include/kpl/env.h b/include/kpl/env.h index 0cab4bc..11f90b3 100644 --- a/include/kpl/env.h +++ b/include/kpl/env.h @@ -77,11 +77,36 @@ typedef enum : uint8_t { } kpl_env_queue_mode; /* + * Increase priority by one + */ +inline void kpl_env_priority_increment(kpl_thread *thread) { + thread->priority++; + thread->env->total_priority++; +} + +/* + * Increase priority by amount + */ +inline void kpl_env_priority_add(kpl_thread *thread, ssize_t amount) { + thread->priority += amount; + thread->env->total_priority += amount; +} + +/* + * Decrease priority by one + */ +inline void kpl_env_priority_decrement(kpl_thread *thread) { + thread->priority--; + thread->env->total_priority--; +} + +/* * 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/gc.h b/include/kpl/gc.h index 074ba49..6ed4fc4 100644 --- a/include/kpl/gc.h +++ b/include/kpl/gc.h @@ -47,6 +47,7 @@ inline void kpl_gc_mark(kpl_object_gc *object_gc) { /* * Move gc_head to gc_mark head * Need task to traverse task and error queue to mark objects all call kpl_interface_gc_mark_object_fn + * Has to be called separately from kpl_gc_sweep, other tasks will be running while gc runs */ inline void kpl_gc_prepare_sweep(kpl_gc *gc) { gc->gc_mark_head = gc->gc_head; diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h index a30b4a7..9bb42b0 100644 --- a/include/kpl/identifier.h +++ b/include/kpl/identifier.h @@ -43,3 +43,16 @@ void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIF * 0 - equal, negative - left < right, positive - left > right */ int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right); + +/* + * Check if two identifiers are equal + * Faster then comparing when equality check is needed + */ +inline bool kpl_identifier_equal(const kpl_identifier *identifier_a, const kpl_identifier *identifier_b) { + for (int32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) + if (!identifier_a->parts[part_index] && !identifier_b->parts[part_index]) + break; + else if (identifier_a->parts[part_index] != identifier_b->parts[part_index]) + return false; + return true; +} diff --git a/include/kpl/io.h b/include/kpl/io.h index 3838a55..30503ad 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -48,14 +48,16 @@ inline void kpl_io_uring_free(kpl_io_uring *io_uring) { * This will wait if io uring enter is called * NULL for none completed * If an error occurred, the error details will be on the kpl_task_object + * *thread is needed to decrease priority */ -kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring); +kpl_object_task *kpl_io_uring_get_completed(kpl_thread *thread); /* * Submits all sqes (added_to_queue has total) in one io uring eneter * This does not wait + * *thread is need to increase priority by the number of operations submitted */ -int32_t kpl_io_uring_submit(kpl_io_uring *io_uring); +int32_t kpl_io_uring_submit(kpl_thread *thread); /* * Only call this though one of the functions below to add an operation to the queue diff --git a/include/kpl/object.h b/include/kpl/object.h index b157719..e6e2340 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -79,7 +79,7 @@ typedef struct _kpl_object_error { int32_t error_line; } kpl_object_error; -typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_env *env); +typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_thread *thread); /* * Coroutine state object diff --git a/source/env.c b/source/env.c index fbfc302..b5d59b8 100644 --- a/source/env.c +++ b/source/env.c @@ -40,17 +40,21 @@ kpl_exit_code kpl_env_init(kpl_env *env) { return KPL_EXIT_CODE_SUCCESS; } -static void kpl_env_thread_queue_task(kpl_env *env, kpl_object_task *task, uint16_t thread_id) { - kpl_thread *thread = &env->threads[thread_id]; +extern inline void kpl_env_priority_increment(kpl_thread *thread); + +extern inline void kpl_env_priority_add(kpl_thread *thread, ssize_t amount); + +extern inline void kpl_env_priority_decrement(kpl_thread *thread); + +static void kpl_env_thread_queue_task(kpl_thread *thread, kpl_object_task *task) { kpl_object_queue_enqueue(&thread->task_queue, task); - thread->priority++; - env->total_priority++; + kpl_env_priority_increment(thread); sem_post(&thread->sem); } void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode) { if (queue_mode == KPL_ENV_QUEUE_MODE_SYNC) - return kpl_env_thread_queue_task(env, task, task->thread_id); + return kpl_env_thread_queue_task(&env->threads[task->thread_id], task); uint16_t thread_id = 0; ssize_t priority = __atomic_load_n(&env->threads[thread_id].priority, __ATOMIC_ACQUIRE); for (int16_t next_thread_id = 1; next_thread_id < env->thread_count; next_thread_id++) { @@ -60,7 +64,7 @@ void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode thread_id = next_thread_id; } } - kpl_env_thread_queue_task(env, task, thread_id); + kpl_env_thread_queue_task(&env->threads[thread_id], task); } static void kpl_env_thread_init(kpl_thread *thread) { @@ -75,7 +79,7 @@ static void kpl_env_thread_init(kpl_thread *thread) { static void kpl_env_thread_free(kpl_thread *thread) { kpl_object_task *tmp_task; - while ((tmp_task = kpl_io_uring_get_completed(&thread->io_uring))) + while ((tmp_task = kpl_io_uring_get_completed(thread))) continue; kpl_io_uring_free(&thread->io_uring); kpl_gc_free(&thread->gc, thread); @@ -83,6 +87,13 @@ static void kpl_env_thread_free(kpl_thread *thread) { sem_destroy(&thread->sem); } +#define KPL_ENV_FLAGS_STOP_THREAD_MASK (KPL_ENV_FLAG_STOP | KPL_ENV_FLAG_HAS_ERROR) + +static void kpl_env_wake_all_threads(kpl_env *env) { + for (int32_t thread_id = 0; thread_id < env->thread_count; thread_id++) + sem_post(&env->threads[thread_id].sem); +} + void *kpl_env_run_loop(void *arg) { kpl_thread *thread = arg; cpu_set_t cpus; @@ -91,9 +102,29 @@ void *kpl_env_run_loop(void *arg) { if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpus)) exit(KPL_EXIT_CODE_THREAD_ERROR); kpl_env_thread_init(thread); - while (!(thread->env->flags & KPL_ENV_FLAG_HAS_ERROR)) { - // TODO RUN - break; + 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(task, thread); + if (!error) + continue; + kpl_object_queue_enqueue(&thread->error_queue, error); + thread->env->flags |= KPL_ENV_FLAG_HAS_ERROR; + kpl_env_wake_all_threads(thread->env); + break; + } + kpl_env_priority_decrement(thread); + if (!thread->env->total_priority) { + kpl_env_wake_all_threads(thread->env); + break; + } + sem_wait(&thread->sem); + if (!thread->env->total_priority || thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) + break; + kpl_env_priority_increment(thread); } // TODO REPORT ERRORS kpl_env_thread_free(thread); diff --git a/source/gc.c b/source/gc.c index 6db368c..07247f7 100644 --- a/source/gc.c +++ b/source/gc.c @@ -14,6 +14,7 @@ kpl_object_gc *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t kpl_memory_allocator_return(&thread->memory_allocator, obj); return NULL; } + obj_gc->object_flags = KPL_OBJECT_FLAGS_EMPTY; obj_gc->gc_prev = obj_gc->gc_next = NULL; __atomic_clear(&obj_gc->gc_mark, __ATOMIC_RELAXED); obj_gc->class.interface = interface; @@ -44,6 +45,13 @@ extern inline void kpl_gc_mark(kpl_object_gc *object_gc); extern inline void kpl_gc_prepare_sweep(kpl_gc *gc); +#define KPL_OBJECT_GC_FLAGS_NO_SWEEP_MASK (KPL_OBJECT_FLAGS_IO_PENDING | KPL_OBJECT_FLAGS_GC_PERSIST) + +static bool kpl_gc_object_is_alive(kpl_object_gc *object_gc) { + return object_gc->gc_mark || + ((kpl_object*) object_gc->class.value.ptr)->object_flags & KPL_OBJECT_GC_FLAGS_NO_SWEEP_MASK; +} + void kpl_gc_sweep(kpl_thread *thread) { if (!thread->gc.gc_mark_head) return; @@ -51,7 +59,7 @@ void kpl_gc_sweep(kpl_thread *thread) { while (mark_head) { kpl_object_gc *tmp = mark_head; mark_head = mark_head->gc_next; - if (tmp->gc_mark) { + if (kpl_gc_object_is_alive(tmp)) { __atomic_clear(&tmp->gc_mark, __ATOMIC_RELAXED); mark_tail = tmp; } else diff --git a/source/identifier.c b/source/identifier.c index 28401a2..176f46f 100644 --- a/source/identifier.c +++ b/source/identifier.c @@ -91,3 +91,5 @@ int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_ } return 0; } + +extern inline bool kpl_identifier_equal(const kpl_identifier *identifier_a, const kpl_identifier *identifier_b); diff --git a/source/io.c b/source/io.c index 6e83d8e..0a377e0 100644 --- a/source/io.c +++ b/source/io.c @@ -37,7 +37,8 @@ static int32_t kpl_io_uring_enter(int32_t ring_fd, uint32_t to_submit, uint32_t return ret < 0 ? -errno : ret; } -kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) { +kpl_object_task *kpl_io_uring_get_completed(kpl_thread *thread) { + kpl_io_uring *io_uring = &thread->io_uring; if (!io_uring->submitted_to_kernel) return NULL; uint32_t cring_head = __atomic_load_n(io_uring->cring_head, __ATOMIC_ACQUIRE); @@ -53,16 +54,19 @@ kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) { __atomic_store_n(io_uring->cring_head, cring_head, __ATOMIC_RELEASE); io_uring->submitted_to_kernel--; task->object_flags ^= KPL_OBJECT_FLAGS_IO_PENDING; + kpl_env_priority_decrement(thread); return task; } -int32_t kpl_io_uring_submit(kpl_io_uring *io_uring) { +int32_t kpl_io_uring_submit(kpl_thread *thread) { + kpl_io_uring *io_uring = &thread->io_uring; if (!io_uring->added_to_queue) return -1; int32_t ret = kpl_io_uring_enter(io_uring->ring_fd, io_uring->added_to_queue, 0, 0); if (ret > 0) { io_uring->added_to_queue -= ret; io_uring->submitted_to_kernel += ret; + kpl_env_priority_add(thread, ret); } return ret; } diff --git a/source/memory.c b/source/memory.c index a9c3321..d8b2585 100644 --- a/source/memory.c +++ b/source/memory.c @@ -103,6 +103,7 @@ void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int3 kpl_object *object = kpl_object_queue_dequeue(&slab_group->object_pool); if (!object) object = kpl_memory_allocator_slab_allocate(slab_group, thread_id); + object->object_flags = KPL_OBJECT_FLAGS_EMPTY; return object; } diff --git a/test/README b/test/README index 4ef67e0..5a0e197 100644 --- a/test/README +++ b/test/README @@ -22,7 +22,7 @@ Both macros can only be used inside a KPL_TEST function Asserting: -KPL_ASSSERT(condition, "Fail Message"); +KPL_ASSERT(condition, "Fail Message"); --- diff --git a/test/identifier.c b/test/identifier.c index bd50795..abbcdac 100644 --- a/test/identifier.c +++ b/test/identifier.c @@ -20,4 +20,5 @@ KPL_TEST(identifier_compare) { KPL_ASSERT(kpl_identifier_compare(&identifier_a, &identifier_b) == 0, "should be 0"); int32_t compare_value = strcmp(string_b, string_c); KPL_ASSERT(kpl_identifier_compare(&identifier_b, &identifier_c) == compare_value, "should equal strcmp"); + KPL_ASSERT(kpl_identifier_equal(&identifier_a, &identifier_b), "should be equal"); } @@ -4,21 +4,23 @@ static const char *linux_version = "/proc/version"; KPL_TEST(statx) { - kpl_io_uring io_uring = {}; - KPL_ASSERT(kpl_io_uring_init(&io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); + kpl_env env = {}; + kpl_thread thread = { .env = &env }; + kpl_io_uring *io_uring = &thread.io_uring; + KPL_ASSERT(kpl_io_uring_init(&thread.io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); struct statx statx_object = {}; kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1); task->io_res = 1; - KPL_ASSERT(kpl_io_uring_statx(&io_uring, task, AT_FDCWD, linux_version, 0, STATX_ALL, &statx_object), + KPL_ASSERT(kpl_io_uring_statx(io_uring, task, AT_FDCWD, linux_version, 0, STATX_ALL, &statx_object), "should add statx operation"); KPL_ASSERT(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING , "task should have io pending flag"); - KPL_ASSERT(kpl_io_uring_submit(&io_uring) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&io_uring), "should return a task"); + KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); + KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); KPL_ASSERT(!(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING) , "task should not have io pending flag"); KPL_ASSERT(task->io_res == 0, "io_res should have success stax return"); KPL_ASSERT(statx_object.stx_nlink > 0, "should have one or more hard link(s)"); free(task); - kpl_io_uring_free(&io_uring); + kpl_io_uring_free(io_uring); } #define TEST_TO_READ 5 @@ -26,28 +28,30 @@ KPL_TEST(statx) { static const char *test_linix_compare = "Linux"; KPL_TEST(open_read_close) { - kpl_io_uring io_uring = {}; - KPL_ASSERT(kpl_io_uring_init(&io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); + kpl_env env = {}; + kpl_thread thread = { .env = &env }; + kpl_io_uring *io_uring = &thread.io_uring; + KPL_ASSERT(kpl_io_uring_init(io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); char buffer[TEST_TO_READ + 1] = {}; kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1); task->io_res = 1; - KPL_ASSERT(kpl_io_uring_openat(&io_uring, task, AT_FDCWD, linux_version, O_RDONLY, 0), + KPL_ASSERT(kpl_io_uring_openat(io_uring, task, AT_FDCWD, linux_version, O_RDONLY, 0), "should add open operation"); - KPL_ASSERT(kpl_io_uring_submit(&io_uring) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&io_uring), "should return a task"); + KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); + KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); KPL_ASSERT(task->io_res > 0, "io_res should have open fd"); int32_t fd = task->io_res; task->io_res = 1; - KPL_ASSERT(kpl_io_uring_read(&io_uring, task, fd, &buffer, TEST_TO_READ, 0), "should add read operation"); - KPL_ASSERT(kpl_io_uring_submit(&io_uring) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&io_uring), "should return a task"); + KPL_ASSERT(kpl_io_uring_read(io_uring, task, fd, &buffer, TEST_TO_READ, 0), "should add read operation"); + KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); + KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); KPL_ASSERT(task->io_res == TEST_TO_READ, "io_res should equal the number of bytes read"); KPL_ASSERT(strcmp(test_linix_compare, buffer) == 0, "should have Linux string in buffer"); task->io_res = 1; - KPL_ASSERT(kpl_io_uring_close(&io_uring, task, fd), "should add close operation"); - KPL_ASSERT(kpl_io_uring_submit(&io_uring) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&io_uring), "should return a task"); + KPL_ASSERT(kpl_io_uring_close(io_uring, task, fd), "should add close operation"); + KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); + KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); KPL_ASSERT(task->io_res == 0, "io_res should have close success"); free(task); - kpl_io_uring_free(&io_uring); + kpl_io_uring_free(io_uring); } |
