system.thread - Palamecia/mint GitHub Wiki

Module

load system.thread

This module provides classes and functions to create multi-threaded applications.

Packages

Enums

System.Mutex.Type

This enum describe the behavior of the mutex.

Constant Value Description
Normal 0 Normal mutex must be unlocked before an other lock is performed. If the threa...
Recursive 1 Recursive mutex can be locked recursively by the same thread but must be unlo...

System.Promise.State

This enum describes the state of the promise.

Constant Value Description
Fullfiled 1 The operation was completed successfully.
Pending 0 Initial state, neither fulfilled nor rejected.
Rejected 2 The operation failed.

Classes

System.Future

This class represents the result of an asynchronous operation.

Members

Modifiers Member Description
+ const () Takes the associated function's result and returns it. If the result is not y...
- final d_ptr Object data.
- final % function Internal associated function.
- @ g_lib Global library handle.
+ const get Takes the associated function's result and returns it. If the result is not y...
+ const isValid Returns true if the object thread have been started and a result is or will...
+ const new Creates a new future for the result of the func function.
- final object Internal associated object.
+ const start Starts the associated function in a new thread. The parameters passed to this...
+ const wait Waits until the associated function completion or until time milliseconds h...

System.Mutex

This class protect an object, data structure or section of code so that only one thread can access it at a time. The purpose of this class is to be used with instances of System.Thread to synchronize two or more threads.

The LockGuard class can be used to ensure that the mutex is correctly locked and unlocked by a section of code.

Members

Modifiers Member Description
+ enum Type This enum describe the behavior of the mutex.
- final d_ptr Object data.
- @ g_lib Global library handle.
+ const getType Returns the behavior of this mutex.
+ const lock Locks the mutex. If another thread has locked the mutex then this call will b...
+ const new Creates a new mutex. The type parameter describe the behavior of the mutex ...
+ const tryLock Attempts to lock the mutex. This function returns true if the lock was obta...
+ const unlock Unlocks the mutex. Attempting to unlock a mutex in a different thread to the ...

System.Promise

This class represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

Instances of this class are meant to be used only once. Once created, the execution of the asynchronous operation is automaticaly started. Other operations can be peroformed on completion using then.

Example:

System.Promise(def (a, b) {
    return a + b
}, i, j).then(def (r) {
    return r + 5
}).then(def (r) {
    print { 'The result is %d\n' % (r / 2)}
})

Members

Modifiers Member Description
- enum State This enum describes the state of the promise.
- final const continuation Performs the resolution of a chained promise. If self is in the System.Prom...
+ const except This method is equivalent to then but only provides function to be called on ...
+ const finally This method is equivalent to then but provides the same function to be called...
- final future Internal future.
- final const getValue Returns the resulting value of the asynchronous operation.
+ const new Creates a new promise that will execute the func function with the given pa...
- final const resolve Performs the resolution of the promise's function. Once resolved, the promise...
- final % state Internal state.
+ const then Chains a new promise to be executed once the current result is known. The o...
- final value Internal value.

System.Thread

This class manages one thread of control within the script. A thread can be associated with any function or object methode that will be executed alongside the other threads by calling the start method. Once started, the thread will continue until the end of the function.

This type is not copyable

Members

Modifiers Member Description
- const clone Disable object copy.
+ @ const current Returns an instance of System.Thread for the current thread.
+ const delete Waites the end of the called function before object destruction.
- final function Internal associated function.
- @ g_lib Global library handle.
+ const getId Returns the thread-id of the object thread or none if not started.
- final id Internal thread id.
+ const isJoinable Returns true if the thread is joinable; otherwise returs false.
+ const isRunning Returns true if the thread is running; otherwise returs false.
+ const join Waites for the thread to finish.
+ const new Creates a new thread that will execute the func function.
- final object Internal associated object.
+ const start Starts the associated function in a new thread. The parameters passed to this...

Functions

Descriptions

System.Future.()

def (self)

Takes the associated function's result and returns it. If the result is not yet available, the method waits until the associated function completion. The associated function must have been started by a call to start otherwise the method immediately returns. After this method's call, the object will not be valid anymore.

System.Future.d_ptr

null

Object data.

System.Future.function

null

Internal associated function.

System.Future.g_lib

lib ('libmint-system')

Global library handle.

System.Future.get

def (self)

Takes the associated function's result and returns it. If the result is not yet available, the method waits until the associated function completion. The associated function must have been started by a call to start otherwise the method immediately returns. After this method's call, the object will not be valid anymore.

System.Future.isValid

def (self)

Returns true if the object thread have been started and a result is or will be available; otherwise returns false.

System.Future.new

def (self, % func)

Creates a new future for the result of the func function.

def (self, object, % member)

Creates a new future for the result of the member methode of object.

System.Future.object

none

Internal associated object.

System.Future.start

def (self, ...)

Starts the associated function in a new thread. The parameters passed to this method are forwarded to the called function.

Returns true if the thread was effectively started; otherwise returs false. The object must not be already running a joinable thread.

