Skip to content

sync #


sync provides cross platform handling of concurrency primitives.

fn channel_select #

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

Wait timeout on any of channels[i] until one of them can push (is_push[i] = true) or pop (is_push[i] = false) object referenced by objrefs[i]. timeout = time.infinite means wait unlimited time. timeout <= 0 means return immediately if no transaction can be performed without waiting.
return value: the index of the channel on which a transaction has taken place -1 if waiting for a transaction has exceeded timeout -2 if all channels are closed

fn new_channel #

fn new_channel<T>(n u32) &Channel

fn new_many_times #

fn new_many_times(times u64) &ManyTimes

new_many_times return a new ManyTimes struct.

fn new_mutex #

fn new_mutex() &Mutex

fn new_once #

fn new_once() &Once

new_once return a new Once struct.

fn new_rwmutex #

fn new_rwmutex() &RwMutex

fn new_semaphore #

fn new_semaphore() &Semaphore

fn new_semaphore_init #

fn new_semaphore_init(n u32) &Semaphore

fn new_waitgroup #

fn new_waitgroup() &WaitGroup

fn thread_id #

fn thread_id() u64

thread_id returns a unique identifier for the caller thread.
All currently running threads in the same process, will have different thread identifiers.
Note: if a thread finishes, and another starts, the identifier of the old thread may be reused for the newly started thread.
In other words, thread IDs are guaranteed to be unique only within a process.
A thread ID may be reused after a terminated thread has been joined (with t.wait()), or when the thread has terminated.

fn (Channel) auto_str #

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

fn (Channel) close #

fn (mut ch Channel) close()

fn (Channel) len #

fn (mut ch Channel) len() int

fn (Channel) closed #

fn (mut ch Channel) closed() bool

fn (Channel) push #

fn (mut ch Channel) push(src voidptr)

fn (Channel) try_push #

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

fn (Channel) pop #

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

fn (Channel) try_pop #

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

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

try_wait() should return as fast as possible so error handling is only done when debugging

fn (Semaphore) timed_wait #

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

fn (Semaphore) destroy #

fn (sem Semaphore) destroy()

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)

struct ManyTimes #

struct ManyTimes {
	m RwMutex
	times u64 = 1
	count u64

fn (ManyTimes) do #

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

do execute the function only setting times.

struct Mutex #

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 (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()

struct Once #

struct Once {
	m RwMutex
	count u64

fn (Once) do #

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

do executes the function f() only once

fn (Once) do_with_param #

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

do_with_param executes f(param) only once` This method can be used as a workaround for passing closures to on Windows (they are not implemented there yet) - just pass your data explicitly.
i.e. instead of: [mut o] () {
``` ... you can use:
   once.do_with_param(fn (mut o One) {
   }, o)

struct RwMutex #

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()