pthread_unix.odin 4.7 KB

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