Custom Thread Support Library in C
Bilkent CS342 Project 2
Introduction
In this project, I have implemented a Thread Support Library (TSL) in C, which provides a set of functions to create, manage, and synchronize threads in a program. The TSL library allows the application to create and control multiple threads, enabling concurrent execution of tasks.
Key Features
The TSL library provides the following functionalities:
- Thread Creation: The
tsl_create_threadfunction allows the application to create a new thread and pass a function pointer and an argument to it. - Thread Yielding: The
tsl_yieldfunction allows a thread to voluntarily yield the CPU to another thread, enabling cooperative scheduling. - Thread Termination: The
tsl_exitfunction allows a thread to terminate its execution. - Thread Joining: The
tsl_joinfunction allows a thread to wait for the completion of another thread and retrieve its return value. - Thread Cancellation: The
tsl_cancelfunction allows a thread to be cancelled and its resources to be reclaimed. - Thread Identification: The
tsl_gettidfunction allows a thread to obtain its own thread ID. - Scheduling Algorithms: The TSL library supports different scheduling algorithms, such as First-Come-First-Served (FCFS) and Random scheduling.
Implementation Details
The TSL library is implemented using the ucontext.h library, which provides a low-level interface for managing user-level threads. The main components of the TSL library are:
- Thread Control Block (TCB): The
tcb_tstruct represents a thread and stores its context, stack, and other relevant information. - Ready Queue and End Queue: The
queue_tstruct represents a queue of threads, which is used to manage the ready and ended threads. - Scheduling Algorithms: The TSL library supports different scheduling algorithms, which are implemented using the
remove_thread_at_positionfunction. - Thread Lifecycle Management: The TSL library manages the lifecycle of threads, including creation, yielding, termination, joining, and cancellation.
Usage Example
Here’s an example of how to use the TSL library in a C program:
#include "tsl.h"
void thread_function(void *arg) {
int id = tsl_gettid();
printf("Thread %d started\n", id);
// Perform some work
printf("Thread %d finished\n", id);
}
int main() {
tsl_init(ALG_FCFS);
tsl_create_thread(thread_function, NULL);
tsl_create_thread(thread_function, NULL);
tsl_yield(TSL_ANY);
return 0;
}
In this example, we initialize the TSL library with the First-Come-First-Served (FCFS) scheduling algorithm, create two threads, and then yield to the scheduler to allow the threads to execute.
Conclusion
The Thread Support Library (TSL) in C provides a flexible and efficient way to manage threads in a program. By leveraging the ucontext.h library, the TSL library offers a range of thread management functions, including creation, yielding, termination, joining, and cancellation. The support for different scheduling algorithms allows the application to choose the most appropriate scheduling strategy for its needs.
The TSL library can be used as a building block for more complex concurrent and parallel applications, enabling developers to harness the power of multi-threading and improve the overall performance and responsiveness of their programs.