diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2019-12-01 11:33:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-01 11:33:23 +0000 |
| commit | 3fd5c3cd851d8f4dfd441141ca7e96889f069933 (patch) | |
| tree | 67f47e79f5c5bb80a3ed1b1e9d79a61c08c0a29d /core/sync/sync.odin | |
| parent | 0c0c83ee295fe8787a4bdc8b826a5432abba2ca9 (diff) | |
| parent | 99121d6ff2b02f3d16b791eb103bb9f9e8b96475 (diff) | |
Merge pull request #458 from Tetralux/linux-threads
Implement core:thread and core:sync on Unix using pthreads
Diffstat (limited to 'core/sync/sync.odin')
| -rw-r--r-- | core/sync/sync.odin | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/core/sync/sync.odin b/core/sync/sync.odin new file mode 100644 index 000000000..5a0512275 --- /dev/null +++ b/core/sync/sync.odin @@ -0,0 +1,27 @@ +package sync + +foreign { + @(link_name="llvm.x86.sse2.pause") + yield_processor :: proc() --- +} + +Ticket_Mutex :: struct { + ticket: u64, + serving: u64, +} + +ticket_mutex_init :: proc(m: ^Ticket_Mutex) { + atomic_store(&m.ticket, 0, .Relaxed); + atomic_store(&m.serving, 0, .Relaxed); +} + +ticket_mutex_lock :: inline proc(m: ^Ticket_Mutex) { + ticket := atomic_add(&m.ticket, 1, .Relaxed); + for ticket != m.serving { + yield_processor(); + } +} + +ticket_mutex_unlock :: inline proc(m: ^Ticket_Mutex) { + atomic_add(&m.serving, 1, .Relaxed); +} |