changeset 39:754c90e96e3d

Add synchronizedQueue. it use pthread_mutex_lock, unlock
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Sat, 16 May 2015 20:17:58 +0900
parents ce9fde200f3e
children 46917f503bce
files src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h
diffstat 3 files changed, 85 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/synchronizedQueue/synchronizedQueue.c	Sat May 16 02:45:31 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueue.c	Sat May 16 20:17:58 2015 +0900
@@ -6,25 +6,34 @@
 #include "allocate.h"
 #include "origin_cs.h"
 
+#ifdef CLANG
+#define _CbC_retrun __return
+#define _CbC_environment __environment
+#endif
+
+#define NUM 50
+
 extern __code initSynchronizedQueueContext(struct Context* context);
 
-__code code1(struct Context* context) {
-    context->data[Allocate]->allocate.size = sizeof(struct Element);
-    context->data[Allocate]->allocate.next = Code2;
-    goto meta(context, Allocator);
-}
+//__code code1(struct Context* context) {
+//    context->data[Allocate]->allocate.size = sizeof(struct Element);
+//    context->data[Allocate]->allocate.next = Code2;
+//    goto meta(context, Allocator);
+//}
 
 __code meta(struct Context* context, enum Code next) {
     goto (context->code[next])(context);
 }
 
-__code code2(struct Context* context) {
-    context->data[Allocate]->allocate.after_put = Code3;
-    context->data[context->dataNum] -> element.value = 1024;
-    goto meta(context, Sender);
-}
+//__code code2(struct Context* context) {
+//    context->data[Allocate]->allocate.after_put = Code3;
+//    context->data[context->dataNum] -> element.value = 1024;
+//    goto meta(context, Sender);
+//}
 
 __code meta_sender(struct Context* context, enum Code next) {
+    // lock
+    pthread_mutex_lock(&context->data[Queue]->queue.mutex);
     goto (context->code[next])(context);
 }
 
@@ -32,15 +41,30 @@
     goto meta(context, Put);
 }
 
+__code code1(struct Context* context) {
+    context->data[Allocate]->allocate.size = sizeof(long);
+    context->data[Allocate]->allocate.next = Code2;
+    goto meta(context, Allocator);
+}
+
+__code code2(struct Context* context) {
+    context->data[Counter] -> count = 0;
+    goto meta(context, Code3);
+}
+
 __code code3(struct Context* context) {
+    long loop = context->data[Counter]->count;
+    if(loop == NUM) {
+        goto meta(context, Exit);
+    }
     context->data[Allocate]->allocate.size = sizeof(struct Element);
     context->data[Allocate]->allocate.next = Code4;
     goto meta(context, Allocator);
 }
 
 __code code4(struct Context* context) {
-    context->data[Allocate]->allocate.after_put = Code5;
-    context->data[context->dataNum] -> element.value = 10;
+    context->data[Allocate]->allocate.after_put = Code3;
+    context->data[context->dataNum] -> element.value = context->data[Counter]->count++;
     goto meta(context, Sender);
 }
 
@@ -53,6 +77,11 @@
         context->data[Queue]->queue.last  = context->data[Queue]->queue.first;
     }
     context->data[Queue]->queue.last->element.next = 0;
+    context->data[Queue]->queue.count++;
+    printf("Put %d\n\n", context->data[Queue]->queue.last->element.value);
+
+    // unlock
+    pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
     goto (context->code[next])(context);
 }
 
@@ -60,26 +89,47 @@
     goto meta_put(context, context->data[Allocate]->allocate.after_put);
 }
 
+__code code5(struct Context* context) {
+    context->data[Allocate]->allocate.size = sizeof(long);
+    context->data[Allocate]->allocate.next = Code6;
+    goto meta(context, Allocator);
+}
 
