5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2005 Pekka Riikonen
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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcincludes.h"
23 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
31 SILC_LOG_DEBUG(("Creating new thread"));
36 if (pthread_attr_init(&attr)) {
37 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
41 if (pthread_attr_setdetachstate(&attr,
42 waitable ? PTHREAD_CREATE_JOINABLE :
43 PTHREAD_CREATE_DETACHED)) {
44 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
45 pthread_attr_destroy(&attr);
49 ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
52 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
53 pthread_attr_destroy(&attr);
57 pthread_attr_destroy(&attr);
59 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
61 return (SilcThread)thread;
63 /* Call thread callback immediately */
64 (*start_func)(context);
69 void silc_thread_exit(void *exit_value)
72 pthread_exit(exit_value);
76 SilcThread silc_thread_self(void)
79 pthread_t self = pthread_self();
80 return (SilcThread)self;
86 bool silc_thread_wait(SilcThread thread, void **exit_value)
89 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
90 if (!pthread_join(*(pthread_t *)thread, exit_value))