sem_trywait.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_trywait.c
  5. *
  6. * Purpose:
  7. * Semaphores aren't actually part of the PThreads standard.
  8. * They are defined by the POSIX Standard:
  9. *
  10. * POSIX 1003.1b-1993 (POSIX.1b)
  11. *
  12. * -------------------------------------------------------------
  13. *
  14. * --------------------------------------------------------------------------
  15. *
  16. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  17. * Copyright(C) 2008 Jason Schmidlapp
  18. *
  19. * Contact Email: jschmidlapp@users.sourceforge.net
  20. *
  21. *
  22. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  23. * Copyright(C) 1998 John E. Bossom
  24. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  25. *
  26. * Contact Email: rpj@callisto.canberra.edu.au
  27. *
  28. * The original list of contributors to the Pthreads-win32 project
  29. * is contained in the file CONTRIBUTORS.ptw32 included with the
  30. * source code distribution. The list can also be seen at the
  31. * following World Wide Web location:
  32. * http://sources.redhat.com/pthreads-win32/contributors.html
  33. *
  34. * This library is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU Lesser General Public
  36. * License as published by the Free Software Foundation; either
  37. * version 2 of the License, or (at your option) any later version.
  38. *
  39. * This library is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  42. * Lesser General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU Lesser General Public
  45. * License along with this library in the file COPYING.LIB;
  46. * if not, write to the Free Software Foundation, Inc.,
  47. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  48. */
  49. #include "pthread.h"
  50. #include "semaphore.h"
  51. #include "implement.h"
  52. int
  53. sem_trywait (sem_t * sem)
  54. /*
  55. * ------------------------------------------------------
  56. * DOCPUBLIC
  57. * This function tries to wait on a semaphore.
  58. *
  59. * PARAMETERS
  60. * sem
  61. * pointer to an instance of sem_t
  62. *
  63. * DESCRIPTION
  64. * This function tries to wait on a semaphore. If the
  65. * semaphore value is greater than zero, it decreases
  66. * its value by one. If the semaphore value is zero, then
  67. * this function returns immediately with the error EAGAIN
  68. *
  69. * RESULTS
  70. * 0 successfully decreased semaphore,
  71. * -1 failed, error in errno
  72. * ERRNO
  73. * EAGAIN the semaphore was already locked,
  74. * EINVAL 'sem' is not a valid semaphore,
  75. * ENOTSUP sem_trywait is not supported,
  76. * EINTR the function was interrupted by a signal,
  77. * EDEADLK a deadlock condition was detected.
  78. *
  79. * ------------------------------------------------------
  80. */
  81. {
  82. int result = 0;
  83. sem_t s = *sem;
  84. if (s == NULL)
  85. {
  86. result = EINVAL;
  87. }
  88. else if ((result = pthread_mutex_lock (&s->lock)) == 0)
  89. {
  90. /* See sem_destroy.c
  91. */
  92. if (*sem == NULL)
  93. {
  94. (void) pthread_mutex_unlock (&s->lock);
  95. errno = EINVAL;
  96. return -1;
  97. }
  98. if (s->value > 0)
  99. {
  100. s->value--;
  101. }
  102. else
  103. {
  104. result = EAGAIN;
  105. }
  106. (void) pthread_mutex_unlock (&s->lock);
  107. }
  108. if (result != 0)
  109. {
  110. errno = result;
  111. return -1;
  112. }
  113. return 0;
  114. } /* sem_trywait */