Skip to content

Commit 6b45931

Browse files
authored
sync: improve documentation (vlang#24799)
1 parent 7039081 commit 6b45931

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

vlib/sync/common_mutex.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module sync
22

3-
// str returns a string representation of the Mutex pointer
3+
// str returns a string representation of the Mutex pointer.
44
pub fn (m &Mutex) str() string {
55
return 'Mutex(${voidptr(m)})'
66
}
77

8-
// str returns a string representation of the RwMutex pointer
8+
// str returns a string representation of the RwMutex pointer.
99
pub fn (m &RwMutex) str() string {
1010
return 'RwMutex(${voidptr(m)})'
1111
}

vlib/sync/cond.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn new_cond(m &Mutex) &Cond {
2020
}
2121
}
2222

23-
// wait waits for condition notification
23+
// wait waits for condition notification.
2424
// NOTE: Spurious wakeups are possible; always use in a loop:
2525
// mutex.lock()
2626
// for !condition {
@@ -56,7 +56,7 @@ pub fn (mut c Cond) wait() {
5656
c.mutex.lock()
5757
}
5858

59-
// signal wakes one waiting thread
59+
// signal wakes one waiting thread.
6060
@[direct_array_access]
6161
pub fn (mut c Cond) signal() {
6262
c.inner_mutex.lock()
@@ -71,7 +71,7 @@ pub fn (mut c Cond) signal() {
7171
}
7272
}
7373

74-
// broadcast wakes all waiting threads
74+
// broadcast wakes all waiting threads.
7575
@[direct_array_access]
7676
pub fn (mut c Cond) broadcast() {
7777
c.inner_mutex.lock()

vlib/sync/once.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn new_once() &Once {
1616
return once
1717
}
1818

19-
// do executes the function `f()` only once
19+
// do executes the function `f()` only once.
2020
pub fn (mut o Once) do(f fn ()) {
2121
if stdatomic.load_u64(&o.count) < 1 {
2222
o.do_slow(f)
@@ -32,7 +32,7 @@ fn (mut o Once) do_slow(f fn ()) {
3232
o.m.unlock()
3333
}
3434

35-
// do_with_param executes `f(param)` only once`
35+
// do_with_param executes `f(param)` only once.
3636
// This method can be used as a workaround for passing closures to once.do/1 on Windows
3737
// (they are not implemented there yet) - just pass your data explicitly.
3838
// i.e. instead of:
@@ -49,6 +49,7 @@ fn (mut o Once) do_slow(f fn ()) {
4949
// }, o)
5050
// ```
5151

52+
// do_with_param executes the function `f()` with parameter `param` only once.
5253
pub fn (mut o Once) do_with_param(f fn (voidptr), param voidptr) {
5354
if stdatomic.load_u64(&o.count) < 1 {
5455
o.do_slow_with_param(f, param)

vlib/sync/sync_default.c.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub fn (mut sem Semaphore) init(n u32) {
215215
// post increases/unlocks the counter of the semaphore by 1.
216216
// If the resulting counter value is > 0, and if there is another thread waiting
217217
// on the semaphore, the waiting thread will decrement the counter by 1
218-
// (locking the semaphore), and then will continue running. See also .wait() .
218+
// (locking the semaphore), and then will continue running. See also .wait().
219219
@[inline]
220220
pub fn (mut sem Semaphore) post() {
221221
C.sem_post(&sem.sem)
@@ -225,7 +225,7 @@ pub fn (mut sem Semaphore) post() {
225225
// It it was not positive, it will waits for the semaphore count to reach a positive number.
226226
// When that happens, it will decrease the semaphore count (lock the semaphore), and will return.
227227
// In effect, it allows you to block threads, until the semaphore, is posted by another thread.
228-
// See also .post() .
228+
// See also .post().
229229
pub fn (mut sem Semaphore) wait() {
230230
for {
231231
if C.sem_wait(&sem.sem) == 0 {
@@ -246,7 +246,7 @@ pub fn (mut sem Semaphore) wait() {
246246
// try_wait tries to decrease the semaphore count by 1, if it was positive.
247247
// If it succeeds in that, it returns true, otherwise it returns false.
248248
// try_wait should return as fast as possible so error handling is only
249-
// done when debugging
249+
// done when debugging.
250250
pub fn (mut sem Semaphore) try_wait() bool {
251251
$if !debug {
252252
return C.sem_trywait(&sem.sem) == 0

vlib/sync/waitgroup.c.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool
1616
// Do not copy an instance of WaitGroup, use a ref instead.
1717
//
1818
// usage: in main thread:
19-
// `wg := sync.new_waitgroup()
19+
// `wg := sync.new_waitgroup()`
2020
// `wg.add(nr_jobs)` before starting jobs with `go ...`
2121
// `wg.wait()` to wait for all jobs to have finished
2222
//
@@ -32,12 +32,14 @@ mut:
3232
sem Semaphore // This blocks wait() until tast_countreleased by add()
3333
}
3434

35+
// new_waitgroup creates a new WaitGroup.
3536
pub fn new_waitgroup() &WaitGroup {
3637
mut wg := WaitGroup{}
3738
wg.init()
3839
return &wg
3940
}
4041

42+
// init initializes a WaitGroup.
4143
pub fn (mut wg WaitGroup) init() {
4244
wg.sem.init(0)
4345
}
@@ -67,12 +69,12 @@ pub fn (mut wg WaitGroup) add(delta int) {
6769
}
6870
}
6971

70-
// done is a convenience fn for add(-1)
72+
// done is a convenience fn for add(-1).
7173
pub fn (mut wg WaitGroup) done() {
7274
wg.add(-1)
7375
}
7476

75-
// wait blocks until all tasks are done (task count becomes zero)
77+
// wait blocks until all tasks are done (task count becomes zero).
7678
pub fn (mut wg WaitGroup) wait() {
7779
nrjobs := int(C.atomic_load_u32(&wg.task_count))
7880
if nrjobs == 0 {

0 commit comments

Comments
 (0)