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) & 0xffffffff);
106 h2 = (rshash(p) & 0xffffffff);
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);
118 psem = sem_open(semname, O_CREAT | O_EXCL, 0644, 1);
119 } while (psem == (sem_t *)SEM_FAILED && errno == EINTR);
120 if (psem == (sem_t *)SEM_FAILED) {
121 if (errno == ENAMETOOLONG) {
122 /* Oh well, good try */
123 semname[APR_POSIXSEM_NAME_MIN] = '\0';
128 psem = sem_open(semname, O_CREAT | O_EXCL, 0644, 1);
129 } while (psem == (sem_t *)SEM_FAILED && errno == EINTR);
132 if (psem == (sem_t *)SEM_FAILED) {
135 /* Ahhh. The joys of Posix sems. Predelete it... */
137 new_mutex->psem_interproc = psem;
138 new_mutex->fname = apr_pstrdup(new_mutex->pool, semname);
139 apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
140 apr_proc_mutex_cleanup,
141 apr_pool_cleanup_null);
145 static apr_status_t proc_mutex_posix_acquire(apr_proc_mutex_t *mutex)
150 rc = sem_wait(mutex->psem_interproc);
151 } while (rc < 0 && errno == EINTR);
155 mutex->curr_locked = 1;
159 static apr_status_t proc_mutex_posix_tryacquire(apr_proc_mutex_t *mutex)
164 rc = sem_trywait(mutex->psem_interproc);
165 } while (rc < 0 && errno == EINTR);
167 if (errno == EAGAIN) {
172 mutex->curr_locked = 1;
176 static apr_status_t proc_mutex_posix_release(apr_proc_mutex_t *mutex)
178 mutex->curr_locked = 0;
179 if (sem_post(mutex->psem_interproc) < 0) {
180 /* any failure is probably fatal, so no big deal to leave
181 * ->curr_locked at 0. */
187 static const apr_proc_mutex_unix_lock_methods_t mutex_posixsem_methods =
189 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(POSIXSEM_IS_GLOBAL)
190 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
194 proc_mutex_posix_create,
195 proc_mutex_posix_acquire,
196 proc_mutex_posix_tryacquire,
197 proc_mutex_posix_release,
198 proc_mutex_posix_cleanup,
199 proc_mutex_no_child_init,
203 #endif /* Posix sem implementation */
205 #if APR_HAS_SYSVSEM_SERIALIZE
207 static struct sembuf proc_mutex_op_on;
208 static struct sembuf proc_mutex_op_try;
209 static struct sembuf proc_mutex_op_off;
211 static void proc_mutex_sysv_setup(void)
213 proc_mutex_op_on.sem_num = 0;
214 proc_mutex_op_on.sem_op = -1;
215 proc_mutex_op_on.sem_flg = SEM_UNDO;
216 proc_mutex_op_try.sem_num = 0;
217 proc_mutex_op_try.sem_op = -1;
218 proc_mutex_op_try.sem_flg = SEM_UNDO | IPC_NOWAIT;
219 proc_mutex_op_off.sem_num = 0;
220 proc_mutex_op_off.sem_op = 1;
221 proc_mutex_op_off.sem_flg = SEM_UNDO;
224 static apr_status_t proc_mutex_sysv_cleanup(void *mutex_)
226 apr_proc_mutex_t *mutex=mutex_;
229 if (mutex->interproc->filedes != -1) {
231 semctl(mutex->interproc->filedes, 0, IPC_RMID, ick);
236 static apr_status_t proc_mutex_sysv_create(apr_proc_mutex_t *new_mutex,
242 new_mutex->interproc = apr_palloc(new_mutex->pool, sizeof(*new_mutex->interproc));
243 new_mutex->interproc->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
245 if (new_mutex->interproc->filedes < 0) {
247 proc_mutex_sysv_cleanup(new_mutex);
251 if (semctl(new_mutex->interproc->filedes, 0, SETVAL, ick) < 0) {
253 proc_mutex_sysv_cleanup(new_mutex);
256 new_mutex->curr_locked = 0;
257 apr_pool_cleanup_register(new_mutex->pool,
258 (void *)new_mutex, apr_proc_mutex_cleanup,
259 apr_pool_cleanup_null);
263 static apr_status_t proc_mutex_sysv_acquire(apr_proc_mutex_t *mutex)
268 rc = semop(mutex->interproc->filedes, &proc_mutex_op_on, 1);
269 } while (rc < 0 && errno == EINTR);
273 mutex->curr_locked = 1;
277 static apr_status_t proc_mutex_sysv_tryacquire(apr_proc_mutex_t *mutex)
282 rc = semop(mutex->interproc->filedes, &proc_mutex_op_try, 1);
283 } while (rc < 0 && errno == EINTR);
285 if (errno == EAGAIN) {
290 mutex->curr_locked = 1;
294 static apr_status_t proc_mutex_sysv_release(apr_proc_mutex_t *mutex)
298 mutex->curr_locked = 0;
300 rc = semop(mutex->interproc->filedes, &proc_mutex_op_off, 1);
301 } while (rc < 0 && errno == EINTR);
308 static const apr_proc_mutex_unix_lock_methods_t mutex_sysv_methods =
310 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(SYSVSEM_IS_GLOBAL)
311 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
315 proc_mutex_sysv_create,
316 proc_mutex_sysv_acquire,
317 proc_mutex_sysv_tryacquire,
318 proc_mutex_sysv_release,
319 proc_mutex_sysv_cleanup,
320 proc_mutex_no_child_init,
324 #endif /* SysV sem implementation */
326 #if APR_HAS_PROC_PTHREAD_SERIALIZE
328 static apr_status_t proc_mutex_proc_pthread_cleanup(void *mutex_)
330 apr_proc_mutex_t *mutex=mutex_;
333 if (mutex->curr_locked == 1) {
334 if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
335 #ifdef HAVE_ZOS_PTHREADS
341 /* curr_locked is set to -1 until the mutex has been created */
342 if (mutex->curr_locked != -1) {
343 if ((rv = pthread_mutex_destroy(mutex->pthread_interproc))) {
344 #ifdef HAVE_ZOS_PTHREADS
350 if (munmap((caddr_t)mutex->pthread_interproc, sizeof(pthread_mutex_t))) {
356 static apr_status_t proc_mutex_proc_pthread_create(apr_proc_mutex_t *new_mutex,
361 pthread_mutexattr_t mattr;
363 fd = open("/dev/zero", O_RDWR);
368 new_mutex->pthread_interproc = (pthread_mutex_t *)mmap(
370 sizeof(pthread_mutex_t),
371 PROT_READ | PROT_WRITE, MAP_SHARED,
373 if (new_mutex->pthread_interproc == (pthread_mutex_t *) (caddr_t) -1) {
379 new_mutex->curr_locked = -1; /* until the mutex has been created */
381 if ((rv = pthread_mutexattr_init(&mattr))) {
382 #ifdef HAVE_ZOS_PTHREADS
385 proc_mutex_proc_pthread_cleanup(new_mutex);
388 if ((rv = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))) {
389 #ifdef HAVE_ZOS_PTHREADS
392 proc_mutex_proc_pthread_cleanup(new_mutex);
393 pthread_mutexattr_destroy(&mattr);
397 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
398 if ((rv = pthread_mutexattr_setrobust_np(&mattr,
399 PTHREAD_MUTEX_ROBUST_NP))) {
400 #ifdef HAVE_ZOS_PTHREADS
403 proc_mutex_proc_pthread_cleanup(new_mutex);
404 pthread_mutexattr_destroy(&mattr);
407 if ((rv = pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT))) {
408 #ifdef HAVE_ZOS_PTHREADS
411 proc_mutex_proc_pthread_cleanup(new_mutex);
412 pthread_mutexattr_destroy(&mattr);
415 #endif /* HAVE_PTHREAD_MUTEX_ROBUST */
417 if ((rv = pthread_mutex_init(new_mutex->pthread_interproc, &mattr))) {
418 #ifdef HAVE_ZOS_PTHREADS
421 proc_mutex_proc_pthread_cleanup(new_mutex);
422 pthread_mutexattr_destroy(&mattr);
426 new_mutex->curr_locked = 0; /* mutex created now */
428 if ((rv = pthread_mutexattr_destroy(&mattr))) {
429 #ifdef HAVE_ZOS_PTHREADS
432 proc_mutex_proc_pthread_cleanup(new_mutex);
436 apr_pool_cleanup_register(new_mutex->pool,
438 apr_proc_mutex_cleanup,
439 apr_pool_cleanup_null);
443 static apr_status_t proc_mutex_proc_pthread_acquire(apr_proc_mutex_t *mutex)
447 if ((rv = pthread_mutex_lock(mutex->pthread_interproc))) {
448 #ifdef HAVE_ZOS_PTHREADS
451 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
452 /* Okay, our owner died. Let's try to make it consistent again. */
453 if (rv == EOWNERDEAD) {
454 pthread_mutex_consistent_np(mutex->pthread_interproc);
462 mutex->curr_locked = 1;
466 static apr_status_t proc_mutex_proc_pthread_tryacquire(apr_proc_mutex_t *mutex)
470 if ((rv = pthread_mutex_trylock(mutex->pthread_interproc))) {
471 #ifdef HAVE_ZOS_PTHREADS
477 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
478 /* Okay, our owner died. Let's try to make it consistent again. */
479 if (rv == EOWNERDEAD) {
480 pthread_mutex_consistent_np(mutex->pthread_interproc);
489 mutex->curr_locked = 1;
493 static apr_status_t proc_mutex_proc_pthread_release(apr_proc_mutex_t *mutex)
497 mutex->curr_locked = 0;
498 if ((rv = pthread_mutex_unlock(mutex->pthread_interproc))) {
499 #ifdef HAVE_ZOS_PTHREADS
507 static const apr_proc_mutex_unix_lock_methods_t mutex_proc_pthread_methods =
509 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
510 proc_mutex_proc_pthread_create,
511 proc_mutex_proc_pthread_acquire,
512 proc_mutex_proc_pthread_tryacquire,
513 proc_mutex_proc_pthread_release,
514 proc_mutex_proc_pthread_cleanup,
515 proc_mutex_no_child_init,
521 #if APR_HAS_FCNTL_SERIALIZE
523 static struct flock proc_mutex_lock_it;
524 static struct flock proc_mutex_unlock_it;
526 static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *);
528 static void proc_mutex_fcntl_setup(void)
530 proc_mutex_lock_it.l_whence = SEEK_SET; /* from current point */
531 proc_mutex_lock_it.l_start = 0; /* -"- */
532 proc_mutex_lock_it.l_len = 0; /* until end of file */
533 proc_mutex_lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
534 proc_mutex_lock_it.l_pid = 0; /* pid not actually interesting */
535 proc_mutex_unlock_it.l_whence = SEEK_SET; /* from current point */
536 proc_mutex_unlock_it.l_start = 0; /* -"- */
537 proc_mutex_unlock_it.l_len = 0; /* until end of file */
538 proc_mutex_unlock_it.l_type = F_UNLCK; /* set exclusive/write lock */
539 proc_mutex_unlock_it.l_pid = 0; /* pid not actually interesting */
542 static apr_status_t proc_mutex_fcntl_cleanup(void *mutex_)
545 apr_proc_mutex_t *mutex=mutex_;
547 if (mutex->curr_locked == 1) {
548 status = proc_mutex_fcntl_release(mutex);
549 if (status != APR_SUCCESS)
553 return apr_file_close(mutex->interproc);
556 static apr_status_t proc_mutex_fcntl_create(apr_proc_mutex_t *new_mutex,
562 new_mutex->fname = apr_pstrdup(new_mutex->pool, fname);
563 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
564 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
565 APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD,
569 new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
570 rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
571 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
575 if (rv != APR_SUCCESS) {
579 new_mutex->curr_locked = 0;
580 unlink(new_mutex->fname);
581 apr_pool_cleanup_register(new_mutex->pool,
583 apr_proc_mutex_cleanup,
584 apr_pool_cleanup_null);
588 static apr_status_t proc_mutex_fcntl_acquire(apr_proc_mutex_t *mutex)
593 rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_lock_it);
594 } while (rc < 0 && errno == EINTR);
598 mutex->curr_locked=1;
602 static apr_status_t proc_mutex_fcntl_tryacquire(apr_proc_mutex_t *mutex)
607 rc = fcntl(mutex->interproc->filedes, F_SETLK, &proc_mutex_lock_it);
608 } while (rc < 0 && errno == EINTR);
610 #if FCNTL_TRYACQUIRE_EACCES
611 if (errno == EACCES) {
613 if (errno == EAGAIN) {
619 mutex->curr_locked = 1;
623 static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *mutex)
627 mutex->curr_locked=0;
629 rc = fcntl(mutex->interproc->filedes, F_SETLKW, &proc_mutex_unlock_it);
630 } while (rc < 0 && errno == EINTR);
637 static const apr_proc_mutex_unix_lock_methods_t mutex_fcntl_methods =
639 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FCNTL_IS_GLOBAL)
640 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
644 proc_mutex_fcntl_create,
645 proc_mutex_fcntl_acquire,
646 proc_mutex_fcntl_tryacquire,
647 proc_mutex_fcntl_release,
648 proc_mutex_fcntl_cleanup,
649 proc_mutex_no_child_init,
653 #endif /* fcntl implementation */
655 #if APR_HAS_FLOCK_SERIALIZE
657 static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *);
659 static apr_status_t proc_mutex_flock_cleanup(void *mutex_)
662 apr_proc_mutex_t *mutex=mutex_;
664 if (mutex->curr_locked == 1) {
665 status = proc_mutex_flock_release(mutex);
666 if (status != APR_SUCCESS)
669 if (mutex->interproc) { /* if it was opened properly */
670 apr_file_close(mutex->interproc);
672 unlink(mutex->fname);
676 static apr_status_t proc_mutex_flock_create(apr_proc_mutex_t *new_mutex,
682 new_mutex->fname = apr_pstrdup(new_mutex->pool, fname);
683 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
684 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
685 APR_UREAD | APR_UWRITE,
689 new_mutex->fname = apr_pstrdup(new_mutex->pool, "/tmp/aprXXXXXX");
690 rv = apr_file_mktemp(&new_mutex->interproc, new_mutex->fname,
691 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_EXCL,
695 if (rv != APR_SUCCESS) {
696 proc_mutex_flock_cleanup(new_mutex);
699 new_mutex->curr_locked = 0;
700 apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
701 apr_proc_mutex_cleanup,
702 apr_pool_cleanup_null);
706 static apr_status_t proc_mutex_flock_acquire(apr_proc_mutex_t *mutex)
711 rc = flock(mutex->interproc->filedes, LOCK_EX);
712 } while (rc < 0 && errno == EINTR);
716 mutex->curr_locked = 1;
720 static apr_status_t proc_mutex_flock_tryacquire(apr_proc_mutex_t *mutex)
725 rc = flock(mutex->interproc->filedes, LOCK_EX | LOCK_NB);
726 } while (rc < 0 && errno == EINTR);
728 if (errno == EWOULDBLOCK || errno == EAGAIN) {
733 mutex->curr_locked = 1;
737 static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *mutex)
741 mutex->curr_locked = 0;
743 rc = flock(mutex->interproc->filedes, LOCK_UN);
744 } while (rc < 0 && errno == EINTR);
751 static apr_status_t proc_mutex_flock_child_init(apr_proc_mutex_t **mutex,
755 apr_proc_mutex_t *new_mutex;
758 new_mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
760 memcpy(new_mutex, *mutex, sizeof *new_mutex);
761 new_mutex->pool = pool;
763 fname = (*mutex)->fname;
765 new_mutex->fname = apr_pstrdup(pool, fname);
766 rv = apr_file_open(&new_mutex->interproc, new_mutex->fname,
767 APR_FOPEN_WRITE, 0, new_mutex->pool);
768 if (rv != APR_SUCCESS) {
775 static const apr_proc_mutex_unix_lock_methods_t mutex_flock_methods =
777 #if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAS_THREADS || defined(FLOCK_IS_GLOBAL)
778 APR_PROCESS_LOCK_MECH_IS_GLOBAL,
782 proc_mutex_flock_create,
783 proc_mutex_flock_acquire,
784 proc_mutex_flock_tryacquire,
785 proc_mutex_flock_release,
786 proc_mutex_flock_cleanup,
787 proc_mutex_flock_child_init,
791 #endif /* flock implementation */
793 void apr_proc_mutex_unix_setup_lock(void)
795 /* setup only needed for sysvsem and fnctl */
796 #if APR_HAS_SYSVSEM_SERIALIZE
797 proc_mutex_sysv_setup();
799 #if APR_HAS_FCNTL_SERIALIZE
800 proc_mutex_fcntl_setup();
804 static apr_status_t proc_mutex_choose_method(apr_proc_mutex_t *new_mutex, apr_lockmech_e mech)
808 #if APR_HAS_FCNTL_SERIALIZE
809 new_mutex->inter_meth = &mutex_fcntl_methods;
815 #if APR_HAS_FLOCK_SERIALIZE
816 new_mutex->inter_meth = &mutex_flock_methods;
821 case APR_LOCK_SYSVSEM:
822 #if APR_HAS_SYSVSEM_SERIALIZE
823 new_mutex->inter_meth = &mutex_sysv_methods;
828 case APR_LOCK_POSIXSEM:
829 #if APR_HAS_POSIXSEM_SERIALIZE
830 new_mutex->inter_meth = &mutex_posixsem_methods;
835 case APR_LOCK_PROC_PTHREAD:
836 #if APR_HAS_PROC_PTHREAD_SERIALIZE
837 new_mutex->inter_meth = &mutex_proc_pthread_methods;
842 case APR_LOCK_DEFAULT:
843 #if APR_USE_FLOCK_SERIALIZE
844 new_mutex->inter_meth = &mutex_flock_methods;
845 #elif APR_USE_SYSVSEM_SERIALIZE
846 new_mutex->inter_meth = &mutex_sysv_methods;
847 #elif APR_USE_FCNTL_SERIALIZE
848 new_mutex->inter_meth = &mutex_fcntl_methods;
849 #elif APR_USE_PROC_PTHREAD_SERIALIZE
850 new_mutex->inter_meth = &mutex_proc_pthread_methods;
851 #elif APR_USE_POSIXSEM_SERIALIZE
852 new_mutex->inter_meth = &mutex_posixsem_methods;
863 APR_DECLARE(const char *) apr_proc_mutex_defname(void)
866 apr_proc_mutex_t mutex;
868 if ((rv = proc_mutex_choose_method(&mutex, APR_LOCK_DEFAULT)) != APR_SUCCESS) {
871 mutex.meth = mutex.inter_meth;
873 return apr_proc_mutex_name(&mutex);
876 static apr_status_t proc_mutex_create(apr_proc_mutex_t *new_mutex, apr_lockmech_e mech, const char *fname)
880 if ((rv = proc_mutex_choose_method(new_mutex, mech)) != APR_SUCCESS) {
884 new_mutex->meth = new_mutex->inter_meth;
886 if ((rv = new_mutex->meth->create(new_mutex, fname)) != APR_SUCCESS) {
893 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
898 apr_proc_mutex_t *new_mutex;
901 new_mutex = apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
902 new_mutex->pool = pool;
904 if ((rv = proc_mutex_create(new_mutex, mech, fname)) != APR_SUCCESS)
911 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
915 return (*mutex)->meth->child_init(mutex, pool, fname);
918 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
920 return mutex->meth->acquire(mutex);
923 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
925 return mutex->meth->tryacquire(mutex);
928 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
930 return mutex->meth->release(mutex);
933 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
935 return ((apr_proc_mutex_t *)mutex)->meth->cleanup(mutex);
938 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
940 return mutex->meth->name;
943 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
945 /* POSIX sems use the fname field but don't use a file,
947 #if APR_HAS_FLOCK_SERIALIZE
948 if (mutex->meth == &mutex_flock_methods) {
952 #if APR_HAS_FCNTL_SERIALIZE
953 if (mutex->meth == &mutex_fcntl_methods) {
960 APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
962 /* Implement OS-specific accessors defined in apr_portable.h */
964 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
965 apr_proc_mutex_t *pmutex)
967 #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
968 if (pmutex->interproc) {
969 ospmutex->crossproc = pmutex->interproc->filedes;
972 ospmutex->crossproc = -1;
975 #if APR_HAS_PROC_PTHREAD_SERIALIZE
976 ospmutex->pthread_interproc = pmutex->pthread_interproc;
981 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
982 apr_os_proc_mutex_t *ospmutex,
988 if ((*pmutex) == NULL) {
989 (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool,
990 sizeof(apr_proc_mutex_t));
991 (*pmutex)->pool = pool;
993 #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
994 apr_os_file_put(&(*pmutex)->interproc, &ospmutex->crossproc, 0, pool);
996 #if APR_HAS_PROC_PTHREAD_SERIALIZE
997 (*pmutex)->pthread_interproc = ospmutex->pthread_interproc;