New PHP5 APC - version 3.0.19, using PHP5 5.2.0-8+etch11,
[php5-apc.git] / apc_pthreadmutex.c
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
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   +----------------------------------------------------------------------+
17
18  */
19
20 /* $Id: apc_pthreadmutex.c,v 3.3.2.1 2008/05/11 18:57:00 rasmus Exp $ */
21
22 #include "apc_pthreadmutex.h"
23
24 #ifdef APC_PTHREADMUTEX_LOCKS
25
26 pthread_mutex_t *apc_pthreadmutex_create(pthread_mutex_t *lock) 
27
28     int result;
29     pthread_mutexattr_t* attr;
30     attr = malloc(sizeof(pthread_mutexattr_t));
31
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.");
39     } 
40
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");
45        }
46 #endif
47
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.");
56     }
57
58     if(pthread_mutex_init(lock, attr)) { 
59         apc_eprint("unable to initialize pthread lock");
60     }
61     return lock;
62 }
63
64 void apc_pthreadmutex_destroy(pthread_mutex_t *lock)
65 {
66     return; /* we don't actually destroy the mutex, as it would destroy it for all processes */
67 }
68
69 void apc_pthreadmutex_lock(pthread_mutex_t *lock)
70 {
71     int result;
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)");
77     }  
78 }
79
80 void apc_pthreadmutex_unlock(pthread_mutex_t *lock)
81 {
82     if(pthread_mutex_unlock(lock)) {
83         apc_eprint("unable to unlock pthread lock");
84     }
85 }
86
87 zend_bool apc_pthreadmutex_nonblocking_lock(pthread_mutex_t *lock)
88 {
89     int rval;
90     rval = pthread_mutex_trylock(lock);
91     if(rval == EBUSY) {     /* Lock is already held */
92         return 0;
93     } else if(rval == 0) {  /* Obtained lock */
94         return 1;
95     } else {                /* Other error */
96         apc_eprint("unable to obtain pthread trylock");
97         return 0;
98     }
99 }
100
101
102 #endif
103
104 /*
105  * Local variables:
106  * tab-width: 4
107  * c-basic-offset: 4
108  * End:
109  * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
110  * vim<600: expandtab sw=4 ts=4 sts=4
111  */