pthread_self.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * pthread_self.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous 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. pthread_t
  39. pthread_self (void)
  40. /*
  41. * ------------------------------------------------------
  42. * DOCPUBLIC
  43. * This function returns a reference to the current running
  44. * thread.
  45. *
  46. * PARAMETERS
  47. * N/A
  48. *
  49. *
  50. * DESCRIPTION
  51. * This function returns a reference to the current running
  52. * thread.
  53. *
  54. * RESULTS
  55. * pthread_t reference to the current thread
  56. *
  57. * ------------------------------------------------------
  58. */
  59. {
  60. pthread_t self;
  61. pthread_t nil = {NULL, 0};
  62. ptw32_thread_t * sp;
  63. #if defined(_UWIN)
  64. if (!ptw32_selfThreadKey)
  65. return nil;
  66. #endif
  67. sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  68. if (sp != NULL)
  69. {
  70. self = sp->ptHandle;
  71. }
  72. else
  73. {
  74. /*
  75. * Need to create an implicit 'self' for the currently
  76. * executing thread.
  77. */
  78. self = ptw32_new ();
  79. sp = (ptw32_thread_t *) self.p;
  80. if (sp != NULL)
  81. {
  82. /*
  83. * This is a non-POSIX thread which has chosen to call
  84. * a POSIX threads function for some reason. We assume that
  85. * it isn't joinable, but we do assume that it's
  86. * (deferred) cancelable.
  87. */
  88. sp->implicit = 1;
  89. sp->detachState = PTHREAD_CREATE_DETACHED;
  90. sp->thread = GetCurrentThreadId ();
  91. #if defined(NEED_DUPLICATEHANDLE)
  92. /*
  93. * DuplicateHandle does not exist on WinCE.
  94. *
  95. * NOTE:
  96. * GetCurrentThread only returns a pseudo-handle
  97. * which is only valid in the current thread context.
  98. * Therefore, you should not pass the handle to
  99. * other threads for whatever purpose.
  100. */
  101. sp->threadH = GetCurrentThread ();
  102. #else
  103. if (!DuplicateHandle (GetCurrentProcess (),
  104. GetCurrentThread (),
  105. GetCurrentProcess (),
  106. &sp->threadH,
  107. 0, FALSE, DUPLICATE_SAME_ACCESS))
  108. {
  109. /*
  110. * Should not do this, but we have no alternative if
  111. * we can't get a Win32 thread handle.
  112. * Thread structs are never freed.
  113. */
  114. ptw32_threadReusePush (self);
  115. /*
  116. * As this is a win32 thread calling us and we have failed,
  117. * return a value that makes sense to win32.
  118. */
  119. return nil;
  120. }
  121. #endif
  122. /*
  123. * No need to explicitly serialise access to sched_priority
  124. * because the new handle is not yet public.
  125. */
  126. sp->sched_priority = GetThreadPriority (sp->threadH);
  127. pthread_setspecific (ptw32_selfThreadKey, (void *) sp);
  128. }
  129. }
  130. return (self);
  131. } /* pthread_self */