pthread_unix.odin 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //+build linux, darwin, freebsd, openbsd, netbsd, haiku
  2. package unix
  3. foreign import "system:pthread"
  4. import "core:c"
  5. timespec :: struct {
  6. tv_sec: i64,
  7. tv_nsec: i64,
  8. }
  9. //
  10. // On success, these functions return 0.
  11. //
  12. @(default_calling_convention="c")
  13. foreign pthread {
  14. pthread_create :: proc(t: ^pthread_t, attrs: ^pthread_attr_t, routine: proc(data: rawptr) -> rawptr, arg: rawptr) -> c.int ---
  15. // retval is a pointer to a location to put the return value of the thread proc.
  16. pthread_join :: proc(t: pthread_t, retval: ^rawptr) -> c.int ---
  17. pthread_kill :: proc(t: pthread_t, sig: c.int) -> c.int ---
  18. pthread_self :: proc() -> pthread_t ---
  19. pthread_equal :: proc(a, b: pthread_t) -> b32 ---
  20. sched_get_priority_min :: proc(policy: c.int) -> c.int ---
  21. sched_get_priority_max :: proc(policy: c.int) -> c.int ---
  22. // NOTE: POSIX says this can fail with OOM.
  23. pthread_attr_init :: proc(attrs: ^pthread_attr_t) -> c.int ---
  24. pthread_attr_destroy :: proc(attrs: ^pthread_attr_t) -> c.int ---
  25. pthread_attr_getschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
  26. pthread_attr_setschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
  27. // states: PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
  28. pthread_attr_setdetachstate :: proc(attrs: ^pthread_attr_t, detach_state: c.int) -> c.int ---
  29. // NOTE(tetra, 2019-11-06): WARNING: Different systems have different alignment requirements.
  30. // For maximum usefulness, use the OS's page size.
  31. // ALSO VERY MAJOR WARNING: `stack_ptr` must be the LAST byte of the stack on systems
  32. // where the stack grows downwards, which is the common case, so far as I know.
  33. // On systems where it grows upwards, give the FIRST byte instead.
  34. // ALSO SLIGHTLY LESS MAJOR WARNING: Using this procedure DISABLES automatically-provided
  35. // guard pages. If you are using this procedure, YOU must set them up manually.
  36. // If you forget to do this, you WILL get stack corruption bugs if you do not EXTREMELY
  37. // know what you are doing!
  38. pthread_attr_setstack :: proc(attrs: ^pthread_attr_t, stack_ptr: rawptr, stack_size: u64) -> c.int ---
  39. pthread_attr_getstack :: proc(attrs: ^pthread_attr_t, stack_ptr: ^rawptr, stack_size: ^u64) -> c.int ---
  40. pthread_sigmask :: proc(how: c.int, set: rawptr, oldset: rawptr) -> c.int ---
  41. sched_yield :: proc() -> c.int ---
  42. }
  43. // NOTE: Unimplemented in Haiku.
  44. when ODIN_OS != .Haiku {
  45. foreign pthread {
  46. // scheds: PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED
  47. pthread_attr_setinheritsched :: proc(attrs: ^pthread_attr_t, sched: c.int) -> c.int ---
  48. pthread_attr_getschedpolicy :: proc(t: ^pthread_attr_t, policy: ^c.int) -> c.int ---
  49. pthread_attr_setschedpolicy :: proc(t: ^pthread_attr_t, policy: c.int) -> c.int ---
  50. }
  51. }
  52. @(default_calling_convention="c")
  53. foreign pthread {
  54. // NOTE: POSIX says this can fail with OOM.
  55. pthread_cond_init :: proc(cond: ^pthread_cond_t, attrs: ^pthread_condattr_t) -> c.int ---
  56. pthread_cond_destroy :: proc(cond: ^pthread_cond_t) -> c.int ---
  57. pthread_cond_signal :: proc(cond: ^pthread_cond_t) -> c.int ---
  58. // same as signal, but wakes up _all_ threads that are waiting
  59. pthread_cond_broadcast :: proc(cond: ^pthread_cond_t) -> c.int ---
  60. // assumes the mutex is pre-locked
  61. pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---
  62. pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
  63. pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---
  64. pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---
  65. // p-shared = "process-shared" - i.e: is this condition shared among multiple processes?
  66. // values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
  67. pthread_condattr_setpshared :: proc(attrs: ^pthread_condattr_t, value: c.int) -> c.int ---
  68. pthread_condattr_getpshared :: proc(attrs: ^pthread_condattr_t, result: ^c.int) -> c.int ---
  69. }
  70. @(default_calling_convention="c")
  71. foreign pthread {
  72. // NOTE: POSIX says this can fail with OOM.
  73. pthread_mutex_init :: proc(mutex: ^pthread_mutex_t, attrs: ^pthread_mutexattr_t) -> c.int ---
  74. pthread_mutex_destroy :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  75. pthread_mutex_trylock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  76. pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  77. pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
  78. pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  79. pthread_mutexattr_init :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
  80. pthread_mutexattr_destroy :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
  81. pthread_mutexattr_settype :: proc(attrs: ^pthread_mutexattr_t, type: c.int) -> c.int ---
  82. // p-shared = "process-shared" - i.e: is this mutex shared among multiple processes?
  83. // values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
  84. pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---
  85. pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---
  86. pthread_testcancel :: proc () ---
  87. }