summaryrefslogtreecommitdiff
path: root/src/itc.c
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2016-03-30 09:22:51 +0200
committerPaweł Redman <pawel.redman@gmail.com>2016-03-30 09:22:51 +0200
commitbc50836fdcd20fda174df392c264cc6f03030cdf (patch)
tree253a730e829cffa24d164db8b3d04fd7a428627b /src/itc.c
parentb72ce003432393e67b030a8a1be8a7b1c580bcf7 (diff)
ITC WIP #1.
Diffstat (limited to 'src/itc.c')
-rw-r--r--src/itc.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/itc.c b/src/itc.c
new file mode 100644
index 0000000..62e6208
--- /dev/null
+++ b/src/itc.c
@@ -0,0 +1,138 @@
+#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);
+}
+
+void itc_chan_destroy(itc_chan *chan)
+{
+ // todo: free queue
+
+ SDL_DestroyMutex(chan->mutex);
+}
+
+void itc_chan_push(itc_chan *chan, int number, void *data)
+{
+ itc_message *msg;
+
+ msg = calloc(1, sizeof(itc_message));
+ assert(msg);
+
+ msg->number = number;
+ msg->data = data;
+
+ SDL_LockMutex(chan->mutex);
+
+ if (!chan->oldest) {
+ chan->oldest = msg;
+ chan->newest = msg;
+ } else {
+ /*
+ * head <---> ... <---> tail
+ *
+ */
+
+ chan->newest->newer = msg;
+ msg->older = chan->newest;
+ chan->newest = msg;
+ }
+
+ SDL_UnlockMutex(chan->mutex);
+}
+
+int itc_chan_pop(itc_chan *chan, int *number, void **data)
+{
+ itc_message *msg;
+
+ SDL_LockMutex(chan->mutex);
+
+ if (!chan->oldest) {
+ SDL_UnlockMutex(chan->mutex);
+ return 1;
+ }
+
+ msg = chan->oldest;
+
+ if (msg->newer) {
+ msg->newer->older = NULL;
+ chan->oldest = msg->newer;
+ } else {
+ chan->newest = NULL;
+ chan->oldest = NULL;
+ }
+
+ SDL_UnlockMutex(chan->mutex);
+
+ *number = msg->number;
+ *data = msg->data;
+ free(msg);
+
+ return 0;
+}
+
+
+
+itc_chan gulg_chan;
+
+int gulgulator(void *unused)
+{
+ int cmd_num;
+ void *cmd_data;
+
+ while (1) {
+ while (!itc_chan_pop(&gulg_chan, &cmd_num, &cmd_data)) {
+ printf("gulgulator: pop %i %p\n", cmd_num, cmd_data);
+ }
+
+ usleep(100 * 1000);
+ }
+}
+
+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);
+
+ printf("main: push %i %p\n", cmd_num, 0xDEADBEEF);
+ itc_chan_push(&gulg_chan, cmd_num, (void*)0xDEADBEEF);
+ }
+
+ printf("EOF\n");
+
+ SDL_WaitThread(thread, NULL);
+}
+