proc_mutex.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include "fspr_general.h"
  17. #include "fspr_lib.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_portable.h"
  20. #include "fspr_arch_proc_mutex.h"
  21. #include "fspr_arch_file_io.h"
  22. #include <string.h>
  23. #include <stddef.h>
  24. #define CurrentTid (*_threadid)
  25. static char *fixed_name(const char *fname, fspr_pool_t *pool)
  26. {
  27. char *semname;
  28. if (fname == NULL)
  29. semname = NULL;
  30. else {
  31. // Semaphores don't live in the file system, fix up the name
  32. while (*fname == '/' || *fname == '\\') {
  33. fname++;
  34. }
  35. semname = fspr_pstrcat(pool, "/SEM32/", fname, NULL);
  36. if (semname[8] == ':') {
  37. semname[8] = '$';
  38. }
  39. }
  40. return semname;
  41. }
  42. APR_DECLARE(fspr_status_t) fspr_proc_mutex_cleanup(void *vmutex)
  43. {
  44. fspr_proc_mutex_t *mutex = vmutex;
  45. return fspr_proc_mutex_destroy(mutex);
  46. }
  47. APR_DECLARE(const char *) fspr_proc_mutex_lockfile(fspr_proc_mutex_t *mutex)
  48. {
  49. return NULL;
  50. }
  51. APR_DECLARE(const char *) fspr_proc_mutex_name(fspr_proc_mutex_t *mutex)
  52. {
  53. return "os2sem";
  54. }
  55. APR_DECLARE(const char *) fspr_proc_mutex_defname(void)
  56. {
  57. return "os2sem";
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_proc_mutex_create(fspr_proc_mutex_t **mutex,
  60. const char *fname,
  61. fspr_lockmech_e mech,
  62. fspr_pool_t *pool)
  63. {
  64. fspr_proc_mutex_t *new;
  65. ULONG rc;
  66. char *semname;
  67. if (mech != APR_LOCK_DEFAULT) {
  68. return APR_ENOTIMPL;
  69. }
  70. new = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  71. new->pool = pool;
  72. new->owner = 0;
  73. new->lock_count = 0;
  74. *mutex = new;
  75. semname = fixed_name(fname, pool);
  76. rc = DosCreateMutexSem(semname, &(new->hMutex), DC_SEM_SHARED, FALSE);
  77. if (!rc) {
  78. fspr_pool_cleanup_register(pool, new, fspr_proc_mutex_cleanup, fspr_pool_cleanup_null);
  79. }
  80. return APR_FROM_OS_ERROR(rc);
  81. }
  82. APR_DECLARE(fspr_status_t) fspr_proc_mutex_child_init(fspr_proc_mutex_t **mutex,
  83. const char *fname,
  84. fspr_pool_t *pool)
  85. {
  86. fspr_proc_mutex_t *new;
  87. ULONG rc;
  88. char *semname;
  89. new = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  90. new->pool = pool;
  91. new->owner = 0;
  92. new->lock_count = 0;
  93. semname = fixed_name(fname, pool);
  94. rc = DosOpenMutexSem(semname, &(new->hMutex));
  95. *mutex = new;
  96. if (!rc) {
  97. fspr_pool_cleanup_register(pool, new, fspr_proc_mutex_cleanup, fspr_pool_cleanup_null);
  98. }
  99. return APR_FROM_OS_ERROR(rc);
  100. }
  101. APR_DECLARE(fspr_status_t) fspr_proc_mutex_lock(fspr_proc_mutex_t *mutex)
  102. {
  103. ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
  104. if (rc == 0) {
  105. mutex->owner = CurrentTid;
  106. mutex->lock_count++;
  107. }
  108. return APR_FROM_OS_ERROR(rc);
  109. }
  110. APR_DECLARE(fspr_status_t) fspr_proc_mutex_trylock(fspr_proc_mutex_t *mutex)
  111. {
  112. ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
  113. if (rc == 0) {
  114. mutex->owner = CurrentTid;
  115. mutex->lock_count++;
  116. }
  117. return APR_FROM_OS_ERROR(rc);
  118. }
  119. APR_DECLARE(fspr_status_t) fspr_proc_mutex_unlock(fspr_proc_mutex_t *mutex)
  120. {
  121. ULONG rc;
  122. if (mutex->owner == CurrentTid && mutex->lock_count > 0) {
  123. mutex->lock_count--;
  124. rc = DosReleaseMutexSem(mutex->hMutex);
  125. return APR_FROM_OS_ERROR(rc);
  126. }
  127. return APR_SUCCESS;
  128. }
  129. APR_DECLARE(fspr_status_t) fspr_proc_mutex_destroy(fspr_proc_mutex_t *mutex)
  130. {
  131. ULONG rc;
  132. fspr_status_t status = APR_SUCCESS;
  133. if (mutex->owner == CurrentTid) {
  134. while (mutex->lock_count > 0 && status == APR_SUCCESS) {
  135. status = fspr_proc_mutex_unlock(mutex);
  136. }
  137. }
  138. if (status != APR_SUCCESS) {
  139. return status;
  140. }
  141. if (mutex->hMutex == 0) {
  142. return APR_SUCCESS;
  143. }
  144. rc = DosCloseMutexSem(mutex->hMutex);
  145. if (!rc) {
  146. mutex->hMutex = 0;
  147. }
  148. return APR_FROM_OS_ERROR(rc);
  149. }
  150. APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  151. /* Implement OS-specific accessors defined in fspr_portable.h */
  152. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_get(fspr_os_proc_mutex_t *ospmutex,
  153. fspr_proc_mutex_t *pmutex)
  154. {
  155. *ospmutex = pmutex->hMutex;
  156. return APR_ENOTIMPL;
  157. }
  158. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_put(fspr_proc_mutex_t **pmutex,
  159. fspr_os_proc_mutex_t *ospmutex,
  160. fspr_pool_t *pool)
  161. {
  162. fspr_proc_mutex_t *new;
  163. new = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  164. new->pool = pool;
  165. new->owner = 0;
  166. new->lock_count = 0;
  167. new->hMutex = *ospmutex;
  168. *pmutex = new;
  169. return APR_SUCCESS;
  170. }