comparison src/synchronizedQueue/synchronizedQueue.c @ 58:4283b87ddbf4

Add stub to synchronizedQueues
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Tue, 16 Jun 2015 15:59:48 +0900
parents 83ee9c75115a
children 2a40d697bf4e
comparison
equal deleted inserted replaced
55:2ff693c5563c 58:4283b87ddbf4
29 // context->data[Allocate]->allocate.after_put = Code3; 29 // context->data[Allocate]->allocate.after_put = Code3;
30 // context->data[context->dataNum] -> element.value = 1024; 30 // context->data[context->dataNum] -> element.value = 1024;
31 // goto meta(context, Sender); 31 // goto meta(context, Sender);
32 //} 32 //}
33 33
34 __code code1(struct Context* context) { 34 __code code1(struct Context* context, struct Allocate* allocate) {
35 context->data[Allocate]->allocate.size = sizeof(long); 35 allocate->size = sizeof(long);
36 context->data[Allocate]->allocate.next = Code2; 36 allocate->next = Code2;
37 goto meta(context, Allocator); 37 goto meta(context, Allocator);
38 } 38 }
39 39
40 __code code2(struct Context* context) { 40 __code code1_stub(struct Context* context) {
41 context->data[Counter] -> count = 0; 41 goto code1(context, &context->data[Allocate]->allocate);
42 }
43
44 __code code2(struct Context* context, long* count) {
45 *count = 0;
42 goto meta(context, Code3); 46 goto meta(context, Code3);
43 } 47 }
44 48
45 __code code3(struct Context* context) { 49 __code code2_stub(struct Context* context) {
46 long loop = context->data[Counter]->count; 50 goto code2(context, &context->data[Counter]->count);
51 }
52
53 __code code3(struct Context* context, long* count, struct Allocate* allocate) {
54 long loop = *count;
47 if(loop == NUM) { 55 if(loop == NUM) {
48 goto meta(context, ThreadExit); 56 goto meta(context, ThreadExit);
49 } 57 }
50 context->data[Allocate]->allocate.size = sizeof(struct Element); 58 allocate->size = sizeof(struct Element);
51 context->data[Allocate]->allocate.next = Code4; 59 allocate->next = Code4;
52 goto meta(context, Allocator); 60 goto meta(context, Allocator);
53 } 61 }
54 62
55 __code code4(struct Context* context) { 63 __code code3_stub(struct Context* context) {
56 context->data[Allocate]->allocate.after_put = Code3; 64 goto code3(context, &context->data[Counter]->count, &context->data[Allocate]->allocate);
57 context->data[context->dataNum] -> element.value = context->data[Counter]->count++; 65 }
66
67 __code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) {
68 allocate->after_put = Code3;
69 element->value = (*count)++;
58 goto meta(context, Sender); 70 goto meta(context, Sender);
59 } 71 }
60 72
61 __code meta_sender(struct Context* context, enum Code next) { 73 __code code4_stub(struct Context* context) {
74 goto code4(context, &context->data[Counter]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element);
75 }
76
77 __code meta_sender(struct Context* context, struct Queue* queue, enum Code next) {
62 // lock 78 // lock
63 pthread_mutex_lock(&context->data[Queue]->queue.mutex); 79 pthread_mutex_lock(&queue->mutex);
64 goto (context->code[next])(context); 80 goto (context->code[next])(context);
65 } 81 }
66 82
67 __code sender(struct Context* context) { 83 __code sender(struct Context* context, struct Queue* queue) {
68 goto meta_sender(context, Put); 84 goto meta_sender(context, queue, Put);
69 } 85 }
70 86
71 __code meta_put(struct Context* context, enum Code next) { 87 __code sender_stub(struct Context* context) {
72 if(context->data[Queue]->queue.first) { 88 goto sender(context, &context->data[Queue]->queue);
73 context->data[Queue]->queue.last->element.next = context->data[context->dataNum]; 89 }
74 context->data[Queue]->queue.last = context->data[Queue]->queue.last->element.next; 90
91 __code meta_put(struct Context* context, struct Queue* queue, enum Code next) {
92 // signal
93 pthread_cond_signal(&queue->cond);
94 // unlock
95 pthread_mutex_unlock(&queue->mutex);
96 goto (context->code[next])(context);
97 }
98
99 __code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) {
100 if(queue->first) {
101 queue->last->next = element;
75 } else { 102 } else {
76 context->data[Queue]->queue.first = context->data[context->dataNum]; 103 queue->first = element;
77 context->data[Queue]->queue.last = context->data[Queue]->queue.first; 104 }
78 } 105 queue->last = element;
79 context->data[Queue]->queue.last->element.next = 0; 106 element->next = 0;
80 context->data[Queue]->queue.count++; 107 queue->count++;
81 printf("Put %d\n\n", context->data[Queue]->queue.last->element.value); 108 printf("Put %d\n\n", element->value);
82 109 goto meta_put(context, queue, allocate->after_put);
83 // signal 110 }
84 pthread_cond_signal(&context->data[Queue]->queue.cond); 111
85 // unlock 112 __code put_stub(struct Context* context) {
86 pthread_mutex_unlock(&context->data[Queue]->queue.mutex); 113 goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element);
87 goto (context->code[next])(context); 114 }
88 } 115
89 116 __code code5(struct Context* context, struct Allocate* allocate) {
90 __code put(struct Context* context) { 117 allocate->size = sizeof(long);
91 goto meta_put(context, context->data[Allocate]->allocate.after_put); 118 allocate->next = Code6;
92 }
93
94 __code code5(struct Context* context) {
95 context->data[Allocate]->allocate.size = sizeof(long);
96 context->data[Allocate]->allocate.next = Code6;
97 goto meta(context, Allocator); 119 goto meta(context, Allocator);
98 } 120 }
99 121
100 __code code6(struct Context* context) { 122 __code code5_stub(struct Context* context) {
101 context->data[Counter] -> count = 0; 123 goto code5(context, &context->data[Allocate]->allocate);
124 }
125
126 __code code6(struct Context* context, long* count) {
127 *count = 0;
102 goto meta(context, Code7); 128 goto meta(context, Code7);
103 } 129 }
104 130
105 __code code7(struct Context* context) { 131 __code code6_stub(struct Context* context) {
106 long loop = context->data[Counter]->count; 132 goto code6(context, &context->data[Counter]->count);
133 }
134
135 __code code7(struct Context* context, long* count, struct Allocate* allocate) {
136 long loop = *count;
107 if(loop == NUM) { 137 if(loop == NUM) {
108 goto meta(context, ThreadExit); 138 goto meta(context, ThreadExit);
109 } 139 }
110 context->data[Counter]->count++; 140 (*count)++;
111 context->data[Allocate]->allocate.after_get = Code7; 141 allocate->after_get = Code7;
112 goto meta(context, Receiver); 142 goto meta(context, Receiver);
113 } 143 }
114 144
115 __code meta_receiver(struct Context* context, enum Code next) { 145 __code code7_stub(struct Context* context) {
146 goto code7(context, &context->data[Counter]->count, &context->data[Allocate]->allocate);
147 }
148
149 __code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) {
116 // lock 150 // lock
117 pthread_mutex_lock(&context->data[Queue]->queue.mutex); 151 pthread_mutex_lock(&queue->mutex);
118 goto (context->code[next])(context); 152 goto (context->code[next])(context);
119 } 153 }
120 154
121 __code receiver(struct Context* context) { 155 __code receiver(struct Context* context, struct Queue* queue) {
122 goto meta_receiver(context, Get); 156 goto meta_receiver(context, queue, Get);
123 } 157 }
124 158
159 __code receiver_stub(struct Context* context) {
160 goto receiver(context, &context->data[Queue]->queue);
161 }
125 __code meta_get(struct Context* context, enum Code next) { 162 __code meta_get(struct Context* context, enum Code next) {
163 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
164 goto (context->code[next])(context);
165 }
166
167 __code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) {
126 // thread wait if queue is empty 168 // thread wait if queue is empty
127 while (context->data[Queue]->queue.count == 0) { 169 while (queue->count == 0) {
128 pthread_cond_wait(&context->data[Queue]->queue.cond, &context->data[Queue]->queue.mutex); 170 pthread_cond_wait(&queue->cond, &queue->mutex);
129 } 171 }
130 printf(" Get %d\n\n", context->data[Queue]->queue.first->element.value); 172 printf(" Get %d\n\n", queue->first->value);
131 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0; 173 queue->first = (queue->first->next) ? queue->first->next : 0;
132 context->data[Queue]->queue.count--; 174 queue->count--;
133 pthread_mutex_unlock(&context->data[Queue]->queue.mutex); 175 goto meta_get(context, allocate->after_get);
134 goto (context->code[next])(context); 176 }
135 } 177
136 178 __code get_stub(struct Context* context) {
137 __code get(struct Context* context) { 179 goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element);
138 goto meta_get(context, context->data[Allocate]->allocate.after_get);
139 } 180 }
140 181
141 __code thread_exit(struct Context* context) { 182 __code thread_exit(struct Context* context) {
142 free(context->code); 183 free(context->code);
143 free(context->data); 184 free(context->data);
144 free(context->heap_start); 185 free(context->heap_start);
145 pthread_exit(0); 186 pthread_exit(0);
187 }
188
189 __code thread_exit_stub(struct Context* context) {
190 goto thread_exit(context);
146 } 191 }
147 192
148 void* thread_func(void* context) { 193 void* thread_func(void* context) {
149 goto start_code((struct Context*)context, Code1); 194 goto start_code((struct Context*)context, Code1);
150 return 0; 195 return 0;