System.Future.wait

def (self, time)

Waits until the associated function completion or until time milliseconds have passed. Returns true if the associated function was completed or false on timeout. The associated function must have been started by a call to start otherwise the method immediately returns false.

def (self)

Waits until the associated function completion. The associated function must have been started by a call to start otherwise the method immediately returns.

System.Mutex.Type.Normal

0

Normal mutex must be unlocked before an other lock is performed. If the thread try to relock an already locked normal mutex, the mutex will deadlock.

System.Mutex.Type.Recursive

1

Recursive mutex can be locked recursively by the same thread but must be unlocked the same amount of times to allow other threads to lock it.

System.Mutex.d_ptr

null

Object data.

System.Mutex.g_lib

lib ('libmint-system')

Global library handle.

System.Mutex.getType

def (const self)

Returns the behavior of this mutex.

System.Mutex.lock

def (self)

Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.

If the mutex use the System.Mutex.Type.Recursive behavior, this method can be called multiple times in the same thread; otherwise multiple calls will cause a dead-lock.

System.Mutex.new

def (self, type = System.Mutex.Type.Normal)

Creates a new mutex. The type parameter describe the behavior of the mutex an must be an instance of System.Mutex.Type.

System.Mutex.tryLock

def (self)

Attempts to lock the mutex. This function returns true if the lock was obtained; otherwise it returns false.

System.Mutex.unlock

def (self)

Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.

System.Promise.State.Fullfiled

1

The operation was completed successfully.

System.Promise.State.Pending

0

Initial state, neither fulfilled nor rejected.

System.Promise.State.Rejected

2

The operation failed.

System.Promise.continuation

def (self, % onFulfilled, % onRejected)

Performs the resolution of a chained promise. If self is in the System.Promise.State.Fullfiled state, the onFulfilled parameter is called with the promise's result; if self is in the System.Promise.State.Rejected state, the onRejected parameter is called with the promise's error. If the required parameter is set to none, the value (result or error) is returned; otherwise the returned value is the result of the function call.

System.Promise.except

def (self, % onRejected)

This method is equivalent to then but only provides function to be called on error.

System.Promise.finally

def (self, % onComplet)

This method is equivalent to then but provides the same function to be called on success or error.

System.Promise.future

null

Internal future.

System.Promise.getValue

def (self)

Returns the resulting value of the asynchronous operation.

System.Promise.new

def (self, % func, ...)

Creates a new promise that will execute the func function with the given parameters asynchronously. If the execution of the function can not be started; none is returned.

System.Promise.resolve

def (self, % func, ...)

Performs the resolution of the promise's function. Once resolved, the promise will be either in the System.Promise.State.Fullfiled state and value will contains the function's result, or in the System.Promise.State.Rejected state and value will contains the function's exception.

System.Promise.state

null

Internal state.

System.Promise.then

def (self, % onFulfilled, % onRejected = none)

Chains a new promise to be executed once the current result is known.

The onFulfilled parameter must be a function taking one parameter that will be called with the promise's result on success (i.e. the value was obtained with return).

The onRejected parameter can be a function taking one parameter that will be called with the promise's result on error (i.e. the value was obtained with raise).

The created promise is returned by the method to be chained.

System.Promise.value

none

Internal value.

System.Thread.clone

none

Disable object copy.

System.Thread.current

def ()

Returns an instance of System.Thread for the current thread.

System.Thread.delete

def (self)

Waites the end of the called function before object destruction.

System.Thread.function

null

Internal associated function.

System.Thread.g_lib

lib ('libmint-system')

Global library handle.

System.Thread.getId

def (const self)

Returns the thread-id of the object thread or none if not started.

System.Thread.id

none

Internal thread id.

System.Thread.isJoinable

def (const self)

Returns true if the thread is joinable; otherwise returs false.

System.Thread.isRunning

def (const self)

Returns true if the thread is running; otherwise returs false.

System.Thread.join

def (self)

Waites for the thread to finish.

System.Thread.new

def (self, % func)

Creates a new thread that will execute the func function.

def (self, object, % member)

Creates a new thread that will execute the member methode of object.

System.Thread.object

none

Internal associated object.

System.Thread.start

def (self, ...)

Starts the associated function in a new thread. The parameters passed to this method are forwarded to the called function.

Returns true if the thread was effectively started; otherwise returs false. The object must not be already running a joinable thread.

System.async

def (% func, ...)

Returns an instance of System.Future for the result of the given func function. The extra parameters passed to this function are forwarded to the called function. The future object is started by this function. If the future failed to start, none is returned instead.

System.asyncMethod

def (self, % func, ...)

Returns an instance of System.Future for the result of the given func method called on the self object. The extra parameters passed to this function are forwarded to the called method. The future object is started by this function. If the future failed to start, none is returned instead.

System.sleep

def (const time)

Forces the current thread to sleep for time milliseconds.

System.wait

def ()

Yields execution of the current thread to another runnable thread, if any. Note that the operating system decides to which thread to switch.