pthread_mutex_init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * pthread_mutex_init.c
  3. *
  4. * Description:
  5. * This translation unit implements mutual exclusion (mutex) primitives.
  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. int
  39. pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr)
  40. {
  41. int result = 0;
  42. pthread_mutex_t mx;
  43. if (mutex == NULL)
  44. {
  45. return EINVAL;
  46. }
  47. if (attr != NULL && *attr != NULL)
  48. {
  49. if ((*attr)->pshared == PTHREAD_PROCESS_SHARED)
  50. {
  51. /*
  52. * Creating mutex that can be shared between
  53. * processes.
  54. */
  55. #if _POSIX_THREAD_PROCESS_SHARED >= 0
  56. /*
  57. * Not implemented yet.
  58. */
  59. #error ERROR [__FILE__, line __LINE__]: Process shared mutexes are not supported yet.
  60. #else
  61. return ENOSYS;
  62. #endif /* _POSIX_THREAD_PROCESS_SHARED */
  63. }
  64. }
  65. mx = (pthread_mutex_t) calloc (1, sizeof (*mx));
  66. if (mx == NULL)
  67. {
  68. result = ENOMEM;
  69. }
  70. else
  71. {
  72. mx->lock_idx = 0;
  73. mx->recursive_count = 0;
  74. mx->robustNode = NULL;
  75. if (attr == NULL || *attr == NULL)
  76. {
  77. mx->kind = PTHREAD_MUTEX_DEFAULT;
  78. }
  79. else
  80. {
  81. mx->kind = (*attr)->kind;
  82. if ((*attr)->robustness == PTHREAD_MUTEX_ROBUST)
  83. {
  84. /*
  85. * Use the negative range to represent robust types.
  86. * Replaces a memory fetch with a register negate and incr
  87. * in pthread_mutex_lock etc.
  88. *
  89. * Map 0,1,..,n to -1,-2,..,(-n)-1
  90. */
  91. mx->kind = -mx->kind - 1;
  92. mx->robustNode = (ptw32_robust_node_t*) malloc(sizeof(ptw32_robust_node_t));
  93. mx->robustNode->stateInconsistent = PTW32_ROBUST_CONSISTENT;
  94. mx->robustNode->mx = mx;
  95. mx->robustNode->next = NULL;
  96. mx->robustNode->prev = NULL;
  97. }
  98. }
  99. mx->ownerThread.p = NULL;
  100. mx->event = CreateEvent (NULL, PTW32_FALSE, /* manual reset = No */
  101. PTW32_FALSE, /* initial state = not signaled */
  102. NULL); /* event name */
  103. if (0 == mx->event)
  104. {
  105. result = ENOSPC;
  106. free (mx);
  107. mx = NULL;
  108. }
  109. }
  110. *mutex = mx;
  111. return (result);
  112. }