Mercurial > hg > Members > Moririn
annotate src/parallel_execution/queue.c @ 233:06133afb3b5b
create worker start_code
author | mir3636 |
---|---|
date | Sun, 22 Jan 2017 20:02:21 +0900 |
parents | c34e6aa10967 |
children |
rev | line source |
---|---|
167 | 1 #include "queue.h" |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
2 #include "origin_cs.h" |
138 | 3 #include <stdio.h> |
86 | 4 |
167 | 5 union Data* createSingleLinkedQueue(struct Context* context) { |
203 | 6 struct Queue* queue = &ALLOCATE(context, Queue)->Queue; |
7 struct SingleLinkedQueue* singleLinkedQueue = &ALLOCATE(context, SingleLinkedQueue)->SingleLinkedQueue; | |
167 | 8 queue->queue = (union Data*)singleLinkedQueue; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
9 singleLinkedQueue->top = NULL; |
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
10 singleLinkedQueue->last = NULL; |
167 | 11 queue->take = C_takeSingleLinkedQueue; |
12 queue->put = C_putSingleLinkedQueue; | |
13 queue->isEmpty = C_isEmptySingleLinkedQueue; | |
14 queue->clear = C_clearSingleLinkedQueue; | |
15 return (union Data*)(queue); | |
86 | 16 } |
17 | |
167 | 18 void printQueue1(union Data* data) { |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
203
diff
changeset
|
19 struct Node* node = &data->Element.data->Node; |
138 | 20 if (node == NULL) { |
21 printf("NULL"); | |
22 } else { | |
23 printf("key = %d ,", node->key); | |
203 | 24 printQueue1((union Data*)data->Element.next); |
138 | 25 } |
26 } | |
27 | |
167 | 28 void printQueue(union Data* data) { |
29 printQueue1(data); | |
138 | 30 printf("\n"); |
31 } | |
32 | |
167 | 33 __code clearSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue,enum Code next) { |
34 queue->top = NULL; | |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
35 goto meta(context, next); |
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
36 } |
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
37 |
167 | 38 __code clearSingleLinkedQueue_stub(struct Context* context) { |
203 | 39 goto clearSingleLinkedQueue(context, (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, context->data[D_Queue]->Queue.next); |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
40 } |
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
41 |
167 | 42 __code putSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, struct Element* element, union Data* data, enum Code next) { |
43 element->next = NULL; | |
130 | 44 element->data = data; |
167 | 45 if (queue->last) { |
46 Element* last = queue->last; | |
47 last->next = element; | |
48 queue->last = element; | |
49 } else { | |
50 queue->top = element; | |
51 queue->last = element; | |
52 } | |
130 | 53 goto meta(context, next); |
86 | 54 } |
55 | |
167 | 56 __code putSingleLinkedQueue_stub(struct Context* context) { |
203 | 57 struct Element* element = &ALLOCATE(context, Element)->Element; |
167 | 58 goto putSingleLinkedQueue(context, |
203 | 59 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
60 element, |
203 | 61 context->data[D_Queue]->Queue.data, |
62 context->data[D_Queue]->Queue.next); | |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
63 } |
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
64 |
167 | 65 __code takeSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, union Data** data, enum Code next) { |
66 if (queue->top) { | |
67 *data = queue->top->data; | |
68 queue->top = queue->top->next; | |
136 | 69 } else { |
70 *data = NULL; | |
71 } | |
130 | 72 goto meta(context, next); |
86 | 73 } |
74 | |
167 | 75 __code takeSingleLinkedQueue_stub(struct Context* context) { |
76 goto takeSingleLinkedQueue(context, | |
203 | 77 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
78 &context->data[D_Queue]->Queue.data, | |
79 context->data[D_Queue]->Queue.next); | |
133 | 80 } |
81 | |
167 | 82 __code isEmptySingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, enum Code next,enum Code whenEmpty) { |
83 if (queue->top) | |
133 | 84 goto meta(context, next); |
85 else | |
86 goto meta(context, whenEmpty); | |
87 } | |
88 | |
167 | 89 __code isEmptySingleLinkedQueue_stub(struct Context* context) { |
90 goto isEmptySingleLinkedQueue(context, | |
203 | 91 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
92 context->data[D_Queue]->Queue.next, | |
93 context->data[D_Queue]->Queue.whenEmpty); | |
132 | 94 } |
95 | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
96 union Data* createSynchronizedQueue(struct Context* context) { |
203 | 97 struct Queue* queue = &ALLOCATE(context, Queue)->Queue; |
98 struct SingleLinkedQueue* singleLinkedQueue = &ALLOCATE(context, SingleLinkedQueue)->SingleLinkedQueue; | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
99 queue->queue = (union Data*)singleLinkedQueue; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
100 singleLinkedQueue->top = NULL; |
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
101 singleLinkedQueue->last = NULL; |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
102 queue->take = C_takeSynchronizedQueue; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
103 queue->put = C_putSynchronizedQueue; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
104 queue->isEmpty = C_isEmptySynchronizedQueue; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
105 queue->clear = C_clearSynchronizedQueue; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
106 return (union Data*)(queue); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
107 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
108 |
203 | 109 __code clearSynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue,enum Code next) { |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
110 struct Element* top = queue->top; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
111 if (__sync_bool_compare_and_swap(&queue->top, top, NULL)) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
112 goto meta(context, next); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
113 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
114 goto meta(context, C_clearSynchronizedQueue); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
115 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
116 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
117 |
203 | 118 __code clearSynchronizedQueue_stub(struct Context* context) { |
119 goto clearSingleLinkedQueue(context, (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, context->data[D_Queue]->Queue.next); | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
120 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
121 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
122 __code putSynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, struct Element* element, union Data* data, enum Code next) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
123 element->next = NULL; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
124 element->data = data; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
125 if (queue->last) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
126 Element* last = queue->last; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
127 if (__sync_bool_compare_and_swap(&queue->last, last, element)) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
128 last->next = element; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
129 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
130 goto meta(context, C_putSynchronizedQueue); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
131 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
132 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
133 if (__sync_bool_compare_and_swap(&queue->top, NULL, element)) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
134 queue->last = element; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
135 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
136 goto meta(context, C_putSynchronizedQueue); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
137 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
138 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
139 goto meta(context, next); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
140 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
141 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
142 __code putSynchronizedQueue_stub(struct Context* context) { |
203 | 143 struct Element* element = &ALLOCATE(context, Element)->Element; |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
144 goto putSynchronizedQueue(context, |
203 | 145 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
146 element, |
203 | 147 context->data[D_Queue]->Queue.data, |
148 context->data[D_Queue]->Queue.next); | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
149 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
150 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
151 __code takeSynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, union Data** data, enum Code next) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
152 if (queue->top) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
153 struct Element* top = queue->top; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
154 if (__sync_bool_compare_and_swap(&queue->top, top, top->next)) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
155 *data = top->data; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
156 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
157 goto meta(context, C_takeSynchronizedQueue); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
158 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
159 } else { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
160 *data = NULL; |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
161 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
162 goto meta(context, next); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
163 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
164 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
165 __code takeSynchronizedQueue_stub(struct Context* context) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
166 goto takeSynchronizedQueue(context, |
203 | 167 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
168 &context->data[D_Queue]->Queue.data, | |
169 context->data[D_Queue]->Queue.next); | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
170 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
171 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
172 __code isEmptySynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, enum Code next,enum Code whenEmpty) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
173 if (queue->top) |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
174 goto meta(context, next); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
175 else |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
176 goto meta(context, whenEmpty); |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
177 } |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
178 |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
179 __code isEmptySynchronizedQueue_stub(struct Context* context) { |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
180 goto isEmptySynchronizedQueue(context, |
203 | 181 (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, |
182 context->data[D_Queue]->Queue.next, | |
183 context->data[D_Queue]->Queue.whenEmpty); | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
184 } |