myThread.h File Reference

MyThread Library Header File. More...

#include <signal.h>
#include <sys/time.h>

Include dependency graph for myThread.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef unsigned int ThreadID
 ID of a Thread Data Type to represent the thread ID.
typedef void(* StartFunction )(void)
 Thread Entry Point To represent a pointer to the start function of a thread.

Functions

void myThread_init ()
 To initialise the threading library.
void myThread_create (StartFunction start)
 To create a new thread.
void myThread_exit (int status)
 To exit a thread.
ThreadID myThread_getID ()
 To get the current running thread's ID.


Detailed Description

This header file contains the prototypes of the threading functions.

Definition in file myThread.h.


Function Documentation

void myThread_init (  ) 

This function initialises the thread library. It performs the following steps :

  1. Creates a Thread data structure for the main thread and puts it in the thread list
  2. Registers the signal handler saveContext() to catch the Timer signal
  3. Starts the Interval Timer
Returns:
Returns Nothing

Definition at line 138 of file myThread.cpp.

References Thread::id, saveContext(), startTimer(), threadList, threadListIterator, and TimerTypeSignal.

00139 {
00140   // Create an entry for the main thread...
00141   Thread mainThread;
00142   // A special ID = 0 for the main thread...
00143   // The main thread does not have a separate stack like the other threads...
00144   mainThread.id = 0;
00145 
00146   // Insert the main thread into the list of threads...
00147   threadList.push_back ( mainThread );
00148 
00149   // Set the current thread to this main thread...
00150   threadListIterator = threadList.begin();
00151   currentThread = mainThread;
00152 
00153   // Register the signal handling routine...
00154   struct sigaction action;
00155   action.sa_handler = saveContext;
00156   sigemptyset ( &action.sa_mask );
00157   action.sa_flags = 0;
00158   sigaction ( TimerTypeSignal , &action , NULL );
00159 
00160   // Start the Timer...
00161   startTimer();
00162 }

void myThread_create ( StartFunction  start  ) 

This function creates a new thread It performs the following steps :

  1. Switches off the Timer
  2. Fills in a new Thread Data Structure, and appends it to the end of the thread list
  3. Restarts the Interval Timer
Parameters:
start The Thread Entry Point
Returns:
Returns Nothing
Note:
This function can be used in any thread to create new threads. However, there is no concept of thread parent and child.
Bug:
The Thread ID assigned may not be unique

Definition at line 187 of file myThread.cpp.

References initialTopOfStack, threadList, TimerType, and zeroTimer.

00188 {
00189   // Switch off the timer...
00190   struct itimerval oldTimer;
00191   setitimer ( TimerType , &zeroTimer , &oldTimer );
00192 
00193   // Create a new thread...
00194   Thread *newThread = new Thread;            // How to deallocate ???????
00195   // Note : This method will not produce unique thread IDs...
00196   newThread -> id = threadList.size();       // we may run out of thread IDs...
00197   newThread -> esp = reinterpret_cast < uInt32 > ( &newThread -> initialTopOfStack );
00198   newThread -> eip = reinterpret_cast < uInt32 > ( start );
00199   asm volatile ( "movw   %%ds , %%ax \n\t"
00200                  "movw   %%ax , %0   \n\t"
00201                  "movw   %%es , %%ax \n\t"
00202                  "movw   %%ax , %1   \n\t"
00203                  "movw   %%fs , %%ax \n\t"
00204                  "movw   %%ax , %2   \n\t"
00205                  "movw   %%gs , %%ax \n\t"
00206                  "movw   %%ax , %3   \n\t"
00207                  "pushfl             \n\t"
00208                  "popl   %4          \n\t"
00209                  : "=m" ( newThread -> context.ds ) , "=m" ( newThread -> context.es ) ,
00210                    "=m" ( newThread -> context.fs ) , "=m" ( newThread -> context.gs ) ,
00211                    "=m" ( newThread -> eflags )
00212                  :
00213                  : "%ax"
00214                  );
00215 
00216   // Insert it into the list...
00217   threadList.push_back ( *newThread );
00218 
00219   // Set the timer to what it was...
00220   setitimer ( TimerType , &oldTimer , NULL );
00221 
00222   // Return to the current thread...
00223   return;
00224 }

void myThread_exit ( int  status  ) 

This function terminates a thread It performs the following steps :

  1. Switches off the Timer
  2. Gets the next thread to be run
  3. Removes the current thread from the list
  4. Restarts the Interval Timer
  5. Switches to the next thread
Parameters:
status The Return Status of a Thread
Returns:
Does not return
Note:
All threads (other than the main thread) should call this function before exiting
Todo:
Record the return status of the thread in some way

Definition at line 250 of file myThread.cpp.

References getNextThread(), startTimer(), switchToThread(), threadList, threadListIterator, TimerType, and zeroTimer.

00251 {
00252   // Status is ignored...
00253   // Switch off the timer...
00254   setitimer ( TimerType , &zeroTimer , NULL );
00255 
00256   // Keep the next thread ready...
00257   std :: list < Thread > :: iterator nextThread = getNextThread();
00258 
00259   // If this is the last thread, then simply exit...
00260   if ( nextThread == threadListIterator )
00261     exit ( status );
00262 
00263   // Remove the thread from the list...
00264   threadList.erase ( threadListIterator );
00265   threadListIterator = nextThread;
00266   currentThread = *nextThread;
00267 
00268   // Reset the timer...
00269   startTimer();
00270 
00271   // Switch to next thread...
00272   switchToThread ( nextThread );
00273 }

ThreadID myThread_getID (  ) 

This function returns the current running thread's ID It performs the following steps :

  1. Switches off the Timer
  2. Gets the current thread ID
  3. Switches the timer back on
Returns:
The current executing thread's ID
Remarks:
The Timer is switched off before accessing the thread list, but this may not really be required

Definition at line 292 of file myThread.cpp.

References Thread::id, TimerType, and zeroTimer.

00293 {
00294   // Switch off the timer...
00295   struct itimerval oldTimer;
00296   setitimer ( TimerType , &zeroTimer , &oldTimer );
00297 
00298   // Get the current thread ID...
00299   ThreadID id = currentThread.id;
00300 
00301   // Set the timer to what it was...
00302   setitimer ( TimerType , &oldTimer , NULL );
00303 
00304   return id;
00305 }


Generated on Fri Sep 18 20:32:22 2009 for "MyThread Library" by  doxygen 1.5.5