pthread_win32_attach_detach_np.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /*
  39. * Handle to quserex.dll
  40. */
  41. static HINSTANCE ptw32_h_quserex;
  42. BOOL
  43. pthread_win32_process_attach_np ()
  44. {
  45. TCHAR QuserExDLLPathBuf[1024];
  46. BOOL result = TRUE;
  47. result = ptw32_processInitialize ();
  48. #if defined(_UWIN)
  49. pthread_count++;
  50. #endif
  51. #if defined(__GNUC__)
  52. ptw32_features = 0;
  53. #else
  54. /*
  55. * This is obsolete now.
  56. */
  57. ptw32_features = PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE;
  58. #endif
  59. /*
  60. * Load QUSEREX.DLL and try to get address of QueueUserAPCEx.
  61. * Because QUSEREX.DLL requires a driver to be installed we will
  62. * assume the DLL is in the system directory.
  63. *
  64. * This should take care of any security issues.
  65. */
  66. #if defined(__GNUC__) || _MSC_VER < 1400
  67. if(GetSystemDirectory(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf)))
  68. {
  69. (void) strncat(QuserExDLLPathBuf,
  70. "\\QUSEREX.DLL",
  71. sizeof(QuserExDLLPathBuf) - strlen(QuserExDLLPathBuf) - 1);
  72. ptw32_h_quserex = LoadLibrary(QuserExDLLPathBuf);
  73. }
  74. #else
  75. /* strncat is secure - this is just to avoid a warning */
  76. if(GetSystemDirectory(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf)) &&
  77. 0 == strncat_s(QuserExDLLPathBuf, sizeof(QuserExDLLPathBuf), "\\QUSEREX.DLL", 12))
  78. {
  79. ptw32_h_quserex = LoadLibrary(QuserExDLLPathBuf);
  80. }
  81. #endif
  82. if (ptw32_h_quserex != NULL)
  83. {
  84. ptw32_register_cancelation = (DWORD (*)(PAPCFUNC, HANDLE, DWORD))
  85. #if defined(NEED_UNICODE_CONSTS)
  86. GetProcAddress (ptw32_h_quserex,
  87. (const TCHAR *) TEXT ("QueueUserAPCEx"));
  88. #else
  89. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx");
  90. #endif
  91. }
  92. if (NULL == ptw32_register_cancelation)
  93. {
  94. ptw32_register_cancelation = ptw32_RegisterCancelation;
  95. if (ptw32_h_quserex != NULL)
  96. {
  97. (void) FreeLibrary (ptw32_h_quserex);
  98. }
  99. ptw32_h_quserex = 0;
  100. }
  101. else
  102. {
  103. /* Initialise QueueUserAPCEx */
  104. BOOL (*queue_user_apc_ex_init) (VOID);
  105. queue_user_apc_ex_init = (BOOL (*)(VOID))
  106. #if defined(NEED_UNICODE_CONSTS)
  107. GetProcAddress (ptw32_h_quserex,
  108. (const TCHAR *) TEXT ("QueueUserAPCEx_Init"));
  109. #else
  110. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Init");
  111. #endif
  112. if (queue_user_apc_ex_init == NULL || !queue_user_apc_ex_init ())
  113. {
  114. ptw32_register_cancelation = ptw32_RegisterCancelation;
  115. (void) FreeLibrary (ptw32_h_quserex);
  116. ptw32_h_quserex = 0;
  117. }
  118. }
  119. if (ptw32_h_quserex)
  120. {
  121. ptw32_features |= PTW32_ALERTABLE_ASYNC_CANCEL;
  122. }
  123. return result;
  124. }
  125. BOOL
  126. pthread_win32_process_detach_np ()
  127. {
  128. if (ptw32_processInitialized)
  129. {
  130. ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  131. if (sp != NULL)
  132. {
  133. /*
  134. * Detached threads have their resources automatically
  135. * cleaned up upon exit (others must be 'joined').
  136. */
  137. if (sp->detachState == PTHREAD_CREATE_DETACHED)
  138. {
  139. ptw32_threadDestroy (sp->ptHandle);
  140. TlsSetValue (ptw32_selfThreadKey->key, NULL);
  141. }
  142. }
  143. /*
  144. * The DLL is being unmapped from the process's address space
  145. */
  146. ptw32_processTerminate ();
  147. if (ptw32_h_quserex)
  148. {
  149. /* Close QueueUserAPCEx */
  150. BOOL (*queue_user_apc_ex_fini) (VOID);
  151. queue_user_apc_ex_fini = (BOOL (*)(VOID))
  152. #if defined(NEED_UNICODE_CONSTS)
  153. GetProcAddress (ptw32_h_quserex,
  154. (const TCHAR *) TEXT ("QueueUserAPCEx_Fini"));
  155. #else
  156. GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Fini");
  157. #endif
  158. if (queue_user_apc_ex_fini != NULL)
  159. {
  160. (void) queue_user_apc_ex_fini ();
  161. }
  162. (void) FreeLibrary (ptw32_h_quserex);
  163. }
  164. }
  165. return TRUE;
  166. }
  167. BOOL
  168. pthread_win32_thread_attach_np ()
  169. {
  170. return TRUE;
  171. }
  172. BOOL
  173. pthread_win32_thread_detach_np ()
  174. {
  175. if (ptw32_processInitialized)
  176. {
  177. /*
  178. * Don't use pthread_self() - to avoid creating an implicit POSIX thread handle
  179. * unnecessarily.
  180. */
  181. ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  182. if (sp != NULL) // otherwise Win32 thread with no implicit POSIX handle.
  183. {
  184. ptw32_mcs_local_node_t stateLock;
  185. ptw32_callUserDestroyRoutines (sp->ptHandle);
  186. ptw32_mcs_lock_acquire (&sp->stateLock, &stateLock);
  187. sp->state = PThreadStateLast;
  188. /*
  189. * If the thread is joinable at this point then it MUST be joined
  190. * or detached explicitly by the application.
  191. */
  192. ptw32_mcs_lock_release (&stateLock);
  193. /*
  194. * Robust Mutexes
  195. */
  196. while (sp->robustMxList != NULL)
  197. {
  198. pthread_mutex_t mx = sp->robustMxList->mx;
  199. ptw32_robust_mutex_remove(&mx, sp);
  200. (void) PTW32_INTERLOCKED_EXCHANGE_LONG(
  201. (PTW32_INTERLOCKED_LONGPTR)&mx->robustNode->stateInconsistent,
  202. (PTW32_INTERLOCKED_LONG)-1);
  203. /*
  204. * If there are no waiters then the next thread to block will
  205. * sleep, wakeup immediately and then go back to sleep.
  206. * See pthread_mutex_lock.c.
  207. */
  208. SetEvent(mx->event);
  209. }
  210. if (sp->detachState == PTHREAD_CREATE_DETACHED)
  211. {
  212. ptw32_threadDestroy (sp->ptHandle);
  213. TlsSetValue (ptw32_selfThreadKey->key, NULL);
  214. }
  215. }
  216. }
  217. return TRUE;
  218. }
  219. BOOL
  220. pthread_win32_test_features_np (int feature_mask)
  221. {
  222. return ((ptw32_features & feature_mask) == feature_mask);
  223. }