sync: improve documentation (#24799)

This commit is contained in:
Laurent Cheylus 2025-06-27 09:29:09 +02:00 committed by GitHub
parent 7039081d66
commit 6b45931598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 13 deletions

View File

@ -1,11 +1,11 @@
module sync module sync
// str returns a string representation of the Mutex pointer // str returns a string representation of the Mutex pointer.
pub fn (m &Mutex) str() string { pub fn (m &Mutex) str() string {
return 'Mutex(${voidptr(m)})' return 'Mutex(${voidptr(m)})'
} }
// str returns a string representation of the RwMutex pointer // str returns a string representation of the RwMutex pointer.
pub fn (m &RwMutex) str() string { pub fn (m &RwMutex) str() string {
return 'RwMutex(${voidptr(m)})' return 'RwMutex(${voidptr(m)})'
} }

View File

@ -20,7 +20,7 @@ pub fn new_cond(m &Mutex) &Cond {
} }
} }
// wait waits for condition notification // wait waits for condition notification.
// NOTE: Spurious wakeups are possible; always use in a loop: // NOTE: Spurious wakeups are possible; always use in a loop:
// mutex.lock() // mutex.lock()
// for !condition { // for !condition {
@ -56,7 +56,7 @@ pub fn (mut c Cond) wait() {
c.mutex.lock() c.mutex.lock()
} }
// signal wakes one waiting thread // signal wakes one waiting thread.
@[direct_array_access] @[direct_array_access]
pub fn (mut c Cond) signal() { pub fn (mut c Cond) signal() {
c.inner_mutex.lock() c.inner_mutex.lock()
@ -71,7 +71,7 @@ pub fn (mut c Cond) signal() {
} }
} }
// broadcast wakes all waiting threads // broadcast wakes all waiting threads.
@[direct_array_access] @[direct_array_access]
pub fn (mut c Cond) broadcast() { pub fn (mut c Cond) broadcast() {
c.inner_mutex.lock() c.inner_mutex.lock()

View File

@ -16,7 +16,7 @@ pub fn new_once() &Once {
return once return once
} }
// do executes the function `f()` only once // do executes the function `f()` only once.
pub fn (mut o Once) do(f fn ()) { pub fn (mut o Once) do(f fn ()) {
if stdatomic.load_u64(&o.count) < 1 { if stdatomic.load_u64(&o.count) < 1 {
o.do_slow(f) o.do_slow(f)
@ -32,7 +32,7 @@ fn (mut o Once) do_slow(f fn ()) {
o.m.unlock() o.m.unlock()
} }
// do_with_param executes `f(param)` only once` // 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 // 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. // (they are not implemented there yet) - just pass your data explicitly.
// i.e. instead of: // i.e. instead of:
@ -49,6 +49,7 @@ fn (mut o Once) do_slow(f fn ()) {
// }, o) // }, o)
// ``` // ```
// do_with_param executes the function `f()` with parameter `param` only once.
pub fn (mut o Once) do_with_param(f fn (voidptr), param voidptr) { pub fn (mut o Once) do_with_param(f fn (voidptr), param voidptr) {
if stdatomic.load_u64(&o.count) < 1 { if stdatomic.load_u64(&o.count) < 1 {
o.do_slow_with_param(f, param) o.do_slow_with_param(f, param)

View File

@ -215,7 +215,7 @@ pub fn (mut sem Semaphore) init(n u32) {
// post increases/unlocks the counter of the semaphore by 1. // post increases/unlocks the counter of the semaphore by 1.
// If the resulting counter value is > 0, and if there is another thread waiting // If the resulting counter value is > 0, and if there is another thread waiting
// on the semaphore, the waiting thread will decrement the counter by 1 // on the semaphore, the waiting thread will decrement the counter by 1
// (locking the semaphore), and then will continue running. See also .wait() . // (locking the semaphore), and then will continue running. See also .wait().
@[inline] @[inline]
pub fn (mut sem Semaphore) post() { pub fn (mut sem Semaphore) post() {
C.sem_post(&sem.sem) C.sem_post(&sem.sem)
@ -225,7 +225,7 @@ pub fn (mut sem Semaphore) post() {
// It it was not positive, it will waits for the semaphore count to reach a positive number. // It it was not positive, it will waits for the semaphore count to reach a positive number.
// When that happens, it will decrease the semaphore count (lock the semaphore), and will return. // When that happens, it will decrease the semaphore count (lock the semaphore), and will return.
// In effect, it allows you to block threads, until the semaphore, is posted by another thread. // In effect, it allows you to block threads, until the semaphore, is posted by another thread.
// See also .post() . // See also .post().
pub fn (mut sem Semaphore) wait() { pub fn (mut sem Semaphore) wait() {
for { for {
if C.sem_wait(&sem.sem) == 0 { if C.sem_wait(&sem.sem) == 0 {
@ -246,7 +246,7 @@ pub fn (mut sem Semaphore) wait() {
// try_wait tries to decrease the semaphore count by 1, if it was positive. // try_wait tries to decrease the semaphore count by 1, if it was positive.
// If it succeeds in that, it returns true, otherwise it returns false. // If it succeeds in that, it returns true, otherwise it returns false.
// try_wait should return as fast as possible so error handling is only // try_wait should return as fast as possible so error handling is only
// done when debugging // done when debugging.
pub fn (mut sem Semaphore) try_wait() bool { pub fn (mut sem Semaphore) try_wait() bool {
$if !debug { $if !debug {
return C.sem_trywait(&sem.sem) == 0 return C.sem_trywait(&sem.sem) == 0

View File

@ -16,7 +16,7 @@ fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool
// Do not copy an instance of WaitGroup, use a ref instead. // Do not copy an instance of WaitGroup, use a ref instead.
// //
// usage: in main thread: // usage: in main thread:
// `wg := sync.new_waitgroup() // `wg := sync.new_waitgroup()`
// `wg.add(nr_jobs)` before starting jobs with `go ...` // `wg.add(nr_jobs)` before starting jobs with `go ...`
// `wg.wait()` to wait for all jobs to have finished // `wg.wait()` to wait for all jobs to have finished
// //
@ -32,12 +32,14 @@ mut:
sem Semaphore // This blocks wait() until tast_countreleased by add() sem Semaphore // This blocks wait() until tast_countreleased by add()
} }
// new_waitgroup creates a new WaitGroup.
pub fn new_waitgroup() &WaitGroup { pub fn new_waitgroup() &WaitGroup {
mut wg := WaitGroup{} mut wg := WaitGroup{}
wg.init() wg.init()
return &wg return &wg
} }
// init initializes a WaitGroup.
pub fn (mut wg WaitGroup) init() { pub fn (mut wg WaitGroup) init() {
wg.sem.init(0) wg.sem.init(0)
} }
@ -67,12 +69,12 @@ pub fn (mut wg WaitGroup) add(delta int) {
} }
} }
// done is a convenience fn for add(-1) // done is a convenience fn for add(-1).
pub fn (mut wg WaitGroup) done() { pub fn (mut wg WaitGroup) done() {
wg.add(-1) wg.add(-1)
} }
// wait blocks until all tasks are done (task count becomes zero) // wait blocks until all tasks are done (task count becomes zero).
pub fn (mut wg WaitGroup) wait() { pub fn (mut wg WaitGroup) wait() {
nrjobs := int(C.atomic_load_u32(&wg.task_count)) nrjobs := int(C.atomic_load_u32(&wg.task_count))
if nrjobs == 0 { if nrjobs == 0 {