Debian Codegraph @

src/core/scheduler.c

raw symbols SCIP
1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot
10 *
11 * Ophcrack is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Ophcrack is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Ophcrack; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * This program is released under the GPL with the additional exemption
26 * that compiling, linking, and/or using OpenSSL is allowed.
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 /* Create and initialise the semaphore. */
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 /* Create and initialise the mutex and conditional variable, and
71 allocate space to store the threads. */
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 // Increment the semaphore by the number of threads which could
124 // potentially be waiting on it. This is necessary to avoid having
125 // threads waiting on the semaphore while in pause.
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 /* Add the task to the appropriate queue. */
162
163 pthread_mutex_lock(mutex);
164
165 list_add_tail(queue, task);
166 ++sched->ntasks;
167
168 /* If the priorty is not disk, then the task is exectuable. We
169 therefore increase the semaphore to reflect that. */
170
171 if (prty != disk)
172 sem_post(sched->sem);
173
174 /* If somebody is acessing the disk at the moment, then the newly
175 added disk task is not executable and therefore we do not
176 increase the semaphore. Otherwise, we increase the semaphore only
177 if the size of the disk queue is equal to one. */
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 /* We wait until an executable task is available. Although the
193 semaphore might suggest that there are still some tasks in the
194 queues, there might be actually nothing left. This might happen
195 if, for example, during a pause, the scheduler has been
196 reset. We therefore add a while in order to guaranty that when
197 the function exits, a task is returned. */
198
199 while (task == 0) {
200 sem_wait(sched->sem);
201 pthread_mutex_lock(mutex);
202
203 /* If we are in pause mode, then we wait until the conditional
204 variable is signaled. */
205
206 if (sched->pause) {
207 message_pause();
208 pthread_cond_wait(cond, mutex);
209 }
210
211 /* Pick a task from the high priority queue if it is not empty. */
212
213 if (queue[high]->size > 0)
214 task = list_rem_head(queue[high]);
215
216 /* Otherwise, if the disk queue is not empty and nobody is
217 accessing the disk at the moment, then pick a task from the
218 disk queue. */
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 /* Finally, if none of the above cases where taken, then pick a
226 task from the low priority queue. */
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 /* Decrease the task counter. */
243
244 pthread_mutex_lock(mutex);
245 ntasks = --sched->ntasks;
246
247 /* If the calling thread was dealing with the disk, then this means
248 that a disk task can now be executed. We therefore increase the
249 semaphore only if the disk queue is not empty. */
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 }