1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
19 #include "apr_arch_proc_mutex.h"
20 #include "apr_arch_file_io.h" /* for apr_mkstemp() */
23 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
25 return apr_pool_cleanup_run(mutex->pool, mutex, apr_proc_mutex_cleanup);
28 #if APR_HAS_POSIXSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || \
29 APR_HAS_PROC_PTHREAD_SERIALIZE || APR_HAS_SYSVSEM_SERIALIZE
30 static apr_status_t proc_mutex_no_child_init(apr_proc_mutex_t **mutex,
38 #if APR_HAS_POSIXSEM_SERIALIZE
41 #define SEM_FAILED (-1)
44 static apr_status_t proc_mutex_posix_cleanup(void *mutex_)
46 apr_proc_mutex_t *mutex = mutex_;
48 if (sem_close(mutex->psem_interproc) < 0) {
55 static unsigned int rshash (char *p) {
56 /* hash function from Robert Sedgwicks 'Algorithms in C' book */
57 unsigned int b = 378551;
58 unsigned int a = 63689;
59 unsigned int retval = 0;
63 retval = retval * a + (*p);
70 static apr_status_t proc_mutex_posix_create(apr_proc_mutex_t *new_mutex,
73 #define APR_POSIXSEM_NAME_MIN 13
77 new_mutex->interproc = apr_palloc(new_mutex->pool,
78 sizeof(*new_mutex->interproc));
80 * This bogusness is to follow what appears to be the
81 * lowest common denominator in Posix semaphore naming:
83 * - be at most 14 chars
84 * - be unique and not match anything on the filesystem
86 * Because of this, we use fname to generate a (unique) hash
87 * and use that as the name of the semaphore. If no filename was
88 * given, we create one based on the time. We tuck the name
89 * away, since it might be useful for debugging. We use 2 hashing
90 * functions to try to avoid collisions.
92 * To make this as robust as possible, we initially try something
93 * larger (and hopefully more unique) and gracefully fail down to the
96 * NOTE: Darwin (Mac OS X) seems to be the most restrictive
97 * implementation. Versions previous to Darwin 6.2 had the 14
98 * char limit, but later rev's allow up to 31 characters.
102 apr_ssize_t flen = strlen(fname);
103 char *p = apr_pstrndup(new_mutex->pool, fname, strlen(fname));
105 h1 = apr_hashfunc_default((const char *)p, &flen);
107 apr_snprintf(semname, sizeof(semname), "/ApR.%xH%x", h1, h2);
112 now = apr_time_now();
113 sec = apr_time_sec(now);
114 usec = apr_time_usec(now);
115 apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
117 psem = sem_open(semname, O_CREAT | O_EXCL, 0644, 1);
118 if (psem == (sem_t *)SEM_FAILED) {
119 if (errno == ENAMETOOLONG) {
120 /* Oh well, good try */
121 semname[APR_POSIXSEM_NAME_MIN] = '\0';
125 psem = sem_open(semname, O_CREAT | O_EXCL, 0644, 1);
128 if (psem == (sem_t *)SEM_FAILED) {
131 /* Ahhh. The joys of Posix sems. Predelete it... */
133 new_mutex->psem_interproc = psem;
134 new_mutex->fname = apr_pstrdup(new_mutex->pool, semname);
135 apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
136 apr_proc_mutex_cleanup,
137 apr_pool_cleanup_null);
141 static apr_status_t proc_mutex_posix_acquire(apr_proc_mutex_t *mutex)
143 if (sem_wait(mutex->psem_interproc) < 0) {
146 mutex->curr_locked = 1;
150 static apr_status_t proc_mutex_posix_tryacquire(apr_proc_mutex_t *mutex)
152 if (sem_trywait(mutex->psem_interproc) < 0) {
153 if (errno == EAGAIN) {
158 mutex->curr_locked = 1;
162 static apr_status_t proc_mutex_posix_release(apr_proc_mutex_t *mutex)
164 mutex->curr_locked = 0;
165 if (sem_post(mutex->psem_interproc) < 0) {
166 /* any failure is probably fatal, so no big deal to leave
167 * ->curr_locked at 0. */
173 static const apr_proc_mutex_unix_lock_methods_t mutex_posixsem_methods =
175 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(POSIXSEM_IS_GLOBAL)
176 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
180 proc_mutex_posix_create,
181 proc_mutex_posix_acquire,
182 proc_mutex_posix_tryacquire,
183 proc_mutex_posix_release,
184 proc_mutex_posix_cleanup,
185 proc_mutex_no_child_init,
189 #endif /* Posix sem implementation */
191 #if APR_HAS_SYSVSEM_SERIALIZE
193 static struct sembuf proc_mutex_op_on;
194 static struct sembuf proc_mutex_op_try;
195 static struct sembuf proc_mutex_op_off;
197 static void proc_mutex_sysv_setup(void)
199 proc_mutex_op_on.sem_num = 0;
200 proc_mutex_op_on.sem_op = -1;
201 proc_mutex_op_on.sem_flg = SEM_UNDO;
202 proc_mutex_op_try.sem_num = 0;
203 proc_mutex_op_try.sem_op = -1;
204 proc_mutex_op_try.sem_flg = SEM_UNDO | IPC_NOWAIT;
205 proc_mutex_op_off.sem_num = 0;
206 proc_mutex_op_off.sem_op = 1;
207 proc_mutex_op_off.sem_flg = SEM_UNDO;
210 static apr_status_t proc_mutex_sysv_cleanup(void *mutex_)
212 apr_proc_mutex_t *mutex=mutex_;
215 if (mutex->interproc->filedes != -1) {
217 semctl(mutex->interproc->filedes, 0, IPC_RMID, ick);
222 static apr_status_t proc_mutex_sysv_create(apr_proc_mutex_t *new_mutex,
228 new_mutex->interproc = apr_palloc(new_mutex->pool, sizeof(*new_mutex->interproc));
229 new_mutex->interproc->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
231 if (new_mutex->interproc->filedes < 0) {
233 proc_mutex_sysv_cleanup(new_mutex);
237 if (semctl(new_mutex->interproc->filedes, 0, SETVAL, ick) < 0) {
239 proc_mutex_sysv_cleanup(new_mutex);
242 new_mutex->curr_locked = 0;
243 apr_pool_cleanup_register(new_mutex->pool,
244 (void *)new_mutex, apr_proc_mutex_cleanup,
245 apr_pool_cleanup_null);
249 static apr_status_t proc_mutex_sysv_acquire(apr_proc_mutex_t *mutex)
254 rc = semop(mutex->interproc->filedes, &proc_mutex_op_on, 1);
255 } while (rc < 0 && errno == EINTR);
259 mutex->curr_locked = 1;
263 static apr_status_t proc_mutex_sysv_tryacquire(apr_proc_mutex_t *mutex)
268 rc = semop(mutex->interproc->filedes, &proc_mutex_op_try, 1);
269 } while (rc < 0 && errno == EINTR);
271 if (errno == EAGAIN) {
276 mutex->curr_locked = 1;
280 static apr_status_t proc_mutex_sysv_release(apr_proc_mutex_t *mutex)
284 mutex->curr_locked = 0;
286 rc = semop(mutex->interproc->filedes, &proc_mutex_op_off, 1);
287 } while (rc < 0 && errno == EINTR);
294 static const apr_proc_mutex_unix_lock_methods_t mutex_sysv_methods =
296 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(SYSVSEM_IS_GLOBAL)
297 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
301 proc_mutex_sysv_create,
302 proc_mutex_sysv_acquire,
303 proc_mutex_sysv_tryacquire,
304 proc_mutex_sysv_release,
305 proc_mutex_sysv_cleanup,
306 proc_mutex_no_child_init,
310 #endif /* SysV sem implementation */
312 #if APR_HAS_PROC_PTHREAD_SERIALIZE
314 static apr_status_t proc_mutex_proc_pthread_cleanup(void *mutex_)
316 apr_proc_mutex_t *mutex=mutex_;
319 if (mutex->curr_locked == 1) {
320 if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
321 #ifdef HAVE_ZOS_PTHREADS
327 /* curr_locked is set to -1 until the mutex has been created */
328 if (mutex->curr_locked != -1) {
329 if ((rv = pthread_mutex_destroy(mutex->pthread_interproc))) {
330 #ifdef HAVE_ZOS_PTHREADS
336 if (munmap((caddr_t)mutex->pthread_interproc, sizeof(pthread_mutex_t))) {
342 static apr_status_t proc_mutex_proc_pthread_create(apr_proc_mutex_t *new_mutex,
347 pthread_mutexattr_t mattr;
349 fd = open("/dev/zero", O_RDWR);
354 new_mutex->pthread_interproc = (pthread_mutex_t *)mmap(
356 sizeof(pthread_mutex_t),
357 PROT_READ | PROT_WRITE, MAP_SHARED,
359 if (new_mutex->pthread_interproc == (pthread_mutex_t *) (caddr_t) -1) {
365 new_mutex->curr_locked = -1; /* until the mutex has been created */
367 if ((rv = pthread_mutexattr_init(&mattr))) {
368 #ifdef HAVE_ZOS_PTHREADS
371 proc_mutex_proc_pthread_cleanup(new_mutex);
374 if ((rv = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))) {
375 #ifdef HAVE_ZOS_PTHREADS
378 proc_mutex_proc_pthread_cleanup(new_mutex);
379 pthread_mutexattr_destroy(&mattr);
383 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
384 if ((rv = pthread_mutexattr_setrobust_np(&mattr,
385 PTHREAD_MUTEX_ROBUST_NP))) {
386 #ifdef HAVE_ZOS_PTHREADS
389 proc_mutex_proc_pthread_cleanup(new_mutex);
390 pthread_mutexattr_destroy(&mattr);
393 if ((rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT))) {
394 #ifdef HAVE_ZOS_PTHREADS
397 proc_mutex_proc_pthread_cleanup(new_mutex);
398 pthread_mutexattr_destroy(&mattr);
401 #endif /* HAVE_PTHREAD_MUTEX_ROBUST */
403 if ((rv = pthread_mutex_init(new_mutex->pthread_interproc, &mattr))) {
404 #ifdef HAVE_ZOS_PTHREADS
407 proc_mutex_proc_pthread_cleanup(new_mutex);
408 pthread_mutexattr_destroy(&mattr);
412 new_mutex->curr_locked = 0; /* mutex created now */
414 if ((rv = pthread_mutexattr_destroy(&mattr))) {
415 #ifdef HAVE_ZOS_PTHREADS
418 proc_mutex_proc_pthread_cleanup(new_mutex);
422 apr_pool_cleanup_register(new_mutex->pool,
424 apr_proc_mutex_cleanup,
425 apr_pool_cleanup_null);
429 static apr_status_t proc_mutex_proc_pthread_acquire(apr_proc_mutex_t *mutex)
433 if ((rv = pthread_mutex_lock(mutex->pthread_interproc))) {
434 #ifdef HAVE_ZOS_PTHREADS
437 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
438 /* Okay, our owner died. Let's try to make it consistent again. */
439 if (rv == EOWNERDEAD) {
440 pthread_mutex_consistent_np(mutex->pthread_interproc);
448 mutex->curr_locked = 1;
452 static apr_status_t proc_mutex_proc_pthread_tryacquire(apr_proc_mutex_t *mutex)
456 if ((rv = pthread_mutex_trylock(mutex->pthread_interproc))) {
457 #ifdef HAVE_ZOS_PTHREADS
463 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
464 /* Okay, our owner died. Let's try to make it consistent again. */
465 if (rv == EOWNERDEAD) {
466 pthread_mutex_consistent_np(mutex->pthread_interproc);
475 mutex->curr_locked = 1;
479 static apr_status_t proc_mutex_proc_pthread_release(apr_proc_mutex_t *mutex)
483 mutex->curr_locked = 0;
484 if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
485 #ifdef HAVE_ZOS_PTHREADS
493 static const apr_proc_mutex_unix_lock_methods_t mutex_proc_pthread_methods =
495 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
496 proc_mutex_proc_pthread_create,
497 proc_mutex_proc_pthread_acquire,
498 proc_mutex_proc_pthread_tryacquire,
499 proc_mutex_proc_pthread_release,
500 proc_mutex_proc_pthread_cleanup,
501 proc_mutex_no_child_init,
507 #if APR_HAS_FCNTL_SERIALIZE
509 static struct flock proc_mutex_lock_it;
510 static struct flock proc_mutex_unlock_it;
512 static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *);
514 static void proc_mutex_fcntl_setup(void)
516 proc_mutex_lock_it.l_whence = SEEK_SET; /* from current point */
517 proc_mutex_lock_it.l_start = 0; /* -"- */
518 proc_mutex_lock_it.l_len = 0; /* until end of file */
519 proc_mutex_lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
520 proc_mutex_lock_it.l_pid = 0; /* pid not actually interesting */
521 proc_mutex_unlock_it.l_whence = SEEK_SET; /* from current point */
522 proc_mutex_unlock_it.l_start = 0; /* -"- */
523 proc_mutex_unlock_it.l_len = 0; /* until end of file */
524 proc_mutex_unlock_it.l_type = F_UNLCK; /* set exclusive/write lock */
525 proc_mutex_unlock_it.l_pid = 0; /* pid not actually interesting */
528 static apr_status_t proc_mutex_fcntl_cleanup(void *mutex_)
531 apr_proc_mutex_t *mutex=mutex_;
533 if (mutex->curr_locked == 1) {
534 status = proc_mutex_fcntl_release(mutex);
535 if (status != APR_SUCCESS)
539 return apr_file_close(mutex->interproc);
542 static apr_status_t proc_mutex_fcntl_create(apr_proc_mutex_t *new_mutex,
548 new_mutex->fname = apr_pstrdup(new_mutex->pool, fname);
549 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
550 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
551 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD,
555 new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
556 rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
557 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
561 if (rv != APR_SUCCESS) {
565 new_mutex->curr_locked = 0;
566 unlink(new_mutex->fname);
567 apr_pool_cleanup_register(new_mutex->pool,
569 apr_proc_mutex_cleanup,
570 apr_pool_cleanup_null);
574 static apr_status_t proc_mutex_fcntl_acquire(apr_proc_mutex_t *mutex)
579 rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_lock_it);
580 } while (rc < 0 && errno == EINTR);
584 mutex->curr_locked=1;
588 static apr_status_t proc_mutex_fcntl_tryacquire(apr_proc_mutex_t *mutex)
593 rc = fcntl(mutex->interproc->filedes, F_SETLK, &proc_mutex_lock_it);
594 } while (rc < 0 && errno == EINTR);
596 #if FCNTL_TRYACQUIRE_EACCES
597 if (errno == EACCES) {
599 if (errno == EAGAIN) {
605 mutex->curr_locked = 1;
609 static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *mutex)
613 mutex->curr_locked=0;
615 rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_unlock_it);
616 } while (rc < 0 && errno == EINTR);
623 static const apr_proc_mutex_unix_lock_methods_t mutex_fcntl_methods =
625 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FCNTL_IS_GLOBAL)
626 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
630 proc_mutex_fcntl_create,
631 proc_mutex_fcntl_acquire,
632 proc_mutex_fcntl_tryacquire,
633 proc_mutex_fcntl_release,
634 proc_mutex_fcntl_cleanup,
635 proc_mutex_no_child_init,
639 #endif /* fcntl implementation */
641 #if APR_HAS_FLOCK_SERIALIZE
643 static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *);
645 static apr_status_t proc_mutex_flock_cleanup(void *mutex_)
648 apr_proc_mutex_t *mutex=mutex_;
650 if (mutex->curr_locked == 1) {
651 status = proc_mutex_flock_release(mutex);
652 if (status != APR_SUCCESS)
655 if (mutex->interproc) { /* if it was opened properly */
656 apr_file_close(mutex->interproc);
658 unlink(mutex->fname);
662 static apr_status_t proc_mutex_flock_create(apr_proc_mutex_t *new_mutex,
668 new_mutex->fname = apr_pstrdup(new_mutex->pool, fname);
669 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
670 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
671 APR_UREAD | APR_UWRITE,
675 new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
676 rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
677 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
681 if (rv != APR_SUCCESS) {
682 proc_mutex_flock_cleanup(new_mutex);
685 new_mutex->curr_locked = 0;
686 apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
687 apr_proc_mutex_cleanup,
688 apr_pool_cleanup_null);
692 static apr_status_t proc_mutex_flock_acquire(apr_proc_mutex_t *mutex)
697 rc = flock(mutex->interproc->filedes, LOCK_EX);
698 } while (rc < 0 && errno == EINTR);
702 mutex->curr_locked = 1;
706 static apr_status_t proc_mutex_flock_tryacquire(apr_proc_mutex_t *mutex)
711 rc = flock(mutex->interproc->filedes, LOCK_EX | LOCK_NB);
712 } while (rc < 0 && errno == EINTR);
714 if (errno == EWOULDBLOCK || errno == EAGAIN) {
719 mutex->curr_locked = 1;
723 static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *mutex)
727 mutex->curr_locked = 0;
729 rc = flock(mutex->interproc->filedes, LOCK_UN);
730 } while (rc < 0 && errno == EINTR);
737 static apr_status_t proc_mutex_flock_child_init(apr_proc_mutex_t **mutex,
741 apr_proc_mutex_t *new_mutex;
744 new_mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
746 memcpy(new_mutex, *mutex, sizeof *new_mutex);
747 new_mutex->pool = pool;
749 fname = (*mutex)->fname;
751 new_mutex->fname = apr_pstrdup(pool, fname);
752 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
753 APR_FOPEN_WRITE, 0, new_mutex->pool);
754 if (rv != APR_SUCCESS) {
761 static const apr_proc_mutex_unix_lock_methods_t mutex_flock_methods =
763 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FLOCK_IS_GLOBAL)
764 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
768 proc_mutex_flock_create,
769 proc_mutex_flock_acquire,
770 proc_mutex_flock_tryacquire,
771 proc_mutex_flock_release,
772 proc_mutex_flock_cleanup,
773 proc_mutex_flock_child_init,
777 #endif /* flock implementation */
779 void apr_proc_mutex_unix_setup_lock(void)
781 /* setup only needed for sysvsem and fnctl */
782 #if APR_HAS_SYSVSEM_SERIALIZE
783 proc_mutex_sysv_setup();
785 #if APR_HAS_FCNTL_SERIALIZE
786 proc_mutex_fcntl_setup();
790 static apr_status_t proc_mutex_choose_method(apr_proc_mutex_t *new_mutex, apr_lockmech_e mech)
794 #if APR_HAS_FCNTL_SERIALIZE
795 new_mutex->inter_meth = &mutex_fcntl_methods;
801 #if APR_HAS_FLOCK_SERIALIZE
802 new_mutex->inter_meth = &mutex_flock_methods;
807 case APR_LOCK_SYSVSEM:
808 #if APR_HAS_SYSVSEM_SERIALIZE
809 new_mutex->inter_meth = &mutex_sysv_methods;
814 case APR_LOCK_POSIXSEM:
815 #if APR_HAS_POSIXSEM_SERIALIZE
816 new_mutex->inter_meth = &mutex_posixsem_methods;
821 case APR_LOCK_PROC_PTHREAD:
822 #if APR_HAS_PROC_PTHREAD_SERIALIZE
823 new_mutex->inter_meth = &mutex_proc_pthread_methods;
828 case APR_LOCK_DEFAULT:
829 #if APR_USE_FLOCK_SERIALIZE
830 new_mutex->inter_meth = &mutex_flock_methods;
831 #elif APR_USE_SYSVSEM_SERIALIZE
832 new_mutex->inter_meth = &mutex_sysv_methods;
833 #elif APR_USE_FCNTL_SERIALIZE
834 new_mutex->inter_meth = &mutex_fcntl_methods;
835 #elif APR_USE_PROC_PTHREAD_SERIALIZE
836 new_mutex->inter_meth = &mutex_proc_pthread_methods;
837 #elif APR_USE_POSIXSEM_SERIALIZE
838 new_mutex->inter_meth = &mutex_posixsem_methods;
849 APR_DECLARE(const char *) apr_proc_mutex_defname(void)
852 apr_proc_mutex_t mutex;
854 if ((rv = proc_mutex_choose_method(&mutex, APR_LOCK_DEFAULT)) != APR_SUCCESS) {
857 mutex.meth = mutex.inter_meth;
859 return apr_proc_mutex_name(&mutex);
862 static apr_status_t proc_mutex_create(apr_proc_mutex_t *new_mutex, apr_lockmech_e mech, const char *fname)
866 if ((rv = proc_mutex_choose_method(new_mutex, mech)) != APR_SUCCESS) {
870 new_mutex->meth = new_mutex->inter_meth;
872 if ((rv = new_mutex->meth->create(new_mutex, fname)) != APR_SUCCESS) {
879 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
884 apr_proc_mutex_t *new_mutex;
887 new_mutex = apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
888 new_mutex->pool = pool;
890 if ((rv = proc_mutex_create(new_mutex, mech, fname)) != APR_SUCCESS)
897 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
901 return (*mutex)->meth->child_init(mutex, pool, fname);
904 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
906 return mutex->meth->acquire(mutex);
909 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
911 return mutex->meth->tryacquire(mutex);
914 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
916 return mutex->meth->release(mutex);
919 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
921 return ((apr_proc_mutex_t *)mutex)->meth->cleanup(mutex);
924 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
926 return mutex->meth->name;
929 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
931 /* POSIX sems use the fname field but don't use a file,
933 #if APR_HAS_FLOCK_SERIALIZE
934 if (mutex->meth == &mutex_flock_methods) {
938 #if APR_HAS_FCNTL_SERIALIZE
939 if (mutex->meth == &mutex_fcntl_methods) {
946 APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
948 /* Implement OS-specific accessors defined in apr_portable.h */
950 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
951 apr_proc_mutex_t *pmutex)
953 #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
954 ospmutex->crossproc = pmutex->interproc->filedes;
956 #if APR_HAS_PROC_PTHREAD_SERIALIZE
957 ospmutex->pthread_interproc = pmutex->pthread_interproc;
962 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
963 apr_os_proc_mutex_t *ospmutex,
969 if ((*pmutex) == NULL) {
970 (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool,
971 sizeof(apr_proc_mutex_t));
972 (*pmutex)->pool = pool;
974 #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
975 apr_os_file_put(&(*pmutex)->interproc, &ospmutex->crossproc, 0, pool);
977 #if APR_HAS_PROC_PTHREAD_SERIALIZE
978 (*pmutex)->pthread_interproc = ospmutex->pthread_interproc;