addition of silc.css
[crypto.git] / lib / silcutil / unix / silcunixthread.c
1 /*
2
3   silcunixthread.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 #ifdef SILC_THREADS
25
26 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
27                               bool waitable)
28 {
29   pthread_attr_t attr;
30   pthread_t thread;
31   int ret;
32
33   SILC_LOG_DEBUG(("Creating new thread"));
34
35   if (!start_func)
36     return NULL;
37
38   if (pthread_attr_init(&attr)) {
39     SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
40     return NULL;
41   }
42
43   if (pthread_attr_setdetachstate(&attr,
44                                   waitable ? PTHREAD_CREATE_JOINABLE : 
45                                   PTHREAD_CREATE_DETACHED)) {
46     SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
47     pthread_attr_destroy(&attr);
48     return NULL;
49   }
50
51   ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func, 
52                        context);
53   if (ret) {
54     SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
55     pthread_attr_destroy(&attr);
56     return NULL;
57   }
58
59   pthread_attr_destroy(&attr);
60
61   SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
62
63   return (SilcThread)thread;
64 }
65
66 void silc_thread_exit(void *exit_value)
67 {
68   pthread_exit(exit_value);
69 }
70
71 SilcThread silc_thread_self(void)
72 {
73   pthread_t self = pthread_self();
74   return (SilcThread)self;
75 }
76
77 bool silc_thread_wait(SilcThread thread, void **exit_value)
78 {
79   SILC_LOG_DEBUG(("Waiting for thread %p", thread));
80   if (!pthread_join(*(pthread_t *)thread, exit_value))
81     return TRUE;
82   return FALSE;
83 }
84
85 #endif /* SILC_THREADS */