-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.h
52 lines (45 loc) · 1.46 KB
/
mutex.h
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
45
46
47
48
49
50
51
52
/*
* mutex.h
*
* ENCE361 HeliProject Group 3
* Created on: 18/03/2021
* Author: CoppyNawaphanarat, Grant Wong, Will Archer
*
* Purpose:
*
* To prevent the race conditions within the codes, so we won't run into deadlocks.
* Therefore, we think that all variables within the ISRs requires a mutex assistance.
*
* To make this works:
*
* - All mutexes must be a static type within their own modules.
* - Only ISRs should be using lock and unlock for their task.
* - Only Non-ISRs should be using mutex_wait until the ISRs unlocks after finishing their tasks.
*
* If this works as we expected, it should be greatly reduce the risk of accessing a variable,
* such as the buffer data, while it is being changed.
*
*/
#ifndef MUTEX_H_
#define MUTEX_H_
/**
* Define the mutex type. Using "volatile" for arise in hardware access (memory-mapped I/O),
* where writing to memory or reading from is used to communicate with peripheral devices.
* Basically, the value may change between different accesses, even if it does not appear to be modified.
*
* For more information: https://en.wikipedia.org/wiki/Volatile_(computer_programming)
*/
typedef volatile bool Mutex;
/**
* Creates a lock within a mutex.
*/
#define mutex_lock(mutex) (mutex = true)
/**
* Unlocks the lock within a mutex.
*/
#define mutex_unlock(mutex) (mutex = false)
/**
* Waiting state for the lock to be release
*/
#define mutex_wait(mutex) while (mutex) continue
#endif /* MUTEX_H_ */