ewl_linux_lock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description : Locking semaphore for hardware sharing
  17. --
  18. ------------------------------------------------------------------------------*/
  19. #include <sys/types.h>
  20. #include <sys/ipc.h>
  21. #include <sys/sem.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <pthread.h>
  25. #include <semaphore.h>
  26. #include <stdio.h>
  27. #include "ewl_linux_lock.h"
  28. #define SYTEM_V_SEMAPHORE
  29. #ifdef SYTEM_V_SEMAPHORE
  30. #ifdef _SEM_SEMUN_UNDEFINED
  31. union semun
  32. {
  33. int val;
  34. struct semid_ds *buf;
  35. unsigned short int *array;
  36. struct seminfo *__buf;
  37. };
  38. #endif
  39. /* Obtain a binary semaphore's ID, allocating if necessary. */
  40. int binary_semaphore_allocation(key_t key, int nsem, int sem_flags)
  41. {
  42. return semget(key, nsem, sem_flags);
  43. }
  44. /* Deallocate a binary semaphore. All users must have finished their
  45. use. Returns -1 on failure. */
  46. int binary_semaphore_deallocate(int semid)
  47. {
  48. return semctl(semid, 0, IPC_RMID);
  49. }
  50. /* Wait on a binary semaphore. Block until the semaphore value is
  51. positive, then decrement it by one. */
  52. int binary_semaphore_wait(int semid, int sem_num)
  53. {
  54. int ret;
  55. struct sembuf operations[1];
  56. /* Use 'sem_num' semaphore from the set. */
  57. operations[0].sem_num = sem_num;
  58. /* Decrement by 1. */
  59. operations[0].sem_op = -1;
  60. /* Permit undo'ing. */
  61. operations[0].sem_flg = SEM_UNDO;
  62. /* signal safe */
  63. do
  64. {
  65. ret = semop(semid, operations, 1);
  66. }
  67. while(ret == -1 && errno == EINTR);
  68. return ret;
  69. }
  70. /* Post to a binary semaphore: increment its value by one */
  71. int binary_semaphore_post(int semid, int sem_num)
  72. {
  73. int ret;
  74. struct sembuf operations[1];
  75. /* Use 'sem_num' semaphore from the set. */
  76. operations[0].sem_num = sem_num;
  77. /* Increment by 1. */
  78. operations[0].sem_op = 1;
  79. /* Permit undo'ing. */
  80. operations[0].sem_flg = SEM_UNDO;
  81. /* signal safe */
  82. do
  83. {
  84. ret = semop(semid, operations, 1);
  85. }
  86. while(ret == -1 && errno == EINTR);
  87. return ret;
  88. }
  89. /* Initialize a binary semaphore with a value of one. */
  90. int binary_semaphore_initialize(int semid, int semnum)
  91. {
  92. union semun argument;
  93. argument.val = 1;
  94. return semctl(semid, semnum, SETVAL, argument);
  95. }
  96. /* Get the value of a binary semaphore */
  97. int binary_semaphore_getval(int semid, int semnum)
  98. {
  99. return semctl(semid, semnum, GETVAL);
  100. }
  101. #else
  102. static sem_t sem0, sem1;
  103. static int sem0_init = 0, sem1_init = 0;
  104. /* Obtain a binary semaphore's ID, allocating if necessary. */
  105. int binary_semaphore_allocation(key_t key, int nsem, int sem_flags)
  106. {
  107. if (sem0_init && sem1_init) return 0;
  108. else if (sem_flags & IPC_CREAT) return 0;
  109. errno = ENOENT;
  110. return -1;
  111. }
  112. /* Deallocate a binary semaphore. All users must have finished their
  113. use. Returns -1 on failure. */
  114. int binary_semaphore_deallocate(int semid)
  115. {
  116. sem_destroy(&sem0);
  117. sem_destroy(&sem1);
  118. sem0_init = 0;
  119. sem1_init = 0;
  120. return 0;
  121. }
  122. /* Wait on a binary semaphore. Block until the semaphore value is
  123. positive, then decrement it by one. */
  124. int binary_semaphore_wait(int semid, int sem_num)
  125. {
  126. if (sem_num == 0)
  127. return sem_wait(&sem0);
  128. else if (sem_num == 1)
  129. return sem_wait(&sem1);
  130. return -1;
  131. }
  132. /* Post to a binary semaphore: increment its value by one */
  133. int binary_semaphore_post(int semid, int sem_num)
  134. {
  135. if (sem_num == 0)
  136. return sem_post(&sem0);
  137. else if (sem_num == 1)
  138. return sem_post(&sem1);
  139. return -1;
  140. }
  141. /* Initialize a binary semaphore with a value of one. */
  142. int binary_semaphore_initialize(int semid, int semnum)
  143. {
  144. int ret = -1;
  145. if (semnum == 0) {
  146. ret = sem_init(&sem0, 0, 1);
  147. if (ret == 0)
  148. sem0_init = 1;
  149. }
  150. else if (semnum == 1) {
  151. ret = sem_init(&sem1, 0, 1);
  152. if (ret == 0)
  153. sem1_init = 1;
  154. }
  155. return ret;
  156. }
  157. /* Get the value of a binary semaphore */
  158. int binary_semaphore_getval(int semid, int semnum)
  159. {
  160. int value = -1;
  161. if (semnum == 0)
  162. sem_getvalue(&sem0, &value);
  163. else if (semnum == 1)
  164. sem_getvalue(&sem1, &value);
  165. return value;
  166. }
  167. #endif