diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2016-03-30 20:29:08 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2016-03-30 20:29:08 +0200 |
commit | 745d5a072d8669d1bc35e7c1f28b75b3484542e8 (patch) | |
tree | ecc9cbd03511764e79251ec256c81cb8796f7c36 /src/itc.c | |
parent | bc50836fdcd20fda174df392c264cc6f03030cdf (diff) |
Finish ITC; implement async sim control.
Diffstat (limited to 'src/itc.c')
-rw-r--r-- | src/itc.c | 77 |
1 files changed, 26 insertions, 51 deletions
@@ -1,33 +1,22 @@ #include "common.h" #include <unistd.h> -typedef struct itc_message_s itc_message; -struct itc_message_s { - int number; - void *data; - - itc_message *older, *newer; -}; - -typedef struct { - itc_message *newest, *oldest; - SDL_mutex *mutex; - -} itc_chan; - - void itc_chan_create(itc_chan *chan) { memset(chan, 0, sizeof(itc_chan)); chan->mutex = SDL_CreateMutex(); assert(chan->mutex); + + chan->sem = SDL_CreateSemaphore(0); + assert(chan->sem); } void itc_chan_destroy(itc_chan *chan) { // todo: free queue + SDL_DestroySemaphore(chan->sem); SDL_DestroyMutex(chan->mutex); } @@ -57,6 +46,7 @@ void itc_chan_push(itc_chan *chan, int number, void *data) chan->newest = msg; } + SDL_SemPost(chan->sem); SDL_UnlockMutex(chan->mutex); } @@ -81,6 +71,7 @@ int itc_chan_pop(itc_chan *chan, int *number, void **data) chan->oldest = NULL; } + SDL_SemWait(chan->sem); // should be instant SDL_UnlockMutex(chan->mutex); *number = msg->number; @@ -90,49 +81,33 @@ int itc_chan_pop(itc_chan *chan, int *number, void **data) return 0; } - - -itc_chan gulg_chan; - -int gulgulator(void *unused) +int itc_chan_pop_block(itc_chan *chan, int *number, void **data) { - int cmd_num; - void *cmd_data; + itc_message *msg; - while (1) { - while (!itc_chan_pop(&gulg_chan, &cmd_num, &cmd_data)) { - printf("gulgulator: pop %i %p\n", cmd_num, cmd_data); - } + SDL_SemWait(chan->sem); + SDL_LockMutex(chan->mutex); - usleep(100 * 1000); + if (!chan->oldest) { + SDL_UnlockMutex(chan->mutex); + return 1; } -} - -void itc_test(void) -{ - SDL_Thread *thread; - char line[512]; - - itc_chan_create(&gulg_chan); - thread = SDL_CreateThread(gulgulator, "gulgulator", NULL); - while (1) { - int cmd_num; - - printf("> "); - fflush(stdout); - - if (!fgets(line, sizeof(line), stdin)) - break; - - cmd_num = atoi(line); + msg = chan->oldest; - printf("main: push %i %p\n", cmd_num, 0xDEADBEEF); - itc_chan_push(&gulg_chan, cmd_num, (void*)0xDEADBEEF); + if (msg->newer) { + msg->newer->older = NULL; + chan->oldest = msg->newer; + } else { + chan->newest = NULL; + chan->oldest = NULL; } - printf("EOF\n"); + SDL_UnlockMutex(chan->mutex); - SDL_WaitThread(thread, NULL); -} + *number = msg->number; + *data = msg->data; + free(msg); + return 0; +} |