INE5357 - Operating Systems II - 2006/2

Group 1

E2: idle-waiting thread synchronization

Introduction

In this exercise we are challenged to add idle-waiting thread synchronization to the classes semaphore, condition and mutex.

Solution

All the classes related to synchronization are subclasses of Synchronizer_Commom. On this parent class there are three main methods: sleep(), wakeup(), wakeup_all(). All three related classes use these three methods two manage the threads. So all the work should be done on these methods.

To manage the sleeping threads we create an instance member attribute queue (_sleepers), defined on synchronizer.h@26-27, to hold all the threads that are on a SLEEP state waiting for this synchronizer. In order to push thread into the list we created three static methods on the class Thread defined on thread.h. We did this to preserve the visibility of the Thread::_link attribute of the thread class. We need this Thread::_link because it is the actual object stored on the list. If we just change its visibility, the user of the Thread would have access to it. This is bad, so many bad things would happen.

The new methods, Thread::sleep(), Thread::wakeup(), Thread::wakeup_all(), defined on thread.h@193,194,195 and implemented on thread.cc@271,311,343, were created in order to do the dirty work. They are responsable to enqueue and dequeue the thread and put it into sleep and running state. A new state was created, SLEEPING, defined on thread.h@37.

We put some code on the Thread destructor to avoid the remove of deleted threads on the sleepers list. To acomplish that, we put a reference to the list defined on thread.h@249 that its updated everytime Thread::sleep(), Thread::wakeup(), Thread::wakeup_all() is called.

Table of Files

Changed files
File Comment
thread.h Added some key behaviour to finnish exercise.
thread.cc Added some key behaviour to finnish exercise.
synchronizer.h Completed the methods needed by semaphore, mutex and condition


Operating Systems II - Group 1