-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.h
More file actions
106 lines (82 loc) · 2.44 KB
/
Thread.h
File metadata and controls
106 lines (82 loc) · 2.44 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef THREAD_H
#define THREAD_H
#include "uthreads.h"
#include <setjmp.h>
#include <signal.h>
#define SAVE_SIGS 1 // Save the signal mask
#define MAIN_THREAD_TID 0
enum State
{
RUNNING, READY, BLOCKED
};
class Thread
{
public:
/**
* Creates a new READY thread.
* @param tid The id of the thread.
* @param entry_point The function to run when the thread is scheduled.
*/
Thread(int tid, thread_entry_point entry_point);
/**
* @return The thread's tid.
*/
int get_tid() const;
/**
* @return The thread's state.
*/
State get_state() const;
/**
* Sets the state of the thread.
* @param newState The new state to set.
*/
void set_state(State newState);
/**
* Increments the number of quantums the thread has been running.
*/
void inc_quantum_running();
/**
* @return The number of quantums the thread has been running.
*/
int get_quantum_running() const;
/**
* Sets the number of quantums the thread will be sleeping.
* @param quantum_sleeping The amount of time the thread will be sleeping.
*/
void set_quantum_sleeping(int quantum_sleeping);
/**
* @return The number of quantums the thread will be sleeping.
*/
int get_quantum_sleeping() const;
/**
* Decrements the number of quantums the thread will be sleeping.
*/
void dec_quantum_sleeping();
/**
* @brief Check if the thread is sleeping.
* @return \c true of the thread is sleeping, \c false otherwise.
*/
bool is_sleeping() const;
/**
* @return The thread's environment.
*/
sigjmp_buf *get_env();
/**
* Sets the is_blocked flag of the thread.
* @param is_blocked The new value for the is_blocked flag.
*/
void set_is_blocked(bool is_blocked);
/**
* @return The value of the is_blocked flag.
*/
bool get_is_blocked() const;
private:
const int tid; // Thread ID
State state; // Thread state
const char stack[STACK_SIZE]{}; // Thread stack
sigjmp_buf env{}; // Thread environment for setjmp/longjmp
int quantums_running; // Number of quantums the thread has been running
int quantum_sleeping; // Number of quantums the thread will be sleeping
bool is_blocked; // Flag indicating if the thread is blocked
};
#endif //THREAD_H