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