pthread_detach.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  76. pte_osMutexLock (pte_thread_reuse_lock);
  77. if (NULL == tp)
  78. {
  79. result = ESRCH;
  80. }
  81. else if (PTHREAD_CREATE_DETACHED == tp->detachState)
  82. {
  83. result = EINVAL;
  84. }
  85. else
  86. {
  87. /*
  88. * Joinable pte_thread_t structs are not scavenged until
  89. * a join or detach is done. The thread may have exited already,
  90. * but all of the state and locks etc are still there.
  91. */
  92. result = 0;
  93. if (pthread_mutex_lock (&tp->cancelLock) == 0)
  94. {
  95. if (tp->state != PThreadStateLast)
  96. {
  97. tp->detachState = PTHREAD_CREATE_DETACHED;
  98. }
  99. else if (tp->detachState != PTHREAD_CREATE_DETACHED)
  100. {
  101. /*
  102. * Thread is joinable and has exited or is exiting.
  103. */
  104. destroyIt = PTE_TRUE;
  105. }
  106. (void) pthread_mutex_unlock (&tp->cancelLock);
  107. }
  108. else
  109. {
  110. /* cancelLock shouldn't fail, but if it does ... */
  111. result = ESRCH;
  112. }
  113. }
  114. pte_osMutexUnlock(pte_thread_reuse_lock);
  115. if (result == 0)
  116. {
  117. /* Thread is joinable */
  118. if (destroyIt)
  119. {
  120. /* The thread has exited or is exiting but has not been joined or
  121. * detached. Need to wait in case it's still exiting.
  122. */
  123. pte_osThreadWaitForEnd(tp->threadId);
  124. pte_threadDestroy (thread);
  125. }
  126. }
  127. return (result);
  128. } /* pthread_detach */