sync_windows.odin 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #import win32 "sys/windows.odin" when ODIN_OS == "windows";
  2. #import "atomics.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(win32.get_current_thread_id());
  14. }
  15. semaphore_init :: proc(s: ^Semaphore) {
  16. s._handle = win32.create_semaphore_a(nil, 0, 1<<31-1, nil);
  17. }
  18. semaphore_destroy :: proc(s: ^Semaphore) {
  19. win32.close_handle(s._handle);
  20. }
  21. semaphore_post :: proc(s: ^Semaphore, count: int) {
  22. win32.release_semaphore(s._handle, i32(count), nil);
  23. }
  24. semaphore_release :: proc(s: ^Semaphore) #inline { semaphore_post(s, 1); }
  25. semaphore_wait :: proc(s: ^Semaphore) {
  26. win32.wait_for_single_object(s._handle, win32.INFINITE);
  27. }
  28. mutex_init :: proc(m: ^Mutex) {
  29. atomics.store(&m._counter, 0);
  30. atomics.store(&m._owner, current_thread_id());
  31. semaphore_init(&m._semaphore);
  32. m._recursion = 0;
  33. }
  34. mutex_destroy :: proc(m: ^Mutex) {
  35. semaphore_destroy(&m._semaphore);
  36. }
  37. mutex_lock :: proc(m: ^Mutex) {
  38. thread_id := current_thread_id();
  39. if atomics.fetch_add(&m._counter, 1) > 0 {
  40. if thread_id != atomics.load(&m._owner) {
  41. semaphore_wait(&m._semaphore);
  42. }
  43. }
  44. atomics.store(&m._owner, thread_id);
  45. m._recursion++;
  46. }
  47. mutex_try_lock :: proc(m: ^Mutex) -> bool {
  48. thread_id := current_thread_id();
  49. if atomics.load(&m._owner) == thread_id {
  50. atomics.fetch_add(&m._counter, 1);
  51. } else {
  52. expected: i32 = 0;
  53. if atomics.load(&m._counter) != 0 {
  54. return false;
  55. }
  56. if atomics.compare_exchange(&m._counter, expected, 1) == 0 {
  57. return false;
  58. }
  59. atomics.store(&m._owner, thread_id);
  60. }
  61. m._recursion++;
  62. return true;
  63. }
  64. mutex_unlock :: proc(m: ^Mutex) {
  65. recursion: i32;
  66. thread_id := current_thread_id();
  67. assert(thread_id == atomics.load(&m._owner));
  68. m._recursion--;
  69. recursion = m._recursion;
  70. if recursion == 0 {
  71. atomics.store(&m._owner, thread_id);
  72. }
  73. if atomics.fetch_add(&m._counter, -1) > 1 {
  74. if recursion == 0 {
  75. semaphore_release(&m._semaphore);
  76. }
  77. }
  78. }