pthread_detach.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * pthread_detach.c
  3. *
  4. * Description:
  5. * This translation unit implements functions related to thread
  6. * synchronisation.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. *
  16. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The original list of contributors to the Pthreads-win32 project
  23. * is contained in the file CONTRIBUTORS.ptw32 included with the
  24. * source code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include <pte_osal.h>
  44. #include "pthread.h"
  45. #include "implement.h"
  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. unsigned char destroyIt = PTE_FALSE;
  75. pte_thread_t * tp = (pte_thread_t *) thread.p;
  76. pte_osMutexLock (pte_thread_reuse_lock);
  77. if (NULL == tp
  78. || thread.x != tp->ptHandle.x)
  79. {
  80. result = ESRCH;
  81. }
  82. else if (PTHREAD_CREATE_DETACHED == tp->detachState)
  83. {
  84. result = EINVAL;
  85. }
  86. else
  87. {
  88. /*
  89. * Joinable pte_thread_t structs are not scavenged until
  90. * a join or detach is done. The thread may have exited already,
  91. * but all of the state and locks etc are still there.
  92. */
  93. result = 0;
  94. if (pthread_mutex_lock (&tp->cancelLock) == 0)
  95. {
  96. if (tp->state != PThreadStateLast)
  97. {
  98. tp->detachState = PTHREAD_CREATE_DETACHED;
  99. }
  100. else if (tp->detachState != PTHREAD_CREATE_DETACHED)
  101. {
  102. /*
  103. * Thread is joinable and has exited or is exiting.
  104. */
  105. destroyIt = PTE_TRUE;
  106. }
  107. (void) pthread_mutex_unlock (&tp->cancelLock);
  108. }
  109. else
  110. {
  111. /* cancelLock shouldn't fail, but if it does ... */
  112. result = ESRCH;
  113. }
  114. }
  115. pte_osMutexUnlock(pte_thread_reuse_lock);
  116. if (result == 0)
  117. {
  118. /* Thread is joinable */
  119. if (destroyIt)
  120. {
  121. /* The thread has exited or is exiting but has not been joined or
  122. * detached. Need to wait in case it's still exiting.
  123. */
  124. pte_osThreadWaitForEnd(tp->threadId);
  125. pte_threadDestroy (thread);
  126. }
  127. }
  128. return (result);
  129. } /* pthread_detach */