Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

Thread Management
[Nut/OS API]

Coperative multi-threading support. More...


Modules

Thread States
 Thread operating states.


Data Structures

struct  _NUTTHREADINFO
 Thread information structure. More...

struct  _NUTTHREADINFO
 Thread information structure. More...

struct  ENTERFRAME
struct  SWITCHFRAME

Typedefs

typedef _NUTTHREADINFO NUTTHREADINFO

Functions

void NutThreadAddPriQueue (NUTTHREADINFO *td, NUTTHREADINFO **tqpp)
 Add a thread to a prioritiy ordered queue.

void NutThreadRemoveQueue (NUTTHREADINFO *td, NUTTHREADINFO *volatile *tqpp)
 Remove a thread from a specified queue.

void NutThreadResumeAsync (HANDLE th)
 Make a previously suspended thread ready to run.

void NutThreadWake (HANDLE timer, HANDLE th)
 Resume a previously suspended thread.

void NutThreadYield (void)
 Give up the CPU.

u_char NutThreadSetPriority (u_char level)
 Set the current thread's priority.

HANDLE NutThreadCreate (u_char *name, void(*fn)(void *), void *arg, u_short stackSize)
 Create a new thread.


Variables

NUTTHREADINFO *volatile runningThread
 Currently running thread.

NUTTHREADINFO *volatile nutThreadList
 List of all created threads.

NUTTHREADINFO *volatile runQueue
 List of ready-to-run threads.


Detailed Description

Coperative multi-threading support.

Typically Nut/OS is at its most useful where there are several concurrent tasks that need to be undertaken at the same time. To support this requirement, Nut/OS offers some kind of light processes called threads. In this context a thread is a sequence of executing software that can be considered to be logically independent from other software that is running on the same CPU.

All threads are executing in the same address space using the same hardware resources, which significantly reduces task switching overhead. Therefore it is important to stop them from causing each other problems. This is particularly an issue where two or more threads need to share a resources like memory locations or peripheral devices.

The system works on the principle that the most urgent thread always runs. One exception to this is if a CPU interrupt arrives and the interrupt has not been disabled. Each thread has a priority which is used to determine how urgent it is. This priority ranges from 0 to 255, with the lowest value indicating the most urgent.

Nut/OS implements cooperative multithreading. That means, that threads are not bound to a fixed timeslice. Unless they are waiting for specific event or explicitely yielding the CPU, they can rely on not being stopped unexpectedly. However, they may be interrupted by hardware interrupt signals. In opposite to pre-emptive multithreading, coorperative multithreading simplifies resource sharing and results in faster and smaller code.


Typedef Documentation

typedef struct _NUTTHREADINFO NUTTHREADINFO
 

Thread information structure type.


Function Documentation

void NutThreadAddPriQueue NUTTHREADINFO   td,
NUTTHREADINFO **    tqpp
 

Add a thread to a prioritiy ordered queue.

Insert the thread into a specified queue behind the last thread with lower or equal priority.

Note:
CPU interrupts must have been disabled before calling this function.
Parameters:
td Pointer to NUTTHREADINFO of the thread to be inserted in the queue.
tqpp Pointer to the root of the queue.

HANDLE NutThreadCreate u_char   name,
void(*    fn)(void *),
void *    arg,
u_short    stackSize
 

Create a new thread.

If the current thread's priority is lower or equal than the default priority (64), then the current thread is stopped and the new one is started.

Parameters:
name String containing the symbolic name of the new thread, up to 8 characters long.
fn The thread's entry point, typically created by the THREAD macro.
arg Argument pointer passed to the new thread.
stackSize Number of bytes of the stack space allocated for the new thread.
Returns:
Pointer to the NUTTHREADINFO structure or 0 to indicate an error.
Examples:
httpd/httpserv.c, portdio/portdio.c, rs232d/rs232d.c, threads/threads.c, and timers/timers.c.

void NutThreadRemoveQueue NUTTHREADINFO   td,
NUTTHREADINFO *volatile *    tqpp
 

Remove a thread from a specified queue.

CPU interrupts must have been disabled before calling this function.

Parameters:
td Pointer to NUTTHREADINFO of the thread to be removed from the queue.
tqpp Pointer to the root of the queue.

void NutThreadResumeAsync HANDLE    th
 

Make a previously suspended thread ready to run.

Parameters:
th Handle of the thread to resume.
Note:
CPU interrupts must have been disabled before calling this function. It is save to call this function from within an interrupt handler.

u_char NutThreadSetPriority u_char    level
 

Set the current thread's priority.

The priority of newly created threads is set to 64, but may be changed when the thread starts running.

When another thread with a higher or equal priority is ready to run, the current thread will be stopped and control of the CPU is passed to the other thread.

The function returns the old priority, which makes it easy to temporarily switch to another priority and later set back the old one.

Parameters:
level New priority level.
Returns:
The old priority of this thread.
Examples:
httpd/httpserv.c, threads/threads.c, and timers/timers.c.

void NutThreadWake HANDLE    timer,
HANDLE    th
 

Resume a previously suspended thread.

This routine is called by the system when a sleep timer elapses.

Note:
This routine is running in interrupt context. Applications typically do not call this function. In any case interrupts must have been disabled.
Parameters:
timer Handle of the elapsed timer.
th Handle of the thread to wake up.

void NutThreadYield void   
 

Give up the CPU.

If another thread within the same or higher priority is ready to run, then the current thread is stopped and the other one is started.

Examples:
playmp3/playmp3.c, portdio/portdio.c, and rs232d/rs232d.c.


Variable Documentation

NUTTHREADINFO* volatile nutThreadList
 

List of all created threads.

Linked list of NUTTHREADINFO structures of all threads. New threads are put in front. This list contains at least two threads, the main application thread followed by the idle thread.

NUTTHREADINFO* volatile runningThread
 

Currently running thread.

Pointer to the NUTTHREADINFO structure of the currently running thread.

NUTTHREADINFO* volatile runQueue
 

List of ready-to-run threads.

Priority ordered linked list of NUTTHREADINFO structures of all threads which are ready to run.


© 2000-2003 by egnite Software GmbH - visit http://www.ethernut.de/