pthread_mutexattr_setrobust.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * pthread_mutexattr_setrobust.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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. int
  39. pthread_mutexattr_setrobust (pthread_mutexattr_t * attr, int robust)
  40. /*
  41. * ------------------------------------------------------
  42. *
  43. * DOCPUBLIC
  44. * The pthread_mutexattr_setrobust() and
  45. * pthread_mutexattr_getrobust() functions respectively set and
  46. * get the mutex robust attribute. This attribute is set in the
  47. * robust parameter to these functions.
  48. *
  49. * PARAMETERS
  50. * attr
  51. * pointer to an instance of pthread_mutexattr_t
  52. *
  53. * robust
  54. * must be one of:
  55. *
  56. * PTHREAD_MUTEX_STALLED
  57. *
  58. * PTHREAD_MUTEX_ROBUST
  59. *
  60. * DESCRIPTION
  61. * The pthread_mutexattr_setrobust() and
  62. * pthread_mutexattr_getrobust() functions respectively set and
  63. * get the mutex robust attribute. This attribute is set in the
  64. * robust parameter to these functions. The default value of the
  65. * robust attribute is PTHREAD_MUTEX_STALLED.
  66. *
  67. * The robustness of mutex is contained in the robustness attribute
  68. * of the mutex attributes. Valid mutex robustness values are:
  69. *
  70. * PTHREAD_MUTEX_STALLED
  71. * No special actions are taken if the owner of the mutex is
  72. * terminated while holding the mutex lock. This can lead to
  73. * deadlocks if no other thread can unlock the mutex.
  74. * This is the default value.
  75. *
  76. * PTHREAD_MUTEX_ROBUST
  77. * If the process containing the owning thread of a robust mutex
  78. * terminates while holding the mutex lock, the next thread that
  79. * acquires the mutex shall be notified about the termination by
  80. * the return value [EOWNERDEAD] from the locking function. If the
  81. * owning thread of a robust mutex terminates while holding the mutex
  82. * lock, the next thread that acquires the mutex may be notified
  83. * about the termination by the return value [EOWNERDEAD]. The
  84. * notified thread can then attempt to mark the state protected by
  85. * the mutex as consistent again by a call to
  86. * pthread_mutex_consistent(). After a subsequent successful call to
  87. * pthread_mutex_unlock(), the mutex lock shall be released and can
  88. * be used normally by other threads. If the mutex is unlocked without
  89. * a call to pthread_mutex_consistent(), it shall be in a permanently
  90. * unusable state and all attempts to lock the mutex shall fail with
  91. * the error [ENOTRECOVERABLE]. The only permissible operation on such
  92. * a mutex is pthread_mutex_destroy().
  93. *
  94. * RESULTS
  95. * 0 successfully set attribute,
  96. * EINVAL 'attr' or 'robust' is invalid,
  97. *
  98. * ------------------------------------------------------
  99. */
  100. {
  101. int result = EINVAL;
  102. if ((attr != NULL && *attr != NULL))
  103. {
  104. switch (robust)
  105. {
  106. case PTHREAD_MUTEX_STALLED:
  107. case PTHREAD_MUTEX_ROBUST:
  108. (*attr)->robustness = robust;
  109. result = 0;
  110. break;
  111. }
  112. }
  113. return (result);
  114. } /* pthread_mutexattr_setrobust */