castroflavio / GTthreads

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GTTHREADS - POSIX Compliant Threads Library
Vinay Bharadwaj
##GTThreads user level thread library.

PLATFORM USED:
Developed on Fedora 15, Ubuntu 13.04. Tested on Fedora 13 x64 Fedora 15 and Ubuntu 13.04 x64.
Atomic operations work only on 64 bit processors due to the use of %rax 64 bit register.

PREEMPTIVE schedule:

The schedule is implemented as follows:
A queue of threads is maintained. For each gtthread_create() called, the corresponding thread is created and added to the
queue. The first time create() is called, the main() function's context is also saved and is added as a thread to the queue
along with the first thread to be created. 

Thereafter, every call to gtthread_create creates a new context for the thread, allocates memory and adds it to the queue.
The schedule switches through the thread contexts in a round robin fashion at the specified time quantum intervals. The
thread execution is preempted by setting a timer and raising a signal at every timer expiry. The associated signal handler 
is called, which saves the thread context and switches back to the schedule. The schedule then switches to the next thread.
Since main() is also implemented as a thread in the queue, the schedule switches to main() periodically.

HOW TO COMPILE:
1) Extract the tarball into a folder.
2) From the shell prompt run make as follows:
	# make
3) This should generate the object files.
4) Compile your program with the library and execute it as follows:
	# gcc main.c *.o
	# ./a.out
   OR

	# gcc -o myprogram main.c *.o
	# myprogram

Some thoughts:
   setcontext() doesn't work well with signals. Works fine with Fedora 15 but on Fedora 13 (default glibc) gives 
segmentation faults (Known bug in glibc. See GNU FAQ for glibc). Replacing setcontext() with swapcontext() resolves the 
problem. setcontext() also caused unexpected behaviour and overflows on infinite for(;;) loops without termination code.

Overall, it was a good experience developing this library. Hope to make more changes and expand it in the future. Need to 
try the sigsetjmp() and siglongjmp() approach next. 

Updates:
1) Included Atomic test and set, increment, decrement, modulus operations.
2) Modularized code.

   

About


Languages

Language:C 94.5%Language:Makefile 5.0%Language:Shell 0.5%