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