summaryrefslogtreecommitdiff
path: root/util/lock/csem.c
blob: a84cbbb2b154c96cec199b77e558690031467650 (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
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
/*
 * Copyright 2003 Sun Microsystems, Inc.
 * Copyright 2010 Google, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following disclaimer
 *    in the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Google Inc. nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Developer's note: This was open sourced by Sun Microsystems, which got it
 * via Cobalt Networks.  It has been fairly extensively modified since then.
 */

#ifndef _GNU_SOURCE
#  define _GNU_SOURCE 1
#endif
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include <sched.h>

#include "csem.h"

#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including <sys/sem.h> */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
	int val;                    /* value for SETVAL */
	struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
	unsigned short int *array;  /* array for GETALL, SETALL */
	struct seminfo *__buf;      /* buffer for IPC_INFO */
};
#endif

/*
 * On some platforms semctl(SETVAL) sets sem_otime, on other platforms it
 * does not.  Figure out what this platform does.
 *
 * Returns 0 if semctl(SETVAL) does not set sem_otime
 * Returns 1 if semctl(SETVAL) does set sem_otime
 * Returns -1 on error
 */
static int does_semctl_set_otime(void)
{
	int sem_id;
	int ret;

	/* create a test semaphore */
	sem_id = semget(IPC_PRIVATE, 1, S_IRUSR|S_IWUSR);
	if (sem_id < 0) {
		return -1;
	}

	/* set the value */
	if (csem_setval(sem_id, 1) < 0) {
		csem_destroy(sem_id);
		return -1;
	}

	/* read sem_otime */
	ret = (csem_get_otime(sem_id) > 0) ? 1 : 0;

	/* clean up */
	csem_destroy(sem_id);

	return ret;
}

int csem_create(key_t key, unsigned val)
{
	static int need_otime_hack = -1;
	int sem_id;

	/* see if we need to trigger a semop to set sem_otime */
	if (need_otime_hack < 0) {
		int ret = does_semctl_set_otime();
		if (ret < 0) {
			return -1;
		}
		need_otime_hack = !ret;
	}

	/* create it or fail */
	sem_id = semget(key, 1, IPC_CREAT|IPC_EXCL | S_IRUSR|S_IWUSR);
	if (sem_id < 0) {
		return -1;
	}

	/* initalize the value */
	if (need_otime_hack) {
		val++;
	}
	if (csem_setval(sem_id, val) < 0) {
		csem_destroy(sem_id);
		return -1;
	}

	if (need_otime_hack) {
		/* force sem_otime to change */
		csem_down(sem_id);
	}

	return sem_id;
}

/* how many times to loop, waiting for sem_otime */
#define MAX_OTIME_LOOPS 1000

int csem_get(key_t key)
{
	int sem_id;
	int i;

	/* CSEM_PRIVATE needs to go through csem_create() to get an
	 * initial value */
	if (key == CSEM_PRIVATE) {
		errno = EINVAL;
		return -1;
	}

	/* get the (assumed existing) semaphore */
	sem_id = semget(key, 1, S_IRUSR|S_IWUSR);
	if (sem_id < 0) {
		return -1;
	}

	/* loop until sem_otime != 0, which means it has been initialized */
	for (i = 0; i < MAX_OTIME_LOOPS; i++) {
		time_t otime = csem_get_otime(sem_id);
		if (otime < 0) {
			/* error */
			return -1;
		}
		if (otime > 0) {
			/* success */
			return sem_id;
		}
		/* retry */
		sched_yield();
	}

	/* fell through - error */
	return -1;
}

int csem_get_or_create(key_t key, unsigned val)
{
	int sem_id;

	/* try to create the semaphore */
	sem_id = csem_create(key, val);
	if (sem_id >= 0 || errno != EEXIST) {
		/* it either succeeded or got an error */
		return sem_id;
	}

	/* it must exist already - get it */
	sem_id = csem_get(key);
	if (sem_id < 0) {
		return -1;
	}

	return sem_id;
}

int csem_destroy(int sem_id)
{
	return semctl(sem_id, 0, IPC_RMID);
}

int csem_getval(int sem_id)
{
	return semctl(sem_id, 0, GETVAL);
}

int csem_setval(int sem_id, unsigned val)
{
	union semun arg;
	arg.val = val;
	if (semctl(sem_id, 0, SETVAL, arg) < 0) {
		return -1;
	}
	return 0;
}

static int csem_up_undoflag(int sem_id, int undoflag)
{
	struct sembuf sops;
	sops.sem_num = 0;
	sops.sem_op = 1;
	sops.sem_flg = undoflag;
	return semop(sem_id, &sops, 1);
}

int csem_up(int sem_id)
{
	return csem_up_undoflag(sem_id, 0);
}

int csem_up_undo(int sem_id)
{
	return csem_up_undoflag(sem_id, SEM_UNDO);
}

static int csem_down_undoflag(int sem_id, int undoflag)
{
	struct sembuf sops;
	sops.sem_num = 0;
	sops.sem_op = -1;
	sops.sem_flg = undoflag;
	return semop(sem_id, &sops, 1);
}

int csem_down(int sem_id)
{
	return csem_down_undoflag(sem_id, 0);
}

int csem_down_undo(int sem_id)
{
	return csem_down_undoflag(sem_id, SEM_UNDO);
}

static int csem_down_timeout_undoflag(int sem_id,
                                      struct timespec *timeout,
                                      int undoflag)
{
	struct sembuf sops;
	sops.sem_num = 0;
	sops.sem_op = -1;
	sops.sem_flg = undoflag;
	return semtimedop(sem_id, &sops, 1, timeout);
}

int csem_down_timeout(int sem_id, struct timespec *timeout)
{
	return csem_down_timeout_undoflag(sem_id, timeout, 0);
}

int csem_down_timeout_undo(int sem_id, struct timespec *timeout)
{
	return csem_down_timeout_undoflag(sem_id, timeout, SEM_UNDO);
}

time_t csem_get_otime(int sem_id)
{
	union semun arg;
	struct semid_ds ds;
	arg.buf = &ds;
	if (semctl(sem_id, 0, IPC_STAT, arg) < 0) {
		return -1;
	}
	return ds.sem_otime;
}