/
dccthread.c
282 lines (219 loc) · 6.79 KB
/
dccthread.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* By Thiago Silva */
#include "dccthread.h"
/* Variable that keeps the index of the thread running currently. */
int current_thread;
/* Variables that keep the final position of the queue */
int THREAD_QUEUE_FINAL_POS;
/* Queue that keeps the threads ready to execute */
dccthread_t *thread_ready_queue[THREAD_QUEUE_SIZE];
/* The two main threads of the program, initialized at the init function */
ucontext_t manager, exiter;
/* Variable used to create new thread id's */
int neueId;
/* TIMER VARIABLES */
struct sigevent timer_event;
struct sigaction timerAction, oldTimerAction;
struct itimerspec deltaTime;
timer_t timer;
sig_atomic_t is_it_manager_running;
/* END - TIMER VARIABLES */
/******************* NOT LISTED *******************/
/* Just returns 1 if the queue is empty or 0 otherwise. */
int is_thread_queue_empty()
{
return current_thread == THREAD_QUEUE_FINAL_POS;
}
/* This function returns 1 if the queue is full. */
int is_thread_queue_full()
{
return current_thread == (THREAD_QUEUE_FINAL_POS+1)%THREAD_QUEUE_SIZE;
}
/* This method initialized the timer */
void init_timer()
{
timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGALRM;
timerAction.sa_handler = (void (*)())dccthread_yield;
timerAction.sa_flags = 0;
deltaTime.it_interval.tv_sec = deltaTime.it_value.tv_sec = 0;
deltaTime.it_value.tv_nsec = deltaTime.it_interval.tv_nsec = 10000000LL;
sigaction(SIGALRM, &timerAction, &oldTimerAction);
timer_create(CLOCK_PROCESS_CPUTIME_ID, &timer_event, &timer);
timer_settime(&timer, 0, &deltaTime, NULL);
}
/* This function send the current thread to the end of the queue. */
int send_to_the_end()
{
thread_ready_queue[THREAD_QUEUE_FINAL_POS] = thread_ready_queue[current_thread];
thread_ready_queue[current_thread]=NULL;
int old_index=THREAD_QUEUE_FINAL_POS;
THREAD_QUEUE_FINAL_POS++; THREAD_QUEUE_FINAL_POS%=THREAD_QUEUE_SIZE;
return old_index;
}
/* Method that blocks interrupts */
void blockInterrupts()
{
sigset_t signal_set;
sigaddset(&signal_set, SIGALRM);
sigprocmask(SIG_BLOCK, &signal_set, NULL);
}
/* Unblocks the interrupts */
void unblockInterrupts()
{
sigset_t signal_set;
sigaddset(&signal_set, SIGALRM);
sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
}
/* This function releases every thread wating for the [index] thread */
void free_waitings(int index)
{
int i=index;
while(i != THREAD_QUEUE_FINAL_POS)
{
if(thread_ready_queue[i]->waiting_for == thread_ready_queue[index]->id)
thread_ready_queue[i]->waiting_for = -1;
i=(i+1)%THREAD_QUEUE_SIZE;
}
}
void nextThread()
{
is_it_manager_running=1;
if(is_thread_queue_empty()) return;
#ifdef DEBUG
printf("Running: nextThread\n");
#endif
while(1)
{
current_thread++;
current_thread%=THREAD_QUEUE_SIZE;
if(is_thread_queue_empty()) return;
#ifdef DEBUG
printf("%s waiting for %d\n", thread_ready_queue[current_thread]->name, thread_ready_queue[current_thread]->waiting_for);
#endif
if(thread_ready_queue[current_thread]->waiting_for != -1)
send_to_the_end();
else break;
}
/*
* Here I unblock the interrupts just to let
* my check work properly on the yield method.
*/
unblockInterrupts();
getcontext(&manager);
makecontext(&manager, nextThread, 0);
#ifdef DEBUG
printf("Running \"%s\"\n", thread_ready_queue[current_thread]->name);
#endif
is_it_manager_running=0;
setcontext(&(thread_ready_queue[current_thread]->context));
}
/* Method that initializes the stack of a context */
void setStackProperties(ucontext_t *context)
{
context->uc_stack.ss_sp = malloc(SIGSTKSZ);
context->uc_stack.ss_size = SIGSTKSZ;
context->uc_stack.ss_flags = 0;
}
/* This method frees every chunk of memory allocated for the [index] thread. */
void free_dcc_thread(int index)
{
free(thread_ready_queue[index]);
thread_ready_queue[index]=NULL;
}
/* This function checks if the [id] thread is done running. */
int is_thread_done(int id)
{
int i=current_thread;
while(i != THREAD_QUEUE_FINAL_POS)
{
if(thread_ready_queue[i]->id == id) return 0;
i=(i+1)%THREAD_QUEUE_SIZE;
}
return 1;
}
/******************* END - NOT LISTED *******************/
void dccthread_init(void (*func)(int), int param)
{
init_timer();
THREAD_QUEUE_FINAL_POS=neueId=0;
current_thread=-1;
memset(thread_ready_queue, 0, sizeof(dccthread_t *) * THREAD_QUEUE_SIZE);
getcontext(&manager);
getcontext(&exiter);
manager.uc_link = &exiter;
setStackProperties(&manager);
setStackProperties(&exiter);
makecontext(&manager, nextThread, 0);
dccthread_create("Main", func, param);
swapcontext(&exiter, &manager);
exit(EXIT_SUCCESS);
}
dccthread_t * dccthread_create(const char *name, void (*func)(int), int param)
{
if(is_thread_queue_full())
return NULL;
if(thread_ready_queue[THREAD_QUEUE_FINAL_POS] != NULL)
free_dcc_thread(THREAD_QUEUE_FINAL_POS);
thread_ready_queue[THREAD_QUEUE_FINAL_POS] = (dccthread_t *) malloc(sizeof(dccthread_t *));
getcontext(&(thread_ready_queue[THREAD_QUEUE_FINAL_POS]->context));
thread_ready_queue[THREAD_QUEUE_FINAL_POS]->context.uc_link = &manager;
setStackProperties(&(thread_ready_queue[THREAD_QUEUE_FINAL_POS]->context));
makecontext(&(thread_ready_queue[THREAD_QUEUE_FINAL_POS]->context), (void (*) (void)) func, 1, param);
thread_ready_queue[THREAD_QUEUE_FINAL_POS]->name = name;
thread_ready_queue[THREAD_QUEUE_FINAL_POS]->waiting_for=-1;
thread_ready_queue[THREAD_QUEUE_FINAL_POS]->id = neueId;
neueId++;
if(neueId < 0) neueId=0;
dccthread_t *ret = thread_ready_queue[THREAD_QUEUE_FINAL_POS];
THREAD_QUEUE_FINAL_POS++;
THREAD_QUEUE_FINAL_POS%=THREAD_QUEUE_SIZE;
return ret;
}
void dccthread_yield(void)
{
if(is_it_manager_running) return;
is_it_manager_running=1;
blockInterrupts();
int old_index = send_to_the_end();
#ifdef DEBUG
printf("Swapping %d -> Manager\n", thread_ready_queue[old_index]->id);
#endif
swapcontext(&(thread_ready_queue[old_index]->context), &manager);
}
void dccthread_exit(void)
{
#ifdef DEBUG
printf("%d exited\n", thread_ready_queue[current_thread]->id);
#endif
free_waitings(current_thread);
setcontext(&manager);
}
void dccthread_wait(dccthread_t *tid)
{
if(tid==NULL)
{
#ifdef DEBUG
printf("NULL pointer on dccthread_wait function\n");
#endif
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("Asking %d to wait for %d\n", thread_ready_queue[current_thread]->id, tid->id);
#endif
if(is_thread_done(tid->id))
return;
int index = send_to_the_end();
thread_ready_queue[index]->waiting_for = tid->id;
swapcontext(&(thread_ready_queue[index]->context), &manager);
}
void dccthread_sleep(struct timespec ts)
{}
dccthread_t * dccthread_self(void)
{
// Return the running thread.
return thread_ready_queue[current_thread];
}
const char *dccthread_name(dccthread_t *tid)
{
return tid->name;
}