diff --git a/core/synchro.c b/core/synchro.c index b004a9e..d485f32 100644 --- a/core/synchro.c +++ b/core/synchro.c @@ -6,21 +6,18 @@ #include "list.h" #include "time.h" -struct wait_queue *waitQueues = NULL; int mutexInit(struct mutex *m) { - uint32_t flags; m->owner = NULL; m->wait = (struct wait_queue *)malloc(sizeof(struct wait_queue)); if (!m->wait) return ENOMEM; + + waitQueueInit(m->wait); list_init(m->wait->thread); - disable_IRQs(flags); - list_add_tail(waitQueues, m->wait); - restore_IRQs(flags); return 0; } @@ -40,7 +37,7 @@ int mutexFree(struct mutex *m) getCurrentThread()); } #endif - list_delete(waitQueues, m->wait); + waitQueueFree(m->wait); free(m->wait); } else { ret = EBUSY; @@ -76,7 +73,7 @@ int mutexUnlock(struct mutex *m) m->owner = NULL; - wake_up(m->wait); + wakeUp(m->wait); restore_IRQs(flags); diff --git a/core/wait.c b/core/wait.c index e2c6ab0..fa70157 100644 --- a/core/wait.c +++ b/core/wait.c @@ -3,7 +3,27 @@ #include "list.h" #include "wait.h" -int wake_up(struct wait_queue *wq) +static struct wait_queue *waitQueues = NULL; + +int waitQueueInit(struct wait_queue *wq) +{ + uint32_t flags; + disable_IRQs(flags); + list_add_tail(waitQueues, wq); + restore_IRQs(flags); + return 0; +} + +int waitQueueFree(struct wait_queue *wq) +{ + uint32_t flags; + disable_IRQs(flags); + list_delete(waitQueues, wq); + restore_IRQs(flags); + return 0; +} + +int wakeUp(struct wait_queue *wq) { struct kthread *th; uint32_t flags; diff --git a/core/wait.h b/core/wait.h index f337733..bc2b4a8 100644 --- a/core/wait.h +++ b/core/wait.h @@ -16,9 +16,11 @@ struct wait_queue { #define DECLARE_WAITQUEUE(name) \ struct wait_queue name = __WAITQUEUE_INITIALIZER(name) +int waitQueueInit(struct wait_queue *); +int waitQueueFree(struct wait_queue *); int wait(struct wait_queue *); int waitTimeout(struct wait_queue *wq, unsigned long msec); -int wake_up(struct wait_queue *); +int wakeUp(struct wait_queue *); #define wait_event(wq, condition) \ do { \ diff --git a/tests/test.c b/tests/test.c index fd76f81..6cff6a6 100644 --- a/tests/test.c +++ b/tests/test.c @@ -274,7 +274,9 @@ void wqThread(void *arg) { (void)arg; DECLARE_WAITQUEUE(test); + waitQueueInit(&test); assert(waitTimeout(&test, 1000) == 1); + waitQueueFree(&test); haveTimeout = 1; }