-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cpp
More file actions
126 lines (98 loc) · 2.09 KB
/
thread.cpp
File metadata and controls
126 lines (98 loc) · 2.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "thread.h"
#define STACK_SIZE 4096
#define JB_SP 6
#define JB_PC 7
#define READY 0
#define RUNNING 1
#define BLOCKED 2
#ifdef __x86_64__
/* code for 64 bit Intel arch */
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%fs:0x30,%0\n"
"rol $0x11,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#else
/* code for 32 bit Intel arch */
typedef unsigned int address_t;
#define JB_SP 4
#define JB_PC 5
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%gs:0x18,%0\n"
"rol $0x9,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#endif
thread::thread (int id , thread_entry_point f)
{
_id = id;
_state = READY;
_sleep = false;
_time_to_sleep = 0;
_quantum_counter = 0;
_f = f;
_stack = new char[STACK_SIZE];
_sp = (address_t) _stack + STACK_SIZE - sizeof(address_t);
_pc = (address_t)f;
sigsetjmp(_env, 1);
(_env->__jmpbuf)[JB_SP] = translate_address(_sp);
(_env->__jmpbuf)[JB_PC] = translate_address(_pc);
sigemptyset(&_env->__saved_mask);
}
thread::~thread ()
{
delete [] _stack;
_stack = nullptr;
}
int thread::get_id ()const
{
return _id;
}
int thread::get_state ()const
{
return _state;
}
int thread::get_quantum_counter () const
{
return _quantum_counter;
}
bool thread::get_sleep ()
{
return _sleep;
}
int thread::get_time_to_sleep ()
{
return _time_to_sleep;
}
void thread::set_state (int change_to)
{
_state = change_to;
}
void thread::set_sleep (bool state)
{
_sleep = state;
}
void thread::set_time_to_sleep (int q)
{
_time_to_sleep = q;
}
void thread::increace_quantum_counter ()
{
_quantum_counter++;
}
void thread::decreace_time_to_sleep ()
{
_time_to_sleep--;
}