Chapter 29
POSIX Threads: liboskit_threads.a

29.1 Introduction

This chapter describes the POSIX threads module and associated support for writing multithreaded kernels. At present, threads support is very new and not every combination of components is known to work; see Section 29.2 for a more detailed description of what has been tested. Section 29.3 describes the application program interface for the core POSIX threads module, while Section 29.5 contains a discussion of how the threads system interacts with the device driver framework.

29.2 Examples and Caveats

The sample kernels in the examples/threads directory (see Section 1.6.1), contain several sample kernels demonstrating the use of the POSIX threads module.

This small set of test programs clearly does not test every possible combination of components. A larger set of test program is in the works. In addition, not all of the thread-safe adaptors are implemented, so some components cannot be used in a multithreaded environment. For now, the POSIX threads module should be used with caution. Note that these examples are compiled and linked against the multithreaded version of the FreeBSD C library (see Section 21), rather than the minimal C library (Section 14).

29.3 POSIX Threads Reference

As with most POSIX threads implementations, this one is slightly different than others. This section briefly covers the specific interfaces, but does not describe the semantics of each interface function in great detail. The reader is advised to consult the POSIX documentation for a more complete description. All of these functions, as well as the types necessary to use them, are defined in the header file <oskit/threads/pthread.h>.

29.3.1 pthread.h: Thread constants and data structures

DESCRIPTION

This header file defines the following standard symbols.

PRIORITY_MIN:
Lowest possible thread scheduling priority.
PRIORITY_NORMAL:
Default thread scheduling priority.
PRIORITY_MAX:
Highest possible thread scheduling priority.
SCHED_FIFO:
The “first in first out” thread scheduling policy.
SCHED_RR:
The “round robin” thread scheduling policy.
PTHREAD_STACK_MIN:
The minumum allowed stack size.
PTHREAD_CREATE_JOINABLE:
Thread attribute; thread is created joinable.
PTHREAD_CREATE_DETACHED:
Thread attribute; thread is created detached.
PTHREAD_PRIO_NONE:
Mutex attribute; mutex does not do priority inheritance.
PTHREAD_PRIO_INHERIT:
Mutex attribute; mutex does priority inheritance.
PTHREAD_MUTEX_NORMAL:
Mutex attribute; normal error checking, no recursion.
PTHREAD_MUTEX_ERRORCHECK:
Mutex attribute; extra error checking, no recursion.
PTHREAD_MUTEX_RECURSIVE:
Mutex attribute; normal error checking, recursion allowed.
PTHREAD_MUTEX_DEFAULT:
Mutex attribute; normal error checking, no recursion.
PTHREAD_CANCEL_ENABLE:
Cancelation state; Cancelation state is enabled.
PTHREAD_CANCEL_DISABLE:
Cancelation state; Cancelation state is disabled.
PTHREAD_CANCEL_DEFERRED:
Cancelation type; Cancelation type deferred,
PTHREAD_CANCEL_ASYNCHRONOUS:
Cancelation type; Cancelation type is asynchronous.
PTHREAD_CANCELED:
The exit status returned by pthread_join for a canceled thread.
pthread_t:
Thread identifier type definition.
pthread_mutex_t:
Mutex type definition.
pthread_cond_t:
Condition variable type definition.
pthread_attr_t:
Thread attributes type definition.
pthread_attr_default:
Default thread attributes object.
pthread_mutexattr_t:
Mutex attributes type definition.
pthread_mutexattr_default:
Default mutex attributes object.
pthread_condattr_t:
Condition variable attributes type definition.
pthread_condattr_default:
Default condition variable attributes object.
sched_param_t:
Type definition for the pthread_setschedparam interface function.

29.3.2 pthread_init: Initialize the threads system

SYNOPSIS

#include <oskit/threads/pthread.h>

void pthread_init(int preemptible);

DESCRIPTION

This function initializes the threads system. It should be called as the first function in the application’s main program function.

When pthread_init returns, the caller is now running within the main thread, although on the same stack as when called. One or more idle threads have also been created, and are running at low priority. At this point, the application is free to use any of the pthread interface functions described in this section.

PARAMETERS
preemptible:
A boolean value specifying whether the threads system should use preemption based scheduling. When preemption based scheduling is not used, it is up to the application to yield the processor using sched_yield as necessary.

29.3.3 pthread_attr_init: Initialize a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_init(pthread_attr_t *attr);

DESCRIPTION

Initialize a thread attributes object for use with pthread_create.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_create, pthread_attr_setprio, pthread_attr_setstacksize

29.3.4 pthread_attr_setdetachstate: Set the detach state in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

DESCRIPTION

Set the thread detach state in a previously initialized threads attribute object, for use with pthread_create.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
detachstate:
Either PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
RETURNS

Returns zero on success. Returns EINVAL if detachstate is invalid.

RELATED INFORMATION

pthread_create, pthread_attr_init

29.3.5 pthread_attr_setprio: Set the priority in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setprio(pthread_attr_t *attr, int pri);

DESCRIPTION

Set the priority value in a previously initialized threads attribute object, for use with pthread_create.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
pri:
A value between PRIORITY_MIN and PRIORITY_MAX.
RETURNS

Returns zero on success. Returns EINVAL if priority is outside the range of PRIORITY_MIN to PRIORITY_MAX.

RELATED INFORMATION

pthread_create, pthread_attr_init, pthread_attr_setstacksize

29.3.6 pthread_attr_setstackaddr: Set the stack address in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);

DESCRIPTION

Set the stack address in a previously initialized threads attribute object, for use with pthread_create. The new thread will be created using the provided stack. It is necessary to call pthread_attr_setstacksize() if the size is not PTHREAD_STACK_MIN.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
stackaddr:
The address of the stack.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_create, pthread_attr_init, pthread_attr_setstacksize

29.3.7 pthread_attr_setguardsize: Set the stack guard size in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setguardsize(pthread_attr_t *attr, oskit_size_t guardsize);

DESCRIPTION

Set the stack guard size in a previously initialized threads attribute object, for use with pthread_create. This much extra space will be allocated at the end of the stack and set as a redzone to catch stack overflow. The guard size is rounded up to a multiple of the native page size. Stack guards are not created for stacks provided with pthread_attr_setstackaddr.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
guardsize:
A reasonable stack guard size.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_create, pthread_attr_init, pthread_attr_setstackaddr

