sync_linux.odin 2.0 KB

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