-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_sem.c
192 lines (160 loc) · 5.1 KB
/
apc_sem.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
/*
+----------------------------------------------------------------------+
| APC |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <[email protected]> |
+----------------------------------------------------------------------+
This software was contributed to PHP by Community Connect Inc. in 2002
and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
Future revisions and derivatives of this source code must acknowledge
Community Connect Inc. as the original contributor of this module by
leaving this note intact in the source code.
All other licensing and usage conditions are those of the PHP Group.
*/
/* $Id: apc_sem.c 307048 2011-01-03 23:53:17Z kalle $ */
#include "apc.h"
#ifdef APC_SEM_LOCKS
#include "apc_sem.h"
#include "php.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <unistd.h>
#if HAVE_SEMUN
/* we have semun, no need to define */
#else
#undef HAVE_SEMUN
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
/* Linux specific part: */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#define HAVE_SEMUN 1
#endif
#ifndef SEM_R
# define SEM_R 0444
#endif
#ifndef SEM_A
# define SEM_A 0222
#endif
/* always use SEM_UNDO, otherwise we risk deadlock */
#define USE_SEM_UNDO
#ifdef USE_SEM_UNDO
# define UNDO SEM_UNDO
#else
# define UNDO 0
#endif
int apc_sem_create(int proj, int initval TSRMLS_DC)
{
int semid;
int perms = 0777;
union semun arg;
key_t key = IPC_PRIVATE;
if ((semid = semget(key, 1, IPC_CREAT | IPC_EXCL | perms)) >= 0) {
/* sempahore created for the first time, initialize now */
arg.val = initval;
if (semctl(semid, 0, SETVAL, arg) < 0) {
apc_error("apc_sem_create: semctl(%d,...) failed:" TSRMLS_CC, semid);
}
}
else if (errno == EEXIST) {
/* sempahore already exists, don't initialize */
if ((semid = semget(key, 1, perms)) < 0) {
apc_error("apc_sem_create: semget(%u,...) failed:" TSRMLS_CC, key);
}
/* insert <sleazy way to avoid race condition> here */
}
else {
apc_error("apc_sem_create: semget(%u,...) failed:" TSRMLS_CC, key);
}
return semid;
}
void apc_sem_destroy(int semid)
{
/* we expect this call to fail often, so we do not check */
union semun arg;
semctl(semid, 0, IPC_RMID, arg);
}
void apc_sem_lock(int semid TSRMLS_DC)
{
struct sembuf op;
op.sem_num = 0;
op.sem_op = -1;
op.sem_flg = UNDO;
if (semop(semid, &op, 1) < 0) {
if (errno != EINTR) {
apc_error("apc_sem_lock: semop(%d) failed:" TSRMLS_CC, semid);
}
}
}
int apc_sem_nonblocking_lock(int semid TSRMLS_DC)
{
struct sembuf op;
op.sem_num = 0;
op.sem_op = -1;
op.sem_flg = UNDO | IPC_NOWAIT;
if (semop(semid, &op, 1) < 0) {
if (errno == EAGAIN) {
return 0; /* Lock is already held */
} else if (errno != EINTR) {
apc_error("apc_sem_lock: semop(%d) failed:" TSRMLS_CC, semid);
}
}
return 1; /* Lock obtained */
}
void apc_sem_unlock(int semid TSRMLS_DC)
{
struct sembuf op;
op.sem_num = 0;
op.sem_op = 1;
op.sem_flg = UNDO;
if (semop(semid, &op, 1) < 0) {
if (errno != EINTR) {
apc_error("apc_sem_unlock: semop(%d) failed:" TSRMLS_CC, semid);
}
}
}
void apc_sem_wait_for_zero(int semid TSRMLS_DC)
{
struct sembuf op;
op.sem_num = 0;
op.sem_op = 0;
op.sem_flg = UNDO;
if (semop(semid, &op, 1) < 0) {
if (errno != EINTR) {
apc_error("apc_sem_waitforzero: semop(%d) failed:" TSRMLS_CC, semid);
}
}
}
int apc_sem_get_value(int semid TSRMLS_DC)
{
union semun arg;
unsigned short val[1];
arg.array = val;
if (semctl(semid, 0, GETALL, arg) < 0) {
apc_error("apc_sem_getvalue: semctl(%d,...) failed:" TSRMLS_CC, semid);
}
return val[0];
}
#endif /* APC_SEM_LOCKS */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim>600: expandtab sw=4 ts=4 sts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4 sts=4
*/