29.3.8 pthread_attr_setstacksize: Set the stack size in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setstacksize(pthread_attr_t *attr, oskit_size_t stacksize);

DESCRIPTION

Set the stack size in a previously initialized threads attribute object, for use with pthread_create.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
stacksize:
A reasonable stack size.
RETURNS

Returns zero on success. Returns EINVAL if stacksize is less than PTHREAD_STACK_MIN.

RELATED INFORMATION

pthread_create, pthread_attr_init, pthread_attr_setprio

29.3.9 pthread_attr_setschedpolicy: Set the scheduling policy in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

DESCRIPTION

Set the scheduling policy in a previously initialized threads attribute object, for use with pthread_create.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
policy:
Either SCHED_FIFO or SCHED_RR.
RETURNS

Returns zero on success. Returns EINVAL if policy is invalid.

RELATED INFORMATION

pthread_create, pthread_attr_init

29.3.10 pthread_mutexattr_init: Initialize a mutex attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutexattr_init(pthread_mutexattr_t *attr);

DESCRIPTION

Initialize an mutex attributes object for use with pthread_mutex_init.

PARAMETERS
attr:
A pointer to the pthread_mutexattr_t object representing the attributes for a mutex initialization.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_init, pthread_mutex_setprotocol

29.3.11 pthread_mutexattr_setprotocol: Set the protocol attribute of a mutex attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);

DESCRIPTION

Set the protocol in a previously initialized mutex attribute object. When a mutex is created with the protocol PTHREAD_PRIO_INHERIT, threads that blocked on the mutex will result in a transfer of priority from higher to lower priority threads.

PARAMETERS
attr:
A pointer to the pthread_mutexattr_t object representing the attributes for a mutex initialization.
protocol:
Either PTHREAD_PRIO_NONE or PTHREAD_PRIO_INHERIT.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_init

29.3.12 pthread_mutexattr_settype: Set the type attribute of a mutex attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);

DESCRIPTION

Set the type in a previously initialized mutex attribute object. PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK, and PTHREAD_MUTEX_DEFAULT are equivalent. PTHREAD_MUTEX_RECURSIVE allows a mutex to be recursively locked.

PARAMETERS
attr:
A pointer to the pthread_mutexattr_t object representing the attributes for a mutex initialization.
type:
One of PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_DEFAULT, or PTHREAD_MUTEX_RECURSIVE.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_init

29.3.13 pthread_condattr_init: Initialize a condition attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_condattr_init(pthread_condattr_t *attr);

DESCRIPTION

Initialize an condition variable attributes object for use with pthread_cond_init.

PARAMETERS
attr:
A pointer to the pthread_condattr_t object representing the attributes for a condition variable initialization.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_cond_init

29.3.14 pthread_cancel: Cancel a running thread

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cancel(pthread_t tid);

DESCRIPTION

Cancel the thread specified by tid. The thread is marked for cancellation, but because of scheduling and device delays, might not be acted upon until some future time.

PARAMETERS
tid:
The thread identifier of the thread to be canceled.
RETURNS

Returns zero on success. EINVAL if tid specifies an invalid thread.

RELATED INFORMATION

pthread_create, pthread_sleep

29.3.15 pthread_cleanup_push: Push a cancellation cleanup handler routine onto the calling thread’s cancellation cleanup stack

SYNOPSIS

#include <oskit/threads/pthread.h>

void pthread_cleanup_push(void (*routine)(void *), void *arg);

DESCRIPTION

Push a cancellation cleanup handler routine onto the calling thread’s cancellation cleanup stack. When requested, the cleanup routine will be popped from the cancellation stack, and invoked with the argument arg.

PARAMETERS
routine:
The cleanup handler routine.
arg:
The argument to pass to the cleanup handler routine.
RELATED INFORMATION

pthread_cancel, pthread_cleaup_pop

29.3.16 pthread_setcancelstate: Set the cancelation state

SYNOPSIS

#include <oskit/threads/pthread.h>

void pthread_setcancelstate(int state, int *oldstate);

DESCRIPTION

Set the cancel state for the current thread, returning the old state in oldstate. Valid states are either PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE. This routine is async-cancel safe.

PARAMETERS
state:
New cancel state.
oldstate:
Location in which to place the original cancel state.
RELATED INFORMATION

pthread_cancel, pthread_setcanceltype

29.3.17 pthread_setcanceltype: Set the cancelation type

SYNOPSIS

#include <oskit/threads/pthread.h>

void pthread_setcanceltype(int type, int *oldtype);

DESCRIPTION

Set the cancel type for the current thread, returning the old type in oldtype. Valid types are either PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS. This routine is async-cancel safe.

PARAMETERS
type:
New cancel type.
oldtype:
Location in which to place the original cancel type.
RELATED INFORMATION

pthread_cancel, pthread_setcancelstate

29.3.18 pthread_testcancel: Check for a cancelation point

SYNOPSIS

#include <oskit/threads/pthread.h>

void pthread_testcancel(void);

DESCRIPTION

Test whether a cancelation is pending, and deliver the cancelation if the cancel state is PTHREAD_CANCEL_ENABLED.

RELATED INFORMATION

pthread_cancel, pthread_setcancelstate

29.3.19 pthread_cond_broadcast: Wakeup all threads waiting on a condition variable

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_broadcast(pthread_cond_t *cond);

DESCRIPTION

Wakeup all threads waiting on a condition variable.

PARAMETERS
cond:
A pointer to the condition variable object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_cond_init, pthread_cond_wait, pthread_cond_signal

29.3.20 pthread_cond_destroy: Destroy a condition variable

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_destroy(pthread_cond_t *cond);

DESCRIPTION

Destroy a condition variable object. The condition variable should be unused, with no threads waiting for it. The memory for the object is left intact; it is up to the caller to deallocate it.

PARAMETERS
cond:
A pointer to the condition variable object.
RETURNS

Returns zero on success. EINVAL if there are threads still waiting.

RELATED INFORMATION

pthread_cond_init

29.3.21 pthread_cond_init: Initialize a condition variable

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);

