UthreadLib

##Thread Construction ###Data Structure: thread_id: unsigned
###Functions:

  1. uthread_create: create a new thread end by call uthread_exit
  2. uthread_yeid: switch to next context. called by timer.
  3. uthread_exit: modify alive flag and save return pointer into stack.
  4. uthread_join: block and check target thread's alive flag. return the pointer from thread stack.

##Mutex Lock ###Data Structure: uthread_mutex_t: unsigned
###Functions:

  1. uthread_mutex_lock: use __sync_bool_compare_and_swap to block current thread and yeid if fail to swap.
  2. uthread_mutex_lock: use __sync_bool_compare_and_swap try to get lock. return -1 if fail
  3. uthread_mutex_unlock: use reversed __sync_bool_compare_and_swap to release the lock

##Cond ###Data Structure: uthread_cond_t: strcut{ int size; int begin; int tail; thread_id* followee; } ###Functions:

  1. uthread_cond_wait: release a mutex lock and wait for cond's signal.
  2. uthread_cond_signal: release a thread that is waiting for this cond.
  3. uthread_cond_broadcast: release all threads that are waiting for this cond.

##Tests

  1. Create a thread to run testfunc2. The thread request a mutex lock, execute some i++ and release the lock. Get its run time and print its return value obtained by uthread_join.
  2. Create four threads running testfunc2. Let main thread waits for them and get their run time.
  3. Use producer/consumer model to test cond function: Create 10 consumers. They are waiting for the same cond. Then create a producer who release 5 signal and then broadcast to release the left ones.<br>

##Make & Run

Make: make in root folder

Run: ./run.sh or run testA in obj folder

Use "sudo apt-get install g++-multilib" to install libc6-dev-i386 package is failto make or can't find the run file.