| 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 |
#include <stdlib.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <assert.h> |
| 35 |
|
| 36 |
#include "scheduler.h" |
| 37 |
#include "message.h" |
| 38 |
|
| 39 |
scheduler_t *scheduler_alloc(int nthreads) { |
| 40 |
scheduler_t *sched = (scheduler_t*)malloc(sizeof(scheduler_t)); |
| 41 |
|
| 42 |
sched->nthreads = nthreads; |
| 43 |
sched->ntasks = 0; |
| 44 |
sched->pause = 0; |
| 45 |
sched->disk = -1; |
| 46 |
|
| 47 |
sched->queue[low] = list_alloc(); |
| 48 |
sched->queue[disk] = list_alloc(); |
| 49 |
sched->queue[high] = list_alloc(); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#ifndef OSX |
| 54 |
sched->sem = (sem_t*)malloc(sizeof(sem_t)); |
| 55 |
|
| 56 |
if (sem_init(sched->sem, 0, 0) != 0) { |
| 57 |
fprintf(stderr, "scheduler_alloc: cannot initialise the semaphore\n"); |
| 58 |
exit(1); |
| 59 |
} |
| 60 |
#else |
| 61 |
sem_unlink("ophcrack_scheduler"); |
| 62 |
sched->sem = sem_open("ophcrack_scheduler", O_CREAT | O_EXCL, 0, 0); |
| 63 |
|
| 64 |
if (sched->sem == SEM_FAILED) { |
| 65 |
fprintf(stderr, "scheduler_alloc: cannot initialise the semaphore\n"); |
| 66 |
exit(1); |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
sched->mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); |
| 74 |
pthread_mutex_init(sched->mutex, 0); |
| 75 |
|
| 76 |
sched->cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); |
| 77 |
pthread_cond_init(sched->cond, 0); |
| 78 |
|
| 79 |
sched->thread = (pthread_t*)malloc(nthreads*sizeof(pthread_t)); |
| 80 |
|
| 81 |
return sched; |
| 82 |
} |
| 83 |
|
| 84 |
void scheduler_free(scheduler_t *sched) { |
| 85 |
list_free(sched->queue[disk]); |
| 86 |
list_free(sched->queue[high]); |
| 87 |
list_free(sched->queue[low]); |
| 88 |
|
| 89 |
sem_destroy(sched->sem); |
| 90 |
pthread_mutex_destroy(sched->mutex); |
| 91 |
pthread_cond_destroy(sched->cond); |
| 92 |
|
| 93 |
free(sched->sem); |
| 94 |
free(sched->mutex); |
| 95 |
free(sched->cond); |
| 96 |
|
| 97 |
free(sched->thread); |
| 98 |
free(sched); |
| 99 |
} |
| 100 |
|
| 101 |
int scheduler_start(scheduler_t *sched, void *(*fun)(void*), void *arg) { |
| 102 |
int i, j; |
| 103 |
int nthreads = sched->nthreads; |
| 104 |
pthread_t *thread = sched->thread; |
| 105 |
|
| 106 |
for (i=0; i<nthreads; ++i) |
| 107 |
if (pthread_create(thread+i, 0, fun, arg) != 0) break; |
| 108 |
|
| 109 |
if (i < nthreads) |
| 110 |
for (j=0; j<i; ++j) pthread_cancel(thread[j]); |
| 111 |
|
| 112 |
return i; |
| 113 |
} |
| 114 |
|
| 115 |
void scheduler_pause(scheduler_t *sched) { |
| 116 |
pthread_mutex_t *mutex = sched->mutex; |
| 117 |
int i; |
| 118 |
|
| 119 |
pthread_mutex_lock(mutex); |
| 120 |
sched->pause = 1; |
| 121 |
pthread_mutex_unlock(mutex); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
for (i=0; i<sched->nthreads; ++i) |
| 128 |
sem_post(sched->sem); |
| 129 |
} |
| 130 |
|
| 131 |
void scheduler_continue(scheduler_t *sched) { |
| 132 |
pthread_mutex_t *mutex = sched->mutex; |
| 133 |
pthread_cond_t *cond = sched->cond; |
| 134 |
|
| 135 |
pthread_mutex_lock(mutex); |
| 136 |
sched->pause = 0; |
| 137 |
pthread_cond_broadcast(cond); |
| 138 |
pthread_mutex_unlock(mutex); |
| 139 |
} |
| 140 |
|
| 141 |
void scheduler_wait(scheduler_t *sched) { |
| 142 |
int i; |
| 143 |
int nthreads = sched->nthreads; |
| 144 |
pthread_t *thread = sched->thread; |
| 145 |
|
| 146 |
for (i=0; i<nthreads; ++i) pthread_join(thread[i], 0); |
| 147 |
} |
| 148 |
|
| 149 |
void scheduler_stop(scheduler_t *sched) { |
| 150 |
int i; |
| 151 |
int nthreads = sched->nthreads; |
| 152 |
pthread_t *thread = sched->thread; |
| 153 |
|
| 154 |
for (i=0; i<nthreads; ++i) pthread_cancel(thread[i]); |
| 155 |
} |
| 156 |
|
| 157 |
void scheduler_add(scheduler_t *sched, void *task, prty_t prty) { |
| 158 |
pthread_mutex_t *mutex = sched->mutex; |
| 159 |
list_t *queue = sched->queue[prty]; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
pthread_mutex_lock(mutex); |
| 164 |
|
| 165 |
list_add_tail(queue, task); |
| 166 |
++sched->ntasks; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if (prty != disk) |
| 172 |
sem_post(sched->sem); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
else if (sched->disk == -1 && queue->size == 1) |
| 180 |
sem_post(sched->sem); |
| 181 |
|
| 182 |
pthread_mutex_unlock(mutex); |
| 183 |
} |
| 184 |
|
| 185 |
void *scheduler_get(scheduler_t *sched, int id) { |
| 186 |
void *task = 0; |
| 187 |
pthread_mutex_t *mutex = sched->mutex; |
| 188 |
pthread_cond_t *cond = sched->cond; |
| 189 |
|
| 190 |
list_t **queue = sched->queue; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
while (task == 0) { |
| 200 |
sem_wait(sched->sem); |
| 201 |
pthread_mutex_lock(mutex); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if (sched->pause) { |
| 207 |
message_pause(); |
| 208 |
pthread_cond_wait(cond, mutex); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if (queue[high]->size > 0) |
| 214 |
task = list_rem_head(queue[high]); |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
else if (queue[disk]->size > 0 && sched->disk == -1) { |
| 221 |
sched->disk = id; |
| 222 |
task = list_rem_head(queue[disk]); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
else |
| 229 |
task = list_rem_head(queue[low]); |
| 230 |
|
| 231 |
pthread_mutex_unlock(mutex); |
| 232 |
} |
| 233 |
|
| 234 |
return task; |
| 235 |
} |
| 236 |
|
| 237 |
int scheduler_done(scheduler_t *sched, int id) { |
| 238 |
int ntasks; |
| 239 |
list_t **queue = sched->queue; |
| 240 |
pthread_mutex_t *mutex = sched->mutex; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
pthread_mutex_lock(mutex); |
| 245 |
ntasks = --sched->ntasks; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (sched->disk == id) { |
| 252 |
sched->disk = -1; |
| 253 |
|
| 254 |
if (queue[disk]->size > 0) |
| 255 |
sem_post(sched->sem); |
| 256 |
} |
| 257 |
|
| 258 |
pthread_mutex_unlock(mutex); |
| 259 |
|
| 260 |
return ntasks; |
| 261 |
} |