DESCRIPTION

Initialize a condition variable object, using the provided condition attributes object. The attributes object may be a NULL pointer, in which case pthread_condattr_default is used.

PARAMETERS
cond:
A pointer to the condition variable object.
attr:
A pointer to the condition variable attributes object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_cond_destroy

29.3.22 pthread_cond_signal: Wakeup one thread waiting on a condition variable

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_signal(pthread_cond_t *cond);

DESCRIPTION

Wakeup the highest priority thread waiting on a condition variable.

PARAMETERS
cond:
A pointer to the condition variable object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_cond_wait, pthread_cond_broadcast

29.3.23 pthread_cond_wait: Wait on a condition variable

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

DESCRIPTION

The current thread is made to wait until the condition variable is signaled or broadcast. The mutex is released prior to waiting, and reacquired before returning.

PARAMETERS
cond:
A pointer to the condition variable object.
mutex:
A pointer to the mutex object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_cond_signal, pthread_cond_broadcast, pthread_cond_timedwait

29.3.24 pthread_cond_timedwait: Wait on a condition variable with timeout

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, oskit_timespec_t *abstime);

DESCRIPTION

The current thread is made to wait until the condition variable is signaled or broadcast, or until the timeout expires. The mutex is released prior to waiting, and reacquired before returning. The timeout is given as an absolute time in the future that bounds the wait.

PARAMETERS
cond:
A pointer to the condition variable object.
mutex:
A pointer to the mutex object.
abstime:
A pointer to an oskit_timespec structure.
RETURNS

Returns zero on success. Returns ETIMEDOUT if the timeout expires.

RELATED INFORMATION

pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait

29.3.25 pthread_create: Create a new thread and start it running

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void (*function)(void *), void *arg);

DESCRIPTION

Create a new thread and schedule it to run. The thread is created using the attributes object attr, which specifies the initial priority, stack size, and detach state. If a NULL attributes object is provided, a system default attributes object is used instead, specifying that the thread is detachable, has priority PRIORITY_NORMAL, and with a reasonable stack size.

This call returns immediately, with the thread id stored in the location given by tid. This thread id should be saved if the application wishes to manipulate the thread’s state at some future time.

The new thread is scheduled to run. When the thread starts up, it will call void (*function)(void *arg).

PARAMETERS
tid:
A pointer to the location where the thread id should be stored.
attr:
A pointer to the thread creation attributes object.
function:
The initial function to call when the thread first starts.
arg:
The argument to the initial function.
RETURNS

Returns zero on success, storing the tid of the new thread into *tid.

RELATED INFORMATION

pthread_join, pthread_detach, pthread_exit

29.3.26 pthread_detach: Detach a thread from its parent

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_detach(pthread_t tid);

DESCRIPTION

The thread specified by tid is detached from its parent. If the thread has already exited, its resources are released.

PARAMETERS
tid:
The thread id of the thread being detached.
RETURNS

Returns zero on success. EINVAL if tid refers to a non-existent thread.

RELATED INFORMATION

pthread_join, pthread_create, pthread_exit

29.3.27 pthread_exit: Terminate a thread with status

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_exit(void *status);

DESCRIPTION

The current thread is terminated, with its status value made available to the parent using pthread_join.

PARAMETERS
status:
The exit status.
RETURNS

This function does not return.

RELATED INFORMATION

pthread_join, pthread_create, pthread_detach

29.3.28 pthread_join: Join with a target thread

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_join(pthread_t tid, void **status);

DESCRIPTION

The current thread indicates that it would like to join with the target thread specified by tid. If the target thread has already terminated, its exit status is provided immediately to the caller. If the target thread has not yet exited, the caller is made to wait. Once the target has exited, all of the threads waiting to join with it are woken up, and the target’s exit status provided to each.

PARAMETERS
tid:
The thread id of the thread being joined with.
status:
A pointer to a location where the target’s exit status is placed.
RETURNS

Returns zero on success, storing the target’s exit status in *status. EINVAL if tid refers to a non-existent thread.

RELATED INFORMATION

pthread_join, pthread_create, pthread_detach

29.3.29 pthread_key_create: Create a thread-specific data key

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));

DESCRIPTION

Create a thread-specific key for use with pthread_setspecific. If specified, the destructor is called on any non-NULL key/value pair when a thread exits.

PARAMETERS
key:
Address where the new key value should be stored.
destructor:
Pointer to the destructor function, which may be NULL.
RETURNS

Returns zero on success, and stores the new key value at *key. Returns EAGAIN if the are no more keys available.

RELATED INFORMATION

pthread_key_delete, pthread_setspecific, pthread_getspecific

29.3.30 pthread_key_delete: Delete a thread-specific data key

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_key_delete(pthread_key_t *key);

DESCRIPTION

Delete the thread-specific key. Attempts to use a key via pthread_setspecific or pthread_getspecific after it has been deleted is undefined.

PARAMETERS
key:
The key that should be deleted.
RETURNS

Returns zero on success. Returns EINVAL if key refers to an invalid key.

RELATED INFORMATION

pthread_key_create, pthread_setspecific, pthread_getspecific

29.3.31 pthread_setspecific: Set a thread-specific data value

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_setspecific(pthread_key_t key, const void *value);

DESCRIPTION

Associate a new thread-specific value with the specified key.

PARAMETERS
key:
The key that should be set.
value:
The new value to associate with the key.
RETURNS

Returns zero on success. Returns EINVAL if key refers to an invalid key.

RELATED INFORMATION

pthread_key_create, pthread_key_delete, pthread_getspecific

29.3.32 pthread_getspecific: Set a thread-specific data value

SYNOPSIS

#include <oskit/threads/pthread.h>

void *pthread_getspecific(pthread_key_t key);

DESCRIPTION

Get the thread-specific value associated the specified key.

PARAMETERS
key:
The key for the value that should be retrieved.
RETURNS

Returns the value of the key. Errors always return zero.

RELATED INFORMATION

pthread_key_create, pthread_key_delete, pthread_setspecific

29.3.33 pthread_mutex_init: Initialize a mutex object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutex_init(pthread_mutex_t *m, pthread_mutexattr_t *attr);

DESCRIPTION

