pthread_unix.odin 5.1 KB

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