#include <signal.h>
#include <sys/time.h>
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. |
Definition in file myThread.h.
void myThread_init | ( | ) |
This function initialises the thread library. It performs the following steps :
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 :
start | The Thread Entry Point |
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 :
status | The Return Status of a Thread |
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 :
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 }