-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitqueue.cpp
64 lines (53 loc) · 979 Bytes
/
waitqueue.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
#include <queue>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include "waitqueue.h"
using namespace std;
WAITQUEUE::WAITQUEUE() {
idle_prob = rand() % 80 + 20;
pthread_mutex_init(&idle_mutex, NULL);
}
void WAITQUEUE::push(TASK &task){
pthread_mutex_lock(&idle_mutex);
idle_queue.push(task);
pthread_mutex_unlock(&idle_mutex);
return;
}
int WAITQUEUE::size()
{
return idle_queue.size();
}
TASK WAITQUEUE::pop(){
TASK task;
pthread_mutex_lock(&idle_mutex);
task = idle_queue.front();
idle_queue.pop();
pthread_mutex_unlock(&idle_mutex);
return task;
}
bool WAITQUEUE::empty()
{
pthread_mutex_lock(&idle_mutex);
bool ret = idle_queue.empty();
pthread_mutex_unlock(&idle_mutex);
return ret;
}
int WAITQUEUE::get_id()
{
int ret=0;
if(!idle_queue.empty())
{
ret = idle_queue.front().id;
}
return ret;
}
int WAITQUEUE::get_last()
{
int ret = -1;
if(!idle_queue.empty())
{
ret = idle_queue.front().last_cpu;
}
return ret;
}