-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
225 lines (186 loc) · 5.7 KB
/
scheduler.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "scheduler.h"
#ifdef USE_PTHREAD
# define lock(l) do { \
useconds_t delay = g_MIN_DELAY; \
while (pthread_mutex_trylock(q->lock) != 0) { \
usleep(delay); \
if (delay < g_MAX_DELAY) \
delay *= 2; \
} \
} while (0)
# define trylock(l) do { \
pthread_mutex_trylock( (l) ) \
} while (0)
# define unlock(l) pthread_mutex_unlock( (l) )
# define lock_init(l) pthread_mutex_init( (l), NULL)
#else
# define lock(l) (void)0
# define unlock(l) (void)0
# define lock_init(l) (void)0
#endif // USE_PTHREAD
const useconds_t g_MAX_DELAY = 1000, // cannot wait for more than 1 millisecond
g_MIN_DELAY = 10; // cannot wait for less than 10 microseconds
task_t g_EMPTY_TASK = {.code = EMPTY_CODE, .data = EMPTY_DATA};
taskq_t* g_QUEUE;
/*
* Simple task queue function that gets the next task from the FIFO queue.
* XXX FIXME Note: this function is *NOT* thread-safe!
*/
task_t* queue_pop(taskq_t* q)
{
/* Thread-safety done here if macro is set */
lock(q->lock);
// Return empty task sentinel if queue is empty or not allocated
if (NULL == q || 0 == q->n_tasks) {
DEBUG(fprintf(stderr, "QUEUE IS EMPTY!\n"));
unlock(q->lock);
return &g_EMPTY_TASK;
}
// remove the task from the queue; store it in task ptr
task_t* t = q->tasks[q->head];
--q->n_tasks;
q->tasks[q->head] = &g_EMPTY_TASK;
q->head = (q->head + 1) % TASKQ_MAX_SZ;
unlock(q->lock);
DEBUG({
// the following should be useless -- this is for debugging purposes
if (q->head == q->tail) // queue is empty !
q->n_tasks = 0; // should already be the case
});
return t;
}
/*
* Simple task queue function that puts a new task at the tail of the FIFO queue
* Note: this function is *NOT* thread-safe!
*/
int queue_push(taskq_t* q, task_t* task)
{
/* thread-safety here, if macro is set */
lock(q->lock);
if ( true == QUEUE_IS_FULL(q) ) {
DEBUG(
fprintf(stderr,
"QUEUE IS FULL! Details:\ntail = %lu, head = %lu, (tail-head) mod %lu = %lu\n",
q->tail, q->head, TASKQ_MAX_SZ, (q->tail-q->head)%TASKQ_MAX_SZ));
unlock(q->lock);
return -1; // error : task queue is full !
}
DEBUG({
/* Debug code to check that the task slot is indeed empty. */
if (! TASK_IS_EMPTY(q->tasks[q->tail]) ) {
task_t *t = q->tasks[q->tail];
fprintf(stderr,
"The task at index %lu is not empty! Address is %p (code: %p, data: %p)\n",
q->tail, (void*)t, (void*)t->code, (void*)t->data);
}
});
q->tasks[q->tail] = task;
q->tail = (q->tail + 1) % TASKQ_MAX_SZ;
++q->n_tasks;
unlock(q->lock);
return 0; // everything went fine
}
task_t* task_create(code_t code, data_t data)
{
task_t* t = malloc(sizeof(task_t));
if (!t) {
fprintf(stderr, "%s:%d:\t", __FILE__, __LINE__);
perror("malloc");
exit(errno);
}
*t = (task_t){ .code = code, .data = data };
DEBUG(printf("Created task %p (%p, %p)\n", (void*)t, (void*)t->code, (void*)t->data));
return t;
}
void task_destroy(task_t* t)
{
memset(t,0,sizeof(task_t));
if (t) free(t);
}
taskq_t* queue_create(void)
{
// allocate memory and sets it to all zeros
taskq_t* q = calloc(1ul, sizeof(taskq_t));
// error management: quit gracefully if allocation fails
if (!q) {
fprintf(stderr, "%s:%d:\t", __FILE__, __LINE__);
perror("malloc");
exit(errno);
}
DEBUG({
/* Initializes task slot to the address of an empty task sentinel */
for (size_t i = 0; i < TASKQ_MAX_SZ; ++i)
q->tasks[i] = &g_EMPTY_TASK;
});
lock_init(q->lock);
q->pause = false;
q->stop = false;
return q;
}
void queue_destroy(taskq_t* q)
{
memset(q,0,sizeof(taskq_t));
if (q != NULL) free(q);
}
void queue_stop(taskq_t* q)
{
q->stop = true;
}
static inline bool queue_is_poppable(taskq_t* q) {
bool state = 0;
lock(q->l);
state = q->n_tasks != 0 && q->pause == false;
unlock(q->lock);
return state;
}
void schedule(taskq_t* q)
{
useconds_t delay = g_MIN_DELAY;
while (! q->stop) {
while ( false == queue_is_poppable(q) )
{
usleep(delay);
if (delay < g_MAX_DELAY)
delay *= 2;
}
delay = g_MIN_DELAY;
task_t* t = queue_pop(q);
if ( false == TASK_IS_EMPTY(t) ) {
// check and set flags to reconfigure FPGA with new bitstream
HW_TASK_INPUT_SIGNAL_CHECK();
t->code(t->data);
// check for end-of-task flag to be set by HW task
HW_TASK_OUTPUT_SIGNAL_CHECK();
task_destroy(t);
}
}
}
int g_CNT = 0;
void* goodbye(void* arg)
{
printf("good-bye, %s!\n", (char*)arg);
--g_CNT;
if (g_CNT <= 0)
queue_stop(g_QUEUE);
return NULL;
}
void* hello(void* arg)
{
printf("Hello, %s!\n", (char*)arg);
queue_push(g_QUEUE, task_create(goodbye,arg));
return NULL;
}
int main(int argc, char** argv)
{
g_QUEUE = queue_create();
g_CNT = argc-1;
DEBUG(printf("argc = %d\n", argc));
for (int i = 1; i < argc; ++i) {
DEBUG(printf("i = %d\n", i));
queue_push(g_QUEUE, task_create(hello, argv[i]));
}
DEBUG(printf("Queue size = %lu\n", g_QUEUE->n_tasks));
schedule(g_QUEUE);
queue_destroy(g_QUEUE);
return EXIT_SUCCESS;
}