pthread_unix.odin 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //+build linux, darwin, freebsd, openbsd
  2. package unix
  3. foreign import "system:pthread"
  4. import "core:c"
  5. import "core:time"
  6. //
  7. // On success, these functions return 0.
  8. //
  9. @(default_calling_convention="c")
  10. foreign pthread {
  11. pthread_create :: proc(t: ^pthread_t, attrs: ^pthread_attr_t, routine: proc(data: rawptr) -> rawptr, arg: rawptr) -> c.int ---
  12. // retval is a pointer to a location to put the return value of the thread proc.
  13. pthread_join :: proc(t: pthread_t, retval: ^rawptr) -> 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. pthread_attr_getschedpolicy :: proc(t: ^pthread_attr_t, policy: ^c.int) -> c.int ---
  24. pthread_attr_setschedpolicy :: proc(t: ^pthread_attr_t, policy: c.int) -> c.int ---
  25. // states: PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
  26. pthread_attr_setdetachstate :: proc(attrs: ^pthread_attr_t, detach_state: c.int) -> c.int ---
  27. // scheds: PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED
  28. pthread_attr_setinheritsched :: proc(attrs: ^pthread_attr_t, sched: 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. sched_yield :: proc() -> c.int ---
  41. }
  42. @(default_calling_convention="c")
  43. foreign pthread {
  44. // NOTE: POSIX says this can fail with OOM.
  45. pthread_cond_init :: proc(cond: ^pthread_cond_t, attrs: ^pthread_condattr_t) -> c.int ---
  46. pthread_cond_destroy :: proc(cond: ^pthread_cond_t) -> c.int ---
  47. pthread_cond_signal :: proc(cond: ^pthread_cond_t) -> c.int ---
  48. // same as signal, but wakes up _all_ threads that are waiting
  49. pthread_cond_broadcast :: proc(cond: ^pthread_cond_t) -> c.int ---
  50. // assumes the mutex is pre-locked
  51. pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---
  52. pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
  53. pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---
  54. pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---
  55. // p-shared = "process-shared" - i.e: is this condition shared among multiple processes?
  56. // values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
  57. pthread_condattr_setpshared :: proc(attrs: ^pthread_condattr_t, value: c.int) -> c.int ---
  58. pthread_condattr_getpshared :: proc(attrs: ^pthread_condattr_t, result: ^c.int) -> c.int ---
  59. }
  60. @(default_calling_convention="c")
  61. foreign pthread {
  62. // NOTE: POSIX says this can fail with OOM.
  63. pthread_mutex_init :: proc(mutex: ^pthread_mutex_t, attrs: ^pthread_mutexattr_t) -> c.int ---
  64. pthread_mutex_destroy :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  65. pthread_mutex_trylock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  66. pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  67. pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
  68. pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
  69. pthread_mutexattr_init :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
  70. pthread_mutexattr_destroy :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
  71. pthread_mutexattr_settype :: proc(attrs: ^pthread_mutexattr_t, type: c.int) -> c.int ---
  72. // p-shared = "process-shared" - i.e: is this mutex shared among multiple processes?
  73. // values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
  74. pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---
  75. pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---
  76. }