Thread Library - CS 702 - Spring 2004


Loyola College > Department of Computer Science > Dr. James Glenn > CS 702 > Examples and Lecture Notes > Thread Library

A simple thread library for Linux-x86 and g++ 3.2.2

Goals

Design

Code

Thread Data Structures

Data Structures

Yielding to Another Thread

yield and activate
  1. save registers on stack of yielding thread (in Thread::yield assembly)
  2. save yielding thread's new stack pointer in Thread structure (in Thread::yield assembly)
  3. enqueue the yielding thread (in Scheduler::threadYielded C++)
  4. dequeue next thread (in Scheduler::scheduleThread C++)
  5. save address of next thread (in Thread::activate assembly)
  6. restore next thread's registers (including ESP) and return to it (in Thread::activate assembly)

Starting a Thread

start Thread
  1. put data in new thread's stack (in Thread::start assembly)
  2. store new thread's stack pointer in Thread structure (in Thread::start assembly)
  3. enqueue new thread (in Scheduler::startThread C++)

Starting the Scheduler

start Scheduler
  1. save main's registers on main's stack (in Scheduler::start assembly)
  2. save main's stack pointer in originalESP (in Scheduler::start assembly)
  3. dequeue next thread (in Scheduler::scheduleThread C++)
  4. save address of next thread (in Thread::activate assembly)
  5. restore next thread's registers (including ESP) and return to it (in Thread::activate assembly)

Blocking a Thread

block and activate