pthread_mutexattr_settype.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * pthread_mutexattr_settype.c
  3. *
  4. * Description:
  5. * This translation unit implements mutual exclusion (mutex) primitives.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  10. * Copyright(C) 2008 Jason Schmidlapp
  11. *
  12. * Contact Email: [email protected]
  13. *
  14. *
  15. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  16. * Copyright(C) 1998 John E. Bossom
  17. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  18. *
  19. * Contact Email: [email protected]
  20. *
  21. * The original list of contributors to the Pthreads-win32 project
  22. * is contained in the file CONTRIBUTORS.ptw32 included with the
  23. * source code distribution. The list can also be seen at the
  24. * following World Wide Web location:
  25. * http://sources.redhat.com/pthreads-win32/contributors.html
  26. *
  27. * This library is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU Lesser General Public
  29. * License as published by the Free Software Foundation; either
  30. * version 2 of the License, or (at your option) any later version.
  31. *
  32. * This library is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. * Lesser General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU Lesser General Public
  38. * License along with this library in the file COPYING.LIB;
  39. * if not, write to the Free Software Foundation, Inc.,
  40. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  41. */
  42. #include "pthread.h"
  43. #include "implement.h"
  44. int
  45. pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind)
  46. /*
  47. * ------------------------------------------------------
  48. *
  49. * DOCPUBLIC
  50. * The pthread_mutexattr_settype() and
  51. * pthread_mutexattr_gettype() functions respectively set and
  52. * get the mutex type attribute. This attribute is set in the
  53. * type parameter to these functions.
  54. *
  55. * PARAMETERS
  56. * attr
  57. * pointer to an instance of pthread_mutexattr_t
  58. *
  59. * type
  60. * must be one of:
  61. *
  62. * PTHREAD_MUTEX_DEFAULT
  63. *
  64. * PTHREAD_MUTEX_NORMAL
  65. *
  66. * PTHREAD_MUTEX_ERRORCHECK
  67. *
  68. * PTHREAD_MUTEX_RECURSIVE
  69. *
  70. * DESCRIPTION
  71. * The pthread_mutexattr_settype() and
  72. * pthread_mutexattr_gettype() functions respectively set and
  73. * get the mutex type attribute. This attribute is set in the
  74. * type parameter to these functions. The default value of the
  75. * type attribute is PTHREAD_MUTEX_DEFAULT.
  76. *
  77. * The type of mutex is contained in the type attribute of the
  78. * mutex attributes. Valid mutex types include:
  79. *
  80. * PTHREAD_MUTEX_NORMAL
  81. * This type of mutex does not detect deadlock. A
  82. * thread attempting to relock this mutex without
  83. * first unlocking it will deadlock. Attempting to
  84. * unlock a mutex locked by a different thread
  85. * results in undefined behavior. Attempting to
  86. * unlock an unlocked mutex results in undefined
  87. * behavior.
  88. *
  89. * PTHREAD_MUTEX_ERRORCHECK
  90. * This type of mutex provides error checking. A
  91. * thread attempting to relock this mutex without
  92. * first unlocking it will return with an error. A
  93. * thread attempting to unlock a mutex which another
  94. * thread has locked will return with an error. A
  95. * thread attempting to unlock an unlocked mutex will
  96. * return with an error.
  97. *
  98. * PTHREAD_MUTEX_DEFAULT
  99. * Same as PTHREAD_MUTEX_NORMAL.
  100. *
  101. * PTHREAD_MUTEX_RECURSIVE
  102. * A thread attempting to relock this mutex without
  103. * first unlocking it will succeed in locking the
  104. * mutex. The relocking deadlock which can occur with
  105. * mutexes of type PTHREAD_MUTEX_NORMAL cannot occur
  106. * with this type of mutex. Multiple locks of this
  107. * mutex require the same number of unlocks to
  108. * release the mutex before another thread can
  109. * acquire the mutex. A thread attempting to unlock a
  110. * mutex which another thread has locked will return
  111. * with an error. A thread attempting to unlock an
  112. * unlocked mutex will return with an error. This
  113. * type of mutex is only supported for mutexes whose
  114. * process shared attribute is
  115. * PTHREAD_PROCESS_PRIVATE.
  116. *
  117. * RESULTS
  118. * 0 successfully set attribute,
  119. * EINVAL 'attr' or 'type' is invalid,
  120. *
  121. * ------------------------------------------------------
  122. */
  123. {
  124. int result = 0;
  125. if ((attr != NULL && *attr != NULL))
  126. {
  127. switch (kind)
  128. {
  129. case PTHREAD_MUTEX_FAST_NP:
  130. case PTHREAD_MUTEX_RECURSIVE_NP:
  131. case PTHREAD_MUTEX_ERRORCHECK_NP:
  132. (*attr)->kind = kind;
  133. break;
  134. default:
  135. result = EINVAL;
  136. break;
  137. }
  138. }
  139. else
  140. {
  141. result = EINVAL;
  142. }
  143. return (result);
  144. } /* pthread_mutexattr_settype */