Mercurial > hg > Gears > GearsAgda
annotate src/synchronizedQueue/synchronizedQueue.c @ 143:34a7a21edc36
recude stack get using traverse field
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 09 Nov 2016 22:33:16 +0900 |
parents | 765ee56d68f1 |
children |
rev | line source |
---|---|
36 | 1 #include <stdlib.h> |
2 #include <stdio.h> | |
3 | |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "synchronizedQueueContext.h" |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 |
36 | 6 #include "allocate.h" |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 #include "origin_cs.h" |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
9 #ifdef CLANG |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
10 #define _CbC_retrun __return |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
11 #define _CbC_environment __environment |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
12 #endif |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
13 |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
14 #define NUM 100 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
15 |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 extern __code initSynchronizedQueueContext(struct Context* context); |
66
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
17 extern void allocator(struct Context* context); |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 |
66
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
19 //__code code1(struct Context* context) { |
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
20 // context->data[Allocate]->allocate.size = sizeof(struct Element); |
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
21 // goto code2(context); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
22 //} |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
23 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
24 __code code1(struct Context* context, struct Allocate* allocate) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
25 allocate->size = sizeof(long); |
66
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
26 allocator(context); |
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
27 goto meta(context, Code2); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
28 } |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
29 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
30 __code code1_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
31 goto code1(context, &context->data[Allocate]->allocate); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
32 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
33 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
34 __code code2(struct Context* context, long* count) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
35 *count = 0; |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
36 goto meta(context, Code3); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
37 } |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
38 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
39 __code code2_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
40 goto code2(context, &context->data[Counter]->count); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
41 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
42 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
43 __code code3(struct Context* context, long* count, struct Allocate* allocate) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
44 long loop = *count; |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
45 if(loop == NUM) { |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
46 goto meta(context, ThreadExit); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
47 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
48 allocate->size = sizeof(struct Element); |
66
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
49 allocator(context); |
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
50 goto meta(context, Code4); |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
51 } |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
52 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
53 __code code3_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
54 goto code3(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
55 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
56 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
57 __code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
58 allocate->after_put = Code3; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
59 element->value = (*count)++; |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
60 goto meta(context, Sender); |
36 | 61 } |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
63 __code code4_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
64 goto code4(context, &context->data[Counter]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
65 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
66 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
67 __code meta_sender(struct Context* context, struct Queue* queue, enum Code next) { |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
68 // lock |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
69 pthread_mutex_lock(&queue->mutex); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
70 goto (context->code[next])(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
71 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
72 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
73 __code sender(struct Context* context, struct Queue* queue) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
74 goto meta_sender(context, queue, Put); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
75 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
76 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
77 __code sender_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
78 goto sender(context, &context->data[Queue]->queue); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
79 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
80 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
81 __code meta_put(struct Context* context, struct Queue* queue, enum Code next) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
82 // signal |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
83 pthread_cond_signal(&queue->cond); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
84 // unlock |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
85 pthread_mutex_unlock(&queue->mutex); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
86 goto (context->code[next])(context); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
87 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
88 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
89 __code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
90 if(queue->first) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
91 queue->last->next = element; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
92 } else { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
93 queue->first = element; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
94 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
95 queue->last = element; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
96 element->next = 0; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
97 queue->count++; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
98 printf("Put %d\n\n", element->value); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
99 goto meta_put(context, queue, allocate->after_put); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
100 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
101 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
102 __code put_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
103 goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
104 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
105 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
106 __code code5(struct Context* context, struct Allocate* allocate) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
107 allocate->size = sizeof(long); |
66
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
108 allocator(context); |
a870c84acd0e
Change synchronizedQueue allocator from __code to Function call
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
109 goto meta(context, Code6); |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
110 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
111 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
112 __code code5_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
113 goto code5(context, &context->data[Allocate]->allocate); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
114 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
115 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
116 __code code6(struct Context* context, long* count) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
117 *count = 0; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
118 goto meta(context, Code7); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
119 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
120 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
121 __code code6_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
122 goto code6(context, &context->data[Counter]->count); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
123 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
124 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
125 __code code7(struct Context* context, long* count, struct Allocate* allocate) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
126 long loop = *count; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
127 if(loop == NUM) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
128 goto meta(context, ThreadExit); |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
129 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
130 (*count)++; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
131 allocate->after_get = Code7; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
132 goto meta(context, Receiver); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
133 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
134 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
135 __code code7_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
136 goto code7(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
137 } |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
138 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
139 __code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
140 // lock |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
141 pthread_mutex_lock(&queue->mutex); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
142 goto (context->code[next])(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
143 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
144 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
145 __code receiver(struct Context* context, struct Queue* queue) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
146 goto meta_receiver(context, queue, Get); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
147 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
148 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
149 __code receiver_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
150 goto receiver(context, &context->data[Queue]->queue); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
151 } |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
152 |
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
153 __code meta_get(struct Context* context, struct Queue* queue, enum Code next) { |
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
154 pthread_mutex_unlock(&queue->mutex); |
36 | 155 goto (context->code[next])(context); |
156 } | |
157 | |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
158 __code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
159 // thread wait if queue is empty |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
160 while (queue->count == 0) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
161 pthread_cond_wait(&queue->cond, &queue->mutex); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
162 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
163 printf(" Get %d\n\n", queue->first->value); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
164 queue->first = (queue->first->next) ? queue->first->next : 0; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
165 queue->count--; |
68
5c3db1bef268
Edit synchronizedQueueIdeal
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
166 goto meta_get(context, queue, allocate->after_get); |
36 | 167 } |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
169 __code get_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
170 goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); |
36 | 171 } |
172 | |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
173 __code thread_exit(struct Context* context) { |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
174 free(context->code); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
175 free(context->data); |
63
2a40d697bf4e
Delete while loop for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
58
diff
changeset
|
176 free(context->heapStart); |
41
4a16cbaab802
Add synchronizedQueue for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
40
diff
changeset
|
177 pthread_exit(0); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
178 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
179 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
180 __code thread_exit_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
181 goto thread_exit(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
182 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
183 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
184 void* thread_func(void* context) { |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
185 goto start_code((struct Context*)context, Code1); |
41
4a16cbaab802
Add synchronizedQueue for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
40
diff
changeset
|
186 return 0; |
36 | 187 } |
188 | |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
189 void* thread_func2(void* context) { |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
190 goto start_code((struct Context*)context, Code5); |
41
4a16cbaab802
Add synchronizedQueue for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
40
diff
changeset
|
191 return 0; |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
192 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
193 |
36 | 194 int main() { |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
195 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
196 initSynchronizedQueueContext(context1); |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
197 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
198 initSynchronizedQueueContext(context2); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
199 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context)); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
200 initSynchronizedQueueContext(context3); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
201 struct Context* context4 = (struct Context*)malloc(sizeof(struct Context)); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
202 initSynchronizedQueueContext(context4); |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
203 context2->data[Queue] = context1->data[Queue]; |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
204 context3->data[Queue] = context1->data[Queue]; |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
205 context4->data[Queue] = context1->data[Queue]; |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
206 pthread_t thread1, thread2, thread3, thread4; |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
207 pthread_create(&thread1, NULL, thread_func, (void *)context1); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
208 pthread_create(&thread2, NULL, thread_func, (void *)context2); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
209 pthread_create(&thread3, NULL, thread_func2, (void *)context3); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
210 pthread_create(&thread4, NULL, thread_func2, (void *)context4); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
211 pthread_join(thread1, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
212 pthread_join(thread2, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
213 pthread_join(thread3, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
214 pthread_join(thread4, NULL); |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 } |