sys_sem.odin 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package posix
  2. import "core:c"
  3. when ODIN_OS == .Darwin {
  4. foreign import lib "system:System.framework"
  5. } else {
  6. foreign import lib "system:c"
  7. }
  8. // sys/sem.h - XSI semaphore facility
  9. foreign lib {
  10. /*
  11. Provides various semaphore control operation as specified by cmd.
  12. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semctl.html ]]
  13. */
  14. @(link_name=LSEMCTL)
  15. semctl :: proc(semid: FD, semnum: c.int, cmd: Sem_Cmd, arg: ^semun = nil) -> c.int ---
  16. /*
  17. Returns the semaphore identifier associated with key.
  18. Returns: -1 (setting errno) on failure, a semaphore file descriptor otherwise
  19. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semget.html ]]
  20. */
  21. semget :: proc(key: key_t, nsems: c.int, semflg: IPC_Flags) -> FD ---
  22. /*
  23. Perform atomically a user-defined array of semaphore operations in array order on the set of
  24. semaphores associated with the semaphore identifier specified by the argument semid.
  25. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/semop.html ]]
  26. */
  27. semop :: proc(semid: FD, sops: [^]sembuf, nsops: c.size_t) -> result ---
  28. }
  29. Sem_Cmd :: enum c.int {
  30. // Returns the value of semncnt.
  31. GETNCNT = GETNCNT,
  32. // Returns the value of sempid.
  33. GETPID = GETPID,
  34. // Return the value of semval.
  35. GETVAL = GETVAL,
  36. // Returns the value of semval for each semaphore in the semaphore set.
  37. GETALL = GETALL,
  38. // Returns the value of semzcnt.
  39. GETZCNT = GETZCNT,
  40. // Sets the value of semval to arg.val.
  41. SETVAL = SETVAL,
  42. // Sets the value of semval for each semaphore in the set.
  43. SETALL = SETALL,
  44. }
  45. semun :: struct #raw_union {
  46. val: c.int,
  47. buf: ^semid_ds,
  48. array: [^]c.ushort,
  49. }
  50. when ODIN_OS == .NetBSD {
  51. @(private) LSEMCTL :: "__semctl50"
  52. } else {
  53. @(private) LSEMCTL :: "semctl"
  54. }
  55. when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  56. SEM_UNDO :: 0o10000
  57. GETNCNT :: 3
  58. GETPID :: 4
  59. GETVAL :: 5
  60. GETALL :: 6
  61. GETZCNT :: 7
  62. SETVAL :: 8
  63. SETALL :: 9
  64. when ODIN_OS == .Darwin {
  65. semid_ds :: struct #max_field_align(4) {
  66. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  67. sem_base: c.int32_t, /* 32 bit base ptr for semaphore set */
  68. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  69. sem_otime: time_t, /* [PSX] last semop() */
  70. sem_pad1: c.int32_t,
  71. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  72. sem_pad2: c.int32_t,
  73. sem_pad3: [4]c.int32_t,
  74. }
  75. } else when ODIN_OS == .FreeBSD {
  76. semid_ds :: struct {
  77. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  78. sem_base: rawptr, /* 32 bit base ptr for semaphore set */
  79. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  80. sem_otime: time_t, /* [PSX] last semop() */
  81. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  82. }
  83. } else when ODIN_OS == .NetBSD {
  84. semid_ds :: struct {
  85. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  86. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  87. sem_otime: time_t, /* [PSX] last semop() */
  88. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  89. _sem_base: rawptr, /* 32 bit base ptr for semaphore set */
  90. }
  91. } else when ODIN_OS == .OpenBSD {
  92. semid_ds :: struct {
  93. sem_perm: ipc_perm, /* [PSX] operation permission structure */
  94. sem_nsems: c.ushort, /* [PSX] number of semaphores in set */
  95. sem_otime: time_t, /* [PSX] last semop() */
  96. sem_pad1: c.long,
  97. sem_ctime: time_t, /* [PSX] last time changed by semctl() */
  98. sem_pad2: c.long,
  99. sem_pad3: [4]c.long,
  100. }
  101. }
  102. sembuf :: struct {
  103. sem_num: c.ushort, /* [PSX] semaphore number */
  104. sem_op: c.short, /* [PSX] semaphore operation */
  105. sem_flg: c.short, /* [PSX] operation flags */
  106. }
  107. } else {
  108. #panic("posix is unimplemented for the current target")
  109. }