-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex_impl.cpp
More file actions
executable file
·44 lines (39 loc) · 1.09 KB
/
mutex_impl.cpp
File metadata and controls
executable file
·44 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* mutex_impl.cpp
*
* EECS 482: Project 2, Winter 2019
*
* Jiajun Peng, Youwei Chen, Xuetong Sun
* pjiajun, skylarch, xuetong
*
* mutex helper functions
*/
#include "mutex.h"
#include "mutex_impl.h"
#include "cpu.h"
#include "cpu_impl.h"
#include "thread_impl.h"
#include <stdexcept>
/**
* Modifies: block_queue, ready_queue, mutex ownership.
* Effects: Release mutex.
*/
void mutex::impl::unlock_helper(){
// Throw error if a thread tries to release mutex it does not hold
if(owner != cpu::self()->impl_ptr->current_thread->thread_id){
assert_interrupts_disabled();
cpu::interrupt_enable();
throw std::runtime_error("Cannot unlock a mutex the thread is not holding");
assert_interrupts_enabled();
cpu::interrupt_disable();
}
owner = -1;
// Make the first blocked thread the owner of the mutex;
// Push it to ready queue;
if (!block_queue.empty()) {
thread::impl * temp = block_queue.front();
block_queue.pop();
cpu::self()->impl_ptr->ready_queue.push(temp);
owner = temp->thread_id;
}
}