sys_sem.odin 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #+build linux, darwin, netbsd, openbsd, freebsd, haiku
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // sys/sem.h - XSI semaphore facility
  10. foreign lib {
  11. /*
  12. Provides various semaphore control operation as specified by cmd.
  13. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semctl.html ]]
  14. */
  15. @(link_name=LSEMCTL)
  16. semctl :: proc(semid: FD, semnum: c.int, cmd: Sem_Cmd, arg: ^semun = nil) -> c.int ---
  17. /*
  18. Returns the semaphore identifier associated with key.
  19. Returns: -1 (setting errno) on failure, a semaphore file descriptor otherwise
  20. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semget.html ]]
  21. */
  22. semget :: proc(key: key_t, nsems: c.int, semflg: IPC_Flags) -> FD ---
  23. /*
  24. Perform atomically a user-defined array of semaphore operations in array order on the set of
  25. semaphores associated with the semaphore identifier specified by the argument semid.
  26. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semop.html ]]
  27. */
  28. semop :: proc(semid: FD, sops: [^]sembuf, nsops: c.size_t) -> result ---
  29. }
  30. Sem_Cmd :: enum c.int {
  31. // Returns the value of semncnt.
  32. GETNCNT = GETNCNT,
  33. // Returns the value of sempid.
  34. GETPID = GETPID,
  35. // Return the value of semval.
  36. GETVAL = GETVAL,
  37. // Returns the value of semval for each semaphore in the semaphore set.
  38. GETALL = GETALL,
  39. // Returns the value of semzcnt.
  40. GETZCNT = GETZCNT,
  41. // Sets the value of semval to arg.val.
  42. SETVAL = SETVAL,
  43. // Sets the value of semval for each semaphore in the set.
  44. SETALL = SETALL,
  45. }
  46. semun :: struct #raw_union {
  47. val: c.int,
  48. buf: ^semid_ds,
  49. array: [^]c.ushort,
  50. }
  51. when ODIN_OS == .NetBSD {
  52. @(private) LSEMCTL :: "__semctl50"
  53. } else {
  54. @(private) LSEMCTL :: "semctl"
  55. }
  56. when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  57. SEM_UNDO :: 0o10000
  58. GETNCNT :: 3
  59. GETPID :: 4
  60. GETVAL :: 5
  61. GETALL :: 6
  62. GETZCNT :: 7
  63. SETVAL :: 8
  64. SETALL :: 9
  65. when ODIN_OS == .Darwin {
  66. semid_ds :: struct #max_field_align(4) {
  67. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  68. sem_base: c.int32_t, /* 32 bit base ptr for semaphore set */
  69. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  70. sem_otime: time_t, /* [PSX] last semop() */
  71. sem_pad1: c.int32_t,
  72. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  73. sem_pad2: c.int32_t,
  74. sem_pad3: [4]c.int32_t,
  75. }
  76. } else when ODIN_OS == .FreeBSD {
  77. semid_ds :: struct {
  78. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  79. sem_base: rawptr, /* 32 bit base ptr for semaphore set */
  80. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  81. sem_otime: time_t, /* [PSX] last semop() */
  82. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  83. }
  84. } else when ODIN_OS == .NetBSD {
  85. semid_ds :: struct {
  86. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  87. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  88. sem_otime: time_t, /* [PSX] last semop() */
  89. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  90. _sem_base: rawptr, /* 32 bit base ptr for semaphore set */
  91. }
  92. } else when ODIN_OS == .OpenBSD {
  93. semid_ds :: struct {
  94. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  95. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  96. sem_otime: time_t, /* [PSX] last semop() */
  97. sem_pad1: c.long,
  98. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  99. sem_pad2: c.long,
  100. sem_pad3: [4]c.long,
  101. }
  102. }
  103. sembuf :: struct {
  104. sem_num: c.ushort, /* [PSX] semaphore number */
  105. sem_op: c.short, /* [PSX] semaphore operation */
  106. sem_flg: c.short, /* [PSX] operation flags */
  107. }
  108. } else when ODIN_OS == .Linux {
  109. SEM_UNDO :: 0x1000 // undo the operation on exit
  110. // Commands for `semctl'.
  111. GETPID :: 11
  112. GETVAL :: 12
  113. GETALL :: 13
  114. GETNCNT :: 14
  115. GETZCNT :: 15
  116. SETVAL :: 16
  117. SETALL :: 17
  118. semid_ds :: struct {
  119. sem_perm: ipc_perm, // [PSX] operation permission structure
  120. sem_otime: time_t, // [PSX] last semop()
  121. __sem_otime_high: c.ulong,
  122. sem_ctime: time_t, // [PSX] last time changed by semctl()
  123. __sem_ctime_high: c.ulong,
  124. sem_nsems: c.ulong, // [PSX] number of semaphores in set
  125. __glibc_reserved3: c.ulong,
  126. __glibc_reserved4: c.ulong,
  127. }
  128. sembuf :: struct {
  129. sem_num: c.ushort, /* [PSX] semaphore number */
  130. sem_op: c.short, /* [PSX] semaphore operation */
  131. sem_flg: c.short, /* [PSX] operation flags */
  132. }
  133. } else when ODIN_OS == .Haiku {
  134. SEM_UNDO :: 10 // undo the operation on exit
  135. // Commands for `semctl'.
  136. GETPID :: 3
  137. GETVAL :: 4
  138. GETALL :: 5
  139. GETNCNT :: 6
  140. GETZCNT :: 7
  141. SETVAL :: 8
  142. SETALL :: 9
  143. semid_ds :: struct {
  144. sem_perm: ipc_perm, // [PSX] operation permission structure
  145. sem_nsems: c.ushort, // [PSX] number of semaphores in set
  146. sem_otime: time_t, // [PSX] last semop()
  147. sem_ctime: time_t, // [PSX] last time changed by semctl()
  148. }
  149. sembuf :: struct {
  150. sem_num: c.ushort, /* [PSX] semaphore number */
  151. sem_op: c.short, /* [PSX] semaphore operation */
  152. sem_flg: c.short, /* [PSX] operation flags */
  153. }
  154. }