-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwthread_queue.c
More file actions
85 lines (69 loc) · 1.31 KB
/
dwthread_queue.c
File metadata and controls
85 lines (69 loc) · 1.31 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
#include "dwthread_queue.h"
#include "dwthread.h"
#include <stdio.h>
#define MAX_QUEUE_LEN 1024
static dwt_task_t *task_queue[MAX_QUEUE_LEN] = {NULL};
static int current_slot = 0;
static int next_slot = 0;
/**
* Add a new thread into the queue.
*/
int dw_inqueue(dwt_task_t *task)
{
if (current_slot >= MAX_QUEUE_LEN) {
return -1;
}else {
task_queue[current_slot] = task;
current_slot++;
}
return 0;
}
/**
* Find a specific thread.
*
* return:
* The task_t structure of thread tid.
*/
dwt_task_t *dw_find(dwt_tid_t tid)
{
int i;
for (i = 0; i <= current_slot; i++) {
if (task_queue[i] && task_queue[i]->tid == tid) {
return task_queue[i];
}
}
return NULL;
}
/**
* Get the next runnable thread.
*
* This is where the whole schedule policy is
* implemented.
*/
dwt_task_t *dw_next()
{
int cnt = 0;
while(1) {
next_slot = (next_slot + 1) % MAX_QUEUE_LEN;
if (task_queue[next_slot]
&& task_queue[next_slot]->statue == DWT_STATUE_RUNNABLE) {
break;
}
}
return task_queue[next_slot];
}
/**
* Remove a specific thread from queue.
*/
dwt_task_t *dw_dequeue(dwt_tid_t tid)
{
int i;
for (i = 0; i <= current_slot; i++) {
if (task_queue[i] && task_queue[i]->tid == tid) {
dwt_task_t *tmp = task_queue[i];
task_queue[i] = NULL;
return tmp;
}
}
return NULL;
}