annotate src/synchronizedQueue/synchronizedQueue.c @ 40:46917f503bce

Add thread_join to synchronizedQueue
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Mon, 18 May 2015 15:24:46 +0900
parents 754c90e96e3d
children 4a16cbaab802
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
1 #include <stdlib.h>
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
2 #include <stdio.h>
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
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
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
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
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
34 __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
35 context->data[Allocate]->allocate.size = sizeof(long);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
36 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
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
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
40 __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
41 context->data[Counter] -> count = 0;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
42 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
43 }
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
44
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 __code code3(struct Context* context) {
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
46 long loop = context->data[Counter]->count;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
47 if(loop == NUM) {
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
48 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
49 }
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
50 context->data[Allocate]->allocate.size = sizeof(struct Element);
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
51 context->data[Allocate]->allocate.next = Code4;
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
52 goto meta(context, Allocator);
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
53 }
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
54
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
55 __code code4(struct Context* context) {
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
56 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
57 context->data[context->dataNum] -> element.value = context->data[Counter]->count++;
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
58 goto meta(context, Sender);
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
59 }
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
61 __code meta_sender(struct Context* context, enum Code next) {
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
62 // lock
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
63 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
64 goto (context->code[next])(context);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
65 }
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
66
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
67 __code sender(struct Context* context) {
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
68 goto meta_sender(context, Put);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
69 }
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
70
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
71 __code meta_put(struct Context* context, enum Code next) {
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
72 if(context->data[Queue]->queue.first) {
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
73 context->data[Queue]->queue.last->element.next = context->data[context->dataNum];
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
74 context->data[Queue]->queue.last = context->data[Queue]->queue.last->element.next;
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
75 } else {
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
76 context->data[Queue]->queue.first = context->data[context->dataNum];
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
77 context->data[Queue]->queue.last = context->data[Queue]->queue.first;
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
78 }
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
79 context->data[Queue]->queue.last->element.next = 0;
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
80 context->data[Queue]->queue.count++;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
81 printf("Put %d\n\n", context->data[Queue]->queue.last->element.value);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
82
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
83 // unlock
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
84 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
85 goto (context->code[next])(context);
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
86 }
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
87
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
88 __code put(struct Context* context) {
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
89 goto meta_put(context, context->data[Allocate]->allocate.after_put);
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 }
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
92 __code code5(struct Context* context) {
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
93 context->data[Allocate]->allocate.size = sizeof(long);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
94 context->data[Allocate]->allocate.next = Code6;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
95 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
96 }
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
98 __code code6(struct Context* context) {
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
99 context->data[Counter] -> count = 0;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
100 goto meta(context, Code7);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
101 }
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
102
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
103 __code code7(struct Context* context) {
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
104 long loop = context->data[Counter]->count;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
105 if(loop == NUM) {
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
106 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
107 }
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
108 context->data[Counter]->count++;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
109 context->data[Allocate]->allocate.after_get = Code7;
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
110 goto meta(context, Receiver);
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
111 }
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
113 __code meta_receiver(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
114 // lock
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
115 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
116 goto (context->code[next])(context);
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
117 }
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
119 __code receiver(struct Context* context) {
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
120 goto meta_receiver(context, Get);
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
123 __code meta_get(struct Context* context, enum Code next) {
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
124 if (context->data[Queue]->queue.count == 0) {
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
125 printf("queue is empty\n");
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
126 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
127 goto meta(context, Receiver);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
128 } else {
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
129 printf("Get %d\n\n", context->data[Queue]->queue.first->element.value);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
130 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
131 context->data[Queue]->queue.count--;
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
132 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
133 goto (context->code[next])(context);
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
134 }
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 }
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
137 __code get(struct Context* context) {
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
138 goto meta_get(context, context->data[Allocate]->allocate.after_get);
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
139 }
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
140
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
141 __code thread_exit(struct Context* context) {
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
142 free(context->code);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
143 free(context->data);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
144 free(context->heap_start);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
145 pthread_exit(NULL);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
146 }
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
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 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
149 goto start_code((struct Context*)context, Code1);
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
150 return NULL;
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
151 }
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
152
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
153 void* thread_func2(void* context) {
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
154 goto start_code((struct Context*)context, Code5);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
155 return NULL;
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
156 }
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
157
36
240c045ebab2 Add normal queue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
158 int main() {
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
159 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
160 initSynchronizedQueueContext(context1);
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
161 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
162 initSynchronizedQueueContext(context2);
39
754c90e96e3d Add synchronizedQueue. it use pthread_mutex_lock, unlock
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
163 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
164 initSynchronizedQueueContext(context3);
40
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
165 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
166 initSynchronizedQueueContext(context4);
38
ce9fde200f3e Add code segment sender & receiver to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
167 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
168 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
169 context4->data[Queue] = context1->data[Queue];
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
170 pthread_t thread1, thread2, thread3, thread4;
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
171 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
172 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
173 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
174 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
175 pthread_join(thread1, NULL);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
176 pthread_join(thread2, NULL);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
177 pthread_join(thread3, NULL);
46917f503bce Add thread_join to synchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
178 pthread_join(thread4, NULL);
30
604135010c3a Add file synchronizedQueue.c
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 }