1290001Sglebius/*
2290001Sglebius * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3290001Sglebius * Copyright (C) 1998-2001  Internet Software Consortium.
4290001Sglebius *
5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any
6290001Sglebius * purpose with or without fee is hereby granted, provided that the above
7290001Sglebius * copyright notice and this permission notice appear in all copies.
8290001Sglebius *
9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11290001Sglebius * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15290001Sglebius * PERFORMANCE OF THIS SOFTWARE.
16290001Sglebius */
17290001Sglebius
18290001Sglebius/* $Id: thread.h,v 1.25 2009/09/29 04:37:08 marka Exp $ */
19290001Sglebius
20290001Sglebius#ifndef ISC_THREAD_H
21290001Sglebius#define ISC_THREAD_H 1
22290001Sglebius
23290001Sglebius#include <windows.h>
24290001Sglebius
25290001Sglebius#include <isc/lang.h>
26290001Sglebius#include <isc/result.h>
27290001Sglebius
28290001Sglebius/*
29290001Sglebius * Inlines to help with wait retrun checking
30290001Sglebius */
31290001Sglebius
32290001Sglebius/* check handle for NULL and INVALID_HANDLE */
33290001Sglebiusinline BOOL IsValidHandle( HANDLE hHandle) {
34290001Sglebius    return ((hHandle != NULL) && (hHandle != INVALID_HANDLE_VALUE));
35290001Sglebius}
36290001Sglebius
37290001Sglebius/* validate wait return codes... */
38290001Sglebiusinline BOOL WaitSucceeded( DWORD dwWaitResult, DWORD dwHandleCount) {
39290001Sglebius    return ((dwWaitResult >= WAIT_OBJECT_0) &&
40290001Sglebius	    (dwWaitResult < WAIT_OBJECT_0 + dwHandleCount));
41290001Sglebius}
42290001Sglebius
43290001Sglebiusinline BOOL WaitAbandoned( DWORD dwWaitResult, DWORD dwHandleCount) {
44290001Sglebius    return ((dwWaitResult >= WAIT_ABANDONED_0) &&
45290001Sglebius	    (dwWaitResult < WAIT_ABANDONED_0 + dwHandleCount));
46290001Sglebius}
47290001Sglebius
48290001Sglebiusinline BOOL WaitTimeout( DWORD dwWaitResult) {
49290001Sglebius    return (dwWaitResult == WAIT_TIMEOUT);
50290001Sglebius}
51290001Sglebius
52290001Sglebiusinline BOOL WaitFailed( DWORD dwWaitResult) {
53290001Sglebius    return (dwWaitResult == WAIT_FAILED);
54290001Sglebius}
55290001Sglebius
56290001Sglebius/* compute object indices for waits... */
57290001Sglebiusinline DWORD WaitSucceededIndex( DWORD dwWaitResult) {
58290001Sglebius    return (dwWaitResult - WAIT_OBJECT_0);
59290001Sglebius}
60290001Sglebius
61290001Sglebiusinline DWORD WaitAbandonedIndex( DWORD dwWaitResult) {
62290001Sglebius    return (dwWaitResult - WAIT_ABANDONED_0);
63290001Sglebius}
64290001Sglebius
65290001Sglebius
66290001Sglebius
67290001Sglebiustypedef HANDLE isc_thread_t;
68290001Sglebiustypedef DWORD isc_threadresult_t;
69290001Sglebiustypedef void * isc_threadarg_t;
70290001Sglebiustypedef isc_threadresult_t (WINAPI *isc_threadfunc_t)(isc_threadarg_t);
71290001Sglebiustypedef DWORD isc_thread_key_t;
72290001Sglebius
73290001Sglebius#define isc_thread_self (unsigned long)GetCurrentThreadId
74290001Sglebius
75290001SglebiusISC_LANG_BEGINDECLS
76290001Sglebius
77290001Sglebiusisc_result_t
78290001Sglebiusisc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *);
79290001Sglebius
80290001Sglebiusisc_result_t
81290001Sglebiusisc_thread_join(isc_thread_t, isc_threadresult_t *);
82290001Sglebius
83290001Sglebiusvoid
84290001Sglebiusisc_thread_setconcurrency(unsigned int level);
85290001Sglebius
86290001Sglebiusint
87290001Sglebiusisc_thread_key_create(isc_thread_key_t *key, void (*func)(void *));
88290001Sglebius
89290001Sglebiusint
90290001Sglebiusisc_thread_key_delete(isc_thread_key_t key);
91290001Sglebius
92290001Sglebiusvoid *
93290001Sglebiusisc_thread_key_getspecific(isc_thread_key_t);
94290001Sglebius
95290001Sglebiusint
96290001Sglebiusisc_thread_key_setspecific(isc_thread_key_t key, void *value);
97290001Sglebius
98290001SglebiusISC_LANG_ENDDECLS
99290001Sglebius
100290001Sglebius#endif /* ISC_THREAD_H */
101