~ubuntu-branches/ubuntu/trusty/keepalived/trusty

« back to all changes in this revision

Viewing changes to lib/scheduler.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2005-04-29 23:22:40 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050429232240-a8m3jtpi3cvuyyy2
Tags: 1.1.11-3
Added a warning about sarge kernels to README.Debian and 
the package description 

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 *              the thread management routine (thread.c) present in the 
8
8
 *              very nice zebra project (http://www.zebra.org).
9
9
 *
10
 
 * Version:     $Id: scheduler.c,v 1.1.7 2004/04/04 23:28:05 acassen Exp $
 
10
 * Version:     $Id: scheduler.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
11
11
 *
12
12
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
13
13
 *
21
21
 *              as published by the Free Software Foundation; either version
22
22
 *              2 of the License, or (at your option) any later version.
23
23
 *
24
 
 * Copyright (C) 2001-2004 Alexandre Cassen, <acassen@linux-vs.org>
 
24
 * Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
25
25
 */
26
26
 
27
27
#include <signal.h>
30
30
#include "scheduler.h"
31
31
#include "memory.h"
32
32
#include "utils.h"
 
33
#include "signals.h"
 
34
 
 
35
/* global vars */
 
36
thread_master *master = NULL;
33
37
 
34
38
/* Make thread master. */
35
39
thread_master *
43
47
 
44
48
/* Add a new thread to the list. */
45
49
static void
46
 
thread_list_add(thread_list * list, thread * thread)
 
50
thread_list_add(thread_list * list, thread * thread_obj)
47
51
{
48
 
        thread->next = NULL;
49
 
        thread->prev = list->tail;
 
52
        thread_obj->next = NULL;
 
53
        thread_obj->prev = list->tail;
50
54
        if (list->tail)
51
 
                list->tail->next = thread;
 
55
                list->tail->next = thread_obj;
52
56
        else
53
 
                list->head = thread;
54
 
        list->tail = thread;
 
57
                list->head = thread_obj;
 
58
        list->tail = thread_obj;
55
59
        list->count++;
56
60
}
57
61
 
58
62
/* Add a new thread to the list. */
59
63
void
60
 
thread_list_add_before(thread_list * list, thread * point, thread * thread)
 
64
thread_list_add_before(thread_list * list, thread * point, thread * thread_obj)
61
65
{
62
 
        thread->next = point;
63
 
        thread->prev = point->prev;
 
66
        thread_obj->next = point;
 
67
        thread_obj->prev = point->prev;
64
68
        if (point->prev)
65
 
                point->prev->next = thread;
 
69
                point->prev->next = thread_obj;
66
70
        else
67
 
                list->head = thread;
68
 
        point->prev = thread;
 
71
                list->head = thread_obj;
 
72
        point->prev = thread_obj;
69
73
        list->count++;
70
74
}
71
75
 
72
76
/* Add a thread in the list sorted by timeval */
73
77
void
74
 
thread_list_add_timeval(thread_list * list, thread * thread)
 
78
thread_list_add_timeval(thread_list * list, thread * thread_obj)
75
79
{
76
80
        struct _thread *tt;
77
81
 
78
82
        for (tt = list->head; tt; tt = tt->next) {
79
 
                if (timer_cmp(thread->sands, tt->sands) <= 0)
 
83
                if (timer_cmp(thread_obj->sands, tt->sands) <= 0)
80
84
                        break;
81
85
        }
82
86
 
83
87
        if (tt)
84
 
                thread_list_add_before(list, tt, thread);
 
88
                thread_list_add_before(list, tt, thread_obj);
85
89
        else
86
 
                thread_list_add(list, thread);
 
90
                thread_list_add(list, thread_obj);
87
91
}
88
92
 
89
93
/* Delete a thread from the list. */
90
94
thread *
91
 
thread_list_delete(thread_list * list, thread * thread)
 
95
thread_list_delete(thread_list * list, thread * thread_obj)
92
96
{
93
 
        if (thread->next)
94
 
                thread->next->prev = thread->prev;
95
 
        else
96
 
                list->tail = thread->prev;
97
 
        if (thread->prev)
98
 
                thread->prev->next = thread->next;
99
 
        else
100
 
                list->head = thread->next;
101
 
        thread->next = thread->prev = NULL;
 
97
        if (thread_obj->next)
 
98
                thread_obj->next->prev = thread_obj->prev;
 
99
        else
 
100
                list->tail = thread_obj->prev;
 
101
        if (thread_obj->prev)
 
102
                thread_obj->prev->next = thread_obj->next;
 
103
        else
 
104
                list->head = thread_obj->next;
 
105
        thread_obj->next = thread_obj->prev = NULL;
102
106
        list->count--;
103
 
        return thread;
 
107
        return thread_obj;
104
108
}
105
109
 
106
110
/* Free all unused thread. */
107
111
static void
108
112
thread_clean_unuse(thread_master * m)
109
113
{
110
 
        thread *thread;
 
114
        thread *thread_obj;
111
115
 
112
 
        thread = m->unuse.head;
113
 
        while (thread) {
 
116
        thread_obj = m->unuse.head;
 
117
        while (thread_obj) {
114
118
                struct _thread *t;
115
119
 
116
 
                t = thread;
117
 
                thread = t->next;
 
120
                t = thread_obj;
 
121
                thread_obj = t->next;
118
122
 
119
123
                thread_list_delete(&m->unuse, t);
120
124
 
126
130
 
127
131
/* Move thread to unuse list. */
128
132
static void
129
 
thread_add_unuse(thread_master * m, thread * thread)
 
133
thread_add_unuse(thread_master * m, thread * thread_obj)
130
134
{
131
135
        assert(m != NULL);
132
 
        assert(thread->next == NULL);
133
 
        assert(thread->prev == NULL);
134
 
        assert(thread->type == THREAD_UNUSED);
135
 
        thread_list_add(&m->unuse, thread);
 
136
        assert(thread_obj->next == NULL);
 
137
        assert(thread_obj->prev == NULL);
 
138
        assert(thread_obj->type == THREAD_UNUSED);
 
139
        thread_list_add(&m->unuse, thread_obj);
136
140
}
137
141
 
138
142
/* Move list element to unuse queue */
139
143
static void
140
 
thread_destroy_list(thread_master * m, thread_list thread_list)
 
144
thread_destroy_list(thread_master * m, thread_list thread_list_obj)
141
145
{
142
 
        thread *thread;
143
 
 
144
 
        thread = thread_list.head;
145
 
 
146
 
        while (thread) {
 
146
        thread *thread_obj;
 
147
 
 
148
        thread_obj = thread_list_obj.head;
 
149
 
 
150
        while (thread_obj) {
147
151
                struct _thread *t;
148
152
 
149
 
                t = thread;
150
 
                thread = t->next;
 
153
                t = thread_obj;
 
154
                thread_obj = t->next;
151
155
 
152
 
                thread_list_delete(&thread_list, t);
 
156
                thread_list_delete(&thread_list_obj, t);
153
157
                t->type = THREAD_UNUSED;
154
158
                thread_add_unuse(m, t);
155
159
        }
215
219
thread_add_read(thread_master * m, int (*func) (thread *)
216
220
                , void *arg, int fd, long timer)
217
221
{
218
 
        thread *thread;
 
222
        thread *thread_obj;
219
223
 
220
224
        assert(m != NULL);
221
225
 
224
228
                return NULL;
225
229
        }
226
230
 
227
 
        thread = thread_new(m);
228
 
        thread->type = THREAD_READ;
229
 
        thread->id = 0;
230
 
        thread->master = m;
231
 
        thread->func = func;
232
 
        thread->arg = arg;
 
231
        thread_obj = thread_new(m);
 
232
        thread_obj->type = THREAD_READ;
 
233
        thread_obj->id = 0;
 
234
        thread_obj->master = m;
 
235
        thread_obj->func = func;
 
236
        thread_obj->arg = arg;
233
237
        FD_SET(fd, &m->readfd);
234
 
        thread->u.fd = fd;
 
238
        thread_obj->u.fd = fd;
235
239
 
236
240
        /* Compute read timeout value */
237
241
        set_time_now();
238
 
        thread->sands = timer_add_long(time_now, timer);
 
242
        thread_obj->sands = timer_add_long(time_now, timer);
239
243
 
240
244
        /* Sort the thread. */
241
 
        thread_list_add_timeval(&m->read, thread);
 
245
        thread_list_add_timeval(&m->read, thread_obj);
242
246
 
243
 
        return thread;
 
247
        return thread_obj;
244
248
}
245
249
 
246
250
/* Add new write thread. */
248
252
thread_add_write(thread_master * m, int (*func) (thread *)
249
253
                 , void *arg, int fd, long timer)
250
254
{
251
 
        thread *thread;
 
255
        thread *thread_obj;
252
256
 
253
257
        assert(m != NULL);
254
258
 
257
261
                return NULL;
258
262
        }
259
263
 
260
 
        thread = thread_new(m);
261
 
        thread->type = THREAD_WRITE;
262
 
        thread->id = 0;
263
 
        thread->master = m;
264
 
        thread->func = func;
265
 
        thread->arg = arg;
 
264
        thread_obj = thread_new(m);
 
265
        thread_obj->type = THREAD_WRITE;
 
266
        thread_obj->id = 0;
 
267
        thread_obj->master = m;
 
268
        thread_obj->func = func;
 
269
        thread_obj->arg = arg;
266
270
        FD_SET(fd, &m->writefd);
267
 
        thread->u.fd = fd;
 
271
        thread_obj->u.fd = fd;
268
272
 
269
273
        /* Compute write timeout value */
270
274
        set_time_now();
271
 
        thread->sands = timer_add_long(time_now, timer);
 
275
        thread_obj->sands = timer_add_long(time_now, timer);
272
276
 
273
277
        /* Sort the thread. */
274
 
        thread_list_add_timeval(&m->write, thread);
 
278
        thread_list_add_timeval(&m->write, thread_obj);
275
279
 
276
 
        return thread;
 
280
        return thread_obj;
277
281
}
278
282
 
279
283
/* Add timer event thread. */
281
285
thread_add_timer(thread_master * m, int (*func) (thread *)
282
286
                 , void *arg, long timer)
283
287
{
284
 
        thread *thread;
 
288
        thread *thread_obj;
285
289
 
286
290
        assert(m != NULL);
287
291
 
288
 
        thread = thread_new(m);
289
 
        thread->type = THREAD_TIMER;
290
 
        thread->id = 0;
291
 
        thread->master = m;
292
 
        thread->func = func;
293
 
        thread->arg = arg;
 
292
        thread_obj = thread_new(m);
 
293
        thread_obj->type = THREAD_TIMER;
 
294
        thread_obj->id = 0;
 
295
        thread_obj->master = m;
 
296
        thread_obj->func = func;
 
297
        thread_obj->arg = arg;
294
298
 
295
299
        /* Do we need jitter here? */
296
300
        set_time_now();
297
 
        thread->sands = timer_add_long(time_now, timer);
 
301
        thread_obj->sands = timer_add_long(time_now, timer);
298
302
 
299
303
        /* Sort by timeval. */
300
 
        thread_list_add_timeval(&m->timer, thread);
 
304
        thread_list_add_timeval(&m->timer, thread_obj);
301
305
 
302
 
        return thread;
 
306
        return thread_obj;
303
307
}
304
308
 
305
309
/* Add a child thread. */
307
311
thread_add_child(thread_master * m, int (*func) (thread *)
308
312
                 , void * arg, pid_t pid, long timer)
309
313
{
310
 
        thread *thread;
 
314
        thread *thread_obj;
311
315
 
312
316
        assert(m != NULL);
313
317
 
314
 
        thread = thread_new(m);
315
 
        thread->type = THREAD_CHILD;
316
 
        thread->id = 0;
317
 
        thread->master = m;
318
 
        thread->func = func;
319
 
        thread->arg = arg;
320
 
        thread->u.c.pid = pid;
321
 
        thread->u.c.status = 0;
 
318
        thread_obj = thread_new(m);
 
319
        thread_obj->type = THREAD_CHILD;
 
320
        thread_obj->id = 0;
 
321
        thread_obj->master = m;
 
322
        thread_obj->func = func;
 
323
        thread_obj->arg = arg;
 
324
        thread_obj->u.c.pid = pid;
 
325
        thread_obj->u.c.status = 0;
322
326
 
323
327
        /* Compute write timeout value */
324
328
        set_time_now();
325
 
        thread->sands = timer_add_long(time_now, timer);
 
329
        thread_obj->sands = timer_add_long(time_now, timer);
326
330
 
327
331
        /* Sort by timeval. */
328
 
        thread_list_add_timeval(&m->child, thread);
 
332
        thread_list_add_timeval(&m->child, thread_obj);
329
333
 
330
 
        return thread;
 
334
        return thread_obj;
331
335
}
332
336
 
333
337
/* Add simple event thread. */
335
339
thread_add_event(thread_master * m, int (*func) (thread *)
336
340
                 , void *arg, int val)
337
341
{
338
 
        thread *thread;
 
342
        thread *thread_obj;
339
343
 
340
344
        assert(m != NULL);
341
345
 
342
 
        thread = thread_new(m);
343
 
        thread->type = THREAD_EVENT;
344
 
        thread->id = 0;
345
 
        thread->master = m;
346
 
        thread->func = func;
347
 
        thread->arg = arg;
348
 
        thread->u.val = val;
349
 
        thread_list_add(&m->event, thread);
 
346
        thread_obj = thread_new(m);
 
347
        thread_obj->type = THREAD_EVENT;
 
348
        thread_obj->id = 0;
 
349
        thread_obj->master = m;
 
350
        thread_obj->func = func;
 
351
        thread_obj->arg = arg;
 
352
        thread_obj->u.val = val;
 
353
        thread_list_add(&m->event, thread_obj);
350
354
 
351
 
        return thread;
 
355
        return thread_obj;
352
356
}
353
357
 
354
358
/* Add simple event thread. */
355
359
thread *
356
360
thread_add_terminate_event(thread_master * m)
357
361
{
358
 
        thread *thread;
 
362
        thread *thread_obj;
359
363
 
360
364
        assert(m != NULL);
361
365
 
362
 
        thread = thread_new(m);
363
 
        thread->type = THREAD_TERMINATE;
364
 
        thread->id = 0;
365
 
        thread->master = m;
366
 
        thread->func = NULL;
367
 
        thread->arg = NULL;
368
 
        thread->u.val = 0;
369
 
        thread_list_add(&m->event, thread);
 
366
        thread_obj = thread_new(m);
 
367
        thread_obj->type = THREAD_TERMINATE;
 
368
        thread_obj->id = 0;
 
369
        thread_obj->master = m;
 
370
        thread_obj->func = NULL;
 
371
        thread_obj->arg = NULL;
 
372
        thread_obj->u.val = 0;
 
373
        thread_list_add(&m->event, thread_obj);
370
374
 
371
 
        return thread;
 
375
        return thread_obj;
372
376
}
373
377
 
374
378
/* Cancel thread from scheduler. */
375
379
void
376
 
thread_cancel(thread * thread)
 
380
thread_cancel(thread * thread_obj)
377
381
{
378
 
        switch (thread->type) {
 
382
        switch (thread_obj->type) {
379
383
        case THREAD_READ:
380
 
                assert(FD_ISSET(thread->u.fd, &thread->master->readfd));
381
 
                FD_CLR(thread->u.fd, &thread->master->readfd);
382
 
                thread_list_delete(&thread->master->read, thread);
 
384
                assert(FD_ISSET(thread_obj->u.fd, &thread_obj->master->readfd));
 
385
                FD_CLR(thread_obj->u.fd, &thread_obj->master->readfd);
 
386
                thread_list_delete(&thread_obj->master->read, thread_obj);
383
387
                break;
384
388
        case THREAD_WRITE:
385
 
                assert(FD_ISSET(thread->u.fd, &thread->master->writefd));
386
 
                FD_CLR(thread->u.fd, &thread->master->writefd);
387
 
                thread_list_delete(&thread->master->write, thread);
 
389
                assert(FD_ISSET(thread_obj->u.fd, &thread_obj->master->writefd));
 
390
                FD_CLR(thread_obj->u.fd, &thread_obj->master->writefd);
 
391
                thread_list_delete(&thread_obj->master->write, thread_obj);
388
392
                break;
389
393
        case THREAD_TIMER:
390
 
                thread_list_delete(&thread->master->timer, thread);
 
394
                thread_list_delete(&thread_obj->master->timer, thread_obj);
391
395
                break;
392
396
        case THREAD_CHILD:
393
397
                /* Does this need to kill the child, or is that the
394
398
                 * caller's job?
395
399
                 * This function is currently unused, so leave it for now.
396
400
                 */
397
 
                thread_list_delete(&thread->master->child, thread);
 
401
                thread_list_delete(&thread_obj->master->child, thread_obj);
398
402
                break;
399
403
        case THREAD_EVENT:
400
 
                thread_list_delete(&thread->master->event, thread);
 
404
                thread_list_delete(&thread_obj->master->event, thread_obj);
401
405
                break;
402
406
        case THREAD_READY:
403
 
                thread_list_delete(&thread->master->ready, thread);
 
407
                thread_list_delete(&thread_obj->master->ready, thread_obj);
404
408
                break;
405
409
        default:
406
410
                break;
407
411
        }
408
412
 
409
 
        thread->type = THREAD_UNUSED;
410
 
        thread_add_unuse(thread->master, thread);
 
413
        thread_obj->type = THREAD_UNUSED;
 
414
        thread_add_unuse(thread_obj->master, thread_obj);
411
415
}
412
416
 
413
417
/* Delete all events which has argument value arg. */
414
418
void
415
419
thread_cancel_event(thread_master * m, void *arg)
416
420
{
417
 
        thread *thread;
 
421
        thread *thread_obj;
418
422
 
419
 
        thread = m->event.head;
420
 
        while (thread) {
 
423
        thread_obj = m->event.head;
 
424
        while (thread_obj) {
421
425
                struct _thread *t;
422
426
 
423
 
                t = thread;
424
 
                thread = t->next;
 
427
                t = thread_obj;
 
428
                thread_obj = t->next;
425
429
 
426
430
                if (t->arg == arg) {
427
431
                        thread_list_delete(&m->event, t);
432
436
}
433
437
 
434
438
/* Compute the wait timer. Take care of timeouted fd */
435
 
TIMEVAL *
 
439
static void
436
440
thread_compute_timer(thread_master * m, TIMEVAL * timer_wait)
437
441
{
438
442
        TIMEVAL timer_min;
468
472
 
469
473
        if (!TIMER_ISNULL(timer_min)) {
470
474
                timer_min = timer_sub(timer_min, time_now);
471
 
                if (timer_min.tv_sec < 0) {
 
475
                if (timer_min.tv_sec < 0 || TIMER_ISNULL(timer_min)) {
472
476
                        timer_min.tv_sec = 0;
473
477
                        timer_min.tv_usec = 10;
474
478
                }
476
480
                timer_wait->tv_usec = timer_min.tv_usec;
477
481
        } else
478
482
                timer_wait = NULL;
479
 
 
480
 
        return timer_wait;
481
483
}
482
484
 
483
485
/* Fetch next ready thread. */
485
487
thread_fetch(thread_master * m, thread * fetch)
486
488
{
487
489
        int ret;
488
 
        thread *thread;
 
490
        thread *thread_obj;
489
491
        fd_set readfd;
490
492
        fd_set writefd;
491
493
        fd_set exceptfd;
492
 
        TIMEVAL *timer_wait;
 
494
        TIMEVAL timer_wait;
493
495
        int status;
494
496
        sigset_t sigset, dummy_sigset, block_sigset, pending;
495
497
 
506
508
        sigemptyset(&block_sigset);
507
509
        sigaddset(&block_sigset, SIGCHLD);
508
510
 
509
 
        /* Timer allocation */
510
 
        timer_wait = (TIMEVAL *) MALLOC(sizeof (TIMEVAL));
 
511
        /* Timer initialization */
 
512
        memset(&timer_wait, 0, sizeof (TIMEVAL));
511
513
 
512
514
retry:  /* When thread can't fetch try to find next thread again. */
513
515
 
514
516
        /* If there is event process it first. */
515
 
        while ((thread = thread_trim_head(&m->event))) {
516
 
                *fetch = *thread;
517
 
                FREE_PTR(timer_wait);
 
517
        while ((thread_obj = thread_trim_head(&m->event))) {
 
518
                *fetch = *thread_obj;
518
519
 
519
520
                /* If daemon hanging event is received return NULL pointer */
520
 
                if (thread->type == THREAD_TERMINATE) {
521
 
                        thread->type = THREAD_UNUSED;
522
 
                        thread_add_unuse(m, thread);
 
521
                if (thread_obj->type == THREAD_TERMINATE) {
 
522
                        thread_obj->type = THREAD_UNUSED;
 
523
                        thread_add_unuse(m, thread_obj);
523
524
                        return NULL;
524
525
                }
525
 
                thread->type = THREAD_UNUSED;
526
 
                thread_add_unuse(m, thread);
 
526
                thread_obj->type = THREAD_UNUSED;
 
527
                thread_add_unuse(m, thread_obj);
527
528
                return fetch;
528
529
        }
529
530
 
530
531
        /* If there is ready threads process them */
531
 
        while ((thread = thread_trim_head(&m->ready))) {
532
 
                *fetch = *thread;
533
 
                thread->type = THREAD_UNUSED;
534
 
                thread_add_unuse(m, thread);
535
 
                FREE_PTR(timer_wait);
 
532
        while ((thread_obj = thread_trim_head(&m->ready))) {
 
533
                *fetch = *thread_obj;
 
534
                thread_obj->type = THREAD_UNUSED;
 
535
                thread_add_unuse(m, thread_obj);
536
536
                return fetch;
537
537
        }
538
538
 
541
541
         * Calculate select wait timer. Take care of timeouted fd.
542
542
         */
543
543
        set_time_now();
544
 
        timer_wait = thread_compute_timer(m, timer_wait);
 
544
        thread_compute_timer(m, &timer_wait);
545
545
 
546
546
        /* Call select function. */
547
547
        readfd = m->readfd;
571
571
                /* Emulate pselect */
572
572
                sigset_t saveset;
573
573
                sigprocmask(SIG_SETMASK, &sigset, &saveset);
574
 
                ret = select(FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
 
574
                ret = select(FD_SETSIZE, &readfd, &writefd, &exceptfd,
 
575
                             (TIMER_ISNULL(timer_wait)) ? NULL : &timer_wait);
575
576
                sigprocmask(SIG_SETMASK, &saveset, NULL);
576
577
        }
577
578
 
 
579
        /*
 
580
         * When we receive a signal, we only add it to the signal_mask. This
 
581
         * is so that we can run our handler functions in a safe place and
 
582
         * not in, for example, the middle of a list modification.
 
583
         */
 
584
        if (signal_pending())
 
585
                signal_run_callback();
 
586
 
578
587
        /* Update current time */
579
588
        set_time_now();
580
589
 
596
605
                                        DBG("waitpid error: %s", strerror(errno));
597
606
                                        assert(0);
598
607
                                } else {
599
 
                                        thread = m->child.head;
600
 
                                        while (thread) {
 
608
                                        thread_obj = m->child.head;
 
609
                                        while (thread_obj) {
601
610
                                                struct _thread *t;
602
 
                                                t = thread;
603
 
                                                thread = t->next;
 
611
                                                t = thread_obj;
 
612
                                                thread_obj = t->next;
604
613
                                                if (pid == t->u.c.pid) {
605
614
                                                        thread_list_delete(&m->child, t);
606
615
                                                        thread_list_add(&m->ready, t);
616
625
        }
617
626
 
618
627
        /* Timeout children */
619
 
        thread = m->child.head;
620
 
        while (thread) {
 
628
        thread_obj = m->child.head;
 
629
        while (thread_obj) {
621
630
                struct _thread *t;
622
631
 
623
 
                t = thread;
624
 
                thread = t->next;
 
632
                t = thread_obj;
 
633
                thread_obj = t->next;
625
634
 
626
635
                if (timer_cmp(time_now, t->sands) >= 0) {
627
636
                        thread_list_delete(&m->child, t);
631
640
        }
632
641
 
633
642
        /* Read thead. */
634
 
        thread = m->read.head;
635
 
        while (thread) {
 
643
        thread_obj = m->read.head;
 
644
        while (thread_obj) {
636
645
                struct _thread *t;
637
646
 
638
 
                t = thread;
639
 
                thread = t->next;
 
647
                t = thread_obj;
 
648
                thread_obj = t->next;
640
649
 
641
650
                if (FD_ISSET(t->u.fd, &readfd)) {
642
651
                        assert(FD_ISSET(t->u.fd, &m->readfd));
655
664
        }
656
665
 
657
666
        /* Write thead. */
658
 
        thread = m->write.head;
659
 
        while (thread) {
 
667
        thread_obj = m->write.head;
 
668
        while (thread_obj) {
660
669
                struct _thread *t;
661
670
 
662
 
                t = thread;
663
 
                thread = t->next;
 
671
                t = thread_obj;
 
672
                thread_obj = t->next;
664
673
 
665
674
                if (FD_ISSET(t->u.fd, &writefd)) {
666
675
                        assert(FD_ISSET(t->u.fd, &writefd));
681
690
        /*... */
682
691
 
683
692
        /* Timer update. */
684
 
        thread = m->timer.head;
685
 
        while (thread) {
 
693
        thread_obj = m->timer.head;
 
694
        while (thread_obj) {
686
695
                struct _thread *t;
687
696
 
688
 
                t = thread;
689
 
                thread = t->next;
 
697
                t = thread_obj;
 
698
                thread_obj = t->next;
690
699
 
691
700
                if (timer_cmp(time_now, t->sands) >= 0) {
692
701
                        thread_list_delete(&m->timer, t);
696
705
        }
697
706
 
698
707
        /* Return one event. */
699
 
        thread = thread_trim_head(&m->ready);
 
708
        thread_obj = thread_trim_head(&m->ready);
700
709
 
701
710
        /* There is no ready thread. */
702
 
        if (!thread)
 
711
        if (!thread_obj)
703
712
                goto retry;
704
713
 
705
 
        *fetch = *thread;
706
 
        thread->type = THREAD_UNUSED;
707
 
        thread_add_unuse(m, thread);
 
714
        *fetch = *thread_obj;
 
715
        thread_obj->type = THREAD_UNUSED;
 
716
        thread_add_unuse(m, thread_obj);
708
717
 
709
 
        FREE(timer_wait);
710
718
        return fetch;
711
719
}
712
720
 
720
728
 
721
729
/* Call thread ! */
722
730
void
723
 
thread_call(thread * thread)
 
731
thread_call(thread * thread_obj)
724
732
{
725
 
        thread->id = thread_get_id();
726
 
        (*thread->func) (thread);
 
733
        thread_obj->id = thread_get_id();
 
734
        (*thread_obj->func) (thread_obj);
727
735
}
728
736
 
729
737
/* Our infinite scheduling loop */
730
 
extern thread_master *master;
731
 
extern unsigned int debug;
732
738
void
733
739
launch_scheduler(void)
734
740
{
735
 
        thread thread;
 
741
        thread thread_obj;
736
742
 
737
743
        /*
738
744
         * Processing the master thread queues,
739
745
         * return and execute one ready thread.
740
746
         */
741
 
        while (thread_fetch(master, &thread)) {
 
747
        while (thread_fetch(master, &thread_obj)) {
742
748
                /* Run until error, used for debuging only */
743
749
#ifdef _DEBUG_
744
750
                if ((debug & 520) == 520) {
746
752
                        thread_add_terminate_event(master);
747
753
                }
748
754
#endif
749
 
                thread_call(&thread);
 
755
                thread_call(&thread_obj);
750
756
        }
751
757
}