sem_getvalue.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_getvalue.c
  5. *
  6. * Purpose:
  7. * Semaphores aren't actually part of PThreads.
  8. * They are defined by the POSIX Standard:
  9. *
  10. * POSIX 1003.1-2001
  11. *
  12. * -------------------------------------------------------------
  13. *
  14. * --------------------------------------------------------------------------
  15. *
  16. * Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The current list of contributors is contained
  23. * in the file CONTRIBUTORS included with the source
  24. * code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include "pthread.h"
  44. #include "semaphore.h"
  45. #include "implement.h"
  46. int
  47. sem_getvalue (sem_t * sem, int *sval)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function stores the current count value of the
  52. * semaphore.
  53. * RESULTS
  54. *
  55. * Return value
  56. *
  57. * 0 sval has been set.
  58. * -1 failed, error in errno
  59. *
  60. * in global errno
  61. *
  62. * EINVAL 'sem' is not a valid semaphore,
  63. * ENOSYS this function is not supported,
  64. *
  65. *
  66. * PARAMETERS
  67. *
  68. * sem pointer to an instance of sem_t
  69. *
  70. * sval pointer to int.
  71. *
  72. * DESCRIPTION
  73. * This function stores the current count value of the semaphore
  74. * pointed to by sem in the int pointed to by sval.
  75. */
  76. {
  77. if (sem == NULL || *sem == NULL || sval == NULL)
  78. {
  79. errno = EINVAL;
  80. return -1;
  81. }
  82. else
  83. {
  84. long value;
  85. register sem_t s = *sem;
  86. int result = 0;
  87. if ((result = pthread_mutex_lock(&s->lock)) == 0)
  88. {
  89. /* See sem_destroy.c
  90. */
  91. if (*sem == NULL)
  92. {
  93. (void) pthread_mutex_unlock (&s->lock);
  94. errno = EINVAL;
  95. return -1;
  96. }
  97. value = s->value;
  98. (void) pthread_mutex_unlock(&s->lock);
  99. *sval = value;
  100. }
  101. return result;
  102. }
  103. } /* sem_getvalue */