/
main.c
309 lines (252 loc) · 6.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* The person who associated a work with this deed has dedicated the
* work to the public domain by waiving all of his or her rights to
* the work worldwide under copyright law, including all related and
* neighboring rights, to the extent allowed by law. You can copy,
* modify, distribute and perform the work, even for commercial
* purposes, all without asking permission.
*/
/*
* On your Linux terminal, run
*
* make all
* ./simpletm
*
* to build and execute this example. Building requires gcc and the
* usual C development tools for Unix.
*/
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define RESOURCE_HAS_LOCAL_VALUE (1ul << 0)
/**
* An integer value with an associated owner.
*/
struct int_resource {
int value;
int local_value;
unsigned long flags;
pthread_t owner;
pthread_mutex_t lock;
};
#define INT_RESOURCE_INITIALIZER \
{ \
.value = 0, \
.local_value = 0, \
.flags = 0, \
.owner = 0, \
.lock = PTHREAD_MUTEX_INITIALIZER \
}
static bool
acquire_int_resource(struct int_resource* res)
{
pthread_t self = pthread_self();
int err = pthread_mutex_lock(&res->lock);
if (err) {
errno = err;
perror("pthread_mutex_lock");
goto err_pthread_mutex_lock;
}
if (res->owner && res->owner != self) {
/* Owned by another thread. */
goto err_has_owner;
} else if (!res->owner) {
/* Now owned by us. */
res->owner = self;
}
err = pthread_mutex_unlock(&res->lock);
if (err) {
errno = err;
perror("pthread_mutex_unlock");
abort(); /* We cannot release; let's abort for now. */
}
return true;
err_has_owner:
/* fall through */
err_pthread_mutex_lock:
err = pthread_mutex_unlock(&res->lock);
if (err) {
errno = err;
perror("pthread_mutex_unlock");
abort(); /* We cannot release; let's abort for now. */
}
return false;
}
static void
release_int_resource(struct int_resource* res, bool commit)
{
pthread_t self = pthread_self();
int err = pthread_mutex_lock(&res->lock);
if (err) {
errno = err;
perror("pthread_mutex_lock");
abort(); /* We cannot release; let's abort for now. */
}
if (res->owner && res->owner == self) {
if (res->flags & RESOURCE_HAS_LOCAL_VALUE) {
if (commit) {
res->value = res->local_value;
}
res->flags &= ~RESOURCE_HAS_LOCAL_VALUE;
}
res->owner = 0;
}
err = pthread_mutex_unlock(&res->lock);
if (err) {
errno = err;
perror("pthread_mutex_unlock");
abort(); /* We cannot release; let's abort for now. */
}
}
static bool
load_int(struct int_resource* res, int* value)
{
bool succ = acquire_int_resource(res);
if (!succ) {
return false;
}
if (res->flags & RESOURCE_HAS_LOCAL_VALUE) {
*value = res->local_value;
} else {
*value = res->value;
}
return true;
}
static bool
store_int(struct int_resource* res, int value)
{
bool succ = acquire_int_resource(res);
if (!succ) {
return false;
}
res->local_value = value;
res->flags |= RESOURCE_HAS_LOCAL_VALUE;
return true;
}
/**
* The globally shared resources.
*/
static struct int_resource g_int_resource[] = {
INT_RESOURCE_INITIALIZER,
INT_RESOURCE_INITIALIZER
};
static void
producer_func(void)
{
unsigned int seed = 1;
int i0 = 0;
while (true) {
sleep(1);
++i0;
int i1 = rand_r(&seed);
printf("Storing i0=%d, i1=%d\n", i0, i1);
bool commit = false;
while (!commit) {
bool succ = store_int(g_int_resource + 0, i0);
if (!succ) {
goto release;
}
succ = store_int(g_int_resource + 1, i1);
if (!succ) {
goto release;
}
commit = true;
release:
release_int_resource(g_int_resource + 0, commit);
release_int_resource(g_int_resource + 1, commit);
}
}
}
static void
verify_load(int i0, int i1)
{
unsigned int seed = 1;
int i;
int value = 0;
for (i = 0; i < i0; ++i) {
value = rand_r(&seed);
}
if (value != i1) {
printf("Incorrect value pair (%d,%d), should be (%d,%d)\n",
i0, i1, i, value);
}
}
static void
consumer_func(void)
{
while (true) {
sleep(1);
int i0, i1;
bool commit = false;
while (!commit) {
bool succ = load_int(g_int_resource + 1, &i1);
if (!succ) {
goto release;
}
succ = load_int(g_int_resource + 0, &i0);
if (!succ) {
goto release;
}
verify_load(i0, i1);
commit = true;
release:
release_int_resource(g_int_resource + 0, commit);
release_int_resource(g_int_resource + 1, commit);
}
printf("Loaded i0=%d, i1=%d\n", i0, i1);
}
}
static void*
producer_func_cb(void* arg)
{
producer_func();
return NULL;
}
static void*
consumer_func_cb(void* arg)
{
consumer_func();
return NULL;
}
int
main(int argc, char* argv[])
{
pthread_t consumer;
int err = pthread_create(&consumer, NULL, consumer_func_cb, NULL);
if (err) {
errno = err;
perror("pthread_create");
goto err_pthread_create_consumer;
}
pthread_t producer;
err = pthread_create(&producer, NULL, producer_func_cb, NULL);
if (err) {
errno = err;
perror("pthread_create");
goto err_pthread_create_producer;
}
err = pthread_join(producer, NULL);
if (err) {
errno = err;
perror("pthread_join");
}
err = pthread_join(consumer, NULL);
if (err) {
errno = err;
perror("pthread_join");
}
return EXIT_SUCCESS;
err_pthread_create_producer:
err = pthread_join(consumer, NULL);
if (err) {
errno = err;
perror("pthread_join");
}
err_pthread_create_consumer:
return EXIT_FAILURE;
}