thread.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_private.h"
  17. #include "win32/fspr_arch_threadproc.h"
  18. #include "fspr_thread_proc.h"
  19. #include "fspr_general.h"
  20. #include "fspr_lib.h"
  21. #include "fspr_portable.h"
  22. #if APR_HAVE_PROCESS_H
  23. #include <process.h>
  24. #endif
  25. #include "fspr_arch_misc.h"
  26. /* Chosen for us by fspr_initialize */
  27. DWORD tls_fspr_thread = 0;
  28. APR_DECLARE(fspr_status_t) fspr_threadattr_create(fspr_threadattr_t **new,
  29. fspr_pool_t *pool)
  30. {
  31. (*new) = (fspr_threadattr_t *)fspr_palloc(pool,
  32. sizeof(fspr_threadattr_t));
  33. if ((*new) == NULL) {
  34. return APR_ENOMEM;
  35. }
  36. (*new)->pool = pool;
  37. (*new)->detach = 0;
  38. (*new)->stacksize = 0;
  39. return APR_SUCCESS;
  40. }
  41. APR_DECLARE(fspr_status_t) fspr_threadattr_detach_set(fspr_threadattr_t *attr,
  42. fspr_int32_t on)
  43. {
  44. attr->detach = on;
  45. return APR_SUCCESS;
  46. }
  47. APR_DECLARE(fspr_status_t) fspr_threadattr_detach_get(fspr_threadattr_t *attr)
  48. {
  49. if (attr->detach == 1)
  50. return APR_DETACH;
  51. return APR_NOTDETACH;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_threadattr_stacksize_set(fspr_threadattr_t *attr,
  54. fspr_size_t stacksize)
  55. {
  56. attr->stacksize = stacksize;
  57. return APR_SUCCESS;
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_threadattr_guardsize_set(fspr_threadattr_t *attr,
  60. fspr_size_t size)
  61. {
  62. return APR_ENOTIMPL;
  63. }
  64. static void *dummy_worker(void *opaque)
  65. {
  66. fspr_thread_t *thd = (fspr_thread_t *)opaque;
  67. TlsSetValue(tls_fspr_thread, thd->td);
  68. return thd->func(thd, thd->data);
  69. }
  70. APR_DECLARE(fspr_status_t) fspr_thread_create(fspr_thread_t **new,
  71. fspr_threadattr_t *attr,
  72. fspr_thread_start_t func,
  73. void *data, fspr_pool_t *pool)
  74. {
  75. fspr_status_t stat;
  76. unsigned temp;
  77. HANDLE handle;
  78. int priority = THREAD_PRIORITY_NORMAL;
  79. (*new) = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_thread_t));
  80. if ((*new) == NULL) {
  81. return APR_ENOMEM;
  82. }
  83. (*new)->pool = pool;
  84. (*new)->data = data;
  85. (*new)->func = func;
  86. (*new)->td = NULL;
  87. stat = fspr_pool_create(&(*new)->pool, pool);
  88. if (stat != APR_SUCCESS) {
  89. return stat;
  90. }
  91. if (attr && attr->priority && attr->priority > 0) {
  92. if (attr->priority >= 99) {
  93. priority = THREAD_PRIORITY_TIME_CRITICAL;
  94. } else if (attr->priority >= 50) {
  95. priority = THREAD_PRIORITY_ABOVE_NORMAL;
  96. } else if (attr->priority >= 10) {
  97. priority = THREAD_PRIORITY_NORMAL;
  98. } else if (attr->priority >= 1) {
  99. priority = THREAD_PRIORITY_LOWEST;
  100. }
  101. }
  102. /* Use 0 for Thread Stack Size, because that will default the stack to the
  103. * same size as the calling thread.
  104. */
  105. #ifndef _WIN32_WCE
  106. if ((handle = (HANDLE)_beginthreadex(NULL,
  107. attr && attr->stacksize > 0 ? attr->stacksize : 0,
  108. (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
  109. (*new), 0, &temp)) == 0) {
  110. return APR_FROM_OS_ERROR(_doserrno);
  111. }
  112. #else
  113. if ((handle = CreateThread(NULL,
  114. attr && attr->stacksize > 0 ? attr->stacksize : 0,
  115. (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
  116. (*new), 0, &temp)) == 0) {
  117. return fspr_get_os_error();
  118. }
  119. #endif
  120. if (priority) {
  121. SetThreadPriority(handle, priority);
  122. }
  123. if (attr && attr->detach) {
  124. CloseHandle(handle);
  125. }
  126. else
  127. (*new)->td = handle;
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(fspr_status_t) fspr_thread_exit(fspr_thread_t *thd,
  131. fspr_status_t retval)
  132. {
  133. thd->exitval = retval;
  134. fspr_pool_destroy(thd->pool);
  135. thd->pool = NULL;
  136. #ifndef _WIN32_WCE
  137. _endthreadex(0);
  138. #else
  139. ExitThread(0);
  140. #endif
  141. return APR_SUCCESS;
  142. }
  143. APR_DECLARE(fspr_status_t) fspr_thread_join(fspr_status_t *retval,
  144. fspr_thread_t *thd)
  145. {
  146. fspr_status_t rv = APR_SUCCESS;
  147. if (!thd->td) {
  148. /* Can not join on detached threads */
  149. return APR_DETACH;
  150. }
  151. rv = WaitForSingleObject(thd->td, INFINITE);
  152. if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
  153. /* If the thread_exit has been called */
  154. if (!thd->pool)
  155. *retval = thd->exitval;
  156. else
  157. rv = APR_INCOMPLETE;
  158. }
  159. else
  160. rv = fspr_get_os_error();
  161. CloseHandle(thd->td);
  162. thd->td = NULL;
  163. return rv;
  164. }
  165. APR_DECLARE(fspr_status_t) fspr_thread_detach(fspr_thread_t *thd)
  166. {
  167. if (thd->td && CloseHandle(thd->td)) {
  168. thd->td = NULL;
  169. return APR_SUCCESS;
  170. }
  171. else {
  172. return fspr_get_os_error();
  173. }
  174. }
  175. APR_DECLARE(void) fspr_thread_yield()
  176. {
  177. /* SwitchToThread is not supported on Win9x, but since it's
  178. * primarily a noop (entering time consuming code, therefore
  179. * providing more critical threads a bit larger timeslice)
  180. * we won't worry too much if it's not available.
  181. */
  182. #ifndef _WIN32_WCE
  183. if (fspr_os_level >= APR_WIN_NT) {
  184. SwitchToThread();
  185. }
  186. #endif
  187. }
  188. APR_DECLARE(fspr_status_t) fspr_thread_data_get(void **data, const char *key,
  189. fspr_thread_t *thread)
  190. {
  191. return fspr_pool_userdata_get(data, key, thread->pool);
  192. }
  193. APR_DECLARE(fspr_status_t) fspr_thread_data_set(void *data, const char *key,
  194. fspr_status_t (*cleanup) (void *),
  195. fspr_thread_t *thread)
  196. {
  197. return fspr_pool_userdata_set(data, key, cleanup, thread->pool);
  198. }
  199. APR_DECLARE(fspr_os_thread_t) fspr_os_thread_current(void)
  200. {
  201. HANDLE hthread = (HANDLE)TlsGetValue(tls_fspr_thread);
  202. HANDLE hproc;
  203. if (hthread) {
  204. return hthread;
  205. }
  206. hproc = GetCurrentProcess();
  207. hthread = GetCurrentThread();
  208. if (!DuplicateHandle(hproc, hthread,
  209. hproc, &hthread, 0, FALSE,
  210. DUPLICATE_SAME_ACCESS)) {
  211. return NULL;
  212. }
  213. TlsSetValue(tls_fspr_thread, hthread);
  214. return hthread;
  215. }
  216. APR_DECLARE(fspr_status_t) fspr_os_thread_get(fspr_os_thread_t **thethd,
  217. fspr_thread_t *thd)
  218. {
  219. if (thd == NULL) {
  220. return APR_ENOTHREAD;
  221. }
  222. *thethd = thd->td;
  223. return APR_SUCCESS;
  224. }
  225. APR_DECLARE(fspr_status_t) fspr_os_thread_put(fspr_thread_t **thd,
  226. fspr_os_thread_t *thethd,
  227. fspr_pool_t *pool)
  228. {
  229. if (pool == NULL) {
  230. return APR_ENOPOOL;
  231. }
  232. if ((*thd) == NULL) {
  233. (*thd) = (fspr_thread_t *)fspr_palloc(pool, sizeof(fspr_thread_t));
  234. (*thd)->pool = pool;
  235. }
  236. (*thd)->td = thethd;
  237. return APR_SUCCESS;
  238. }
  239. APR_DECLARE(fspr_status_t) fspr_thread_once_init(fspr_thread_once_t **control,
  240. fspr_pool_t *p)
  241. {
  242. (*control) = fspr_pcalloc(p, sizeof(**control));
  243. return APR_SUCCESS;
  244. }
  245. APR_DECLARE(fspr_status_t) fspr_thread_once(fspr_thread_once_t *control,
  246. void (*func)(void))
  247. {
  248. if (!InterlockedExchange(&control->value, 1)) {
  249. func();
  250. }
  251. return APR_SUCCESS;
  252. }
  253. APR_DECLARE(int) fspr_os_thread_equal(fspr_os_thread_t tid1,
  254. fspr_os_thread_t tid2)
  255. {
  256. /* Since the only tid's we support our are own, and
  257. * fspr_os_thread_current returns the identical handle
  258. * to the one we created initially, the test is simple.
  259. */
  260. return (tid1 == tid2);
  261. }
  262. APR_POOL_IMPLEMENT_ACCESSOR(thread)