sync_unix.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // +build linux, darwin
  2. package sync
  3. import "core:sys/unix"
  4. // A lock that can only be held by one thread at once.
  5. Mutex :: struct {
  6. handle: unix.pthread_mutex_t,
  7. }
  8. // Blocks until signalled, and then lets past exactly
  9. // one thread.
  10. Condition :: struct {
  11. handle: unix.pthread_cond_t,
  12. // NOTE(tetra, 2019-11-11): Used to mimic the more sane behavior of Windows' AutoResetEvent.
  13. // This means that you may signal the condition before anyone is waiting to cause the
  14. // next thread that tries to wait to just pass by uninterrupted, without sleeping.
  15. // Without this, signalling a condition will only wake up a thread which is already waiting,
  16. // but not one that is about to wait, which can cause your program to become out of sync in
  17. // ways that are hard to debug or fix.
  18. flag: bool, // atomically mutated
  19. mutex: Mutex,
  20. }
  21. mutex_init :: proc(m: ^Mutex) {
  22. // NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the mutex.
  23. attrs: unix.pthread_mutexattr_t;
  24. assert(unix.pthread_mutexattr_init(&attrs) == 0);
  25. defer unix.pthread_mutexattr_destroy(&attrs); // ignores destruction error
  26. assert(unix.pthread_mutex_init(&m.handle, &attrs) == 0);
  27. }
  28. mutex_destroy :: proc(m: ^Mutex) {
  29. assert(unix.pthread_mutex_destroy(&m.handle) == 0);
  30. m.handle = {};
  31. }
  32. mutex_lock :: proc(m: ^Mutex) {
  33. assert(unix.pthread_mutex_lock(&m.handle) == 0);
  34. }
  35. // Returns false if someone else holds the lock.
  36. mutex_try_lock :: proc(m: ^Mutex) -> bool {
  37. return unix.pthread_mutex_trylock(&m.handle) == 0;
  38. }
  39. mutex_unlock :: proc(m: ^Mutex) {
  40. assert(unix.pthread_mutex_unlock(&m.handle) == 0);
  41. }
  42. condition_init :: proc(c: ^Condition) {
  43. // NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the condition.
  44. attrs: unix.pthread_condattr_t;
  45. assert(unix.pthread_condattr_init(&attrs) == 0);
  46. defer unix.pthread_condattr_destroy(&attrs); // ignores destruction error
  47. assert(unix.pthread_cond_init(&c.handle, &attrs) == 0);
  48. mutex_init(&c.mutex);
  49. c.flag = false;
  50. }
  51. condition_destroy :: proc(c: ^Condition) {
  52. assert(unix.pthread_cond_destroy(&c.handle) == 0);
  53. mutex_destroy(&c.mutex);
  54. c.handle = {};
  55. }
  56. // Awaken exactly one thread who is waiting on the condition.
  57. condition_signal :: proc(c: ^Condition) {
  58. mutex_lock(&c.mutex);
  59. defer mutex_unlock(&c.mutex);
  60. atomic_swap(&c.flag, true, .Sequentially_Consistent);
  61. assert(unix.pthread_cond_signal(&c.handle) == 0);
  62. }
  63. // Wait for the condition to be signalled.
  64. // Does not block if the condition has been signalled and no one
  65. // has waited on it yet.
  66. condition_wait_for :: proc(c: ^Condition) {
  67. mutex_lock(&c.mutex);
  68. defer mutex_unlock(&c.mutex);
  69. // NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
  70. // the thread that gets signalled and wakes up, discovers that the flag was taken and goes
  71. // back to sleep.
  72. // Though this overall behavior is the most sane, there may be a better way to do this that means that
  73. // the first thread to wait, gets the flag first.
  74. if atomic_swap(&c.flag, false, .Sequentially_Consistent) do return;
  75. for {
  76. assert(unix.pthread_cond_wait(&c.handle, &c.mutex.handle) == 0);
  77. if atomic_swap(&c.flag, false, .Sequentially_Consistent) do break;
  78. }
  79. }