1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/sync/once.v
2021-12-28 10:16:22 +02:00

34 lines
463 B
V

module sync
import sync.stdatomic
pub struct Once {
mut:
m RwMutex
pub:
count u64
}
// new_once return a new Once struct.
pub fn new_once() &Once {
mut once := &Once{}
once.m.init()
return once
}
// do execute the function only once.
pub fn (mut o Once) do(f fn ()) {
if stdatomic.load_u64(&o.count) < 1 {
o.do_slow(f)
}
}
fn (mut o Once) do_slow(f fn ()) {
o.m.@lock()
if o.count < 1 {
stdatomic.store_u64(&o.count, 1)
f()
}
o.m.unlock()
}