1 /*	$NetBSD: thread.h,v 1.6 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2013  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: thread.h,v 1.25 2009/09/29 04:37:08 marka Exp  */
21 
22 #ifndef ISC_THREAD_H
23 #define ISC_THREAD_H 1
24 
25 #include <windows.h>
26 
27 #include <isc/lang.h>
28 #include <isc/result.h>
29 
30 /*
31  * Inlines to help with wait return checking
32  */
33 
34 /* check handle for NULL and INVALID_HANDLE */
35 inline BOOL IsValidHandle( HANDLE hHandle) {
36     return ((hHandle != NULL) && (hHandle != INVALID_HANDLE_VALUE));
37 }
38 
39 /* validate wait return codes... */
40 inline BOOL WaitSucceeded( DWORD dwWaitResult, DWORD dwHandleCount) {
41     return ((dwWaitResult >= WAIT_OBJECT_0) &&
42 	    (dwWaitResult < WAIT_OBJECT_0 + dwHandleCount));
43 }
44 
45 inline BOOL WaitAbandoned( DWORD dwWaitResult, DWORD dwHandleCount) {
46     return ((dwWaitResult >= WAIT_ABANDONED_0) &&
47 	    (dwWaitResult < WAIT_ABANDONED_0 + dwHandleCount));
48 }
49 
50 inline BOOL WaitTimeout( DWORD dwWaitResult) {
51     return (dwWaitResult == WAIT_TIMEOUT);
52 }
53 
54 inline BOOL WaitFailed( DWORD dwWaitResult) {
55     return (dwWaitResult == WAIT_FAILED);
56 }
57 
58 /* compute object indices for waits... */
59 inline DWORD WaitSucceededIndex( DWORD dwWaitResult) {
60     return (dwWaitResult - WAIT_OBJECT_0);
61 }
62 
63 inline DWORD WaitAbandonedIndex( DWORD dwWaitResult) {
64     return (dwWaitResult - WAIT_ABANDONED_0);
65 }
66 
67 
68 
69 typedef HANDLE isc_thread_t;
70 typedef DWORD isc_threadresult_t;
71 typedef void * isc_threadarg_t;
72 typedef isc_threadresult_t (WINAPI *isc_threadfunc_t)(isc_threadarg_t);
73 typedef DWORD isc_thread_key_t;
74 
75 #define isc_thread_self (unsigned long)GetCurrentThreadId
76 
77 ISC_LANG_BEGINDECLS
78 
79 isc_result_t
80 isc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *);
81 
82 isc_result_t
83 isc_thread_join(isc_thread_t, isc_threadresult_t *);
84 
85 void
86 isc_thread_setconcurrency(unsigned int level);
87 
88 int
89 isc_thread_key_create(isc_thread_key_t *key, void (*func)(void *));
90 
91 int
92 isc_thread_key_delete(isc_thread_key_t key);
93 
94 void *
95 isc_thread_key_getspecific(isc_thread_key_t);
96 
97 int
98 isc_thread_key_setspecific(isc_thread_key_t key, void *value);
99 
100 #define isc_thread_yield() Sleep(0)
101 
102 ISC_LANG_ENDDECLS
103 
104 #endif /* ISC_THREAD_H */
105