Module sync stdlib

sync
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
3
Imported by:
16
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imports

Overview

sync provides cross platform handling of concurrency primitives.

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

#fn channel_select

fn channel_select(mut channels &[]&Channel, dir []Direction, mut objrefs &[]voidptr, timeout time.Duration) int

#fn new_rwmutex

Windows
fn new_rwmutex() &RwMutex

#fn new_rwmutex

fn new_rwmutex() &RwMutex

#fn new_rwmutex

fn new_rwmutex() &RwMutex

#fn new_semaphore_init

Windows
fn new_semaphore_init(n u32) &Semaphore

#fn new_semaphore_init

fn new_semaphore_init(n u32) &Semaphore

#fn new_semaphore_init

fn new_semaphore_init(n u32) &Semaphore

#fn new_waitgroup

fn new_waitgroup() &WaitGroup

#fn thread_id

Windows
fn thread_id() u64

#fn thread_id

fn thread_id() u64

Structs

#struct Channel

pub struct Channel {
	ringbuf   &u8 = unsafe { nil } // queue for buffered channels
	statusbuf &u8 = unsafe { nil } // flags to synchronize write/read in ringbuf
	objsize   u32
mut: // atomic
	writesem           Semaphore // to wake thread that wanted to write, but buffer was full
	readsem            Semaphore // to wake thread that wanted to read, but buffer was empty
	writesem_im        Semaphore
	readsem_im         Semaphore
	write_adr          C.atomic_uintptr_t // if != NULL the next obj can be written here without wait
	read_adr           C.atomic_uintptr_t // if != NULL an obj can be read from here without wait
	adr_read           C.atomic_uintptr_t // used to identify origin of writesem
	adr_written        C.atomic_uintptr_t // used to identify origin of readsem
	write_free         u32 // for queue state
	read_avail         u32
	buf_elem_write_idx u32
	buf_elem_read_idx  u32
	// for select
	write_subscriber &Subscription = unsafe { nil }
	read_subscriber  &Subscription = unsafe { nil }
	write_sub_mtx    u16
	read_sub_mtx     u16
	closed           u16
pub:
	cap u32 // queue length in #objects
}

#fn new_channel[T]

fn new_channel[T](n u32) &Channel

#fn (&Channel) auto_str

fn (ch &Channel) auto_str(typename string) string

#fn (&Channel) close

fn (mut ch &Channel) close()

#fn (&Channel) len

inline
fn (mut ch &Channel) len() int

#fn (&Channel) closed

inline
fn (mut ch &Channel) closed() bool

#fn (&Channel) push

inline
fn (mut ch &Channel) push(src voidptr)

#fn (&Channel) try_push

inline
fn (mut ch &Channel) try_push(src voidptr) ChanState

#fn (&Channel) try_push_priv

fn (mut ch &Channel) try_push_priv(src voidptr, no_block bool) ChanState

#fn (&Channel) pop

inline
fn (mut ch &Channel) pop(dest voidptr) bool

#fn (&Channel) try_pop

inline
fn (mut ch &Channel) try_pop(dest voidptr) ChanState

#fn (&Channel) try_pop_priv

fn (mut ch &Channel) try_pop_priv(dest voidptr, no_block bool) ChanState

#struct ManyTimes

pub struct ManyTimes {
mut:
	m RwMutex
pub:
	times u64 = 1
	count u64
}

#fn new_many_times

fn new_many_times(times u64) &ManyTimes

new_many_times return a new ManyTimes struct.

#fn (&ManyTimes) do

fn (mut m &ManyTimes) do(f fn ())

do execute the function only setting times.

#fn (&ManyTimes) do_slow

fn (mut m &ManyTimes) do_slow(f fn ())

#struct Once

pub struct Once {
mut:
	m RwMutex
pub:
	count u64
}

#fn new_once

fn new_once() &Once

new_once return a new Once struct.

#fn (&Once) do

fn (mut o &Once) do(f fn ())

do executes the function f() only once

#fn (&Once) do_slow

fn (mut o &Once) do_slow(f fn ())

#fn (&Once) do_with_param

fn (mut o &Once) do_with_param(f fn (voidptr), param voidptr)

#fn (&Once) do_slow_with_param

fn (mut o &Once) do_slow_with_param(f fn (voidptr), param voidptr)

#struct Mutex

heap
pub struct Mutex {
	mutex C.pthread_mutex_t
}

[init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.

#fn new_mutex

fn new_mutex() &Mutex

#fn (&Mutex) init

fn (mut m &Mutex) init()

#fn (&Mutex) @lock

fn (mut m &Mutex) @lock()

@lock(), for manual mutex handling, since lock is a keyword

#fn (&Mutex) unlock

fn (mut m &Mutex) unlock()

#fn (&Mutex) destroy

Windows
fn (mut m &Mutex) destroy()

#struct RwMutex

heap
pub struct RwMutex {
	mutex C.pthread_rwlock_t
}

#fn (&RwMutex) init

fn (mut m &RwMutex) init()

#fn (&RwMutex) @rlock

fn (mut m &RwMutex) @rlock()

RwMutex has separate read- and write locks

#fn (&RwMutex) @lock

fn (mut m &RwMutex) @lock()

#fn (&RwMutex) runlock

fn (mut m &RwMutex) runlock()

Windows SRWLocks have different function to unlock So provide two functions here, too, to have a common interface

#fn (&RwMutex) unlock

fn (mut m &RwMutex) unlock()

#struct Semaphore

heap
pub struct Semaphore {
	mtx  C.pthread_mutex_t
	cond C.pthread_cond_t
mut:
	count u32
}

#fn new_semaphore

inline
fn new_semaphore() &Semaphore

#fn (&Semaphore) init

fn (mut sem &Semaphore) init(n u32)

#fn (&Semaphore) post

fn (mut sem &Semaphore) post()

#fn (&Semaphore) wait

fn (mut sem &Semaphore) wait()

#fn (&Semaphore) try_wait

fn (mut sem &Semaphore) try_wait() bool

#fn (&Semaphore) timed_wait

fn (mut sem &Semaphore) timed_wait(timeout time.Duration) bool

#fn (&Semaphore) destroy

fn (mut sem &Semaphore) destroy()

#struct WaitGroup

heap
pub struct WaitGroup {
mut:
	task_count u32       // current task count - reading/writing should be atomic
	wait_count u32       // current wait count - reading/writing should be atomic
	sem        Semaphore // This blocks wait() until tast_countreleased by add()
}

WaitGroup Do not copy an instance of WaitGroup, use a ref instead.

usage: in main thread:

wg := sync.new_waitgroup() wg.add(nr_jobs)before starting jobs withgo ... wg.wait()` to wait for all jobs to have finished

in each parallel job:

wg.done() when finished

[init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function.

#fn (&WaitGroup) init

fn (mut wg &WaitGroup) init()

#fn (&WaitGroup) add

fn (mut wg &WaitGroup) add(delta int)

add increments (+ve delta) or decrements (-ve delta) task count by delta and unblocks any wait() calls if task count becomes zero.

add panics if task count drops below zero.

#fn (&WaitGroup) done

fn (mut wg &WaitGroup) done()

done is a convenience fn for add(-1)

#fn (&WaitGroup) wait

fn (mut wg &WaitGroup) wait()

wait blocks until all tasks are done (task count becomes zero)

Interfaces

This section is empty.

Enums

This section is empty.