2021-12-05 19:56:03 +03:00
|
|
|
module sync
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
import sync.stdatomic
|
2021-12-05 19:56:03 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:15:28 +03:00
|
|
|
// do executes the function `f()` only once
|
2021-12-05 19:56:03 +03:00
|
|
|
pub fn (mut o Once) do(f fn ()) {
|
2021-12-28 11:12:40 +03:00
|
|
|
if stdatomic.load_u64(&o.count) < 1 {
|
2021-12-05 19:56:03 +03:00
|
|
|
o.do_slow(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut o Once) do_slow(f fn ()) {
|
|
|
|
o.m.@lock()
|
|
|
|
if o.count < 1 {
|
2021-12-28 11:12:40 +03:00
|
|
|
stdatomic.store_u64(&o.count, 1)
|
2021-12-05 19:56:03 +03:00
|
|
|
f()
|
|
|
|
}
|
|
|
|
o.m.unlock()
|
|
|
|
}
|
2022-02-08 18:15:28 +03:00
|
|
|
|
|
|
|
// do_with_param executes `f(param)` only once`
|
|
|
|
// This method can be used as a workaround for passing closures to once.do/1 on Windows
|
|
|
|
// (they are not implemented there yet) - just pass your data explicitly.
|
|
|
|
// i.e. instead of:
|
|
|
|
// ```v
|
|
|
|
// once.do(fn [mut o] () {
|
|
|
|
// o.add(5)
|
|
|
|
// })
|
|
|
|
// ```
|
2023-01-30 22:25:33 +03:00
|
|
|
//
|
2022-02-08 18:15:28 +03:00
|
|
|
// ... you can use:
|
|
|
|
// ```v
|
|
|
|
// once.do_with_param(fn (mut o One) {
|
|
|
|
// o.add(5)
|
|
|
|
// }, o)
|
|
|
|
// ```
|
|
|
|
|
|
|
|
pub fn (mut o Once) do_with_param(f fn (voidptr), param voidptr) {
|
|
|
|
if stdatomic.load_u64(&o.count) < 1 {
|
|
|
|
o.do_slow_with_param(f, param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut o Once) do_slow_with_param(f fn (p voidptr), param voidptr) {
|
|
|
|
o.m.@lock()
|
|
|
|
if o.count < 1 {
|
|
|
|
stdatomic.store_u64(&o.count, 1)
|
|
|
|
f(param)
|
|
|
|
}
|
|
|
|
o.m.unlock()
|
|
|
|
}
|