Initialize a mutex object, using the provided mutex attributes object. The attributes object may be a NULL pointer, in which case pthread_mutexattr_default is used.

PARAMETERS
mutex:
A pointer to the mutex object.
attr:
A pointer to the mutex attributes object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock

29.3.34 pthread_mutex_destroy: Destroy a mutex object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *m);

DESCRIPTION

The mutex object is destroyed, although the memory for the object is not deallocated. The mutex must not be held.

PARAMETERS
mutex:
A pointer to the mutex object.
RETURNS

Returns zero on success. Returns EBUSY if the mutex is still held.

RELATED INFORMATION

pthread_mutex_init

29.3.35 pthread_mutex_lock: Lock a unlocked mutex object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutex_lock(pthread_mutex_t *m);

DESCRIPTION

Lock a mutex object. If the mutex is currently locked, the thread waits (is suspended) for the mutex to become available.

PARAMETERS
mutex:
A pointer to the mutex object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_init, pthread_mutex_unlock, pthread_mutex_trylock

29.3.36 pthread_mutex_trylock: Attempt to lock a unlocked mutex object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutex_trylock(pthread_mutex_t *m);

DESCRIPTION

Attempt to lock a mutex object. This function always returns immediately.

PARAMETERS
mutex:
A pointer to the mutex object.
RETURNS

Returns zero on success. Returns EBUSY if the mutex object is locked.

RELATED INFORMATION

pthread_mutex_init, pthread_mutex_unlock, pthread_mutex_lock

29.3.37 pthread_mutex_unlock: Unlock a mutex object

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *m);

DESCRIPTION

Unlock a mutex object. If there other threads waiting to acquire the mutex, the highest priority thread is woken up and granted the mutex.

PARAMETERS
mutex:
A pointer to the mutex object.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_mutex_init, pthread_mutex_trylock, pthread_mutex_lock

29.3.38 pthread_self: Return the thread identifier of the current thread

SYNOPSIS

#include <oskit/threads/pthread.h>

pthread_t pthread_self(void);

DESCRIPTION

Return the thread identifier of the current thread.

RETURNS

Returns the thread identifier.

RELATED INFORMATION

pthread_create

29.3.39 pthread_setschedparam: Set the scheduling parameters for a thread

SYNOPSIS

#include <oskit/threads/pthread.h>

int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);

DESCRIPTION

Change the scheduling parameters for a thread. The thread’s scheduling policy and priority are changed. If the change causes a thread to have a higher priority than the currently running thread, a reschedule operation is performed.

PARAMETERS
tid:
The thread identifier of the thread whose scheduling parameters should be changed.
policy:
The new scheduling policy, as defined in pthread.h
param:
A pointer to the sched_param_t object representing the new scheduling parameters.
RETURNS

Returns zero on success. EINVAL if tid specifies an invalid thread or policy specifies an invalid policy.

RELATED INFORMATION

pthread_create, sched_yield, pthread_setprio

29.3.40 pthread_sigmask: examine and change blocked signals

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <signal.h>

int pthread_sigmask(int how, const sigset_t *set, [out] sigset_t *oset);

DESCRIPTION

Examine or change the per-thread signal mask. This function operates identically to the POSIX function sigprocmask, but on the current thread.

PARAMETERS
how:
One of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK.
set:
If not a null pointer, a pointer to the new signal set.
oset:
If not a null pointer, a pointer to where the old signal set should be stored.
RETURNS

Returns zero on success. No errors are reported.

RELATED INFORMATION

pthread_kill sigprocmask

29.3.41 pthread_kill: send a signal to a thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <signal.h>

int pthread_kill(pthread_t tid, int sig);

DESCRIPTION

Send a signal to a specific thread.

PARAMETERS
tid:
The thread to send the signal to.
sig:
The signal to send.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_sigmask

29.3.42 sched_yield: Yield the processor

SYNOPSIS

#include <oskit/threads/pthread.h>

void sched_yield(void);

DESCRIPTION

The calling thread voluntarily yields the processor. The highest priority thread is chosen for execution.

RELATED INFORMATION

pthread_setprio, pthread_setschedparam

29.4 Oskit API Extensions

The following functions are extensions to the POSIX threads API, and should be considered extremely non-portable. They are included in the API as a convenience.

29.4.1 oskit_pthread_sleep: Sleep for an interval of time

SYNOPSIS

#include <oskit/threads/pthread.h>

int oskit_pthread_sleep(oskit_s64_t milliseconds);

DESCRIPTION

The calling thread is put to sleep for the number of milliseconds specified. The thread will be woken up after the elapsed time, and will return ETIMEDOUT. If the timeout is zero, the thread is put to sleep forever. The thread may be woken up early, using the oskit_pthread_wakeup function, in which case the return value is zero.

PARAMETERS
milliseconds:
The number of milliseconds the thread should sleep for.
RETURNS

Returns ETIMEDOUT if the timeout expires, or zero if the thread is woken up early.

RELATED INFORMATION

oskit_pthread_wakeup

29.4.2 oskit_pthread_wakeup: Wakeup a thread in oskit_pthread_sleep

SYNOPSIS

#include <oskit/threads/pthread.h>

int oskit_pthread_wakeup(pthread_t tid);

DESCRIPTION

Wakeup a thread that is sleeping in oskit_pthread_sleep, causing it to return from its sleep before the timeout expires.

PARAMETERS
tid:
The thread to wakeup.
RETURNS

Returns zero on success. EINVAL if tid specifies an invalid thread or the current thread.

RELATED INFORMATION

oskit_pthread_sleep

29.4.3 oskit_pthread_setprio: Change the priority of a thread

SYNOPSIS

#include <oskit/threads/pthread.h>

int oskit_pthread_setprio(pthread_t tid, int newpri);

DESCRIPTION

Change the priority of a thread. If the change causes a thread to have a higher priority than the currently running thread, a reschedule operation is performed.

PARAMETERS
tid:
The thread identifier of the thread whose priority should be changed.
newpri:
The new priority, which must be from PRIORITY_MIN to PRIORITY_MAX.
RETURNS

Returns zero on success. EINVAL if tid specifies an invalid thread or newpri specifies an invalid priority.

RELATED INFORMATION

