-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.cpp
More file actions
executable file
·62 lines (55 loc) · 1.59 KB
/
shared.cpp
File metadata and controls
executable file
·62 lines (55 loc) · 1.59 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
// the shared.cpp file, this file handles contains shared methods
// used throughout the program, also defines the mutex struct
// which holds all mutexes
#include "includes.hpp"
#include "shared.hpp"
// get the current time in milliseconds
long int get_current_time(){
struct timeval t;
gettimeofday(&t, NULL);
long int milli = t.tv_sec * 1000 + t.tv_usec / 1000;
return milli;
}
// converts the state of a task to a string
string state_to_string(enum State s){
if (s == WAIT){
return "WAIT";
}
if (s == RUN){
return "RUN";
}
if (s == IDLE){
return "IDLE";
}
return "";
}
// Initializes all mutexes
void Mutexes::initialize_mutexes(){
this->initalize_mutex(&this->printing_mutex);
this->initalize_mutex(&this->resources_mutex);
}
// ***the next 3 methods were influenced by resources on eclass***
// Initialize a mutex and perform error checking
void Mutexes::initalize_mutex(pthread_mutex_t* mutex){
int rval = pthread_mutex_init(mutex, NULL);
if (rval){
perror("mutex initiliazation failed");
exit(1);
}
}
// lock a mutex and perform error checking
void Mutexes::lock_mutex(pthread_mutex_t* mutex){
int rval = pthread_mutex_lock(mutex);
if (rval){
perror("mutex locking failed");
exit(1);
}
}
// unlock a mutex and perform error checking
void Mutexes::unlock_mutex(pthread_mutex_t* mutex){
int rval = pthread_mutex_unlock(mutex);
if (rval){
perror("mutex unlocking failed");
exit(1);
}
}