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: Brian Shire <shire@php.net> |
16 +----------------------------------------------------------------------+
20 /* $Id: apc_pthreadmutex.c,v 3.3.2.1 2008/05/11 18:57:00 rasmus Exp $ */
22 #include "apc_pthreadmutex.h"
24 #ifdef APC_PTHREADMUTEX_LOCKS
26 pthread_mutex_t *apc_pthreadmutex_create(pthread_mutex_t *lock)
29 pthread_mutexattr_t* attr;
30 attr = malloc(sizeof(pthread_mutexattr_t));
32 result = pthread_mutexattr_init(attr);
33 if(result == ENOMEM) {
34 apc_eprint("pthread mutex error: Insufficient memory exists to create the mutex attribute object.");
35 } else if(result == EINVAL) {
36 apc_eprint("pthread mutex error: attr does not point to writeable memory.");
37 } else if(result == EFAULT) {
38 apc_eprint("pthread mutex error: attr is an invalid pointer.");
41 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
42 result = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ADAPTIVE_NP);
43 if (result == EINVAL) {
44 apc_eprint("pthread_mutexattr_settype: unable to set adaptive mutexes");
48 /* pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ERRORCHECK); */
49 result = pthread_mutexattr_setpshared(attr, PTHREAD_PROCESS_SHARED);
50 if(result == EINVAL) {
51 apc_eprint("pthread mutex error: attr is not an initialized mutex attribute object, or pshared is not a valid process-shared state setting.");
52 } else if(result == EFAULT) {
53 apc_eprint("pthread mutex error: attr is an invalid pointer.");
54 } else if(result == ENOTSUP) {
55 apc_eprint("pthread mutex error: pshared was set to PTHREAD_PROCESS_SHARED.");
58 if(pthread_mutex_init(lock, attr)) {
59 apc_eprint("unable to initialize pthread lock");
64 void apc_pthreadmutex_destroy(pthread_mutex_t *lock)
66 return; /* we don't actually destroy the mutex, as it would destroy it for all processes */
69 void apc_pthreadmutex_lock(pthread_mutex_t *lock)
72 result = pthread_mutex_lock(lock);
73 if(result == EINVAL) {
74 apc_eprint("unable to obtain pthread lock (EINVAL)");
75 } else if(result == EDEADLK) {
76 apc_eprint("unable to obtain pthread lock (EDEADLK)");
80 void apc_pthreadmutex_unlock(pthread_mutex_t *lock)
82 if(pthread_mutex_unlock(lock)) {
83 apc_eprint("unable to unlock pthread lock");
87 zend_bool apc_pthreadmutex_nonblocking_lock(pthread_mutex_t *lock)
90 rval = pthread_mutex_trylock(lock);
91 if(rval == EBUSY) { /* Lock is already held */
93 } else if(rval == 0) { /* Obtained lock */
95 } else { /* Other error */
96 apc_eprint("unable to obtain pthread trylock");
109 * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
110 * vim<600: expandtab sw=4 ts=4 sts=4