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.
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).
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>.
This header file defines the following standard symbols.
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.
Initialize a thread attributes object for use with pthread_create.
Returns zero on success.
pthread_create, pthread_attr_setprio, pthread_attr_setstacksize
#include <oskit/threads/pthread.h>
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
Set the thread detach state in a previously initialized threads attribute object, for use with pthread_create.
Returns zero on success. Returns EINVAL if detachstate is invalid.
pthread_create, pthread_attr_init
Set the priority value in a previously initialized threads attribute object, for use with pthread_create.
Returns zero on success. Returns EINVAL if priority is outside the range of PRIORITY_MIN to PRIORITY_MAX.
pthread_create, pthread_attr_init, pthread_attr_setstacksize
#include <oskit/threads/pthread.h>
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
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.
Returns zero on success.
pthread_create, pthread_attr_init, pthread_attr_setstacksize
#include <oskit/threads/pthread.h>
int pthread_attr_setguardsize(pthread_attr_t *attr, oskit_size_t guardsize);
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.
Returns zero on success.
pthread_create, pthread_attr_init, pthread_attr_setstackaddr
#include <oskit/threads/pthread.h>
int pthread_attr_setstacksize(pthread_attr_t *attr, oskit_size_t stacksize);
Set the stack size in a previously initialized threads attribute object, for use with pthread_create.
Returns zero on success. Returns EINVAL if stacksize is less than PTHREAD_STACK_MIN.
pthread_create, pthread_attr_init, pthread_attr_setprio
#include <oskit/threads/pthread.h>
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
Set the scheduling policy in a previously initialized threads attribute object, for use with pthread_create.
Returns zero on success. Returns EINVAL if policy is invalid.
pthread_create, pthread_attr_init
Initialize an mutex attributes object for use with pthread_mutex_init.
Returns zero on success.
pthread_mutex_init, pthread_mutex_setprotocol
#include <oskit/threads/pthread.h>
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
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.
Returns zero on success.
pthread_mutex_init
#include <oskit/threads/pthread.h>
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
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.
Returns zero on success.
pthread_mutex_init
Initialize an condition variable attributes object for use with pthread_cond_init.
Returns zero on success.
pthread_cond_init
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.
Returns zero on success. EINVAL if tid specifies an invalid thread.
pthread_create, pthread_sleep
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.
pthread_cancel, pthread_cleaup_pop
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.
pthread_cancel, pthread_setcanceltype
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.
pthread_cancel, pthread_setcancelstate
Test whether a cancelation is pending, and deliver the cancelation if the cancel state is PTHREAD_CANCEL_ENABLED.
pthread_cancel, pthread_setcancelstate
Wakeup all threads waiting on a condition variable.
Returns zero on success.
pthread_cond_init, pthread_cond_wait, pthread_cond_signal
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.
Returns zero on success. EINVAL if there are threads still waiting.
pthread_cond_init
#include <oskit/threads/pthread.h>
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
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.
Returns zero on success.
pthread_cond_destroy
Wakeup the highest priority thread waiting on a condition variable.
Returns zero on success.
pthread_cond_wait, pthread_cond_broadcast
#include <oskit/threads/pthread.h>
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
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.
Returns zero on success.
pthread_cond_signal, pthread_cond_broadcast, pthread_cond_timedwait
#include <oskit/threads/pthread.h>
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, oskit_timespec_t *abstime);
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.
Returns zero on success. Returns ETIMEDOUT if the timeout expires.
pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait
#include <oskit/threads/pthread.h>
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void (*function)(void *), void *arg);
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).
Returns zero on success, storing the tid of the new thread into *tid.
pthread_join, pthread_detach, pthread_exit
The thread specified by tid is detached from its parent. If the thread has already exited, its resources are released.
Returns zero on success. EINVAL if tid refers to a non-existent thread.
pthread_join, pthread_create, pthread_exit
The current thread is terminated, with its status value made available to the parent using pthread_join.
This function does not return.
pthread_join, pthread_create, pthread_detach
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.
Returns zero on success, storing the target’s exit status in *status. EINVAL if tid refers to a non-existent thread.
pthread_join, pthread_create, pthread_detach
#include <oskit/threads/pthread.h>
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
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.
Returns zero on success, and stores the new key value at *key. Returns EAGAIN if the are no more keys available.
pthread_key_delete, pthread_setspecific, pthread_getspecific
Delete the thread-specific key. Attempts to use a key via pthread_setspecific or pthread_getspecific after it has been deleted is undefined.
Returns zero on success. Returns EINVAL if key refers to an invalid key.
pthread_key_create, pthread_setspecific, pthread_getspecific
Associate a new thread-specific value with the specified key.
Returns zero on success. Returns EINVAL if key refers to an invalid key.
pthread_key_create, pthread_key_delete, pthread_getspecific
Get the thread-specific value associated the specified key.
Returns the value of the key. Errors always return zero.
pthread_key_create, pthread_key_delete, pthread_setspecific
#include <oskit/threads/pthread.h>
int pthread_mutex_init(pthread_mutex_t *m, pthread_mutexattr_t *attr);
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.
Returns zero on success.
pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock
The mutex object is destroyed, although the memory for the object is not deallocated. The mutex must not be held.
Returns zero on success. Returns EBUSY if the mutex is still held.
pthread_mutex_init
Lock a mutex object. If the mutex is currently locked, the thread waits (is suspended) for the mutex to become available.
Returns zero on success.
pthread_mutex_init, pthread_mutex_unlock, pthread_mutex_trylock
Attempt to lock a mutex object. This function always returns immediately.
Returns zero on success. Returns EBUSY if the mutex object is locked.
pthread_mutex_init, pthread_mutex_unlock, pthread_mutex_lock
Unlock a mutex object. If there other threads waiting to acquire the mutex, the highest priority thread is woken up and granted the mutex.
Returns zero on success.
pthread_mutex_init, pthread_mutex_trylock, pthread_mutex_lock
Return the thread identifier of the current thread.
Returns the thread identifier.
pthread_create
#include <oskit/threads/pthread.h>
int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);
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.
Returns zero on success. EINVAL if tid specifies an invalid thread or policy specifies an invalid policy.
pthread_create, sched_yield, pthread_setprio
#include <oskit/threads/pthread.h>
#include <signal.h>
int pthread_sigmask(int how, const sigset_t *set, [out] sigset_t *oset);
Examine or change the per-thread signal mask. This function operates identically to the POSIX function sigprocmask, but on the current thread.
Returns zero on success. No errors are reported.
pthread_kill sigprocmask
Send a signal to a specific thread.
Returns zero on success.
pthread_sigmask
The calling thread voluntarily yields the processor. The highest priority thread is chosen for execution.
pthread_setprio, pthread_setschedparam
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.
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.
Returns ETIMEDOUT if the timeout expires, or zero if the thread is woken up early.
oskit_pthread_wakeup
Wakeup a thread that is sleeping in oskit_pthread_sleep, causing it to return from its sleep before the timeout expires.
Returns zero on success. EINVAL if tid specifies an invalid thread or the current thread.
oskit_pthread_sleep
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.
Returns zero on success. EINVAL if tid specifies an invalid thread or newpri specifies an invalid priority.
pthread_create, sched_yield, pthread_setschedparam
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.
osenv_process_unlock, osenv_sleep, osenv_wakeup
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.
osenv_process_lock, osenv_sleep, osenv_wakeup
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:
|
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.
#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);
Create and return an oskit_socket thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_dir thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_blkio thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
Create and return an oskit_absio thread-safe adaptor.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
This section describes the “interthread” communication primitives provided by the pthread library.
#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);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#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);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
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.
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
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.
#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);
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:
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.
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:
|
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:
The return values are described above.
pthread_sched_setstate, pthread_attr_setscheduler
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
oskit_error_t pthread_sched_message_recv(schedmsg_t *msg, oskit_s32_t timeout);
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.
Returns 0 on success, or OSKIT_EAGAIN if the caller specified a non-blocking receive and no message was available.
pthread_sched_donate_wait_recv
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
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.
Always returns zero.
#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);
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.
Returns zero on success. Returns ETIMEDOUT if the timeout expires.
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int pthread_attr_setscheduler(pthread_attr_t *attr, pthread_t tid);
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.
Returns zero on success.
pthread_attr_setopaque
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int pthread_attr_setopaque(pthread_attr_t *attr, oskit_u32_t opaque);
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.
Returns zero on success.
pthread_attr_setscheduler
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int create_fixedpri_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int create_lotto_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int create_stride_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/threads/pthread.h>
#include <oskit/threads/cpuinherit.h>
int create_ratemono_scheduler(pthread_t *tid, const pthread_attr_t *attr, int preemptible);
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.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.