xv6 machine problem 2: Kernel threads

Objectives

For this machine problem you will be adding more system calls to xv6 that:

  1. Support kernel-level threading, so that concurrency within a single user-level process is possible.
  2. Provide a basic synchronization mechanism -- the mutex lock -- for said threads to use.

Threading API

You will implement the following system calls to support thread creation and management:

int thread_create(void (*tmain)(void *), void *stack, void *arg);

int thread_join(void **stack);

Mutex

To allow your newly minted threads to coordinate critical sections and safely share data, you will implement the following system calls that expose a mutex lock. The lock will be implemented internally using a spinlock.

int mtx_create(int locked);

int mtx_lock(int lock_id);

int mtx_unlock(int lock_id);

Note that you can build arbitrarily more sophisticated synchronization mechanisms (e.g., semaphores) in userspace by leveraging this mutex lock.

Testing

To test your code, you should write a program that implements the producer/consumer problem by sharing a bounded buffer between a parent and child (or sibling) threads, using the mutex as a primitive for implementing synchronization.

Add this program to your codebase and the Makefile (see how getcount was added for reference), and be sure to commit and push it as part of your submission.

Submission

As before, submit your work with the command git push origin master, after committing and testing all your changes.