sem_post.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_post.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 <stdio.h>
  50. #include <stdlib.h>
  51. #include "pthread.h"
  52. #include "semaphore.h"
  53. #include "implement.h"
  54. int
  55. sem_post (sem_t * sem)
  56. /*
  57. * ------------------------------------------------------
  58. * DOCPUBLIC
  59. * This function posts a wakeup to a semaphore.
  60. *
  61. * PARAMETERS
  62. * sem
  63. * pointer to an instance of sem_t
  64. *
  65. * DESCRIPTION
  66. * This function posts a wakeup to a semaphore. If there
  67. * are waiting threads (or processes), one is awakened;
  68. * otherwise, the semaphore value is incremented by one.
  69. *
  70. * RESULTS
  71. * 0 successfully posted semaphore,
  72. * -1 failed, error in errno
  73. * ERRNO
  74. * EINVAL 'sem' is not a valid semaphore,
  75. * ENOSYS semaphores are not supported,
  76. * ERANGE semaphore count is too big
  77. *
  78. * ------------------------------------------------------
  79. */
  80. {
  81. int result = 0;
  82. sem_t s = *sem;
  83. if (s == NULL)
  84. {
  85. result = EINVAL;
  86. }
  87. else 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. result = EINVAL;
  95. return -1;
  96. }
  97. if (s->value < SEM_VALUE_MAX)
  98. {
  99. pte_osResult osResult = pte_osSemaphorePost(s->sem, 1);
  100. if (++s->value <= 0
  101. && (osResult != PTE_OS_OK))
  102. {
  103. s->value--;
  104. result = EINVAL;
  105. }
  106. }
  107. else
  108. {
  109. result = ERANGE;
  110. }
  111. (void) pthread_mutex_unlock (&s->lock);
  112. }
  113. if (result != 0)
  114. {
  115. errno = result;
  116. return -1;
  117. }
  118. return 0;
  119. } /* sem_post */