Back to blog
Mar 25, 2024
3 min read

Custom Thread Support Library in C

A C-based thread support library enabling user-level threading with various scheduling algorithms.

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:

  1. Thread Creation: The tsl_create_thread function allows the application to create a new thread and pass a function pointer and an argument to it.
  2. Thread Yielding: The tsl_yield function allows a thread to voluntarily yield the CPU to another thread, enabling cooperative scheduling.
  3. Thread Termination: The tsl_exit function allows a thread to terminate its execution.
  4. Thread Joining: The tsl_join function allows a thread to wait for the completion of another thread and retrieve its return value.
  5. Thread Cancellation: The tsl_cancel function allows a thread to be cancelled and its resources to be reclaimed.
  6. Thread Identification: The tsl_gettid function allows a thread to obtain its own thread ID.
  7. 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:

  1. Thread Control Block (TCB): The tcb_t struct represents a thread and stores its context, stack, and other relevant information.
  2. Ready Queue and End Queue: The queue_t struct represents a queue of threads, which is used to manage the ready and ended threads.
  3. Scheduling Algorithms: The TSL library supports different scheduling algorithms, which are implemented using the remove_thread_at_position function.
  4. 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.