-__code code5(struct Context* context) {
-    context->data[Allocate]->allocate.after_get = Code6;
+__code code6(struct Context* context) {
+    context->data[Counter] -> count = 0;
+    goto meta(context, Code7);
+}
+
+__code code7(struct Context* context) {
+    long loop = context->data[Counter]->count;
+    if(loop == NUM) {
+        goto meta(context, Exit);
+    }
+    context->data[Counter]->count++;
+    context->data[Allocate]->allocate.after_get = Code7;
     goto meta(context, Receiver);
 }
 
 __code meta_receiver(struct Context* context, enum Code next) {
+    // lock
+    pthread_mutex_lock(&context->data[Queue]->queue.mutex);
+    goto (context->code[next])(context);
 }
 
-__code receiver(struct Context* context, enum Code next) {
+__code receiver(struct Context* context) {
     goto meta(context, Get);
 }
 
 __code meta_get(struct Context* context, enum Code next) {
     if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) {
         printf("queue is empty\n");
-        goto (context->code[Exit])(context);
+        goto meta(context, Receiver);
+    } else {
+        printf("Get %d\n\n", context->data[Queue]->queue.first->element.value);
+        context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
+        context->data[Queue]->queue.count--;
     }
-    printf("Get %d\n", context->data[Queue]->queue.first->element.value);
-    context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
+    pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
     goto (context->code[next])(context);
 }
 
@@ -87,16 +137,22 @@
     goto meta_get(context, context->data[Allocate]->allocate.after_get);
 }
 
-__code code6(struct Context* context) {
-    goto meta(context, Exit);
+void* thread_func(void* context) {
+    goto start_code((struct Context*)context, Code1);
+    return NULL;
 }
 
-
 int main() {
     struct Context* context1 = (struct Context*)malloc(sizeof(struct Context));
     initSynchronizedQueueContext(context1);
     struct Context* context2 = (struct Context*)malloc(sizeof(struct Context));
     initSynchronizedQueueContext(context2);
+    struct Context* context3 = (struct Context*)malloc(sizeof(struct Context));
+    initSynchronizedQueueContext(context3);
     context2->data[Queue] = context1->data[Queue];
-    goto start_code(context1, Code1);
+    context3->data[Queue] = context1->data[Queue];
+    pthread_t thread1, thread2;
+    pthread_create(&thread1, NULL, thread_func, (void *)context2);
+    pthread_create(&thread2, NULL, thread_func, (void *)context3);
+    goto start_code(context1, Code5);
 }
--- a/src/synchronizedQueue/synchronizedQueueContext.c	Sat May 16 02:45:31 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueueContext.c	Sat May 16 20:17:58 2015 +0900
@@ -8,6 +8,7 @@
 extern __code code4(struct Context*);
 extern __code code5(struct Context*);
 extern __code code6(struct Context*);
+extern __code code7(struct Context*);
 extern __code meta(struct Context*);
 extern __code allocate(struct Context*);
 extern __code sender(struct Context*);
@@ -29,6 +30,7 @@
     context->code[Code4]     = code4;
     context->code[Code5]     = code5;
     context->code[Code6]     = code6;
+    context->code[Code7]     = code7;
     context->code[Allocator] = allocate;
     context->code[Sender]    = sender;
     context->code[Put]       = put;
@@ -44,6 +46,7 @@
     context->data[Queue]    = context->heap;
     context->heap          += sizeof(struct Queue);
     context->data[Queue]->queue.first = 0;
+    pthread_mutex_init(&context->data[Queue]->queue.mutex, NULL);
 
     context->dataNum = Queue;
 }
--- a/src/synchronizedQueue/synchronizedQueueContext.h	Sat May 16 02:45:31 2015 +0900
+++ b/src/synchronizedQueue/synchronizedQueueContext.h	Sat May 16 20:17:58 2015 +0900
@@ -1,5 +1,6 @@
 /* Context definition for  list example */
 
+#include <pthread.h>
 #define ALLOCATE_SIZE 100
 
 enum Code {
@@ -9,6 +10,7 @@
     Code4,
     Code5,
     Code6,
+    Code7,
     Allocator,
     Sender,
     Put,
@@ -20,6 +22,7 @@
 enum UniqueData {
     Allocate,
     Queue,
+    Counter,
 };
 
 struct Context {
@@ -39,6 +42,7 @@
         union Data* first;
         union Data* last;
         int   count;
+        pthread_mutex_t mutex;
     } queue;
     struct Element {
         int value;