ptw32_new.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * ptw32_new.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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. pthread_t
  39. ptw32_new (void)
  40. {
  41. pthread_t t;
  42. pthread_t nil = {NULL, 0};
  43. ptw32_thread_t * tp;
  44. /*
  45. * If there's a reusable pthread_t then use it.
  46. */
  47. t = ptw32_threadReusePop ();
  48. if (NULL != t.p)
  49. {
  50. tp = (ptw32_thread_t *) t.p;
  51. }
  52. else
  53. {
  54. /* No reuse threads available */
  55. tp = (ptw32_thread_t *) calloc (1, sizeof(ptw32_thread_t));
  56. if (tp == NULL)
  57. {
  58. return nil;
  59. }
  60. /* ptHandle.p needs to point to it's parent ptw32_thread_t. */
  61. t.p = tp->ptHandle.p = tp;
  62. t.x = tp->ptHandle.x = 0;
  63. }
  64. /* Set default state. */
  65. tp->seqNumber = ++ptw32_threadSeqNumber;
  66. tp->sched_priority = THREAD_PRIORITY_NORMAL;
  67. tp->detachState = PTHREAD_CREATE_JOINABLE;
  68. tp->cancelState = PTHREAD_CANCEL_ENABLE;
  69. tp->cancelType = PTHREAD_CANCEL_DEFERRED;
  70. tp->stateLock = 0;
  71. tp->threadLock = 0;
  72. tp->robustMxListLock = 0;
  73. tp->robustMxList = NULL;
  74. tp->cancelEvent = CreateEvent (0, (int) PTW32_TRUE, /* manualReset */
  75. (int) PTW32_FALSE, /* setSignaled */
  76. NULL);
  77. if (tp->cancelEvent == NULL)
  78. {
  79. ptw32_threadReusePush (tp->ptHandle);
  80. return nil;
  81. }
  82. return t;
  83. }