summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/pthread_rwlock/pthread_rwlock.c
blob: 6e0c4674aba7b7bc3f25523550cb4c33cebdf520 (plain)
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
/* $OpenBSD: pthread_rwlock.c,v 1.3 2014/05/20 01:25:24 guenther Exp $ */
/* PUBLIC DOMAIN Feb 2012 <guenther@openbsd.org> */

#include <sys/types.h>
#include <assert.h>
#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/*
 * Set up an rwlock with a few reader threads, start a writer blocking,
 * then let go the reader threads one by one.  Verify that the writer
 * thread gets, gets out, and then the rwlock can be locked for reading
 * again.
 */

pthread_rwlock_t	rw;

pthread_mutex_t m;
pthread_cond_t c;
enum
{
	UNLOCKED,
	NUM_READERS = 6,
	WRITE_STARTED,
	WRITE,
	WRITE_DONE,
} state;
time_t write_started;

static void *
reader(void *arg)
{
	int	me = *(int *)arg;
	int diff;

	pthread_mutex_lock(&m);
	assert(state < NUM_READERS);
	pthread_rwlock_rdlock(&rw);
	state++;
	printf("reader %d locked, state = %d\n", me, state);
	pthread_cond_broadcast(&c);
	pthread_mutex_unlock(&m);

	pthread_mutex_lock(&m);
	while (state < WRITE_STARTED) {
		pthread_cond_wait(&c, &m);
		printf("reader %d woken, state = %d\n", me, state);
	}

	diff = difftime(time(NULL), write_started);
	if (diff < 2)
		sleep(3 - diff);

	pthread_rwlock_unlock(&rw);
	printf("reader %d unlocked\n", me);
	sleep(1);

	pthread_mutex_unlock(&m);

	pthread_mutex_lock(&m);
	while (state >= WRITE_STARTED) {
		pthread_cond_wait(&c, &m);
		printf("reader %d woken, state = %d\n", me, state);
	}
	state++;
	printf("reader %d trying again (%d)\n", me, state);
	pthread_rwlock_rdlock(&rw);
	printf("reader %d locked again (%d)\n", me, state);
	pthread_cond_broadcast(&c);
	while (state != NUM_READERS) {
		pthread_cond_wait(&c, &m);
		printf("reader %d woken, state = %d\n", me, state);
	}
	pthread_mutex_unlock(&m);
	pthread_rwlock_unlock(&rw);

	printf("reader %d exiting\n", me);
	return NULL;
}

static void *
writer(void *arg)
{
	pthread_mutex_lock(&m);
	printf("writer started, state = %d\n", state);
	while (state != NUM_READERS) {
		pthread_cond_wait(&c, &m);
		printf("writer woken, state = %d\n", state);
	}
	state = WRITE_STARTED;
	printf("writer starting\n");
	write_started = time(NULL);
	pthread_cond_broadcast(&c);
	pthread_mutex_unlock(&m);

	pthread_rwlock_wrlock(&rw);
	printf("writer locked\n");

	pthread_mutex_lock(&m);
	state = WRITE;
	pthread_cond_broadcast(&c);

	while (state == WRITE)
		pthread_cond_wait(&c, &m);

	printf("writer unlocking\n");
	pthread_rwlock_unlock(&rw);
	state = UNLOCKED;
	pthread_cond_broadcast(&c);
	pthread_mutex_unlock(&m);

	printf("writer exiting\n");
	return NULL;
}


int
main(void)
{
	pthread_t	tr[NUM_READERS], tw;
	int	ids[NUM_READERS], i, r;

	pthread_rwlock_init(&rw, NULL);
	pthread_mutex_init(&m, NULL);
	pthread_cond_init(&c, NULL);
	state = UNLOCKED;

	for (i = 0; i < NUM_READERS; i++) {
		ids[i] = i;
		if ((r = pthread_create(&tr[i], NULL, reader, &ids[i])))
			errc(1, r, "create %d", i);
	}

	if ((r = pthread_create(&tw, NULL, writer, NULL)))
		errc(1, r, "create writer");

	pthread_mutex_lock(&m);
	while (state != WRITE)
		pthread_cond_wait(&c, &m);
	state = WRITE_DONE;
	pthread_mutex_unlock(&m);
	pthread_cond_broadcast(&c);

	pthread_join(tw, NULL);

	for (i = 0; i < NUM_READERS; i++)
		pthread_join(tr[i], NULL);
	return 0;
}