primitives_pthreads.odin 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //+build linux, freebsd
  2. //+private
  3. package sync2
  4. import "core:time"
  5. import "core:sys/unix"
  6. _Mutex_State :: enum i32 {
  7. Unlocked = 0,
  8. Locked = 1,
  9. Waiting = 2,
  10. }
  11. _Mutex :: struct {
  12. pthread_mutex: unix.pthread_mutex_t,
  13. }
  14. _mutex_lock :: proc(m: ^Mutex) {
  15. err := unix.pthread_mutex_lock(&m.impl.pthread_mutex)
  16. assert(err == 0)
  17. }
  18. _mutex_unlock :: proc(m: ^Mutex) {
  19. err := unix.pthread_mutex_unlock(&m.impl.pthread_mutex)
  20. assert(err == 0)
  21. }
  22. _mutex_try_lock :: proc(m: ^Mutex) -> bool {
  23. err := unix.pthread_mutex_trylock(&m.impl.pthread_mutex)
  24. return err == 0
  25. }
  26. _Cond :: struct {
  27. pthread_cond: unix.pthread_cond_t,
  28. }
  29. _cond_wait :: proc(c: ^Cond, m: ^Mutex) {
  30. err := unix.pthread_cond_wait(&c.impl.pthread_cond, &m.impl.pthread_mutex)
  31. assert(err == 0)
  32. }
  33. _cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, duration: time.Duration) -> bool {
  34. tv_sec := i64(duration/1e9)
  35. tv_nsec := i64(duration%1e9)
  36. err := unix.pthread_cond_timedwait(&c.impl.pthread_cond, &m.impl.pthread_mutex, &{tv_sec, tv_nsec})
  37. return err == 0
  38. }
  39. _cond_signal :: proc(c: ^Cond) {
  40. err := unix.pthread_cond_signal(&c.impl.pthread_cond)
  41. assert(err == 0)
  42. }
  43. _cond_broadcast :: proc(c: ^Cond) {
  44. err := unix.pthread_cond_broadcast(&c.impl.pthread_cond)
  45. assert(err == 0)
  46. }