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"
26 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
33 SILC_LOG_DEBUG(("Creating new thread"));
38 if (pthread_attr_init(&attr)) {
39 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
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);
51 ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
54 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
55 pthread_attr_destroy(&attr);
59 pthread_attr_destroy(&attr);
61 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)ret));
63 return (SilcThread)ret;
66 void silc_thread_exit(void *exit_value)
68 pthread_exit(exit_value);
71 SilcThread silc_thread_self(void)
73 pthread_t self = pthread_self();
74 return (SilcThread)self;
77 bool silc_thread_wait(SilcThread thread, void **exit_value)
79 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
80 if (!pthread_join(*(pthread_t *)thread, exit_value))
85 #endif /* SILC_THREADS */