pthread_create, sched_yield, pthread_setschedparam

29.4.4 osenv_process_lock: Lock the process lock

SYNOPSIS

#include <oskit/threads/pthread.h>

void osenv_process_lock(void);

DESCRIPTION

Attempt to lock the process lock. If the lock cannot be immediately granted, the thread is put to sleep until it can be. The process lock is provided so that the client operating system can protect the device driver framework from concurrent execution. It is expected than any entry into the device framework will first take the process lock. If the thread executing inside the device driver framework blocks by calling osenv_sleep, the process lock will be released so that another thread may enter it safely. When the thread is woken up later, it will take the process lock again before returning from the sleep.

Attempts to recursively lock the process lock will result in a panic. This is intended as a debugging measure to prevent indiscriminate nesting of components that try to take the lock.

RELATED INFORMATION

osenv_process_unlock, osenv_sleep, osenv_wakeup

29.4.5 osenv_process_unlock: Unlock the process lock

SYNOPSIS

#include <oskit/threads/pthread.h>

void osenv_process_unlock(void);

DESCRIPTION

Release the process lock. If another thread is waiting to lock the process lock, it will be woken up. The process lock is provided so that the client operating system can protect the device driver framework from concurrent execution.

RELATED INFORMATION

osenv_process_lock, osenv_sleep, osenv_wakeup

29.5 Thread-safe Adaptors

To facilitate the use of the device driver framework within a multithreaded client operating system, a number of adaptors are provided. An adaptor acts as COM interface wrapper on another COM interface. Adaptors are intended to provide thread-safety with respect to the device driver framework. The thread system is expected to provide an implementation of a process lock that is used to prevent concurrent execution inside the device driver framework. An adaptor method simply takes the process lock, calls the appropriate method in the underlying COM interface, and then releases the process lock when the method returns. If a thread blocks inside a device driver (osenv_sleep), the process lock is released at that time, allowing another thread to enter the driver set. When the original thread is woken up, it will reacquire the process lock before being allowed to return from the sleep. Thus, only one thread is allowed to operate inside the driver set at a time.

Implementationally, an adaptor is a COM interface that maintains a reference to the original, non thread-safe COM interface. Operations using the adaptor behave just like the original, invoking the corresponding method in the original. It should be noted that the query, addref, and release methods all operate on the adaptor itself. When the last reference to an adaptor is released, the reference to the underlying COM interface is released. As an example, consider the oskit_dir_t adaptor as it is used when mounting the root filesystem in a multithreaded client operating system. In order to provide a thread-safe implementation to the C library, the root directory that is passed to fs_init is first wrapped up in a thread-safe adaptor. All subsequent references to the corresponding filesystem go through the adaptor, and are thus thread-safe. A sample code fragment follows:

 #include <oskit/c/fs.h>
 #include <oskit/com/wrapper.h>
 #include <oskit/threads/pthread.h>
 
 oskit_error_t
 mountroot(oskit_dir_t *fsroot)
 {
     oskit_dir_t    *wrappedroot;
     oskit_error_t  err;
 
     rc = oskit_wrap_dir(fsroot,
                 (void (*)(void *))osenv_process_lock,
                 (void (*)(void *))osenv_process_unlock,
                 0, &wrappedroot);
     if (rc)
         return rc;
 
     /* Don't need the root anymore, the wrapper has a ref. */
     oskit_dir_release(fsroot);
 
     return fs_init(wrappedroot);
 }

The adaptor prototypes are found in <oskit/com/wrapper.h>, and have a common format. Each one takes the COM interface to be wrapped up, and returns the adaptor. Additional arguments are the process lock and unlock routines, as well as an optional cookie to be passed to the lock and unlock routines. It should be noted that the process lock is specific to the thread implementation, and thus the adaptor interface is intended to be as generic as possible. For the pthread interface, the process lock does not need a cookie value.

29.5.1 oskit_wrap_socket: Wrap an oskit_socket in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_socket(struct oskit_socket *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_socket **out);

DESCRIPTION

Create and return an oskit_socket thread-safe adaptor.

