pthread_unix.odin 4.9 KB

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