sem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <sys/sem.h>
  17. #include <stddef.h>
  18. #include <stdlib.h> /* for NULL */
  19. #include "ipc.h"
  20. #if defined(__UCLIBC_USE_TIME64__)
  21. #include "internal/time64_helpers.h"
  22. #endif
  23. #ifdef L_semctl
  24. /* Return identifier for array of NSEMS semaphores associated with
  25. KEY. */
  26. #include <stdarg.h>
  27. /* arg for semctl system calls. */
  28. union semun {
  29. int val; /* value for SETVAL */
  30. struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
  31. unsigned short *array; /* array for GETALL & SETALL */
  32. struct seminfo *__buf; /* buffer for IPC_INFO */
  33. void *__pad;
  34. };
  35. #ifdef __NR_semctl
  36. #define __NR___semctl __NR_semctl
  37. static __inline__ _syscall4(int, __semctl, int, semid, int, semnum, int, cmd, void *, arg)
  38. #endif
  39. int semctl(int semid, int semnum, int cmd, ...)
  40. {
  41. union semun arg;
  42. va_list ap;
  43. /* Get the argument. */
  44. va_start (ap, cmd);
  45. arg = va_arg (ap, union semun);
  46. va_end (ap);
  47. #ifdef __NR_semctl
  48. int __ret = __semctl(semid, semnum, cmd | __IPC_64, arg.__pad);
  49. #if defined(__UCLIBC_USE_TIME64__)
  50. if (arg.__pad != NULL) {
  51. arg.buf->sem_otime = (__time_t)arg.buf->__sem_otime_internal_1 | (__time_t)(arg.buf->__sem_otime_internal_2) << 32;
  52. arg.buf->sem_ctime = (__time_t)arg.buf->__sem_ctime_internal_1 | (__time_t)(arg.buf->__sem_ctime_internal_2) << 32;
  53. }
  54. #endif
  55. return __ret;
  56. #else
  57. return __syscall_ipc(IPCOP_semctl, semid, semnum, cmd|__IPC_64, &arg, NULL);
  58. #endif
  59. }
  60. #endif
  61. #ifdef L_semget
  62. #ifdef __NR_semget
  63. _syscall3(int, semget, key_t, key, int, nsems, int, semflg)
  64. #else
  65. /* Return identifier for array of NSEMS semaphores associated
  66. * with KEY. */
  67. int semget (key_t key, int nsems, int semflg)
  68. {
  69. return __syscall_ipc(IPCOP_semget, key, nsems, semflg, NULL, 0);
  70. }
  71. #endif
  72. #endif
  73. #ifdef L_semop
  74. #ifdef __NR_semop
  75. _syscall3(int, semop, int, semid, struct sembuf *, sops, size_t, nsops)
  76. #else
  77. /* Perform user-defined atomical operation of array of semaphores. */
  78. int semop (int semid, struct sembuf *sops, size_t nsops)
  79. {
  80. return __syscall_ipc(IPCOP_semop, semid, (int) nsops, 0, sops, NULL);
  81. }
  82. #endif
  83. #endif
  84. #ifdef L_semtimedop
  85. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_semtimedop_time64)
  86. int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout)
  87. {
  88. return INLINE_SYSCALL(semtimedop_time64, 4, semid, sops, nsops, TO_TS64_P(timeout));
  89. }
  90. #elif defined(__NR_semtimedop)
  91. _syscall4(int, semtimedop, int, semid, struct sembuf *, sops, size_t, nsops, const struct timespec *, timeout)
  92. #else
  93. int semtimedop(int semid, struct sembuf *sops, size_t nsops,
  94. const struct timespec *timeout)
  95. {
  96. return __syscall_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, (void *) timeout);
  97. }
  98. #endif
  99. #endif