thread.c 6.4 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 "fspr_arch_threadproc.h"
  19. #include "fspr_thread_proc.h"
  20. #include "fspr_general.h"
  21. #include "fspr_lib.h"
  22. #include "fspr_portable.h"
  23. #include "fspr_arch_file_io.h"
  24. #include <stdlib.h>
  25. APR_DECLARE(fspr_status_t) fspr_threadattr_create(fspr_threadattr_t **new, fspr_pool_t *pool)
  26. {
  27. (*new) = (fspr_threadattr_t *)fspr_palloc(pool, sizeof(fspr_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(fspr_status_t) fspr_threadattr_detach_set(fspr_threadattr_t *attr, fspr_int32_t on)
  37. {
  38. attr->attr |= APR_THREADATTR_DETACHED;
  39. return APR_SUCCESS;
  40. }
  41. APR_DECLARE(fspr_status_t) fspr_threadattr_detach_get(fspr_threadattr_t *attr)
  42. {
  43. return (attr->attr & APR_THREADATTR_DETACHED) ? APR_DETACH : APR_NOTDETACH;
  44. }
  45. APR_DECLARE(fspr_status_t) fspr_threadattr_stacksize_set(fspr_threadattr_t *attr,
  46. fspr_size_t stacksize)
  47. {
  48. attr->stacksize = stacksize;
  49. return APR_SUCCESS;
  50. }
  51. APR_DECLARE(fspr_status_t) fspr_threadattr_guardsize_set(fspr_threadattr_t *attr,
  52. fspr_size_t size)
  53. {
  54. return APR_ENOTIMPL;
  55. }
  56. static void fspr_thread_begin(void *arg)
  57. {
  58. fspr_thread_t *thread = (fspr_thread_t *)arg;
  59. thread->exitval = thread->func(thread, thread->data);
  60. }
  61. APR_DECLARE(fspr_status_t) fspr_thread_create(fspr_thread_t **new, fspr_threadattr_t *attr,
  62. fspr_thread_start_t func, void *data,
  63. fspr_pool_t *pool)
  64. {
  65. fspr_status_t stat;
  66. fspr_thread_t *thread;
  67. thread = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_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 = fspr_pool_create(&thread->pool, pool);
  77. if (stat != APR_SUCCESS) {
  78. return stat;
  79. }
  80. if (attr == NULL) {
  81. stat = fspr_threadattr_create(&thread->attr, thread->pool);
  82. if (stat != APR_SUCCESS) {
  83. return stat;
  84. }
  85. }
  86. thread->tid = _beginthread(fspr_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(fspr_os_thread_t) fspr_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(fspr_status_t) fspr_thread_exit(fspr_thread_t *thd, fspr_status_t retval)
  103. {
  104. thd->exitval = retval;
  105. _endthread();
  106. return -1; /* If we get here something's wrong */
  107. }
  108. APR_DECLARE(fspr_status_t) fspr_thread_join(fspr_status_t *retval, fspr_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(fspr_status_t) fspr_thread_detach(fspr_thread_t *thd)
  121. {
  122. thd->attr->attr |= APR_THREADATTR_DETACHED;
  123. return APR_SUCCESS;
  124. }
  125. void fspr_thread_yield()
  126. {
  127. DosSleep(0);
  128. }
  129. APR_DECLARE(fspr_status_t) fspr_os_thread_get(fspr_os_thread_t **thethd, fspr_thread_t *thd)
  130. {
  131. *thethd = &thd->tid;
  132. return APR_SUCCESS;
  133. }
  134. APR_DECLARE(fspr_status_t) fspr_os_thread_put(fspr_thread_t **thd, fspr_os_thread_t *thethd,
  135. fspr_pool_t *pool)
  136. {
  137. if ((*thd) == NULL) {
  138. (*thd) = (fspr_thread_t *)fspr_pcalloc(pool, sizeof(fspr_thread_t));
  139. (*thd)->pool = pool;
  140. }
  141. (*thd)->tid = *thethd;
  142. return APR_SUCCESS;
  143. }
  144. int fspr_os_thread_equal(fspr_os_thread_t tid1, fspr_os_thread_t tid2)
  145. {
  146. return tid1 == tid2;
  147. }
  148. APR_DECLARE(fspr_status_t) fspr_thread_data_get(void **data, const char *key, fspr_thread_t *thread)
  149. {
  150. return fspr_pool_userdata_get(data, key, thread->pool);
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_thread_data_set(void *data, const char *key,
  153. fspr_status_t (*cleanup) (void *),
  154. fspr_thread_t *thread)
  155. {
  156. return fspr_pool_userdata_set(data, key, cleanup, thread->pool);
  157. }
  158. APR_POOL_IMPLEMENT_ACCESSOR(thread)
  159. static fspr_status_t thread_once_cleanup(void *vcontrol)
  160. {
  161. fspr_thread_once_t *control = (fspr_thread_once_t *)vcontrol;
  162. if (control->sem) {
  163. DosCloseEventSem(control->sem);
  164. }
  165. return APR_SUCCESS;
  166. }
  167. APR_DECLARE(fspr_status_t) fspr_thread_once_init(fspr_thread_once_t **control,
  168. fspr_pool_t *p)
  169. {
  170. ULONG rc;
  171. *control = (fspr_thread_once_t *)fspr_pcalloc(p, sizeof(fspr_thread_once_t));
  172. rc = DosCreateEventSem(NULL, &(*control)->sem, 0, TRUE);
  173. fspr_pool_cleanup_register(p, control, thread_once_cleanup, fspr_pool_cleanup_null);
  174. return APR_FROM_OS_ERROR(rc);
  175. }
  176. APR_DECLARE(fspr_status_t) fspr_thread_once(fspr_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. }