sem_getvalue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-embedded (PTE) - POSIX Threads Library for embedded systems
  17. * Copyright(C) 2008 Jason Schmidlapp
  18. *
  19. * Contact Email: [email protected]
  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: [email protected]
  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_getvalue (sem_t * sem, int *sval)
  54. /*
  55. * ------------------------------------------------------
  56. * DOCPUBLIC
  57. * This function stores the current count value of the
  58. * semaphore.
  59. * RESULTS
  60. *
  61. * Return value
  62. *
  63. * 0 sval has been set.
  64. * -1 failed, error in errno
  65. *
  66. * in global errno
  67. *
  68. * EINVAL 'sem' is not a valid semaphore,
  69. * ENOSYS this function is not supported,
  70. *
  71. *
  72. * PARAMETERS
  73. *
  74. * sem pointer to an instance of sem_t
  75. *
  76. * sval pointer to int.
  77. *
  78. * DESCRIPTION
  79. * This function stores the current count value of the semaphore
  80. * pointed to by sem in the int pointed to by sval.
  81. */
  82. {
  83. if (sem == NULL || *sem == NULL || sval == NULL)
  84. {
  85. errno = EINVAL;
  86. return -1;
  87. }
  88. else
  89. {
  90. long value;
  91. register sem_t s = *sem;
  92. int result = 0;
  93. if ((result = pthread_mutex_lock(&s->lock)) == 0)
  94. {
  95. /* See sem_destroy.c
  96. */
  97. if (*sem == NULL)
  98. {
  99. (void) pthread_mutex_unlock (&s->lock);
  100. errno = EINVAL;
  101. return -1;
  102. }
  103. value = s->value;
  104. (void) pthread_mutex_unlock(&s->lock);
  105. *sval = value;
  106. }
  107. return result;
  108. }
  109. } /* sem_getvalue */