sem_post_multiple.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_post_multiple.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: [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 <stdio.h>
  50. #include <stdlib.h>
  51. #include "pthread.h"
  52. #include "semaphore.h"
  53. #include "implement.h"
  54. int
  55. sem_post_multiple (sem_t * sem, int count)
  56. /*
  57. * ------------------------------------------------------
  58. * DOCPUBLIC
  59. * This function posts multiple wakeups to a semaphore.
  60. *
  61. * PARAMETERS
  62. * sem
  63. * pointer to an instance of sem_t
  64. *
  65. * count
  66. * counter, must be greater than zero.
  67. *
  68. * DESCRIPTION
  69. * This function posts multiple wakeups to a semaphore. If there
  70. * are waiting threads (or processes), n <= count are awakened;
  71. * the semaphore value is incremented by count - n.
  72. *
  73. * RESULTS
  74. * 0 successfully posted semaphore,
  75. * -1 failed, error in errno
  76. * ERRNO
  77. * EINVAL 'sem' is not a valid semaphore
  78. * or count is less than or equal to zero.
  79. * ERANGE semaphore count is too big
  80. *
  81. * ------------------------------------------------------
  82. */
  83. {
  84. int result = 0;
  85. long waiters;
  86. sem_t s = *sem;
  87. if (s == NULL || count <= 0)
  88. {
  89. result = EINVAL;
  90. }
  91. else if ((result = pthread_mutex_lock (&s->lock)) == 0)
  92. {
  93. /* See sem_destroy.c
  94. */
  95. if (*sem == NULL)
  96. {
  97. (void) pthread_mutex_unlock (&s->lock);
  98. result = EINVAL;
  99. return -1;
  100. }
  101. if (s->value <= (SEM_VALUE_MAX - count))
  102. {
  103. waiters = -s->value;
  104. s->value += count;
  105. if (waiters > 0)
  106. {
  107. pte_osSemaphorePost(s->sem, (waiters<=count)?waiters:count);
  108. result = 0;
  109. }
  110. /*
  111. else
  112. {
  113. s->value -= count;
  114. result = EINVAL;
  115. }
  116. */
  117. }
  118. else
  119. {
  120. result = ERANGE;
  121. }
  122. (void) pthread_mutex_unlock (&s->lock);
  123. }
  124. if (result != 0)
  125. {
  126. errno = result;
  127. return -1;
  128. }
  129. return 0;
  130. } /* sem_post_multiple */