pthread_detach.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * pthread_detach.c
  3. *
  4. * Description:
  5. * This translation unit implements functions related to thread
  6. * synchronisation.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. /*
  40. * Not needed yet, but defining it should indicate clashes with build target
  41. * environment that should be fixed.
  42. */
  43. #if !defined(WINCE)
  44. # include <signal.h>
  45. #endif
  46. int
  47. pthread_detach (pthread_t thread)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function detaches the given thread.
  52. *
  53. * PARAMETERS
  54. * thread
  55. * an instance of a pthread_t
  56. *
  57. *
  58. * DESCRIPTION
  59. * This function detaches the given thread. You may use it to
  60. * detach the main thread or to detach a joinable thread.
  61. * NOTE: detached threads cannot be joined;
  62. * storage is freed immediately on termination.
  63. *
  64. * RESULTS
  65. * 0 successfully detached the thread,
  66. * EINVAL thread is not a joinable thread,
  67. * ENOSPC a required resource has been exhausted,
  68. * ESRCH no thread could be found for 'thread',
  69. *
  70. * ------------------------------------------------------
  71. */
  72. {
  73. int result;
  74. BOOL destroyIt = PTW32_FALSE;
  75. ptw32_thread_t * tp = (ptw32_thread_t *) thread.p;
  76. ptw32_mcs_local_node_t node;
  77. ptw32_mcs_lock_acquire(&ptw32_thread_reuse_lock, &node);
  78. if (NULL == tp
  79. || thread.x != tp->ptHandle.x)
  80. {
  81. result = ESRCH;
  82. }
  83. else if (PTHREAD_CREATE_DETACHED == tp->detachState)
  84. {
  85. result = EINVAL;
  86. }
  87. else
  88. {
  89. ptw32_mcs_local_node_t stateLock;
  90. /*
  91. * Joinable ptw32_thread_t structs are not scavenged until
  92. * a join or detach is done. The thread may have exited already,
  93. * but all of the state and locks etc are still there.
  94. */
  95. result = 0;
  96. ptw32_mcs_lock_acquire (&tp->stateLock, &stateLock);
  97. if (tp->state != PThreadStateLast)
  98. {
  99. tp->detachState = PTHREAD_CREATE_DETACHED;
  100. }
  101. else if (tp->detachState != PTHREAD_CREATE_DETACHED)
  102. {
  103. /*
  104. * Thread is joinable and has exited or is exiting.
  105. */
  106. destroyIt = PTW32_TRUE;
  107. }
  108. ptw32_mcs_lock_release (&stateLock);
  109. }
  110. ptw32_mcs_lock_release(&node);
  111. if (result == 0)
  112. {
  113. /* Thread is joinable */
  114. if (destroyIt)
  115. {
  116. /* The thread has exited or is exiting but has not been joined or
  117. * detached. Need to wait in case it's still exiting.
  118. */
  119. (void) WaitForSingleObject(tp->threadH, INFINITE);
  120. ptw32_threadDestroy (thread);
  121. }
  122. }
  123. return (result);
  124. } /* pthread_detach */