summaryrefslogtreecommitdiff
path: root/threads.c
blob: 69c9b8fa2017474410e2baef2b2326d51c3598ca (plain)
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
/* Threads handling.
 *
 * (C) 2003 Anope Team
 * Contact us at info@anope.org
 *
 * Please read COPYING and README for furhter details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church. 
 * 
 * $Id: threads.c,v 1.5 2003/07/20 01:15:50 dane Exp $ 
 *
 */

#include "services.h"

#ifdef USE_THREADS

/*************************************************************************/

static Thread *threads;

static int thread_cancel(Thread * thr);

/*************************************************************************/

static int thread_cancel(Thread * thr)
{
    if (pthread_cancel(thr->th))
        return 0;

    if (thr->next)
        thr->next->prev = thr->prev;
    if (thr->prev)
        thr->prev->next = thr->next;
    else
        threads = thr->next;

    return 1;
}

/*************************************************************************/

int thread_create(pthread_t * th, void *(*start_routine) (void *),
                  void *arg)
{
    Thread *thr;

    if (pthread_create(th, NULL, start_routine, arg))
        return 0;
    if (pthread_detach(*th))
        return 0;

    /* Add the thread to our internal list */
    thr = scalloc(sizeof(Thread), 1);
    thr->th = *th;
    thr->next = threads;
    if (thr->next)
        thr->next->prev = thr;
    threads = thr;

    return 1;
}

/*************************************************************************/

int thread_killall(void)
{
    Thread *thr, *next;

    for (thr = threads; thr; thr = next) {
        next = thr;
        if (!thread_cancel(thr))
            return 0;
    }

    return 1;
}

/*************************************************************************/

#endif