bea9552d43e69443a97e9ded1e652e45b2030460
[silc.git] / lib / silcutil / win32 / silcwin32thread.c
1 /*
2
3   silcwin32thread.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 /* These routines are based on GLib's WIN32 gthread implementation and
21    thus credits should go there. */
22 /* XXX Is the use of Tls necessary?? */
23 /* $Id$ */
24
25 #include "silcincludes.h"
26
27 #ifdef SILC_THREADS
28
29 /* Thread structure for WIN32 */
30 typedef struct {
31   HANDLE thread;
32   SilcThreadStart start_func;
33   void *context;
34   bool waitable;
35 } *SilcWin32Thread;
36
37 static DWORD silc_thread_tls;
38
39 /* Actual routine that is called by WIN32 when the thread is created.
40    We will call the start_func from here. When this returns the thread
41    is destroyed. */
42
43 unsigned __stdcall silc_thread_win32_start(void *context)
44 {
45   SilcWin32Thread thread = (SilcWin32Thread)context;
46
47   TlsSetValue(silc_thread_tls, context);
48   silc_thread_exit(thread->start_func(thread->context));
49
50   return 0;
51 }
52 #endif
53
54 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
55                               bool waitable)
56 {
57 #ifdef SILC_THREADS
58   SilcWin32Thread thread;
59   unsigned id;
60
61   SILC_LOG_DEBUG(("Creating new thread"));
62
63   thread = silc_calloc(1, sizeof(*thread));
64   thread->start_func = start_func;
65   thread->context = context;
66   thread->waitable = waitable;
67   thread->thread = (HANDLE)_beginthreadex(NULL, 0, silc_thread_win32_start,
68                                           (void *)thread, 0, &id);
69   if (!thread->thread) {
70     SILC_LOG_ERROR(("Could not create new thread"));
71     silc_free(thread);
72     return NULL;
73   }
74
75   return (SilcThread)thread;
76 #else
77   /* Call thread callback immediately */
78   (*start_func)(context);
79   return NULL;
80 #endif
81 }
82
83 void silc_thread_exit(void *exit_value)
84 {
85 #ifdef SILC_THREADS
86   SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
87   
88   if (thread) {
89     /* If the thread is waitable the memory is freed only in silc_thread_wait
90        by another thread. If not waitable, free it now. */
91     if (!thread->waitable) {
92       CloseHandle(thread->thread);
93       silc_free(thread);
94     }
95
96     TlsSetValue(silc_thread_tls, NULL);
97   }
98
99   _endthreadex(0);
100 #endif
101 }
102
103 SilcThread silc_thread_self(void)
104 {
105 #ifdef SILC_THREADS
106   SilcWin32Thread self = TlsGetValue(silc_thread_tls);
107
108   if (!self) {
109     /* This should only happen for the main thread! */
110     HANDLE handle = GetCurrentThread ();
111     HANDLE process = GetCurrentProcess ();
112     self = silc_calloc(1, sizeof(*self));
113     DuplicateHandle(process, handle, process, 
114                     &self->thread, 0, FALSE, 
115                     DUPLICATE_SAME_ACCESS);
116     TlsSetValue(silc_thread_tls, self);
117   }
118
119   return (SilcThread)self;
120 #else
121   return NULL;
122 #endif
123 }
124
125 bool silc_thread_wait(SilcThread thread, void **exit_value)
126 {
127 #ifdef SILC_THREADS
128   SilcWin32Thread self = (SilcWin32Thread)thread;
129
130   SILC_LOG_DEBUG(("Waiting for thread %p", self));
131
132   if (!self->waitable)
133     return FALSE;
134
135   /* The thread is waitable thus we will free all memory after the
136      WaitForSingleObject returns, the thread is destroyed after that. */
137   WaitForSingleObject(self->thread, INFINITE);
138   CloseHandle(self->thread);
139   silc_free(self);
140   if (exit_value)
141     *exit_value = NULL;
142
143   return TRUE;
144 #else
145   return FALSE;
146 #endif
147 }