comparison src/synchronizedQueue/synchronizedQueue.c @ 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
comparison
equal deleted inserted replaced
38:ce9fde200f3e 39:754c90e96e3d
4 #include "synchronizedQueueContext.h" 4 #include "synchronizedQueueContext.h"
5 5
6 #include "allocate.h" 6 #include "allocate.h"
7 #include "origin_cs.h" 7 #include "origin_cs.h"
8 8
9 #ifdef CLANG
10 #define _CbC_retrun __return
11 #define _CbC_environment __environment
12 #endif
13
14 #define NUM 50
15
9 extern __code initSynchronizedQueueContext(struct Context* context); 16 extern __code initSynchronizedQueueContext(struct Context* context);
10 17
11 __code code1(struct Context* context) { 18 //__code code1(struct Context* context) {
12 context->data[Allocate]->allocate.size = sizeof(struct Element); 19 // context->data[Allocate]->allocate.size = sizeof(struct Element);
13 context->data[Allocate]->allocate.next = Code2; 20 // context->data[Allocate]->allocate.next = Code2;
14 goto meta(context, Allocator); 21 // goto meta(context, Allocator);
15 } 22 //}
16 23
17 __code meta(struct Context* context, enum Code next) { 24 __code meta(struct Context* context, enum Code next) {
18 goto (context->code[next])(context); 25 goto (context->code[next])(context);
19 } 26 }
20 27
21 __code code2(struct Context* context) { 28 //__code code2(struct Context* context) {
22 context->data[Allocate]->allocate.after_put = Code3; 29 // context->data[Allocate]->allocate.after_put = Code3;
23 context->data[context->dataNum] -> element.value = 1024; 30 // context->data[context->dataNum] -> element.value = 1024;
24 goto meta(context, Sender); 31 // goto meta(context, Sender);
25 } 32 //}
26 33
27 __code meta_sender(struct Context* context, enum Code next) { 34 __code meta_sender(struct Context* context, enum Code next) {
35 // lock
36 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
28 goto (context->code[next])(context); 37 goto (context->code[next])(context);
29 } 38 }
30 39
31 __code sender(struct Context* context) { 40 __code sender(struct Context* context) {
32 goto meta(context, Put); 41 goto meta(context, Put);
33 } 42 }
34 43
44 __code code1(struct Context* context) {
45 context->data[Allocate]->allocate.size = sizeof(long);
46 context->data[Allocate]->allocate.next = Code2;
47 goto meta(context, Allocator);
48 }
49
50 __code code2(struct Context* context) {
51 context->data[Counter] -> count = 0;
52 goto meta(context, Code3);
53 }
54
35 __code code3(struct Context* context) { 55 __code code3(struct Context* context) {
56 long loop = context->data[Counter]->count;
57 if(loop == NUM) {
58 goto meta(context, Exit);
59 }
36 context->data[Allocate]->allocate.size = sizeof(struct Element); 60 context->data[Allocate]->allocate.size = sizeof(struct Element);
37 context->data[Allocate]->allocate.next = Code4; 61 context->data[Allocate]->allocate.next = Code4;
38 goto meta(context, Allocator); 62 goto meta(context, Allocator);
39 } 63 }
40 64
41 __code code4(struct Context* context) { 65 __code code4(struct Context* context) {
42 context->data[Allocate]->allocate.after_put = Code5; 66 context->data[Allocate]->allocate.after_put = Code3;
43 context->data[context->dataNum] -> element.value = 10; 67 context->data[context->dataNum] -> element.value = context->data[Counter]->count++;
44 goto meta(context, Sender); 68 goto meta(context, Sender);
45 } 69 }
46 70
47 __code meta_put(struct Context* context, enum Code next) { 71 __code meta_put(struct Context* context, enum Code next) {
48 if(context->data[Queue]->queue.first) { 72 if(context->data[Queue]->queue.first) {
51 } else { 75 } else {
52 context->data[Queue]->queue.first = context->data[context->dataNum]; 76 context->data[Queue]->queue.first = context->data[context->dataNum];
53 context->data[Queue]->queue.last = context->data[Queue]->queue.first; 77 context->data[Queue]->queue.last = context->data[Queue]->queue.first;
54 } 78 }
55 context->data[Queue]->queue.last->element.next = 0; 79 context->data[Queue]->queue.last->element.next = 0;
80 context->data[Queue]->queue.count++;
81 printf("Put %d\n\n", context->data[Queue]->queue.last->element.value);
82
83 // unlock
84 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
56 goto (context->code[next])(context); 85 goto (context->code[next])(context);
57 } 86 }
58 87
59 __code put(struct Context* context) { 88 __code put(struct Context* context) {
60 goto meta_put(context, context->data[Allocate]->allocate.after_put); 89 goto meta_put(context, context->data[Allocate]->allocate.after_put);
61 } 90 }
62 91
92 __code code5(struct Context* context) {
93 context->data[Allocate]->allocate.size = sizeof(long);
94 context->data[Allocate]->allocate.next = Code6;
95 goto meta(context, Allocator);
96 }
63 97
64 __code code5(struct Context* context) { 98 __code code6(struct Context* context) {
65 context->data[Allocate]->allocate.after_get = Code6; 99 context->data[Counter] -> count = 0;
100 goto meta(context, Code7);
101 }
102
103 __code code7(struct Context* context) {
104 long loop = context->data[Counter]->count;
105 if(loop == NUM) {
106 goto meta(context, Exit);
107 }
108 context->data[Counter]->count++;
109 context->data[Allocate]->allocate.after_get = Code7;
66 goto meta(context, Receiver); 110 goto meta(context, Receiver);
67 } 111 }
68 112
69 __code meta_receiver(struct Context* context, enum Code next) { 113 __code meta_receiver(struct Context* context, enum Code next) {
114 // lock
115 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
116 goto (context->code[next])(context);
70 } 117 }
71 118
72 __code receiver(struct Context* context, enum Code next) { 119 __code receiver(struct Context* context) {
73 goto meta(context, Get); 120 goto meta(context, Get);
74 } 121 }
75 122
76 __code meta_get(struct Context* context, enum Code next) { 123 __code meta_get(struct Context* context, enum Code next) {
77 if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) { 124 if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) {
78 printf("queue is empty\n"); 125 printf("queue is empty\n");
79 goto (context->code[Exit])(context); 126 goto meta(context, Receiver);
127 } else {
128 printf("Get %d\n\n", context->data[Queue]->queue.first->element.value);
129 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
130 context->data[Queue]->queue.count--;
80 } 131 }
81 printf("Get %d\n", context->data[Queue]->queue.first->element.value); 132 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
82 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
83 goto (context->code[next])(context); 133 goto (context->code[next])(context);
84 } 134 }
85 135
86 __code get(struct Context* context) { 136 __code get(struct Context* context) {
87 goto meta_get(context, context->data[Allocate]->allocate.after_get); 137 goto meta_get(context, context->data[Allocate]->allocate.after_get);
88 } 138 }
89 139
90 __code code6(struct Context* context) { 140 void* thread_func(void* context) {
91 goto meta(context, Exit); 141 goto start_code((struct Context*)context, Code1);
142 return NULL;
92 } 143 }
93
94 144
95 int main() { 145 int main() {
96 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); 146 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context));
97 initSynchronizedQueueContext(context1); 147 initSynchronizedQueueContext(context1);
98 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); 148 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context));
99 initSynchronizedQueueContext(context2); 149 initSynchronizedQueueContext(context2);
150 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context));
151 initSynchronizedQueueContext(context3);
100 context2->data[Queue] = context1->data[Queue]; 152 context2->data[Queue] = context1->data[Queue];
101 goto start_code(context1, Code1); 153 context3->data[Queue] = context1->data[Queue];
154 pthread_t thread1, thread2;
155 pthread_create(&thread1, NULL, thread_func, (void *)context2);
156 pthread_create(&thread2, NULL, thread_func, (void *)context3);
157 goto start_code(context1, Code5);
102 } 158 }