-
Notifications
You must be signed in to change notification settings - Fork 1
/
uthreads_helpers.cpp
85 lines (72 loc) · 1.58 KB
/
uthreads_helpers.cpp
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
#include <iostream>
#include <signal.h>
#include <cstdlib>
#include "uthreads_helpers.h"
using namespace std;
// initialize a new uthread
UThread::UThread()
{
total_qntm = 0;
state = READY;
}
// destructor for the uthread - empty because there is no need to release anything.
UThread::~UThread()
{
}
// get the uthread's state
int UThread::get_state() const
{
return state;
}
// get the uthread's stack
const char* UThread::get_stack()
{
return stack;
}
// get the uthread's enviroment
sigjmp_buf& UThread::get_env()
{
return env;
}
// set new state to the uthread
void UThread::set_state(int new_state)
{
state = new_state;
}
// save the entire uthread enviroment
bool UThread::save_env()
{
int ret_val = sigsetjmp(env,1);
return ret_val;
}
// load the uthread's enviroment
void UThread::load_env(){
siglongjmp(env,1);
}
// increase the total quantums elapsed counter
void UThread::qntm_elapse()
{
total_qntm++;
}
// get the total quantum elapsed during the runnign time of this thread
int UThread::get_qntms() const
{
return total_qntm;
}
// Handles errors reports as describe in ex2 description:
// prints the suitable prefix to the error massage according to the is_system argument, and
// appends to that the given msg string.
// returning the predefine FAILURE_VAL if is_system is false, else: exiting with "exit(1)"
int handle_err(std::string msg, bool is_system)
{
if(is_system)
{
cerr<<"system error: "<<msg<<endl;
exit(1);
}
else
{
cerr<<"thread library error: "<<msg<<endl;
}
return FAILURE_VAL;
}