Documentation
¶
Overview ¶
Package threads provides SRFI-18 multithreading primitives.
Threads ¶
- make-thread, thread?
- thread-name, thread-specific, thread-specific-set!
- thread-start!, thread-yield!, thread-sleep!
- thread-terminate!, thread-join!
- current-thread
- thread-state (not SRFI-18; follows Gambit)
Mutexes ¶
- make-mutex, mutex?
- mutex-name, mutex-specific, mutex-specific-set!
- mutex-state
- mutex-lock!, mutex-unlock!
Condition Variables ¶
- make-condition-variable, condition-variable?
- condition-variable-name
- condition-variable-specific, condition-variable-specific-set!
- condition-variable-signal!, condition-variable-broadcast!
Time ¶
- current-time, time?, time->seconds, seconds->time
Exceptions ¶
- join-timeout-exception?, terminated-thread-exception?
- abandoned-mutex-exception?
- uncaught-exception?, uncaught-exception-reason
Use Extension or AddToRegistry to register all primitives.
Index ¶
- Variables
- func PrimCurrentThread(mc machine.CallContext) error
- func PrimCurrentTime(mc machine.CallContext) error
- func PrimMakeConditionVariable(mc machine.CallContext) error
- func PrimMakeMutex(mc machine.CallContext) error
- func PrimMakeThread(cc machine.CallContext) error
- func PrimMutexLock(mc machine.CallContext) error
- func PrimMutexUnlock(mc machine.CallContext) error
- func PrimSecondsToTime(mc machine.CallContext) error
- func PrimThreadJoin(mc machine.CallContext) error
- func PrimThreadSleep(mc machine.CallContext) error
- func PrimThreadStart(mc machine.CallContext) error
- func PrimThreadYield(mc machine.CallContext) error
Constants ¶
This section is empty.
Variables ¶
var AddToRegistry = Builder.AddToRegistry
AddToRegistry registers all threading primitives.
var Builder = registry.NewRegistryBuilder(addThreads, addMutexes, addConditionVariables, addTime)
Builder aggregates all threading registration functions.
var Extension = registry.NewDescribedExtension("threads", "Concurrency: SRFI-18 threads, mutexes, condition variables, time objects.", AddToRegistry)
Extension is the threads extension.
var PrimAbandonedMutexExceptionQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.AbandonedMutexException) return ok })
PrimAbandonedMutexExceptionQ tests whether an object is an SRFI-18 abandoned-mutex-exception (the object mutex-lock! raises when it acquires a mutex whose owner terminated while holding it). (abandoned-mutex-exception? obj) -> boolean
var PrimConditionVariableBroadcast = helpers.MakeUnarySideEffect(werr.ErrNotAConditionVariable, "condition-variable-broadcast!", func(cv *values.ConditionVariable) {
cv.Broadcast()
})
PrimConditionVariableBroadcast signals all waiting threads (condition-variable-broadcast! cv) -> void
var PrimConditionVariableName = helpers.MakeUnaryAccessor(werr.ErrNotAConditionVariable, "condition-variable-name", func(cv *values.ConditionVariable) values.Value { return values.NewString(cv.Name()) })
PrimConditionVariableName returns the condition variable's name (condition-variable-name cv) -> string
var PrimConditionVariableQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.ConditionVariable) return ok })
PrimConditionVariableQ tests if an object is a condition variable (condition-variable? obj) -> boolean
var PrimConditionVariableSignal = helpers.MakeUnarySideEffect(werr.ErrNotAConditionVariable, "condition-variable-signal!", func(cv *values.ConditionVariable) {
cv.Signal()
})
PrimConditionVariableSignal signals one waiting thread (condition-variable-signal! cv) -> void
var PrimConditionVariableSpecific = helpers.MakeUnaryAccessor(werr.ErrNotAConditionVariable, "condition-variable-specific", func(cv *values.ConditionVariable) values.Value { return values.ValueOrVoid(cv.Specific()) })
PrimConditionVariableSpecific returns the condition variable's specific field (condition-variable-specific cv) -> value
var PrimConditionVariableSpecificSet = helpers.MakeBinarySetter(werr.ErrNotAConditionVariable, "condition-variable-specific-set!", func(cv *values.ConditionVariable, val values.Value) {
cv.SetSpecific(val)
})
PrimConditionVariableSpecificSet sets the condition variable's specific field (condition-variable-specific-set! cv obj) -> void
var PrimJoinTimeoutExceptionQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.JoinTimeoutException) return ok })
PrimJoinTimeoutExceptionQ tests whether an object is an SRFI-18 join-timeout-exception (the object thread-join! raises when its timeout is reached and no timeout-val was supplied). (join-timeout-exception? obj) -> boolean
var PrimMutexName = helpers.MakeUnaryAccessor(werr.ErrNotAMutex, "mutex-name", func(mutex *values.Mutex) values.Value { return values.NewString(mutex.Name()) })
PrimMutexName returns the mutex's name (mutex-name mutex) -> string
var PrimMutexQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.Mutex) return ok })
PrimMutexQ tests if an object is a mutex (mutex? obj) -> boolean
var PrimMutexSpecific = helpers.MakeUnaryAccessor(werr.ErrNotAMutex, "mutex-specific", func(mutex *values.Mutex) values.Value { return values.ValueOrVoid(mutex.Specific()) })
PrimMutexSpecific returns the mutex's specific field (mutex-specific mutex) -> value
var PrimMutexSpecificSet = helpers.MakeBinarySetter(werr.ErrNotAMutex, "mutex-specific-set!", func(mutex *values.Mutex, val values.Value) {
mutex.SetSpecific(val)
})
PrimMutexSpecificSet sets the mutex's specific field (mutex-specific-set! mutex obj) -> void
var PrimMutexState = helpers.MakeUnaryAccessor(werr.ErrNotAMutex, "mutex-state", func(mutex *values.Mutex) values.Value {
return mutex.StateValue()
})
PrimMutexState returns the mutex's state (mutex-state mutex) -> symbol or thread
SRFI-18's four answers, all reachable:
- the owner thread — held, and a thread owns it
- 'not-owned — HELD, by a caller with no Thread object. Two ways in, both ordinary: PRIMORDIAL ownership (a top-level lock, because mutex-lock!'s default owner is mc.Thread() and the primordial context has none), or an explicit #f owner argument
- 'abandoned — the owner terminated while holding it
- 'not-abandoned — not held, and not abandoned
var PrimTerminatedThreadExceptionQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.TerminatedThreadException) return ok })
PrimTerminatedThreadExceptionQ tests whether an object is an SRFI-18 terminated-thread-exception (the object thread-join! raises when the joined thread died by thread-terminate!). (terminated-thread-exception? obj) -> boolean
var PrimThreadName = helpers.MakeUnaryAccessor(werr.ErrNotAThread, "thread-name", func(thread *values.Thread) values.Value { return values.NewString(thread.Name()) })
PrimThreadName returns the thread's name (thread-name thread) -> string
var PrimThreadQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.Thread) return ok })
PrimThreadQ tests if an object is a thread (thread? obj) -> boolean
var PrimThreadSpecific = helpers.MakeUnaryAccessor(werr.ErrNotAThread, "thread-specific", func(thread *values.Thread) values.Value { return values.ValueOrVoid(thread.Specific()) })
PrimThreadSpecific returns the thread's specific field (thread-specific thread) -> value
var PrimThreadSpecificSet = helpers.MakeBinarySetter(werr.ErrNotAThread, "thread-specific-set!", func(thread *values.Thread, val values.Value) {
thread.SetSpecific(val)
})
PrimThreadSpecificSet sets the thread's specific field (thread-specific-set! thread obj) -> void
var PrimThreadState = helpers.MakeUnaryAccessor(werr.ErrNotAThread, "thread-state", func(t *values.Thread) values.Value {
return t.StateSymbol()
})
PrimThreadState returns the thread's state as a symbol: new, runnable, blocked, or terminated. (thread-state thread) -> symbol
NOT SRFI-18 — that spec has mutex-state but no thread-state. The name and the symbol vocabulary follow Gambit's thread-state.
var PrimThreadTerminate = helpers.MakeUnarySideEffect(werr.ErrNotAThread, "thread-terminate!", func(thread *values.Thread) {
thread.Terminate()
})
PrimThreadTerminate forcefully terminates a thread (thread-terminate! thread) -> void
var PrimTimeQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.Time) return ok })
PrimTimeQ tests if an object is a time (time? obj) -> boolean
var PrimTimeToSeconds = helpers.MakeUnaryAccessor(werr.ErrNotATime, "time->seconds", func(t *values.Time) values.Value { return values.NewFloat(t.Seconds()) })
PrimTimeToSeconds converts a time to seconds (time->seconds time) -> number
var PrimUncaughtExceptionQ = helpers.MakeTypePredicate(func(o values.Value) bool { _, ok := o.(*values.UncaughtException) return ok })
PrimUncaughtExceptionQ tests whether an object is an SRFI-18 uncaught-exception (the object thread-join! raises when a joined thread died via an uncaught exception). (uncaught-exception? obj) -> boolean
var PrimUncaughtExceptionReason = helpers.MakeUnaryAccessor(werr.ErrNotAnUncaughtException, "uncaught-exception-reason", func(u *values.UncaughtException) values.Value { return values.ValueOrVoid(u.Reason) })
PrimUncaughtExceptionReason returns the original condition the joined thread raised. (uncaught-exception-reason uncaught-exception) -> value. ValueOrVoid guards a nil reason so a Go nil never reaches the value register.
Functions ¶
func PrimCurrentThread ¶
func PrimCurrentThread(mc machine.CallContext) error
PrimCurrentThread returns the current executing thread. Returns the thread object if running inside a thread, or the symbol 'primordial for the main goroutine. (current-thread) -> thread
func PrimCurrentTime ¶
func PrimCurrentTime(mc machine.CallContext) error
PrimCurrentTime returns the current time (current-time) -> time
func PrimMakeConditionVariable ¶
func PrimMakeConditionVariable(mc machine.CallContext) error
PrimMakeConditionVariable creates a new condition variable (make-condition-variable [name]) -> condition-variable
func PrimMakeMutex ¶
func PrimMakeMutex(mc machine.CallContext) error
PrimMakeMutex creates a new mutex (make-mutex [name]) -> mutex
func PrimMakeThread ¶
func PrimMakeThread(cc machine.CallContext) error
PrimMakeThread creates a new thread (make-thread thunk [name]) -> thread
func PrimMutexLock ¶
func PrimMutexLock(mc machine.CallContext) error
PrimMutexLock acquires the mutex (mutex-lock! mutex [timeout [thread]]) -> boolean Returns #t if acquired, #f if timeout. Acquiring a mutex abandoned by a terminated owner returns #t and additionally signals an abandoned-mutex exception.
func PrimMutexUnlock ¶
func PrimMutexUnlock(mc machine.CallContext) error
PrimMutexUnlock releases the mutex (mutex-unlock! mutex [condition-variable [timeout]]) -> boolean
func PrimSecondsToTime ¶
func PrimSecondsToTime(mc machine.CallContext) error
PrimSecondsToTime converts seconds to a time (seconds->time x) -> time
func PrimThreadJoin ¶
func PrimThreadJoin(mc machine.CallContext) error
PrimThreadJoin waits for a thread to terminate (thread-join! thread [timeout [timeout-val]]) -> value
func PrimThreadSleep ¶
func PrimThreadSleep(mc machine.CallContext) error
PrimThreadSleep pauses execution for a time (thread-sleep! timeout) -> void timeout can be a time object or a number (seconds)
func PrimThreadStart ¶
func PrimThreadStart(mc machine.CallContext) error
PrimThreadStart starts a thread (thread-start! thread) -> thread
A started thread is recorded against the calling engine's namespace so Engine.Close can terminate it (close.go). A thread that fails to start is not recorded.
func PrimThreadYield ¶
func PrimThreadYield(mc machine.CallContext) error
PrimThreadYield yields execution to other threads (thread-yield!) -> void
Types ¶
This section is empty.