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.
- dphils: A computational example that tests basic POSIX threads operations such as thread creation,
mutexes, and conditions. Solves the classic Dining Philosophers problem.
- disktest: A contrived disk thrashing program that tests the interaction between POSIX threads and
the NetBSD filesystem (see Section 37). A number of threads are created, where each one creates and
copies files in varying block sizes.
- disknet: Another contrived program that builds on the disk thrashing program above. Also tested
is the interaction bewteen POSIX threads and the BSD network interface. Half of the threads created
thrash the disk and the other half connect to a server process and send and receive data blocks. This
program achieves reasonable interleaving of work.
- http_proxy: A simplified HTTP proxy daemon that tests the interaction between POSIX threads and
the BSD network interface. For each new connection request, three threads are created to manage that
connection and forward data between the client and the server.
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
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
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
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
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
RELATED INFORMATION
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
RELATED INFORMATION
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
RELATED INFORMATION
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
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
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
RELATED INFORMATION
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
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
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
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 IN<