Browse Source

Add sync.Once

gingerBill 5 năm trước cách đây
mục cha
commit
ce85b73eca
1 tập tin đã thay đổi với 30 bổ sung0 xóa
  1. 30 0
      core/thread/thread.odin

+ 30 - 0
core/thread/thread.odin

@@ -1,6 +1,8 @@
 package thread
 
 import "core:runtime"
+import "core:sync"
+import "core:intrinsics"
 
 Thread_Proc :: #type proc(^Thread);
 
@@ -50,3 +52,31 @@ create_and_start :: proc(fn: Thread_Proc, init_context: Maybe(runtime.Context) =
 	start(t);
 	return t;
 }
+
+
+Once :: struct {
+	m:    sync.Blocking_Mutex,
+	done: bool,
+}
+once_init :: proc(o: ^Once) {
+	sync.blocking_mutex_init(&o.m);
+	intrinsics.atomic_store_rel(&o.done, false);
+}
+once_destroy :: proc(o: ^Once) {
+	sync.blocking_mutex_destroy(&o.m);
+}
+
+once_do :: proc(o: ^Once, fn: proc()) {
+	if intrinsics.atomic_load(&o.done) == false {
+		_once_do_slow(o, fn);
+	}
+}
+
+_once_do_slow :: proc(o: ^Once, fn: proc()) {
+	sync.blocking_mutex_lock(&o.m);
+	defer sync.blocking_mutex_unlock(&o.m);
+	if !o.done {
+		fn();
+		intrinsics.atomic_store_rel(&o.done, true);
+	}
+}