updates.
[silc.git] / lib / silcutil / beos / silcbeosthread.c
1 /*
2
3   silcbeosthread.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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; version 2 of the License.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22
23 #ifdef SILC_THREADS
24
25 /* Thread structure for BeOS */
26 typedef struct {
27   thread_id thread;
28   SilcThreadStart start_func;
29   void *context;
30   bool waitable;
31 } *SilcBeosThread;
32
33 /* Actual routine that is called by BeOS when the thread is created.
34    We will call the start_func from here. */
35
36 static void *silc_thread_beos_start(void *context)
37 {
38   SilcBeosThread thread = (SilcBeosThread)context;
39   return (*thread->start_func)(thread->context);
40 }
41
42 #endif
43
44 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
45                               bool waitable)
46 {
47 #ifdef SILC_THREADS
48   int ret;
49   SilcBeosThread thread = silc_calloc(1, sizeof(*thread));
50   if (!thread)
51     return NULL;
52
53   thread->start_func = start_func;
54   thread->context = context;
55   thread->waitable = waitable;
56
57   /* Create the thread, and run it */
58   thread->thread = spawn_thread((thread_func)silc_thread_beos_start,
59                                 B_NORMAL_PRIORITY, thread);
60   ret = resume_thread(thread->thread);
61   if (ret < B_NO_ERROR) {
62     SILC_LOG_ERROR(("Could not create new thread"));
63     silc_free(thread);
64     return NULL;
65   }
66
67   return (SilcThread)thread->thread;
68 #else
69   /* Call thread callback immediately */
70   (*start_func)(context);
71   return NULL;
72 #endif
73 }
74
75 void silc_thread_exit(void *exit_value)
76 {
77 #ifdef SILC_THREADS
78   exit_thread((status_t)exit_value);
79 #endif
80 }
81
82 SilcThread silc_thread_self(void)
83 {
84 #ifdef SILC_THREADS
85   return (SilcThread)find_thread(NULL);
86 #else
87   return NULL;
88 #endif
89 }
90
91 bool silc_thread_wait(SilcThread thread, void **exit_value)
92 {
93 #ifdef SILC_THREADS
94   status_t ret, retval;
95
96   ret = wait_for_thread((thread_id)thread, &retval);
97   if (ret == B_NO_ERROR) {
98     if (exit_value)
99       *exit_value = retval;
100     return TRUE;
101   }
102
103   return FALSE;
104 #else
105   return FALSE;
106 #endif
107 }