Last update Sun Feb 26 16:21:16 2006
std
 std.atomic
 std.exception
 std.intrinsic
 std.memory
 std.stdarg
 std.thread

std.thread

The thread module provides support for thread creation and management.

Design Issues:

One design goal of Ares is to avoid forcing the use of a particular programming style, so long as allowing such flexibility does not compromise the overall API design. This goal was realized here by allowing threads to be created in the familiar C style (ie. by composition), or by derivation, similar to the Java style. Composition is further supported by virtue of the thread local storage facility, which allows for thread local data to be stored by the main thread as well as by user threads.

Future Directions:

Support for lightwewight user threads is a long-term consideration, though the design of this module is largely settled for now.

$(DDOC_MODULE_MEMBERS
class ThreadError: object.Exception;
All exceptions thrown from this module derive from this class.

class Thread;
$(DDOC_DECL_DD This class encapsulates all threading functionality for the D programming language. As thread manipulation is a required facility for garbage collection, all user threads should derive from this class, and instances of this class should never be explicitly deleted. A new thread may be created using either derivation or composition, as in the following example.

Example:
 class DerivedThread : Thread
 {
protected:
void run() { printf( "Derived thread running.\n" ); } }

void threadFunc() { printf( "Ccomposed thread running!\n" ); }

// create instances of each type Thread derived = new DerivedThread(); Thread composed = new Thread( &threadFunc );

// start both threads derived.start(); composed.start();

// wait for the threads to complete derived.join(); composed.join();

 


this();
Initializes a thread object which has no associated executable function.

this(void(* fn)());
Initialized a thread object which is associated with a static D function.

Params:
void(* fn)() The thread function.

this(void delegate() dg);
Initializes a thread object which is associated with a dynamic D function.

Params:
void delegate() dg The thread function.

final void start();
Starts the thread with run() as the target method. The default behavior of this is to run the function or delegate passed upon construction.

In:
This routine may only be called once per thread instance.

Throws:
ThreadError if the thread fails to start.

final void join();
Waits for this thread to complete.

Throws:
ThreadError if the operation fails.

final char[] name();
Gets the user-readable label for this thread.

Returns:
The name of this thread.

final void name(char[] n);
Sets the user-readable label for this thread.

Params:
char[] n The new name for this thread.

final bool isRunning();
Tests whether this thread is running. This function should be callable from anywhere within the application without a risk of deadlock.

Returns:
true if the thread is running, false if not.

final void suspend();
Suspends thread execution and loads stack and register data for use by the suspending thread. Registers should be stored in the order they would typically appear when pushed onto the execution stack. This function is reentrant, but resume must be called once for each call to suspend.

Throws:
ThreadError if the thread is not currently running or if the thread can not be suspended.

final void resume();
Resumes thread execution and frees stack and register data. This function is reentrant and must be called once for every call to suspend before the thread is resumed.

Throws:
ThreadError if the thread is not currently running or if the thread can not be resumed.

final void* stackTop();
Should be preceded by a call to suspend.

Returns:
A pointer to the logical top of the execution stack for this thread. Note that on some operating systems the address may be less than the address returned by stackBottom.

final void* stackBottom();
Should be preceded by a call to suspend.

Returns:
A pointer to the logical bottom of the execution stack for this thread. Note that on some operating systems the address may be greater than the address returned by stackTop.

final void* regFirst();
Should be preceded by a call to suspend.

Returns:
A pointer to the beginning of an inclusive range of registers stored by suspend. For example, if a system has registers X, Y, Z, and they are typically pushed onto the stack in that order, then this function will point to the value of X on systems where the stack grows up, or Z on systems where the stack grows down.

final void* regLast();
Should be preceded by a call to suspend.

Returns:
A pointer to the end of an inclusive range of registers stored by suspend. For example, if a system has registers X, Y, Z, and they are typically pushed onto the stack in that order, then this function will point to the value of Z on systems where the stack grows up, or X on systems where the stack grows down.

static void sleep(uint milliseconds);
Suspends the calling thread for at least the supplied time.

Params:
uint milliseconds The minimum duration the calling thread should be suspended.

static void yield();
Forces a context switch to occur away from the calling thread.

static Thread getThis();
The calling thread.

Returns:
The thread object representing the calling thread. The result of deleting this object is undefined.

static Thread[] getAll();
This function is not intended to be used by the garbage collector, so memory allocation is allowed.

Returns:
An array containing references to all threads currently being tracked by the system. The result of deleting any contained objects is undefined.

static int opApply(int delegate(inout Thread) dg);
Operates on all threads currently tracked by the system.

static void suspendAll();
Calls suspend on all running threads.

static void resumeAll();
Calls resume on all running threads.

const uint LOCAL_MAX;
Indicates the number of local storage pointers available at program startup. It is recommended that this number be at least 64.

static uint createLocal();
Reserves a local storage pointer for use and initializes this location to null for all running threads.

Returns:
A key representing the array offset of this memory location.

static void deleteLocal(uint key);
Marks the supplied key as available and sets the associated location to null for all running threads. It is assumed that any key passed to this function is valid. The result of calling this function for a key which is still in use is undefined.

Params:
uint key The key to delete.

static void* getLocal(uint key);
Gets the data associated with the supplied key value. It is assumed that any key passed to this function is valid.

Params:
uint key The location which holds the desired data.

Returns:
The data associated with the supplied key.

static void* setLocal(uint key, void* val);
Stores the supplied value in the specified location. It is assumed that any key passed to this function is valid.

Params:
uint key The location to store the supplied data.
void* val The data to store.

Returns:
A copy of the data which has just been stored.

protected void run();
This is the entry point for the newly invoked thread. Default behavior is to invoke the function or delegate passed on object construction. This function may be overridden to create custom thread objects via subclassing.



boolC multiThreaded();
This function is used to determine whether the the process is multi-threaded. Optimizations may only be performed on this value if the programmer can guarantee that no path from the enclosed code will start a thread.

Returns:
True if Thread.start() has been called in this process.

voidC suspendAllThreads();
Identical to Thread.suspendAll().

voidC resumeAllThreads();
Identical to Thread.resumeAll().

voidC scanAllThreads(void delegate(void*, void*) fn);
The main entry point for garbage collection. The supplied delegate will be passed ranges representing both stack and register values.

Params:
void delegate(void*, void*) fn The scanner function. It should scan from p1 through p2 - 1.

class ThreadGroup;
This class is intended to simplify certain common programming techniques.

Thread create(void(* fn)());
Creates and starts a new Thread object that executes fn and adds it to the list of tracked threads.

Params:
void(* fn)() The thread function.

Returns:
A reference to the newly created thread.

Thread create(void delegate() dg);
Creates and stats a new Thread object that executes dg and adds it to the list of tracked threads.

Params:
void delegate() dg The thread function.

Returns:
A reference to the newly created thread.

void add(Thread t);
Add t to the list of tracked threads if it is not already being tracked.

Params:

In:
assert( t );

void remove(Thread t);
Removes t from the list of tracked threads. No operation will be performed if t is not currently being tracked by this object.

Params:

In:
assert( t );

int opApply(int delegate(inout Thread) dg);
Operates on all threads currently tracked by this object.

void joinAll();
Iteratively joins all tracked threads. This function will block add, remove, and opApply until it completes.