pthread_win32_attach_detach_np.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * pthread_win32_attach_detach_np.c
  3. *
  4. * Description:
  5. * This translation unit implements non-portable thread functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. #include <tchar.h>
  39. /*
  40. * Handle to quserex.dll
  41. */
  42. static HINSTANCE ptw32_h_quserex;
  43. BOOL
  44. pthread_win32_process_attach_np ()
  45. {
  46. TCHAR QuserExDLLPathBuf[1024];
  47. BOOL result = TRUE;
  48. result = ptw32_processInitialize ();
  49. #if defined(_UWIN)
  50. pthread_count++;
  51. #endif
  52. #if defined(__GNUC__)
  53. ptw32_features = 0;
  54. #else
  55. /*
  56. * This is obsolete now.
  57. */
  58. ptw32_features = PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE;
  59. #endif
  60. /*
  61. * Load QUSEREX.DLL and try to get address of QueueUserAPCEx.
  62. * Because QUSEREX.DLL requires a driver to be installed we will
  63. * assume the DLL is in the system directory.
  64. *
  65. * This should take care of any security issues.
  66. */
  67. #if defined(__GNUC__) || _MSC_VER < 1400
  68. if(GetSystemDirectory(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf)))
  69. {
  70. (void) strncat(QuserExDLLPathBuf,
  71. "\\QUSEREX.DLL",
  72. sizeof(QuserExDLLPathBuf) - strlen(QuserExDLLPathBuf) - 1);
  73. ptw32_h_quserex = LoadLibrary(QuserExDLLPathBuf);
  74. }
  75. #else
  76. /* strncat is secure - this is just to avoid a warning */
  77. if(GetSystemDirectory(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf) / sizeof(TCHAR)) &&
  78. 0 == _tcsncat_s(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf) / sizeof(TCHAR), TEXT("\\QUSEREX.DLL"), 12))
  79. {
  80. ptw32_h_quserex = LoadLibrary(QuserExDLLPathBuf);
  81. }
  82. #endif
  83. if (ptw32_h_quserex != NULL)
  84. {
  85. ptw32_register_cancelation = (DWORD (*)(PAPCFUNC, HANDLE, DWORD))
  86. #if defined(NEED_UNICODE_CONSTS)
  87. GetProcAddress (ptw32_h_quserex,
  88. (const TCHAR *) TEXT ("QueueUserAPCEx"));
  89. #else
  90. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx");
  91. #endif
  92. }
  93. if (NULL == ptw32_register_cancelation)
  94. {
  95. ptw32_register_cancelation = ptw32_RegisterCancelation;
  96. if (ptw32_h_quserex != NULL)
  97. {
  98. (void) FreeLibrary (ptw32_h_quserex);
  99. }
  100. ptw32_h_quserex = 0;
  101. }
  102. else
  103. {
  104. /* Initialise QueueUserAPCEx */
  105. BOOL (*queue_user_apc_ex_init) (VOID);
  106. queue_user_apc_ex_init = (BOOL (*)(VOID))
  107. #if defined(NEED_UNICODE_CONSTS)
  108. GetProcAddress (ptw32_h_quserex,
  109. (const TCHAR *) TEXT ("QueueUserAPCEx_Init"));
  110. #else
  111. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Init");
  112. #endif
  113. if (queue_user_apc_ex_init == NULL || !queue_user_apc_ex_init ())
  114. {
  115. ptw32_register_cancelation = ptw32_RegisterCancelation;
  116. (void) FreeLibrary (ptw32_h_quserex);
  117. ptw32_h_quserex = 0;
  118. }
  119. }
  120. if (ptw32_h_quserex)
  121. {
  122. ptw32_features |= PTW32_ALERTABLE_ASYNC_CANCEL;
  123. }
  124. return result;
  125. }
  126. BOOL
  127. pthread_win32_process_detach_np ()
  128. {
  129. if (ptw32_processInitialized)
  130. {
  131. ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  132. if (sp != NULL)
  133. {
  134. /*
  135. * Detached threads have their resources automatically
  136. * cleaned up upon exit (others must be 'joined').
  137. */
  138. if (sp->detachState == PTHREAD_CREATE_DETACHED)
  139. {
  140. ptw32_threadDestroy (sp->ptHandle);
  141. TlsSetValue (ptw32_selfThreadKey->key, NULL);
  142. }
  143. }
  144. /*
  145. * The DLL is being unmapped from the process's address space
  146. */
  147. ptw32_processTerminate ();
  148. if (ptw32_h_quserex)
  149. {
  150. /* Close QueueUserAPCEx */
  151. BOOL (*queue_user_apc_ex_fini) (VOID);
  152. queue_user_apc_ex_fini = (BOOL (*)(VOID))
  153. #if defined(NEED_UNICODE_CONSTS)
  154. GetProcAddress (ptw32_h_quserex,
  155. (const TCHAR *) TEXT ("QueueUserAPCEx_Fini"));
  156. #else
  157. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Fini");
  158. #endif
  159. if (queue_user_apc_ex_fini != NULL)
  160. {
  161. (void) queue_user_apc_ex_fini ();
  162. }
  163. (void) FreeLibrary (ptw32_h_quserex);
  164. }
  165. }
  166. return TRUE;
  167. }
  168. BOOL
  169. pthread_win32_thread_attach_np ()
  170. {
  171. return TRUE;
  172. }
  173. BOOL
  174. pthread_win32_thread_detach_np ()
  175. {
  176. if (ptw32_processInitialized)
  177. {
  178. /*
  179. * Don't use pthread_self() - to avoid creating an implicit POSIX thread handle
  180. * unnecessarily.
  181. */
  182. ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  183. if (sp != NULL) // otherwise Win32 thread with no implicit POSIX handle.
  184. {
  185. ptw32_mcs_local_node_t stateLock;
  186. ptw32_callUserDestroyRoutines (sp->ptHandle);
  187. ptw32_mcs_lock_acquire (&sp->stateLock, &stateLock);
  188. sp->state = PThreadStateLast;
  189. /*
  190. * If the thread is joinable at this point then it MUST be joined
  191. * or detached explicitly by the application.
  192. */
  193. ptw32_mcs_lock_release (&stateLock);
  194. /*
  195. * Robust Mutexes
  196. */
  197. while (sp->robustMxList != NULL)
  198. {
  199. pthread_mutex_t mx = sp->robustMxList->mx;
  200. ptw32_robust_mutex_remove(&mx, sp);
  201. (void) PTW32_INTERLOCKED_EXCHANGE_LONG(
  202. (PTW32_INTERLOCKED_LONGPTR)&mx->robustNode->stateInconsistent,
  203. (PTW32_INTERLOCKED_LONG)-1);
  204. /*
  205. * If there are no waiters then the next thread to block will
  206. * sleep, wakeup immediately and then go back to sleep.
  207. * See pthread_mutex_lock.c.
  208. */
  209. SetEvent(mx->event);
  210. }
  211. if (sp->detachState == PTHREAD_CREATE_DETACHED)
  212. {
  213. ptw32_threadDestroy (sp->ptHandle);
  214. TlsSetValue (ptw32_selfThreadKey->key, NULL);
  215. }
  216. }
  217. }
  218. return TRUE;
  219. }
  220. BOOL
  221. pthread_win32_test_features_np (int feature_mask)
  222. {
  223. return ((ptw32_features & feature_mask) == feature_mask);
  224. }