2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2008 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Daniel Cowgill <dcowgill@communityconnect.com> |
16 +----------------------------------------------------------------------+
18 This software was contributed to PHP by Community Connect Inc. in 2002
19 and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
20 Future revisions and derivatives of this source code must acknowledge
21 Community Connect Inc. as the original contributor of this module by
22 leaving this note intact in the source code.
24 All other licensing and usage conditions are those of the PHP Group.
28 /* $Id: apc_sem.c,v 3.16.2.1 2008/05/11 18:57:00 rasmus Exp $ */
33 #include <sys/types.h>
40 /* we have semun, no need to define */
44 int val; /* value for SETVAL */
45 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
46 unsigned short *array; /* array for GETALL, SETALL */
47 /* Linux specific part: */
48 struct seminfo *__buf; /* buffer for IPC_INFO */
60 /* always use SEM_UNDO, otherwise we risk deadlock */
64 # define UNDO SEM_UNDO
69 int apc_sem_create(const char* pathname, int proj, int initval)
79 if (pathname != NULL) {
80 if ((key = ftok(pathname, proj)) < 0) {
81 apc_eprint("apc_sem_create: ftok(%s,%d) failed:", pathname, proj);
85 if ((semid = semget(key, 1, IPC_CREAT | IPC_EXCL | perms)) >= 0) {
86 /* sempahore created for the first time, initialize now */
88 if (semctl(semid, 0, SETVAL, arg) < 0) {
89 apc_eprint("apc_sem_create: semctl(%d,...) failed:", semid);
92 else if (errno == EEXIST) {
93 /* sempahore already exists, don't initialize */
94 if ((semid = semget(key, 1, perms)) < 0) {
95 apc_eprint("apc_sem_create: semget(%u,...) failed:", key);
97 /* insert <sleazy way to avoid race condition> here */
100 apc_eprint("apc_sem_create: semget(%u,...) failed:", key);
106 void apc_sem_destroy(int semid)
108 /* we expect this call to fail often, so we do not check */
110 semctl(semid, 0, IPC_RMID, arg);
113 void apc_sem_lock(int semid)
121 if (semop(semid, &op, 1) < 0) {
122 if (errno != EINTR) {
123 apc_eprint("apc_sem_lock: semop(%d) failed:", semid);
128 void apc_sem_unlock(int semid)
136 if (semop(semid, &op, 1) < 0) {
137 if (errno != EINTR) {
138 apc_eprint("apc_sem_unlock: semop(%d) failed:", semid);
143 void apc_sem_wait_for_zero(int semid)
151 if (semop(semid, &op, 1) < 0) {
152 if (errno != EINTR) {
153 apc_eprint("apc_sem_waitforzero: semop(%d) failed:", semid);
158 int apc_sem_get_value(int semid)
161 unsigned short val[1];
164 if (semctl(semid, 0, GETALL, arg) < 0) {
165 apc_eprint("apc_sem_getvalue: semctl(%d,...) failed:", semid);
175 * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
176 * vim<600: expandtab sw=4 ts=4 sts=4