This commit is contained in:
gingerBill
2020-04-21 23:26:09 +01:00
2 changed files with 7 additions and 14 deletions
+5 -4
View File
@@ -1,8 +1,9 @@
package sync package sync
foreign { import "core:intrinsics"
@(link_name="llvm.x86.sse2.pause")
yield_processor :: proc() --- cpu_relax :: inline proc() {
intrinsics.cpu_relax();
} }
Ticket_Mutex :: struct { Ticket_Mutex :: struct {
@@ -18,7 +19,7 @@ ticket_mutex_init :: proc(m: ^Ticket_Mutex) {
ticket_mutex_lock :: inline proc(m: ^Ticket_Mutex) { ticket_mutex_lock :: inline proc(m: ^Ticket_Mutex) {
ticket := atomic_add(&m.ticket, 1, .Relaxed); ticket := atomic_add(&m.ticket, 1, .Relaxed);
for ticket != m.serving { for ticket != m.serving {
yield_processor(); intrinsics.cpu_relax();
} }
} }
+2 -10
View File
@@ -2,6 +2,7 @@
package thread; package thread;
import "core:runtime" import "core:runtime"
import "core:intrinsics"
import "core:sync" import "core:sync"
import "core:sys/unix" import "core:sys/unix"
@@ -40,15 +41,6 @@ Thread_Priority :: enum {
// Creates a thread which will run the given procedure. // Creates a thread which will run the given procedure.
// It then waits for `start` to be called. // It then waits for `start` to be called.
// //
// You may provide a slice of bytes to use as the stack for the new thread,
// but if you do, you are expected to set up the guard pages yourself.
//
// The stack must also be aligned appropriately for the platform.
// We require it's at least 16 bytes aligned to help robustness; other
// platforms may require page-size alignment.
// Note also that pthreads requires the stack is at least 6 OS pages in size:
// 4 are required by pthreads, and two extra for guards pages that will be applied.
//
create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread { create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
__linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr { __linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
t := (^Thread)(t); t := (^Thread)(t);
@@ -134,7 +126,7 @@ join :: proc(t: ^Thread) {
if sync.atomic_swap(&t.already_joined, true, .Sequentially_Consistent) { if sync.atomic_swap(&t.already_joined, true, .Sequentially_Consistent) {
for { for {
if sync.atomic_load(&t.done, .Sequentially_Consistent) do return; if sync.atomic_load(&t.done, .Sequentially_Consistent) do return;
sync.yield_processor(); intrinsics.cpu_relax();
} }
} }