sync_linux.odin 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import (
  2. "atomics.odin";
  3. "os.odin";
  4. )
  5. type Semaphore struct {
  6. // _handle: win32.Handle,
  7. }
  8. type Mutex struct {
  9. _semaphore: Semaphore,
  10. _counter: i32,
  11. _owner: i32,
  12. _recursion: i32,
  13. }
  14. proc current_thread_id() -> i32 {
  15. return i32(os.current_thread_id());
  16. }
  17. proc semaphore_init(s: ^Semaphore) {
  18. // s._handle = win32.CreateSemaphoreA(nil, 0, 1<<31-1, nil);
  19. }
  20. proc semaphore_destroy(s: ^Semaphore) {
  21. // win32.CloseHandle(s._handle);
  22. }
  23. proc semaphore_post(s: ^Semaphore, count: int) {
  24. // win32.ReleaseSemaphore(s._handle, cast(i32)count, nil);
  25. }
  26. proc semaphore_release(s: ^Semaphore) #inline {
  27. semaphore_post(s, 1);
  28. }
  29. proc semaphore_wait(s: ^Semaphore) {
  30. // win32.WaitForSingleObject(s._handle, win32.INFINITE);
  31. }
  32. proc mutex_init(m: ^Mutex) {
  33. atomics.store(&m._counter, 0);
  34. atomics.store(&m._owner, current_thread_id());
  35. semaphore_init(&m._semaphore);
  36. m._recursion = 0;
  37. }
  38. proc mutex_destroy(m: ^Mutex) {
  39. semaphore_destroy(&m._semaphore);
  40. }
  41. proc mutex_lock(m: ^Mutex) {
  42. var thread_id = current_thread_id();
  43. if atomics.fetch_add(&m._counter, 1) > 0 {
  44. if thread_id != atomics.load(&m._owner) {
  45. semaphore_wait(&m._semaphore);
  46. }
  47. }
  48. atomics.store(&m._owner, thread_id);
  49. m._recursion++;
  50. }
  51. proc mutex_try_lock(m: ^Mutex) -> bool {
  52. var thread_id = current_thread_id();
  53. if atomics.load(&m._owner) == thread_id {
  54. atomics.fetch_add(&m._counter, 1);
  55. } else {
  56. var expected: i32 = 0;
  57. if atomics.load(&m._counter) != 0 {
  58. return false;
  59. }
  60. if atomics.compare_exchange(&m._counter, expected, 1) == 0 {
  61. return false;
  62. }
  63. atomics.store(&m._owner, thread_id);
  64. }
  65. m._recursion++;
  66. return true;
  67. }
  68. proc mutex_unlock(m: ^Mutex) {
  69. var recursion: i32;
  70. var thread_id = current_thread_id();
  71. assert(thread_id == atomics.load(&m._owner));
  72. m._recursion--;
  73. recursion = m._recursion;
  74. if recursion == 0 {
  75. atomics.store(&m._owner, thread_id);
  76. }
  77. if atomics.fetch_add(&m._counter, -1) > 1 {
  78. if recursion == 0 {
  79. semaphore_release(&m._semaphore);
  80. }
  81. }
  82. }