sem_init.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * libs/libc/sem/sem_init.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <limits.h>
  41. #include <errno.h>
  42. #include <nuttx/semaphore.h>
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: nxsem_init
  48. *
  49. * Description:
  50. * This function initializes the UNNAMED semaphore sem. Following a
  51. * successful call to nxsem_init(), the semaphore may be used in subsequent
  52. * calls to nxsem_wait(), nxsem_post(), and nxsem_trywait(). The semaphore
  53. * remains usable until it is destroyed.
  54. *
  55. * Only sem itself may be used for performing synchronization. The result
  56. * of referring to copies of sem in calls to nxsem_wait(), nxsem_trywait(),
  57. * nxsem_post(), and nxsem_destroy() is undefined.
  58. *
  59. * Input Parameters:
  60. * sem - Semaphore to be initialized
  61. * pshared - Process sharing (not used)
  62. * value - Semaphore initialization value
  63. *
  64. * Returned Value:
  65. * This is an internal OS interface and should not be used by applications.
  66. * It follows the NuttX internal error return policy: Zero (OK) is
  67. * returned on success. A negated errno value is returned on failure.
  68. *
  69. ****************************************************************************/
  70. int nxsem_init(FAR sem_t *sem, int pshared, unsigned int value)
  71. {
  72. /* Verify that a semaphore was provided and the count is within the valid
  73. * range.
  74. */
  75. if (sem != NULL && value <= SEM_VALUE_MAX)
  76. {
  77. /* Initialize the semaphore count */
  78. sem->semcount = (int16_t)value;
  79. /* Initialize to support priority inheritance */
  80. #ifdef CONFIG_PRIORITY_INHERITANCE
  81. sem->flags = 0;
  82. # if CONFIG_SEM_PREALLOCHOLDERS > 0
  83. sem->hhead = NULL;
  84. # else
  85. sem->holder[0].htcb = NULL;
  86. sem->holder[0].counts = 0;
  87. sem->holder[1].htcb = NULL;
  88. sem->holder[1].counts = 0;
  89. # endif
  90. #endif
  91. return OK;
  92. }
  93. return -EINVAL;
  94. }
  95. /****************************************************************************
  96. * Name: sem_init
  97. *
  98. * Description:
  99. * This function initializes the UNNAMED semaphore sem. Following a
  100. * successful call to sem_init(), the semaphore may be used in subsequent
  101. * calls to sem_wait(), sem_post(), and sem_trywait(). The semaphore
  102. * remains usable until it is destroyed.
  103. *
  104. * Only sem itself may be used for performing synchronization. The result
  105. * of referring to copies of sem in calls to sem_wait(), sem_trywait(),
  106. * sem_post(), and sem_destroy() is undefined.
  107. *
  108. * Input Parameters:
  109. * sem - Semaphore to be initialized
  110. * pshared - Process sharing (not used)
  111. * value - Semaphore initialization value
  112. *
  113. * Returned Value:
  114. * This returns zero (OK) if successful. Otherwise, -1 (ERROR) is
  115. * returned and the errno value is set appropriately.
  116. *
  117. ****************************************************************************/
  118. int sem_init(FAR sem_t *sem, int pshared, unsigned int value)
  119. {
  120. int ret;
  121. ret = nxsem_init(sem, pshared, value);
  122. if (ret < 0)
  123. {
  124. set_errno(-ret);
  125. ret = ERROR;
  126. }
  127. return ret;
  128. }