MIT6.S081 Lab9
Lab9 – Multithreading
User thread implementation
In this section, we will implement a user-level thread. Like many user-level thread libraries do, it should have its ownpc, reregister and stack
. For each thread, lab has already written the storage area in the struct thread
.
But because we still needs to save / restore the register through thread_switch
, we also need a struct thread_context
to store register status.
1 | struct thread_context{ |
Like what kernel does in kernel/swtch.S(), we copy the context switch assembly code to save ra, sp and callee saved registers
.
In our implementation, ra
is used to store the return address of thread_switch.S namely the last switch out point’s address. Cooperates with ra, sp
provides stack pointer points to stack which stores more variables on stack.
Finally, we have to define where ra
and sp
starts.
D note sp
grows from high to low, so we need to set sp = &state
and ra
as the address of func
Using thread
Last two sections require us to do coding on pthread. Some main API are following:
1 | pthread_mutex_t lock; // declare a lock |
Actually I don’t want talk much about these because they are so easy for me? Maybe Barrier
is worthy a concise description.
To make all threads block till all of them synchronize at one point, we need pthread_cond_wait()
and pthread_cond_broadcase()
.
In each round, if reached thread number are not enough, then they call cond_wait()
to release the lock and sleep. Till the last thread reaches, it increment round and set 0 the thread counter then cond_broadcast()
.
Note, don’t forget to unlock properly.