diff --git a/inc/mthread/mthread.h b/inc/mthread/mthread.h index 869e0ddd4c3b0ecfc0b1e4eaca697a5a5761046a..6806967497f838685af43acaeb86b8f7fac5bfdd 100644 --- a/inc/mthread/mthread.h +++ b/inc/mthread/mthread.h @@ -36,8 +36,6 @@ typedef struct mthread_cond_s mthread_cond_t; struct mthread_condattr_s; typedef struct mthread_condattr_s mthread_condattr_t; -typedef unsigned int mthread_key_t; - typedef mthread_tst_t mthread_once_t; struct mthread_sem_s { @@ -54,53 +52,7 @@ extern mthread_t mthread_self(void); extern int mthread_equal (mthread_t __thread1, mthread_t __thread2); extern void mthread_exit (void *__retval); extern int mthread_join (mthread_t __th, void **__thread_return); - -/* Functions for mutex handling. */ - -extern int mthread_mutex_init (mthread_mutex_t *__mutex, const mthread_mutexattr_t *__mutex_attr); -extern int mthread_mutex_destroy(mthread_mutex_t *__mutex); -extern int mthread_mutex_trylock(mthread_mutex_t *__mutex); -extern int mthread_mutex_lock (mthread_mutex_t *__mutex); -extern int mthread_mutex_unlock (mthread_mutex_t *__mutex); - -/* Functions for handling conditional variables. */ - -extern int mthread_cond_init (mthread_cond_t *__cond, const mthread_condattr_t *__cond_attr); -extern int mthread_cond_destroy (mthread_cond_t *__cond); -extern int mthread_cond_signal (mthread_cond_t *__cond); -extern int mthread_cond_broadcast(mthread_cond_t *__cond); -extern int mthread_cond_wait (mthread_cond_t *__cond, mthread_mutex_t *__mutex); - -/* Functions for handling thread-specific data. */ - -extern int mthread_key_create (mthread_key_t *__key, void (*__destr_function) (void *)); -extern int mthread_key_delete (mthread_key_t __key); -extern int mthread_setspecific (mthread_key_t __key, const void *__pointer); -extern void *mthread_getspecific(mthread_key_t __key); - - -/* Functions for handling initialization. */ - -/* Guarantee that the initialization function INIT_ROUTINE will be called - only once, even if mthread_once is executed several times with the - same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or - variable initialized to MTHREAD_ONCE_INIT. - - The initialization functions might throw exception which is why - this function is not marked with . */ -extern int mthread_once(mthread_once_t *__once_control, void (*__init_routine) (void)); - -/* Functions for handling semaphore. */ - -extern int mthread_sem_init(mthread_sem_t *sem, unsigned int value); -extern int mthread_sem_wait(mthread_sem_t *sem); /* P(sem), wait(sem) */ -extern int mthread_sem_post(mthread_sem_t *sem); /* V(sem), signal(sem) */ - -extern int mthread_sem_getvalue(mthread_sem_t *sem, int *sval); -extern int mthread_sem_trywait (mthread_sem_t *sem); -extern int mthread_sem_destroy (mthread_sem_t *sem); /* undo sem_init() */ - -extern void mthread_yield(); +extern void mthread_yield (); /* Initialize mthread. */ diff --git a/inc/mthread/mthread_internal.h b/inc/mthread/mthread_internal.h index 285f337ed5d1ad1393382f36a9d2c4983a9cdcfb..832a019ddd28a08b7a88c22caf28402d04fd6e2b 100644 --- a/inc/mthread/mthread_internal.h +++ b/inc/mthread/mthread_internal.h @@ -31,14 +31,6 @@ typedef struct { typedef enum { RUNNING, BLOCKED, ZOMBIE } mthread_status_t; typedef volatile enum { JOINABLE = 0, DETACHED = 1, DETACHED_FREE = 2 } mthread_detached_flag_t; -#define INIT_KEYS_LIST 48 - -struct keys_list { - volatile mthread_key_t *list; - unsigned int first_avail; - unsigned int size; -}; - struct mthread_s { ucontext_t uc; volatile void *res; @@ -50,7 +42,6 @@ struct mthread_s { int not_migrable; mthread_virtual_processor_t *vp; void *stack; - struct keys_list keys; }; #define MTHREAD_LIST_INIT { .first = NULL, .last = NULL, .lock = 0 } diff --git a/meson.build b/meson.build index c73a83c2b77a585050199de16d311ac03bd6888f..b8a724ab4d0e3f2a163daef1c5ae4fc9d348bde4 100644 --- a/meson.build +++ b/meson.build @@ -3,7 +3,6 @@ project( 'lektor' , version: '0.1.0' , license: 'ISC' , default_options: [ 'c_std=c18' - , 'cpp_std=c++17' , 'warning_level=3' , 'werror=true' , 'strip=true' @@ -40,11 +39,6 @@ lektor_modules = [] ## Sources for mthread mthread_sources = [ 'src/mthread/mthread.c' - , 'src/mthread/mthread_cond.c' - , 'src/mthread/mthread_key.c' - , 'src/mthread/mthread_mutex.c' - , 'src/mthread/mthread_once.c' - , 'src/mthread/mthread_sem.c' , 'src/mthread/mthread_tst.c' ] @@ -59,7 +53,6 @@ common_sources = [ 'src/common.c' core_sources = [ 'src/mkv/write.c' , 'src/mkv/utils.c' , 'src/mkv/mkv.c' - , 'src/commands.c' , 'src/database/stickers.c' , 'src/database/open.c' , 'src/database/queue.c' @@ -72,6 +65,7 @@ core_sources = [ 'src/mkv/write.c' , 'src/net/listen.c' , 'src/net/message.c' , 'src/module/repo.c' + , 'src/commands.c' , 'src/config.c' , 'src/uri.c' , 'src/thread.c' diff --git a/src/mthread/mthread.c b/src/mthread/mthread.c index 3cd1d0ac2227e4deb4b93e90967dad36f471e787..a83d2b2a399c1e289848f11c7552201797c0c1ab 100644 --- a/src/mthread/mthread.c +++ b/src/mthread/mthread.c @@ -13,24 +13,6 @@ static mthread_list_t joined_list; /* Is mthread initialized */ static volatile int is_mthread_init = 0; -static void -__mthread_clear_keys(mthread_t *thread) -{ - RETURN_UNLESS(thread, "Invalid argument", NOTHING); - unsigned int i; - struct keys_list *keys = &(*thread)->keys; - - if (!keys) { - keys->first_avail = 0; - return; - } - - for (i = 0; i < keys->first_avail; ++i) - mthread_key_delete(keys->list[i]); - - keys->first_avail = 0; -} - static inline void mthread_list_init(mthread_list_t *list) { @@ -370,7 +352,6 @@ mthread_join(mthread_t __th, void **__thread_return) if (__thread_return != NULL) *__thread_return = (void *)__th->res; - __mthread_clear_keys(&__th); LOG_INFO("THREAD END", "Thread %p joined", (void *) __th); mthread_insert_last(__th, &(joined_list)); return 0; diff --git a/src/mthread/mthread_cond.c b/src/mthread/mthread_cond.c deleted file mode 100644 index 1956f4420e5c3bbae4a10546c247369b9e9d3c01..0000000000000000000000000000000000000000 --- a/src/mthread/mthread_cond.c +++ /dev/null @@ -1,101 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <mthread/mthread_internal.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -/* Functions for handling conditional variables. */ - -/* Initialize condition variable COND using attributes ATTR, or use - the default values if later is NULL. */ -int -mthread_cond_init(mthread_cond_t *__cond, const mthread_condattr_t *__cond_attr) -{ - (void)__cond_attr; - __cond->lock = 0; - __cond->list = safe_malloc(sizeof(struct mthread_list_s)); - memset(__cond->list, 0, sizeof(struct mthread_list_s)); - LOG_INFO("MTHREAD", "%s", "Successfully created a condition"); - return 0; -} - -/* Destroy condition variable COND. */ -int -mthread_cond_destroy(mthread_cond_t *__cond) -{ - if (__cond->list) - free((void *) __cond->list); - return 0; -} - -/* Wake up one thread waiting for condition variable COND. */ -int -mthread_cond_signal(mthread_cond_t *__cond) -{ - mthread_t first, self = mthread_self(); - mthread_virtual_processor_t *vp; - int ret = 1; - mthread_spinlock_lock(&__cond->lock); - - if (__cond->list->first != NULL) { - first = mthread_remove_first(__cond->list); - vp = mthread_get_vp(); - first->status = RUNNING; - mthread_insert_last(first, &(vp->ready_list)); - LOG_INFO("MTHREAD", "Wake up the thread %p from the thread %p", - (void *) first, (void *) self); - ret = 0; - } else - LOG_INFO("MTHREAD", "No thread to wake up found from the thread %p", - (void *) self); - - mthread_spinlock_unlock(&__cond->lock); - return ret; -} - -/* Wake up all threads waiting for condition variables COND. */ -int -mthread_cond_broadcast(mthread_cond_t *__cond) -{ - mthread_t first, self = mthread_self(); - mthread_virtual_processor_t *vp; - mthread_spinlock_lock(&__cond->lock); - - while (__cond->list->first != NULL) { - first = mthread_remove_first(__cond->list); - vp = mthread_get_vp(); - first->status = RUNNING; - LOG_INFO("MTHREAD", "Wake up the thread %p from the thread %p", - (void *) first, (void *) self); - mthread_insert_last(first, &(vp->ready_list)); - } - - mthread_spinlock_unlock(&__cond->lock); - return 0; -} - -/* Wait for condition variable COND to be signaled or broadcast. - MUTEX is assumed to be locked before. */ -int -mthread_cond_wait(mthread_cond_t *__cond, mthread_mutex_t *__mutex) -{ - mthread_spinlock_lock(&__cond->lock); - mthread_t self = mthread_self(); - mthread_virtual_processor_t *vp = mthread_get_vp(); - mthread_insert_last(self, __cond->list); - self->status = BLOCKED; - mthread_mutex_unlock(__mutex); - - LOG_ERROR("MTHREAD", "Waiting in condition for thread %p", - (void *) self); - - vp->p = &__cond->lock; - mthread_yield(); - - mthread_mutex_lock(__mutex); - LOG_ERROR("MTHREAD", "Waike up in condition for thread %p", - (void *) self); - - return 0; -} diff --git a/src/mthread/mthread_key.c b/src/mthread/mthread_key.c deleted file mode 100644 index 96d650ddc7cb9eaf7b7923c894be5fbc84445a9d..0000000000000000000000000000000000000000 --- a/src/mthread/mthread_key.c +++ /dev/null @@ -1,165 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <mthread/mthread_internal.h> -#include <errno.h> -#include <stdlib.h> - -/* TODO -> use binaty trees for better performance in searches. */ -typedef struct cell_s { - mthread_key_t key; - const volatile void *volatile spec; - void (* destr)(void *); - volatile struct cell_s *volatile next; -} cell_t; - -/* Default is 0. Index start at zero. Because of that the vairables hold the - first available slots and not the last used slots in memory. */ -static volatile mthread_key_t first_avail_key = 1; -static volatile cell_t *volatile key_table = NULL; - -/* Protects anything concerning the keys. */ -static mthread_tst_t lock = 0; - -/* Functions for handling thread-specific data. */ - -/* Create a key value identifying a location in the thread-specific - data area. Each thread maintains a distinct thread-specific data - area. DESTR_FUNCTION, if non-NULL, is called with the value - associated to that key when the key is destroyed. - DESTR_FUNCTION is not called if the value associated is NULL when - the key is destroyed. */ -int -mthread_key_create(mthread_key_t *__key, void (*__destr_function) (void *)) -{ - cell_t *new_cell = (cell_t *) safe_malloc(sizeof(cell_t)); - mthread_spinlock_lock(&lock); - - /* Creation. */ - new_cell->key = first_avail_key; - new_cell->spec = NULL; - new_cell->destr = __destr_function; - new_cell->next = NULL; - - first_avail_key++; - - if (key_table) { - new_cell->next = key_table; - key_table = new_cell; - } else - key_table = new_cell; - - /* Register key into the thread. */ - - struct keys_list *keys = &(mthread_self()->keys); - if (!keys->list) { - keys->list = (mthread_key_t *) safe_malloc(INIT_KEYS_LIST * sizeof(mthread_key_t)); - keys->first_avail = 0; - keys->size = INIT_KEYS_LIST; - } - - if (keys->size == keys->first_avail) { - /* Extend size if no space left. */ - void *new = realloc((void *) keys->list, (keys->size + INIT_KEYS_LIST) * sizeof(mthread_key_t)); - GOTO_UNLESS(new, "Could not increase the size of the key list", end); - keys->size += INIT_KEYS_LIST; - keys->list = new; - } - - /* Register the key into the thread. */ - keys->list[keys->first_avail] = new_cell->key; - keys->first_avail++; - - /* Returns. */ - *__key = new_cell->key; -end: - mthread_spinlock_unlock(&lock); - return errno; -} - -/* Destroy KEY. */ -int -mthread_key_delete(mthread_key_t __key) -{ - mthread_spinlock_lock(&lock); - - cell_t *it = (cell_t *) key_table; - cell_t *last = NULL; - - while (it != NULL && it->key != __key) { - last = it; - it = (cell_t *) it->next; - } - - if (!it) { - errno = EINVAL; - LOG_ERROR("MTHREAD", "Key %u not found", __key); - goto end; - } - - if (it->destr) { - LOG_INFO("MTHREAD", "Call function on data at %p", - (void *) it->spec); - it->destr((void *) it->spec); - } - - /* Handle deletion in the linked list. */ - - if (last) - last->next = it->next; - else - key_table = it->next; - - free(it); - LOG_INFO("MTHREAD", "Deleted key %u successfully", __key); - /* End of the function, simply returns. */ -end: - mthread_spinlock_unlock(&lock); - return errno; -} - -/* Store POINTER in the thread-specific data slot identified by KEY. */ -int -mthread_setspecific(mthread_key_t __key, const void *__pointer) -{ - mthread_spinlock_lock(&lock); - - cell_t *it = (cell_t *) key_table; - while (it != NULL && it->key != __key) - it = (cell_t *) it->next; - - if (!it) { - errno = EINVAL; - LOG_ERROR("MTHREAD", "Key %u not found", __key); - goto end; - } - - it->spec = __pointer; - /* End of the function, simply returns. */ -end: - mthread_spinlock_unlock(&lock); - return errno; -} - -/* Return current value of the thread-specific data slot identified by KEY. */ -void * -mthread_getspecific(mthread_key_t __key) -{ - void *ret = NULL; - mthread_spinlock_lock(&lock); - - cell_t *it = (cell_t *) key_table; - while (it != NULL && it->key != __key) - it = (cell_t *) it->next; - - if (!it) { - errno = EINVAL; - LOG_ERROR("MTHREAD", "Key %u not found", __key); - goto end; - } - - ret = (void *) it->spec; - /* End of the function, simply returns. */ -end: - mthread_spinlock_unlock(&lock); - return (void *) ret; -} diff --git a/src/mthread/mthread_mutex.c b/src/mthread/mthread_mutex.c deleted file mode 100644 index beb344bae6282d57d80d78c82aa8efbed26864f8..0000000000000000000000000000000000000000 --- a/src/mthread/mthread_mutex.c +++ /dev/null @@ -1,120 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <common/common.h> -#include <errno.h> -#include <string.h> -#include <mthread/mthread_internal.h> - -/* Functions for mutex handling. */ - -/* Initialize MUTEX using attributes in *MUTEX_ATTR, or use the - default values if later is NULL. */ -int -mthread_mutex_init(mthread_mutex_t *__mutex, const mthread_mutexattr_t *__mutex_attr) -{ - UNUSED(__mutex_attr); - __mutex->list = safe_malloc(sizeof(mthread_list_t)); - __mutex->list->first = NULL; - __mutex->list->last = NULL; - __mutex->nb_thread = 0; - __mutex->lock = 0; - LOG_INFO("MTHREAD", "%s", "MUTEX initialized"); - return 0; -} - -/* Destroy MUTEX. */ -int -mthread_mutex_destroy(mthread_mutex_t *__mutex) -{ - mthread_spinlock_lock(&__mutex->lock); - - if (__mutex->nb_thread != 0) - return EBUSY; - free(__mutex->list); - __mutex->list = NULL; - - mthread_spinlock_unlock(&__mutex->lock); - return 0; -} - -/* Try to lock MUTEX. */ -int -mthread_mutex_trylock(mthread_mutex_t *__mutex) -{ - int retval = EINVAL; - - if (__mutex->list == NULL) { - __mutex->list = safe_malloc(sizeof(mthread_list_t)); - memset(__mutex->list, 0, sizeof(mthread_list_t)); - } - - mthread_spinlock_lock(&__mutex->lock); - - if (__mutex->nb_thread == 0) { - __mutex->nb_thread = 1; - mthread_spinlock_unlock(&__mutex->lock); - retval = 0; - goto end; - } - - mthread_spinlock_unlock(&__mutex->lock); - LOG_WARN("MTHREAD", "%s", "MUTEX busy"); - retval = EBUSY; - -end: - return retval; -} - -/* Wait until lock for MUTEX becomes available and lock it. */ -int -mthread_mutex_lock(mthread_mutex_t *__mutex) -{ - mthread_t self; - mthread_virtual_processor_t *vp; - - if (__mutex->list == NULL) { - __mutex->list = safe_malloc(sizeof(mthread_list_t)); - memset(__mutex->list, 0, sizeof(mthread_list_t)); - } - - mthread_spinlock_lock(&__mutex->lock); - - if (__mutex->nb_thread == 0) { - __mutex->nb_thread = 1; - mthread_spinlock_unlock(&__mutex->lock); - } else { - self = mthread_self(); - mthread_insert_last(self, __mutex->list); - self->status = BLOCKED; - vp = mthread_get_vp(); - vp->p = &__mutex->lock; - mthread_yield(); - } - - return 0; -} - -/* Unlock MUTEX. */ -int -mthread_mutex_unlock(mthread_mutex_t *__mutex) -{ - mthread_t first; - mthread_virtual_processor_t *vp; - - if (__mutex->list == NULL) { - __mutex->list = safe_malloc(sizeof(mthread_list_t)); - memset(__mutex->list, 0, sizeof(mthread_list_t)); - } - - mthread_spinlock_lock(&__mutex->lock); - if (__mutex->list->first != NULL) { - first = mthread_remove_first(__mutex->list); - vp = mthread_get_vp(); - first->status = RUNNING; - mthread_insert_last(first, &(vp->ready_list)); - } else - __mutex->nb_thread = 0; - - mthread_spinlock_unlock(&__mutex->lock); - return 0; -} diff --git a/src/mthread/mthread_once.c b/src/mthread/mthread_once.c deleted file mode 100644 index 9a269a39c13c61a7fac78fd713321ee9d8a17416..0000000000000000000000000000000000000000 --- a/src/mthread/mthread_once.c +++ /dev/null @@ -1,22 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <mthread/mthread_internal.h> -#include <errno.h> - -/* Functions for handling initialization. */ - -/* Guarantee that the initialization function INIT_ROUTINE will be called - only once, even if mthread_once is executed several times with the - same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or - extern variable initialized to MTHREAD_ONCE_INIT. - - The initialization functions might throw exception which is why - this function is not marked with . */ -int -mthread_once (mthread_once_t *__once_control, void (*__init_routine) (void)) -{ - if (mthread_test_and_set(__once_control)) - return EAGAIN; - __init_routine(); - return 0; -} diff --git a/src/mthread/mthread_sem.c b/src/mthread/mthread_sem.c deleted file mode 100644 index 6d5acbcfc7b2ea4ce26c15b13e2fc33b364e21c8..0000000000000000000000000000000000000000 --- a/src/mthread/mthread_sem.c +++ /dev/null @@ -1,74 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <mthread/mthread_internal.h> - -/* Functions for handling semaphore. */ - -int -mthread_sem_init(mthread_sem_t *sem, unsigned int value) -{ - sem->value = value; - mthread_mutex_init(&sem->mutex, NULL); - return 0; -} - -/* P(sem), wait(sem) */ -int -mthread_sem_wait(mthread_sem_t *sem) -{ - /* Here we loop while we can't enter the semaphore because there are already - the maximum number of clients. */ -loop: - mthread_mutex_lock(&sem->mutex); - if (sem->value == 0) { - mthread_mutex_unlock(&sem->mutex); - mthread_yield(); - goto loop; - } - - sem->value --; - mthread_mutex_unlock(&sem->mutex); - return 0; -} - -/* V(sem), signal(sem) */ -int -mthread_sem_post(mthread_sem_t *sem) -{ - mthread_mutex_lock(&sem->mutex); - sem->value ++; - mthread_mutex_unlock(&sem->mutex); - return 0; -} - -int -mthread_sem_getvalue(mthread_sem_t *sem, int *sval) -{ - *sval = sem->value; - return 0; -} - -int -mthread_sem_trywait(mthread_sem_t *sem) -{ - if (mthread_mutex_trylock(&sem->mutex)) - return 1; - - if (sem->value == 0) { - mthread_mutex_unlock(&sem->mutex); - return 1; - } - - sem->value --; - mthread_mutex_unlock(&sem->mutex); - return 0; -} - -/* undo sem_init() */ -int -mthread_sem_destroy(mthread_sem_t *sem) -{ - mthread_mutex_destroy(&sem->mutex); - sem->value = 0; - return 0; -}