thread.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #define INCL_DOSERRORS
  17. #define INCL_DOS
  18. #include "apr_arch_threadproc.h"
  19. #include "apr_thread_proc.h"
  20. #include "apr_general.h"
  21. #include "apr_lib.h"
  22. #include "apr_portable.h"
  23. #include "apr_arch_file_io.h"
  24. #include <stdlib.h>
  25. APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
  26. {
  27. (*new) = (apr_threadattr_t *)apr_palloc(pool, sizeof(apr_threadattr_t));
  28. if ((*new) == NULL) {
  29. return APR_ENOMEM;
  30. }
  31. (*new)->pool = pool;
  32. (*new)->attr = 0;
  33. (*new)->stacksize = 0;
  34. return APR_SUCCESS;
  35. }
  36. APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_int32_t on)
  37. {
  38. attr->attr |= APR_THREADATTR_DETACHED;
  39. return APR_SUCCESS;
  40. }
  41. APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
  42. {
  43. return (attr->attr & APR_THREADATTR_DETACHED) ? APR_DETACH : APR_NOTDETACH;
  44. }
  45. APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
  46. apr_size_t stacksize)
  47. {
  48. attr->stacksize = stacksize;
  49. return APR_SUCCESS;
  50. }
  51. APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
  52. apr_size_t size)
  53. {
  54. return APR_ENOTIMPL;
  55. }
  56. static void apr_thread_begin(void *arg)
  57. {
  58. apr_thread_t *thread = (apr_thread_t *)arg;
  59. thread->exitval = thread->func(thread, thread->data);
  60. }
  61. APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
  62. apr_thread_start_t func, void *data,
  63. apr_pool_t *pool)
  64. {
  65. apr_status_t stat;
  66. apr_thread_t *thread;
  67. thread = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
  68. *new = thread;
  69. if (thread == NULL) {
  70. return APR_ENOMEM;
  71. }
  72. thread->pool = pool;
  73. thread->attr = attr;
  74. thread->func = func;
  75. thread->data = data;
  76. stat = apr_pool_create(&thread->pool, pool);
  77. if (stat != APR_SUCCESS) {
  78. return stat;
  79. }
  80. if (attr == NULL) {
  81. stat = apr_threadattr_create(&thread->attr, thread->pool);
  82. if (stat != APR_SUCCESS) {
  83. return stat;
  84. }
  85. }
  86. thread->tid = _beginthread(apr_thread_begin, NULL,
  87. thread->attr->stacksize > 0 ?
  88. thread->attr->stacksize : APR_THREAD_STACKSIZE,
  89. thread);
  90. if (thread->tid < 0) {
  91. return errno;
  92. }
  93. return APR_SUCCESS;
  94. }
  95. APR_DECLARE(apr_os_thread_t) apr_os_thread_current()
  96. {
  97. PIB *ppib;
  98. TIB *ptib;
  99. DosGetInfoBlocks(&ptib, &ppib);
  100. return ptib->tib_ptib2->tib2_ultid;
  101. }
  102. APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
  103. {
  104. thd->exitval = retval;
  105. _endthread();
  106. return -1; /* If we get here something's wrong */
  107. }
  108. APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
  109. {
  110. ULONG rc;
  111. TID waittid = thd->tid;
  112. if (thd->attr->attr & APR_THREADATTR_DETACHED)
  113. return APR_EINVAL;
  114. rc = DosWaitThread(&waittid, DCWW_WAIT);
  115. if (rc == ERROR_INVALID_THREADID)
  116. rc = 0; /* Thread had already terminated */
  117. *retval = thd->exitval;
  118. return APR_OS2_STATUS(rc);
  119. }
  120. APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
  121. {
  122. thd->attr->attr |= APR_THREADATTR_DETACHED;
  123. return APR_SUCCESS;
  124. }
  125. void apr_thread_yield()
  126. {
  127. DosSleep(0);
  128. }
  129. APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
  130. {
  131. *thethd = &thd->tid;
  132. return APR_SUCCESS;
  133. }
  134. APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd,
  135. apr_pool_t *pool)
  136. {
  137. if ((*thd) == NULL) {
  138. (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
  139. (*thd)->pool = pool;
  140. }
  141. (*thd)->tid = *thethd;
  142. return APR_SUCCESS;
  143. }
  144. int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
  145. {
  146. return tid1 == tid2;
  147. }
  148. APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
  149. {
  150. return apr_pool_userdata_get(data, key, thread->pool);
  151. }
  152. APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
  153. apr_status_t (*cleanup) (void *),
  154. apr_thread_t *thread)
  155. {
  156. return apr_pool_userdata_set(data, key, cleanup, thread->pool);
  157. }
  158. APR_POOL_IMPLEMENT_ACCESSOR(thread)
  159. static apr_status_t thread_once_cleanup(void *vcontrol)
  160. {
  161. apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
  162. if (control->sem) {
  163. DosCloseEventSem(control->sem);
  164. }
  165. return APR_SUCCESS;
  166. }
  167. APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
  168. apr_pool_t *p)
  169. {
  170. ULONG rc;
  171. *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
  172. rc = DosCreateEventSem(NULL, &(*control)->sem, 0, TRUE);
  173. apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
  174. return APR_FROM_OS_ERROR(rc);
  175. }
  176. APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
  177. void (*func)(void))
  178. {
  179. if (!control->hit) {
  180. ULONG count, rc;
  181. rc = DosResetEventSem(control->sem, &count);
  182. if (rc == 0 && count) {
  183. control->hit = 1;
  184. func();
  185. }
  186. }
  187. return APR_SUCCESS;
  188. }