5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 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; either version 2 of the License, or
12 (at your option) any later version.
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.
22 #include "silcincludes.h"
24 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
32 SILC_LOG_DEBUG(("Creating new thread"));
37 if (pthread_attr_init(&attr)) {
38 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
42 if (pthread_attr_setdetachstate(&attr,
43 waitable ? PTHREAD_CREATE_JOINABLE :
44 PTHREAD_CREATE_DETACHED)) {
45 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
46 pthread_attr_destroy(&attr);
50 ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
53 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
54 pthread_attr_destroy(&attr);
58 pthread_attr_destroy(&attr);
60 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
62 return (SilcThread)thread;
64 /* Call thread callback immediately */
65 (*start_func)(context);
70 void silc_thread_exit(void *exit_value)
73 pthread_exit(exit_value);
77 SilcThread silc_thread_self(void)
80 pthread_t self = pthread_self();
81 return (SilcThread)self;
87 bool silc_thread_wait(SilcThread thread, void **exit_value)
90 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
91 if (!pthread_join(*(pthread_t *)thread, exit_value))