sync_linux.odin 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package sync
  2. /*
  3. import "core:atomics"
  4. import "core:os"
  5. Semaphore :: struct {
  6. // _handle: win32.Handle,
  7. }
  8. Mutex :: struct {
  9. _semaphore: Semaphore,
  10. _counter: i32,
  11. _owner: i32,
  12. _recursion: i32,
  13. }
  14. current_thread_id :: proc() -> i32 {
  15. return i32(os.current_thread_id());
  16. }
  17. semaphore_init :: proc(s: ^Semaphore) {
  18. // s._handle = win32.CreateSemaphoreA(nil, 0, 1<<31-1, nil);
  19. }
  20. semaphore_destroy :: proc(s: ^Semaphore) {
  21. // win32.CloseHandle(s._handle);
  22. }
  23. semaphore_post :: proc(s: ^Semaphore, count: int) {
  24. // win32.ReleaseSemaphore(s._handle, cast(i32)count, nil);
  25. }
  26. semaphore_release :: inline proc(s: ^Semaphore) {
  27. semaphore_post(s, 1);
  28. }
  29. semaphore_wait :: proc(s: ^Semaphore) {
  30. // win32.WaitForSingleObject(s._handle, win32.INFINITE);
  31. }
  32. mutex_init :: proc(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. mutex_destroy :: proc(m: ^Mutex) {
  39. semaphore_destroy(&m._semaphore);
  40. }
  41. mutex_lock :: proc(m: ^Mutex) {
  42. 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 += 1;
  50. }
  51. mutex_try_lock :: proc(m: ^Mutex) -> bool {
  52. thread_id := current_thread_id();
  53. if atomics.load(&m._owner) == thread_id {
  54. atomics.fetch_add(&m._counter, 1);
  55. } else {
  56. 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 += 1;
  66. return true;
  67. }
  68. mutex_unlock :: proc(m: ^Mutex) {
  69. recursion: i32;
  70. thread_id := current_thread_id();
  71. assert(thread_id == atomics.load(&m._owner));
  72. m._recursion -= 1;
  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. }
  83. */