Mercurial > hg > Members > Moririn
annotate src/synchronizedQueue/synchronizedQueueIdeal.c @ 258:4fe19a06d666
generate next args
author | mir3636 |
---|---|
date | Sat, 28 Jan 2017 12:25:35 +0900 |
parents | 5c3db1bef268 |
children |
rev | line source |
---|---|
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdlib.h> |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdio.h> |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "synchronizedQueueContext.h" |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include "allocate.h" |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 #include "origin_cs.h" |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 #ifdef CLANG |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 #define _CbC_retrun __return |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 #define _CbC_environment __environment |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 #endif |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #define NUM 100 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 extern __code initSynchronizedQueueContext(struct Context* context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 extern void allocator(struct Context* context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 __code meta(struct Context* context, enum Code next) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 goto (context->code[next])(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 //__code code1(struct Context* context) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 // context->data[Allocate]->allocate.size = sizeof(struct Element); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 // goto code2(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 //} |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 __code code1(struct Context* context, struct Allocate* allocate) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 allocate->size = sizeof(long); |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
30 allocator(); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 goto code2(count) |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 __code code2(struct Context* context, long* count) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 *count = 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 goto code3(count, allocate); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 __code code3(struct Context* context, long* count, struct Allocate* allocate) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 long loop = *count; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 if(loop == NUM) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 goto thread_exit(); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 allocate->size = sizeof(struct Element); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 allocator(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 got code4(count, allocate, element); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 __code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 allocate->after_put = Code3; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 element->value = (*count)++; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 struct Queue* queue = &context->data[Queue]->queue; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 goto sender(queue); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 __code meta_sender(struct Context* context, struct Queue* queue, enum Code next) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 // lock |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 pthread_mutex_lock(&queue->mutex); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 goto (context->code[next])(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 __code sender(struct Context* context, struct Queue* queue) { |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
63 goto put(queue); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 __code meta_put(struct Context* context, struct Queue* queue, enum Code next) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 // signal |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 pthread_cond_signal(&queue->cond); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 // unlock |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 pthread_mutex_unlock(&queue->mutex); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 goto (context->code[next])(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 __code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 if(queue->first) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 queue->last->next = element; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 } else { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 queue->first = element; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 queue->last = element; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 element->next = 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 queue->count++; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 printf("Put %d\n\n", element->value); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 goto meta_put(context, queue, allocate->after_put); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 __code code5(struct Context* context, struct Allocate* allocate) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 allocate->size = sizeof(long); |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
89 allocator(); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 long* count = &context->data[Counter]->count |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 goto code6(count); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 __code code5_stub(struct Context* context) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 goto code5(context, &context->data[Allocate]->allocate); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 __code code6(struct Context* context, long* count) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 *count = 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 struct Allocate* allocate = &context->data[Allocate]->allocate; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 goto code7(count, allocate); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 __code code7(struct Context* context, long* count, struct Allocate* allocate) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 long loop = *count; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 if(loop == NUM) { |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
107 goto threadExit(); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 (*count)++; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 allocate->after_get = Code7; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 goto receiver(queue); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 __code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 // lock |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 pthread_mutex_lock(&queue->mutex); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 goto (context->code[next])(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 __code receiver(struct Context* context, struct Queue* queue) { |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
121 goto get(queue); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123 |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
124 __code meta_get(struct Context* context, struct Queue* queue, enum Code next) { |
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
125 pthread_mutex_unlock(&queue->mutex); |
67
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126 goto (context->code[next])(context); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
127 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 __code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 // thread wait if queue is empty |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 while (queue->count == 0) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 pthread_cond_wait(&queue->cond, &queue->mutex); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 printf(" Get %d\n\n", queue->first->value); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135 queue->first = (queue->first->next) ? queue->first->next : 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 queue->count--; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137 goto meta_get(context, allocate->after_get); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140 __code thread_exit(struct Context* context) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 free(context->code); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 free(context->data); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 free(context->heapStart); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144 pthread_exit(0); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147 void* thread_func(void* context) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 goto start_code((struct Context*)context, Code1); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 return 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
151 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 void* thread_func2(void* context) { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 goto start_code((struct Context*)context, Code5); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
154 return 0; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 } |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 int main() { |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 initSynchronizedQueueContext(context1); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 initSynchronizedQueueContext(context2); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context)); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 initSynchronizedQueueContext(context3); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 struct Context* context4 = (struct Context*)malloc(sizeof(struct Context)); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 initSynchronizedQueueContext(context4); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 context2->data[Queue] = context1->data[Queue]; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 context3->data[Queue] = context1->data[Queue]; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 context4->data[Queue] = context1->data[Queue]; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 pthread_t thread1, thread2, thread3, thread4; |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 pthread_create(&thread1, NULL, thread_func, (void *)context1); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171 pthread_create(&thread2, NULL, thread_func, (void *)context2); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 pthread_create(&thread3, NULL, thread_func2, (void *)context3); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 pthread_create(&thread4, NULL, thread_func2, (void *)context4); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 pthread_join(thread1, NULL); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 pthread_join(thread2, NULL); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 pthread_join(thread3, NULL); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 pthread_join(thread4, NULL); |
9653f09ea8eb
Add synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 } |