primitives_darwin.odin 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //+build darwin
  2. //+private
  3. package sync2
  4. import "core:c"
  5. import "core:time"
  6. import "core:intrinsics"
  7. foreign import pthread "System.framework"
  8. _current_thread_id :: proc "contextless" () -> int {
  9. tid: u64
  10. // NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
  11. // For older versions there is `syscall(SYS_thread_selfid)`, but not really
  12. // the same thing apparently.
  13. foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
  14. pthread_threadid_np(nil, &tid)
  15. return int(tid)
  16. }
  17. _Mutex :: struct {
  18. mutex: Atomic_Mutex,
  19. }
  20. _mutex_lock :: proc(m: ^Mutex) {
  21. atomic_mutex_lock(&m.impl.mutex)
  22. }
  23. _mutex_unlock :: proc(m: ^Mutex) {
  24. atomic_mutex_unlock(&m.impl.mutex)
  25. }
  26. _mutex_try_lock :: proc(m: ^Mutex) -> bool {
  27. return atomic_mutex_try_lock(&m.impl.mutex)
  28. }
  29. _Cond :: struct {
  30. cond: Atomic_Cond,
  31. }
  32. _cond_wait :: proc(c: ^Cond, m: ^Mutex) {
  33. atomic_cond_wait(&c.impl.cond, &m.impl.mutex)
  34. }
  35. _cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, duration: time.Duration) -> bool {
  36. return atomic_cond_wait_with_timeout(&c.impl.cond, &m.impl.mutex, duration)
  37. }
  38. _cond_signal :: proc(c: ^Cond) {
  39. atomic_cond_signal(&c.impl.cond)
  40. }
  41. _cond_broadcast :: proc(c: ^Cond) {
  42. atomic_cond_broadcast(&c.impl.cond)
  43. }