sem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <sys/sem.h>
  18. #include "ipc.h"
  19. #ifdef L_semctl
  20. /* Return identifier for array of NSEMS semaphores associated with
  21. KEY. */
  22. #include <stdarg.h>
  23. /* arg for semctl system calls. */
  24. union semun {
  25. int val; /* value for SETVAL */
  26. struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
  27. unsigned short *array; /* array for GETALL & SETALL */
  28. struct seminfo *__buf; /* buffer for IPC_INFO */
  29. void *__pad;
  30. };
  31. #ifdef __NR_semctl
  32. #define __NR___semctl __NR_semctl
  33. static inline _syscall4(int, __semctl, int, semid, int, semnum, int, cmd, void *, arg);
  34. #endif
  35. int semctl(int semid, int semnum, int cmd, ...)
  36. {
  37. union semun arg;
  38. va_list ap;
  39. /* Get the argument. */
  40. va_start (ap, cmd);
  41. arg = va_arg (ap, union semun);
  42. va_end (ap);
  43. #ifdef __NR_semctl
  44. return __semctl(semid, semnum, cmd | __IPC_64, arg.__pad);
  45. #else
  46. return __syscall_ipc(IPCOP_semctl, semid, semnum, cmd | __IPC_64, &arg);
  47. #endif
  48. }
  49. #endif
  50. #ifdef L_semget
  51. /* for definition of NULL */
  52. #include <stdlib.h>
  53. #ifdef __NR_semget
  54. _syscall3(int, semget, key_t, key, int, nsems, int, semflg);
  55. #else
  56. /* Return identifier for array of NSEMS semaphores associated
  57. * with KEY. */
  58. int semget (key_t key, int nsems, int semflg)
  59. {
  60. return __syscall_ipc(IPCOP_semget, key, nsems, semflg, NULL);
  61. }
  62. #endif
  63. #endif
  64. #ifdef L_semop
  65. #ifdef __NR_semop
  66. _syscall3(int, semop, int, semid, struct sembuf *, sops, size_t, nsops);
  67. #else
  68. /* Perform user-defined atomical operation of array of semaphores. */
  69. int semop (int semid, struct sembuf *sops, size_t nsops)
  70. {
  71. return __syscall_ipc(IPCOP_semop, semid, (int) nsops, 0, sops);
  72. }
  73. #endif
  74. #endif