Skip to content

User-level threads with synchronization and round-robin scheduling

Notifications You must be signed in to change notification settings

aaron-ang/threading-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Threading Library

This project implements a simple user-level scheduler within a process with thread synchronization.

Scheduler

Implement pthread_create, pthread_exit, pthread_join, and pthread_self interfaces. Threads are cycled in a round-robin fashion and the scheduler uses SIGALRM for timeouts.

Synchronization features

  1. Thread locking and unlocking, to prevent the scheduler from running at specific times.
  2. An implementation of pthread barriers, to ensure one thread can wait for another thread to finish, and to collect a completed thread’s exit status.
  3. Support for mutexes in threads, which will enable mutual exclusion from critical regions of multithreaded code.

Data Structures

enum thread_status { TS_EXITED, TS_READY, TS_RUNNING, TS_BLOCKED };

typedef struct thread_control_block {
  pthread_t id;
  jmp_buf registers;
  void *stack;
  enum thread_status status;
  void *ret_val;
  bool has_mutex;
} TCB;

typedef struct {
  int flag;
} mutex_t;

typedef union {
  pthread_mutex_t mutex;
  mutex_t my_mutex;
} my_mutex_t;

typedef struct {
  int *waitlist;
  unsigned limit;
  unsigned count;
} barrier_t;

typedef union {
  pthread_barrier_t barrier;
  barrier_t my_barrier;
} my_barrier_t;

Additions

Helper functions

  • void scheduler_init(): Initializes the scheduler by assigning the first thread as the main thread
  • void init_handler(): Initializes the signal handler (schedule) for the timer
  • TCB *get_new_thread(): Returns a new thread from the thread pool
  • void thread_init(TCB *new_thread): Allocates the stack for the new thread
  • void reg_init(TCB *new_thread, void *(*start_routine)(void *), void *arg): Initializes the registers for the new thread to run the start routine

Test cases

About

User-level threads with synchronization and round-robin scheduling

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published