PARAMETERS
in:
The oskit_socket COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_socket adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.2 oskit_wrap_stream: Wrap an oskit_stream in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_stream(struct oskit_stream *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_stream **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_stream COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_stream adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.3 oskit_wrap_asyncio: Wrap an oskit_asyncio in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_asyncio(struct oskit_asyncio *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_asyncio **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_asyncio COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_asyncio adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.4 oskit_wrap_sockio: Wrap an oskit_sockio in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_sockio(struct oskit_sockio *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_sockio **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_sockio COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_sockio adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.5 oskit_wrap_posixio: Wrap an oskit_posixio in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_posixio(struct oskit_posixio *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_posixio **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_posixio COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_posixio adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.6 oskit_wrap_file: Wrap an oskit_file in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_file(struct oskit_file *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_file **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_file COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_file adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.7 oskit_wrap_dir: Wrap an oskit_dir in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_dir(struct oskit_dir *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_dir **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_dir COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_dir adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.8 oskit_wrap_filesystem: Wrap an oskit_filesystem in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_filesystem(struct oskit_filesystem *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_filesystem **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_filesystem COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_filesystem adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.9 oskit_wrap_openfile: Wrap an oskit_openfile in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_openfile(struct oskit_openfile *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_openfile **out);

DESCRIPTION

Create and return an oskit_dir thread-safe adaptor.

PARAMETERS
in:
The oskit_openfile COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_openfile adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.10 oskit_wrap_blkio: Wrap an oskit_blkio in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_blkio(struct oskit_blkio *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_blkio **out);

DESCRIPTION

Create and return an oskit_blkio thread-safe adaptor.

PARAMETERS
in:
The oskit_blkio COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_blkio adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.5.11 oskit_wrap_absio: Wrap an oskit_absio in a thread-safe adaptor

SYNOPSIS

#include <oskit/com/wrapper.h>

oskit_error_t oskit_wrap_absio(struct oskit_absio *in, void (*lock)(void *), void (*unlock)(void *), void *cookie, struct oskit_absio **out);

DESCRIPTION

Create and return an oskit_absio thread-safe adaptor.

PARAMETERS
in:
The oskit_absio COM interface to be wrapped.
lock:
The process lock routine.
unlock:
The process unlock routine.
cookie:
A cookie to be passed to the lock and unlock routines.
out:
The oskit_absio adaptor COM interface.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.6 InterThread Communication

This section describes the “interthread” communication primitives provided by the pthread library.

29.6.1 oskit_ipc_send: Send a message to another thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/ipc.h>

oskit_error_t oskit_ipc_send(pthread_t dst, void *msg, oskit_size_t msg_size, oskit_s32_t timeout);

DESCRIPTION

Send a message to another thread. The destination thread is specified by its pthread_t. The sending thread blocks until the receiving thread notices the message and actually initiates a receive operation for it. Control returns to the caller only when the receiver has initiated the receive.

The timeout value is currently ignored.

PARAMETERS
dst:
The pthread_t of the destination thread.
msg:
The message buffer.
msg_size:
The size of the message, in bytes.
timeout:
A timeout value. Currently ignored.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.6.2 oskit_ipc_recv: Receive a message from a specific thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/ipc.h>

oskit_error_t oskit_ipc_recv(pthread_t src, void *msg, oskit_size_t msg_size, oskit_size_t *actual, oskit_s32_t timeout);

DESCRIPTION

Receive a message from another thread. The sending thread is specified by its pthread_t. If the specified sending thread has not attempted to send a message to current thread, the thread is blocked until such time as the sender initiates a send operation to the current thread. However, if the sender is blocked trying to send a message to the current thread, the message is immediately received and the sender is woken up.

The timeout value is either zero or non-zero. A zero value means do not wait, but simply check to see if a message from the sender is pending. A non-zero value means wait forever.

PARAMETERS
src:
The pthread_t of the sending thread.
msg:
The message buffer.
msg_size:
The size of the message buffer, in bytes.
actual:
The location in which to place the number of bytes received.
timeout:
A timeout value. Currently only zero and non-zero values are legal. Zero means no wait, non-zero means wait forever.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.6.3 oskit_ipc_wait: Receive a message from any thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/ipc.h>

oskit_error_t oskit_ipc_wait(pthread_t *src, void *msg, oskit_size_t msg_size, oskit_size_t *actual, oskit_s32_t timeout);

DESCRIPTION

This function operates identically to oskit_ipc_recv, except that the sending thread does not need to be a specific thread. The first thread that attempts to send to the current thread will succeed. The pthread_t of that thread is returned to the caller in src.

PARAMETERS
src:
The location in which to place the pthread_t of the sending thread.
msg:
The message buffer.
msg_size:
The size of the message buffer, in bytes.
actual:
The location in which to place the number of bytes received.
timeout:
A timeout value. Currently only zero and non-zero values are legal. Zero means no wait, non-zero means wait forever.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.6.4 oskit_ipc_call: make a synchronous IPC call to another thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/ipc.h>

oskit_error_t oskit_ipc_call(pthread_t dst, void *sendmsg, oskit_size_t sendmsg_size, void *recvmsg, oskit_size_t recvmsg_size, oskit_size_t *actual, oskit_s32_t timeout);

DESCRIPTION

Make a synchronous IPC call to another thread, and wait for a reply. The destination thread is specified by its pthread_t. The sending thread is blocked until the receiving thread replies to the IPC using oskit_ipc_reply. The send buffer and the reply buffer are specified separately, with the actual number bytes contained in the reply returned in the location pointed to by actual.

PARAMETERS
dst:
The pthread_t of the destination thread.
sendmsg:
The message buffer to send.
sendmsg_size:
The size of the send message buffer, in bytes.
recvmsg:
The message receive buffer.
recvmsg_size:
The size of the receive message buffer, in bytes.
actual:
The location in which to place the number of bytes contained in the reply message.
timeout:
A timeout value. Currently ignored.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.6.5 oskit_ipc_reply: reply to a synchronous IPC invocation

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/ipc.h>

oskit_error_t oskit_ipc_reply(pthread_t src, void *msg, oskit_size_t msg_size);

DESCRIPTION

Reply to a synchronous IPC invocation made with oskit_ipc_call. The destination thread is specified by its pthread_t, and it must be blocked in a call operation, waiting for the reply message. If the destination thread is canceled before the reply is made, this call with return OSKIT_ECANCELED.

PARAMETERS
dst:
The pthread_t of the destination thread.
msg:
The message buffer.
msg_size:
The size of the message, in bytes.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.7 CPU Inheritance Framework

The CPU Inheritance framework is a novel processor scheduling system that allows arbitrary threads to act as schedulers for other threads. When the C preprocessor symbol CPU_INHERIT is defined, the default POSIX scheduler is replaced by a CPU inheritance support module, plus a number of example schedulers that demonstrate how to write an application level scheduler using the OSKit provided CPU inheritance interface. The primary advantage of CPU inheritance scheduling is that widely different scheduling policies can be implemented, and yet still function properly together. Additionally, CPU inheritance scheduling neatly addresses the problem of priority inversion by providing a general interface for priority inheritance that can be used by either scheduler threads or arbitrary application threads. In the sections that follow, the CPU inheritance interface functions are described. The reader is encouraged to look at the example schedulers in threads/cpuinherit.

29.7.1 pthread_sched_become_scheduler: Become an application level scheduler

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

void pthread_sched_become_scheduler(void);

DESCRIPTION

Inform the CPU inheritance framework that the current thread is an application level scheduler. Certain initializations are performed that allow the current thread to donate its own CPU resources to other threads, and to receive scheduling messages regarding threads under its controls. Once this call is performed, the thread will generally enter a loop waiting for scheduling messages to inform it of new threads that it needs to schedule, or changes in the status of threads already under its control. For example, when a thread blocked on a mutex finally takes the mutex, an unblock message will be sent to that thread’s scheduler informing it that the thread in question should now be run.

29.7.2 pthread_sched_donate_wait_recv: Donate CPU time to a thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int pthread_sched_donate_wait_recv(pthread_t tid, sched_wakecond_t wakecond, schedmsg_t *msg, oskit_s32_t timeout);

DESCRIPTION

Donate CPU time to a target thread whose pthread identifier is tid. The donation will terminate when the thread gives up the CPU, or when the timeout value is reached. In this way, a scheduler can implement preemptive scheduling by allowing each thread to run for a maximum time value, at which time control returns to the dontating scheduler.

The wakecond flag specifies under which circumstances control should be returned to the scheduler when the target thread blocks. There are currently just two values allowed:

WAKEUP_ON_BLOCK:
Control returns to the scheduler whenever the target thread blocks or otherwise gives up the CPU. This allows the scheduler to make a new scheduling decision.
WAKEUP_ON_SWITCH:
Control returns to the scheduler’s scheduler whenever the target thread blocks or otherwise gives up the CPU. This is typically used when a scheduler has just one thread to schedule, and is not interested in when the target thread blocks or unblocks, but only when some other thread wakes up, requiring an actual scheduling decision to be made.

The return value indicates how the donation was terminated, and is one of the following constants. Its is up to the scheduler to determine the course of action. For example, a donation that returns a SCHEDULE_YIELDED would typically result in the target thread being placed back on the scheduler’s run queue so that it will be run at some later point.

SCHEDULE_NOTREADY:
The target thread is not ready to receive the donation, perhaps because the target thread is blocked.
SCHEDULE_BLOCKED:
The target thread ran and then blocked for some reason. The thread should not be run until the scheduler receives an MSG_SCHED_UNBLOCK message for the target thread.
SCHEDULE_YIELDED:
The target thread ran and then voluntarily gave up the CPU. The thread will typically be placed back on the run queue for that scheduler.
SCHEDULE_PREEMPTED:
The target thread ran and was then preempted by the threads system. The thread will typically be placed back on the run queue for that scheduler.
SCHEDULE_TIMEDOUT:
The target thread ran until the timeout expired, and was then preempted so that the scheduler may pick another thread to run. The thread will typically be placed back on the run queue for that scheduler.
SCHEDULE_MSGRECV:
This constant is bitwise or’ed into the result value whenever a thread donation terminates and a scheduling message was returned in the message block pointed to by msg. The scheduler will need to take appropriate action based on the both the return value of the donation, and the contents of the message.

Upon return from the donation, it is possible that a scheduling message will also be ready. Rather than have the scheduler invoke a separate message operation to retrieve the message, the message reception operation is combined with the donation. This is indicated in the return value when the SCHEDULE_MSGRECV bit is set. The format of the message is as follows:

         typedef struct schedmsg {
                 schedmsg_types_t        type;
                 pthread_t               tid;
                 oskit_u32_t             opaque;
                 oskit_u32_t             opaque2;
         } schedmsg_t;
         

The message refers to the thread identifed by tid, while opaque and opaque2 are message specific values. Only some message types have associated message values (described below). The message types are as follows:

MSG_SCHED_NEWTHREAD:
A new thread was created for the current scheduler. The scheduler for a thread is specified using the thread attributes and the textttpthread_attr_setscheduler routine. Once the thread is created and ready to be scheduled, a message is sent to the specified scheduler so that it may set up the necessary data structures, and add the new thread to its run queue. The thread id of the new thread is given by the tid field of the message. Depending on the scheduler, the opaque and opaque2 fields may also be valid.
MSG_SCHED_UNBLOCK:
The thread specified by the tid field of the message has been unblocked. The scheduler will typically add the thread to its internal run queue so that it will be run again.
MSG_SCHED_SETSTATE:
Alter the scheduling parameters for the thread specified by the tid field. This message is sent as a result of the application calling pthread_sched_setstate. The opaque and opaque2 fields are obviously specific to the scheduler.
MSG_SCHED_EXITED:
The thread specified by the tid field of the message has called pthread_exit. The scheduler will typically remove the thread from its internal data structures and free any associated resources.
PARAMETERS
tid:
The pthread_t of the thread to donate to.
wakecond:
The wakeup condition.
msg:
A message buffer in which to place a message if one is available when the donation ends.
timeout:
A timeout value, in milliseconds. The donation will be terminated after the time has elapsed.
RETURNS

The return values are described above.

RELATED INFORMATION

pthread_sched_setstate, pthread_attr_setscheduler

29.7.3 pthread_sched_message_recv: Scheduling message receive

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

oskit_error_t pthread_sched_message_recv(schedmsg_t *msg, oskit_s32_t timeout);

DESCRIPTION

Initiate a scheduling message receive operation. If there are any scheduling messages queued on the scheduler’s message queue, the first message will be dequeued and copied into the message buffer pointed to by msg. The format of the message is described in Section 29.7.2. If timeout is zero, and no message is ready for delivery, the call will return immediately with the error value OSKIT_EAGAIN. If timeout is any non-zero value, the caller will block until a message is available. A future release will allow the specification of an actual timeout value.

PARAMETERS
msg:
A message buffer in which to place a message if one is available.
timeout:
A timeout value, in milliseconds.
RETURNS

Returns 0 on success, or OSKIT_EAGAIN if the caller specified a non-blocking receive and no message was available.

RELATED INFORMATION

pthread_sched_donate_wait_recv

29.7.4 pthread_sched_setstate: Set the scheduling parameters for a thread

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int pthread_sched_setstate(pthread_t tid, int opaque);

DESCRIPTION

Change the scheduling parameter for the thread specified by tid. An scheduler specific opaque value should be passed, which is then sent via a scheduling message to the message queue of the scheduler responsible for the given thread. This interface routine is entirely ad-hoc, and is intended to be used until something better is formulated.

PARAMETERS
tid:
The pthread_t of the thread to donate to.
opaque:
An opaque value that hopefully makes sense to the thread’s scheduler.
RETURNS

Always returns zero.

29.7.5 pthread_cond_donate_wait: Timed condition wait with CPU donation

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int pthread_cond_donate_wait(pthread_cond_t *c, pthread_mutex_t *m, oskit_timespec_t *abstime, pthread_t donee_tid);

DESCRIPTION

The current thread is made to wait until the condition variable is signaled or broadcast, or until the timeout expires. The mutex is released prior to waiting, and reacquired before returning. The timeout is given as an absolute time in the future that bounds the wait.

In addition to the normal operation for timed conditional wait, the caller specifies a thread to which the current thread’s CPU time should be donated. This allows a thread to wait on a condition, but specify that any CPU time that it would have received is donated to thread donee_tid.

PARAMETERS
cond:
A pointer to the condition variable object.
mutex:
A pointer to the mutex object.
abstime:
A pointer to an oskit_timespec structure.
donee_tid:
The pthread_t of the thread to which the current thread’s CPU time should be donated.
RETURNS

Returns zero on success. Returns ETIMEDOUT if the timeout expires.

29.7.6 pthread_attr_setscheduler: Set the scheduler in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int pthread_attr_setscheduler(pthread_attr_t *attr, pthread_t tid);

DESCRIPTION

Set the scheduler thread in a previously initialized threads attribute object, for use with pthread_create. Any thread created with the given attributes object will have it’s scheduler thread set to tid. The caller can thus set up an arbitrary scheduler and thread hierarchy by using this routine.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
tid:
The pthread_t of the thread that will function as the scheduler for new threads.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_attr_setopaque

29.7.7 pthread_attr_setopaque: Set the scheduling parameter in a thread attributes object

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int pthread_attr_setopaque(pthread_attr_t *attr, oskit_u32_t opaque);

DESCRIPTION

Set the scheduling parameter in a previously initialized threads attribute object, for use with pthread_create. This opaque value will be passed to the thread’s scheduler in the initial MSG_SCHED_NEWTHREAD message, after the thread is created and ready to be scheduled. The opaque value should make sense to the scheduler selected with pthread_attr_setscheduler.

PARAMETERS
attr:
A pointer to the pthread_attr_t object representing the attributes for a thread creation.
opaque:
An opaque value that hopefully makes sense to the scheduler.
RETURNS

Returns zero on success.

RELATED INFORMATION

pthread_attr_setscheduler

29.7.8 Example Schedulers

This section briefly describes a number of sample schedulers that are provided as examples of how to use the CPU Inheritance framework.

29.7.9 create_fixedpri_scheduler: Create a fixed priority scheduler

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int create_fixedpri_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);

DESCRIPTION

Create a new Fixed Priority scheduler. The pthread_t of the new scheduler is returned in tid. The attributes structure to use when creating the new thread is attr. The preemptible flag indicates whether the new scheduler should use time-based preemption to achieve fairness. Aside from the usual attributes that can be specified, the caller may also specify the new scheduler’s scheduler by using pthread_attr_setscheduler. Thus, the caller can set up an arbitrary hierarchy of schedulers and threads.

This fixed priority scheduler roughly corresponds to the POSIX pthread scheduler, and implements both FIFO and round robin policies. The standard pthread scheduling interface routines may be used when altering the scheduling parameters for threads that are scheduled by this scheduler.

PARAMETERS
tid:
A pointer to the location where the thread id of the new scheduler should be stored.
attr:
A pointer to the thread creation attributes object.
preemptible:
A flag indicating whether the new scheduler should use time-based preemption.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.7.10 create_lotto_scheduler: Create a lottery scheduler

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int create_lotto_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);

DESCRIPTION

Create a new Lottery scheduler. The pthread_t of the new scheduler is returned in tid. The attributes structure to use when creating the new thread is attr. The preemptible flag indicates whether the new scheduler should use time-based preemption to achieve fairness. Aside from the usual attributes that can be specified, the caller may also specify the new scheduler’s scheduler by using pthread_attr_setscheduler. Thus, the caller can set up an arbitrary hierarchy of schedulers and threads.

When creating threads that are scheduled by a Lottery scheduler, the caller should set the opaque scheduling parameter in the thread creation attributes structure. This opaque value represents a ticket value, and should be an integer (usually a small to moderately sized integer). As with any Lottery scheduler, the larger the ticket value, the more CPU a thread is likely to receive.

PARAMETERS
tid:
A pointer to the location where the thread id of the new scheduler should be stored.
attr:
A pointer to the thread creation attributes object.
preemptible:
A flag indicating whether the new scheduler should use time-based preemption.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.7.11 create_stride_scheduler: Create a Stride scheduler

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int create_stride_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);

DESCRIPTION

Create a new Stride scheduler. The pthread_t of the new scheduler is returned in tid. The attributes structure to use when creating the new thread is attr. The preemptible flag indicates whether the new scheduler should use time-based preemption to achieve fairness. Aside from the usual attributes that can be specified, the caller may also specify the new scheduler’s scheduler by using pthread_attr_setscheduler. Thus, the caller can set up an arbitrary hierarchy of schedulers and threads.

When creating threads that are scheduled by a Stride scheduler, the caller should set the opaque scheduling parameter in the thread creation attributes structure. This opaque value represents a ticket value, and should be an integer (usually a small to moderately sized integer). As with any Stride scheduler, the larger the ticket value, the more CPU a thread is likely to receive.

PARAMETERS
tid:
A pointer to the location where the thread id of the new scheduler should be stored.
attr:
A pointer to the thread creation attributes object.
preemptible:
A flag indicating whether the new scheduler should use time-based preemption.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

29.7.12 create_ratemono_scheduler: Create a Rate Monotonic scheduler

SYNOPSIS

#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>

int create_ratemono_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);

DESCRIPTION

Create a new Rate Monotonic scheduler. The pthread_t of the new scheduler is returned in tid. The attributes structure to use when creating the new thread is attr. The preemptible flag indicates whether the new scheduler should use time-based preemption to achieve fairness. Aside from the usual attributes that can be specified, the caller may also specify the new scheduler’s scheduler by using pthread_attr_setscheduler. Thus, the caller can set up an arbitrary hierarchy of schedulers and threads.

When creating threads that are scheduled by a Rate Monotonic scheduler, the caller should set the opaque scheduling parameter in the thread creation attributes structure. This opaque value represents a period, and is used to create the ordered scheduling list. It should be an integer (usually a small to moderately sized integer).

Note that this rate monotonic scheduler is extremely simplified, and should be considered strictly as an example of how to write a scheduler; it does not implement a proper Rate Monotonic scheduling policy.

PARAMETERS
tid:
A pointer to the location where the thread id of the new scheduler should be stored.
attr:
A pointer to the thread creation attributes object.
preemptible:
A flag indicating whether the new scheduler should use time-based preemption.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.