Mercurial > hg > Members > Moririn
annotate src/synchronizedQueue/synchronizedQueue.c @ 63:2a40d697bf4e
Delete while loop for cas
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 13 Jul 2015 23:20:31 +0900 |
parents | 4283b87ddbf4 |
children | a870c84acd0e |
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); |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
18 //__code code1(struct Context* context) { |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
19 // context->data[Allocate]->allocate.size = sizeof(struct Element); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
20 // context->data[Allocate]->allocate.next = Code2; |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
21 // goto meta(context, Allocator); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
22 //} |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 __code meta(struct Context* context, enum Code next) { |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 goto (context->code[next])(context); |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 } |
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
28 //__code code2(struct Context* context) { |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
29 // context->data[Allocate]->allocate.after_put = Code3; |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
30 // context->data[context->dataNum] -> element.value = 1024; |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
31 // goto meta(context, Sender); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
32 //} |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
33 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
34 __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
|
35 allocate->size = sizeof(long); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
36 allocate->next = Code2; |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
37 goto meta(context, Allocator); |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
38 } |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
39 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
40 __code code1_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
41 goto code1(context, &context->data[Allocate]->allocate); |
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 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
44 __code code2(struct Context* context, long* count) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
45 *count = 0; |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
46 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
|
47 } |
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
48 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
49 __code code2_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
50 goto code2(context, &context->data[Counter]->count); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
51 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
52 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
53 __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
|
54 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
|
55 if(loop == NUM) { |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
56 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
|
57 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
58 allocate->size = sizeof(struct Element); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
59 allocate->next = Code4; |
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, Allocator); |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
61 } |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
62 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
63 __code code3_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
64 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
|
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 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
|
68 allocate->after_put = Code3; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
69 element->value = (*count)++; |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
70 goto meta(context, Sender); |
36 | 71 } |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
73 __code code4_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
74 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
|
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 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
|
78 // lock |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
79 pthread_mutex_lock(&queue->mutex); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
80 goto (context->code[next])(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
81 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
82 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
83 __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
|
84 goto meta_sender(context, queue, Put); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
85 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
86 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
87 __code sender_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
88 goto sender(context, &context->data[Queue]->queue); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
89 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
90 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
91 __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
|
92 // signal |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
93 pthread_cond_signal(&queue->cond); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
94 // unlock |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
95 pthread_mutex_unlock(&queue->mutex); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
96 goto (context->code[next])(context); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
97 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
98 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
99 __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
|
100 if(queue->first) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
101 queue->last->next = element; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
102 } else { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
103 queue->first = 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 queue->last = element; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
106 element->next = 0; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
107 queue->count++; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
108 printf("Put %d\n\n", element->value); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
109 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
|
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 put_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
113 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
|
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 code5(struct Context* context, struct Allocate* allocate) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
117 allocate->size = sizeof(long); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
118 allocate->next = Code6; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
119 goto meta(context, Allocator); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
120 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
121 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
122 __code code5_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
123 goto code5(context, &context->data[Allocate]->allocate); |
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 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
126 __code code6(struct Context* context, long* count) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
127 *count = 0; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
128 goto meta(context, Code7); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
129 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
130 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
131 __code code6_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
132 goto code6(context, &context->data[Counter]->count); |
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(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
|
136 long loop = *count; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
137 if(loop == NUM) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
138 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
|
139 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
140 (*count)++; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
141 allocate->after_get = Code7; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
142 goto meta(context, Receiver); |
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 code7_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
146 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
|
147 } |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
148 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
149 __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
|
150 // lock |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
151 pthread_mutex_lock(&queue->mutex); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
152 goto (context->code[next])(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
153 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
154 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
155 __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
|
156 goto meta_receiver(context, queue, Get); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
157 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
158 |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
159 __code receiver_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
160 goto receiver(context, &context->data[Queue]->queue); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
161 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
162 __code meta_get(struct Context* context, enum Code next) { |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
163 pthread_mutex_unlock(&context->data[Queue]->queue.mutex); |
36 | 164 goto (context->code[next])(context); |
165 } | |
166 | |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
167 __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
|
168 // thread wait if queue is empty |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
169 while (queue->count == 0) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
170 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
|
171 } |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
172 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
|
173 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
|
174 queue->count--; |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
175 goto meta_get(context, allocate->after_get); |
36 | 176 } |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
178 __code get_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
179 goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); |
36 | 180 } |
181 | |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
182 __code thread_exit(struct Context* context) { |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
183 free(context->code); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
184 free(context->data); |
63
2a40d697bf4e
Delete while loop for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
58
diff
changeset
|
185 free(context->heapStart); |
41
4a16cbaab802
Add synchronizedQueue for cas
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
40
diff
changeset
|
186 pthread_exit(0); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
187 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
188 |
58
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
189 __code thread_exit_stub(struct Context* context) { |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
190 goto thread_exit(context); |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
191 } |
4283b87ddbf4
Add stub to synchronizedQueues
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
192 |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
193 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
|
194 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
|
195 return 0; |
36 | 196 } |
197 | |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
198 void* thread_func2(void* context) { |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
199 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
|
200 return 0; |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
201 } |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
202 |
36 | 203 int main() { |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
204 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
|
205 initSynchronizedQueueContext(context1); |
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
206 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
|
207 initSynchronizedQueueContext(context2); |
39
754c90e96e3d
Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
38
diff
changeset
|
208 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
|
209 initSynchronizedQueueContext(context3); |
40
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
210 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
|
211 initSynchronizedQueueContext(context4); |
38
ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
36
diff
changeset
|
212 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
|
213 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
|
214 context4->data[Queue] = context1->data[Queue]; |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
215 pthread_t thread1, thread2, thread3, thread4; |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 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
|
220 pthread_join(thread1, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
221 pthread_join(thread2, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
222 pthread_join(thread3, NULL); |
46917f503bce
Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
39
diff
changeset
|
223 pthread_join(thread4, NULL); |
30
604135010c3a
Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 } |