aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_synch.h
blob: 94058d3257d202f210aec5322bf2b1685dc84314 (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
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
/*
 * Copyright 2016 Garrett D'Amore <garrett@damore.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/*
 * This is more of a direct #include of a .c rather than .h file.
 * But having it be a .h makes compiler rules work out properly.  Do
 * not include this more than once into your program, or you will
 * get multiple symbols defined.
 */

/*
 * POSIX synchronization (mutexes and condition variables).  This uses
 * pthreads.
 */

#include <pthread.h>
#include <time.h>

struct nni_mutex {
	pthread_mutex_t	mx;
};

struct nni_cond {
	pthread_cond_t	cv;
	pthread_mutex_t	*mx;
};

int
nni_mutex_create(nni_mutex_t *mp)
{
	struct nni_mutex *m;
	pthread_mutexattr_t attr;
	int rv;

	if ((m = nni_alloc(sizeof (*m))) == NULL) {
		return (NNG_ENOMEM);
	}

	/* We ask for more error checking... */
	if (pthread_mutexattr_init(&attr) != 0) {
		nni_free(m, sizeof (*m));
		return (NNG_ENOMEM);
	}

	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
		nni_panic("pthread_mutexattr_settype failed");
	}

	rv = pthread_mutex_init(&m->mx, &attr);

	if (pthread_mutexattr_destroy(&attr) != 0) {
		nni_panic("pthread_mutexattr_destroy failed");
	}

	if (rv != 0) {
		nni_free(m, sizeof (*m));
		return (NNG_ENOMEM);
	}
	*mp = m;
	return (0);
}

void
nni_mutex_destroy(nni_mutex_t m)
{
	if (pthread_mutex_destroy(&m->mx) != 0) {
		nni_panic("pthread_mutex_destroy failed");
	}
	/*
	 * If destroy fails for some reason, we can't really do
	 * anything about it.  This would actually represent a programming
	 * bug, and the right thing to do here would be to panic.
	 */
	nni_free(m, sizeof (*m));
}

void
nni_mutex_enter(nni_mutex_t m)
{
	if (pthread_mutex_lock(&m->mx) != 0) {
		nni_panic("pthread_mutex_lock failed");
	}
}

void
nni_mutex_exit(nni_mutex_t m)
{
	if (pthread_mutex_unlock(&m->mx) != 0) {
		nni_panic("pthread_mutex_unlock failed");
	}
}

int
nni_mutex_tryenter(nni_mutex_t m)
{
	if (pthread_mutex_trylock(&m->mx) != 0) {
		return (NNG_EBUSY);
	}
	return (0);
}

int
cond_attr(pthread_condattr_t **attrpp)
{
#if defined(NNG_USE_GETTIMEOFDAY) || NNG_USE_CLOCKID == CLOCK_REALTIME
	*attrpp = NULL;
	return (0);
#else
	/* In order to make this fast, avoid reinitializing attrs. */
	static pthread_condattr_t attr;
	static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
	static int init = 0;
	int rv;

	/*
	 * For efficiency's sake, we try to reuse the same attr for the
	 * life of the library.  This avoids many reallocations.  Technically
	 * this means that we will leak the attr on exit(), but this is
	 * preferable to constantly allocating and reallocating it.
	 */
	if (init) {
		*attrpp = &attr;
		return (0);
	}

	(void) pthread_mutex_lock(&mx);
	while (!init) {
		if ((rv = pthread_condattr_init(&attr)) != 0) {
			(void) pthread_mutex_unlock(&mx);
			return (NNG_ENOMEM);
		}
		rv = pthread_condattr_setclock(&attr, NNG_USE_CLOCKID);
		if (rv != 0) {
			nni_panic("condattr_setclock: %s", strerror(rv));
		}
		init = 1;
	}
	(void) pthread_mutex_unlock(&mx);
	*attrpp = &attr;
	return (0);
#endif
}

int
nni_cond_create(nni_cond_t *cvp, nni_mutex_t mx)
{
	/*
	 * By preference, we use a CLOCK_MONOTONIC version of condition
	 * variables, which insulates us from changes to the system time.
	 */
	struct nni_cond *c;
	pthread_condattr_t *attrp;
	int rv;

	if ((rv = cond_attr(&attrp)) != 0) {
		return (rv);
	}
	if ((c = nni_alloc(sizeof (*c))) == NULL) {
		return (NNG_ENOMEM);
	}
	c->mx = &mx->mx;
	if (pthread_cond_init(&c->cv, attrp) != 0) {
		/* In theory could be EAGAIN, but handle like ENOMEM */
		nni_free(c, sizeof (*c));
		return (NNG_ENOMEM);
	}

	*cvp = c;
	return (0);
}

void
nni_cond_destroy(nni_cond_t c)
{
	if (pthread_cond_destroy(&c->cv) != 0) {
		nni_panic("pthread_cond_destroy failed");
	}
	nni_free(c, sizeof (*c));
}

void
nni_cond_signal(nni_cond_t c)
{
	if (pthread_cond_signal(&c->cv) != 0) {
		nni_panic("pthread_cond_signal failed");
	}
}

void
nni_cond_broadcast(nni_cond_t c)
{
	if (pthread_cond_broadcast(&c->cv) != 0) {
		nni_panic("pthread_cond_broadcast failed");
	}
}

void
nni_cond_wait(nni_cond_t c)
{
	if (pthread_cond_wait(&c->cv, c->mx) != 0) {
		nni_panic("pthread_cond_wait failed");
	}
}

int
nni_cond_timedwait(nni_cond_t c, uint64_t usec)
{
	struct timespec ts;
	int rv;

	usec += nni_clock();

	ts.tv_sec = usec / 1000000;
	ts.tv_nsec = (usec % 10000) * 1000;

	if ((rv = pthread_cond_timedwait(&c->cv, c->mx, &ts)) != 0) {
		if (rv == ETIMEDOUT) {
			return (NNG_ETIMEDOUT);
		} else {
			nni_panic("pthread_cond_timedwait returned %d", rv);
		}
	}
	return (0);
}