1258945Sroberto/*
2280849Scy * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 1998-2001  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18280849Scy/* $Id: thread.h,v 1.25 2009/09/29 04:37:08 marka Exp $ */
19258945Sroberto
20258945Sroberto#ifndef ISC_THREAD_H
21258945Sroberto#define ISC_THREAD_H 1
22258945Sroberto
23258945Sroberto#include <windows.h>
24258945Sroberto
25258945Sroberto#include <isc/lang.h>
26258945Sroberto#include <isc/result.h>
27258945Sroberto
28258945Sroberto/*
29258945Sroberto * Inlines to help with wait retrun checking
30258945Sroberto */
31258945Sroberto
32258945Sroberto/* check handle for NULL and INVALID_HANDLE */
33258945Srobertoinline BOOL IsValidHandle( HANDLE hHandle) {
34258945Sroberto    return ((hHandle != NULL) && (hHandle != INVALID_HANDLE_VALUE));
35258945Sroberto}
36258945Sroberto
37258945Sroberto/* validate wait return codes... */
38258945Srobertoinline BOOL WaitSucceeded( DWORD dwWaitResult, DWORD dwHandleCount) {
39258945Sroberto    return ((dwWaitResult >= WAIT_OBJECT_0) &&
40280849Scy	    (dwWaitResult < WAIT_OBJECT_0 + dwHandleCount));
41258945Sroberto}
42258945Sroberto
43258945Srobertoinline BOOL WaitAbandoned( DWORD dwWaitResult, DWORD dwHandleCount) {
44258945Sroberto    return ((dwWaitResult >= WAIT_ABANDONED_0) &&
45280849Scy	    (dwWaitResult < WAIT_ABANDONED_0 + dwHandleCount));
46258945Sroberto}
47258945Sroberto
48258945Srobertoinline BOOL WaitTimeout( DWORD dwWaitResult) {
49258945Sroberto    return (dwWaitResult == WAIT_TIMEOUT);
50258945Sroberto}
51280849Scy
52258945Srobertoinline BOOL WaitFailed( DWORD dwWaitResult) {
53258945Sroberto    return (dwWaitResult == WAIT_FAILED);
54258945Sroberto}
55258945Sroberto
56258945Sroberto/* compute object indices for waits... */
57258945Srobertoinline DWORD WaitSucceededIndex( DWORD dwWaitResult) {
58258945Sroberto    return (dwWaitResult - WAIT_OBJECT_0);
59258945Sroberto}
60258945Sroberto
61258945Srobertoinline DWORD WaitAbandonedIndex( DWORD dwWaitResult) {
62258945Sroberto    return (dwWaitResult - WAIT_ABANDONED_0);
63258945Sroberto}
64258945Sroberto
65258945Sroberto
66258945Sroberto
67258945Srobertotypedef HANDLE isc_thread_t;
68280849Scytypedef DWORD isc_threadresult_t;
69258945Srobertotypedef void * isc_threadarg_t;
70258945Srobertotypedef isc_threadresult_t (WINAPI *isc_threadfunc_t)(isc_threadarg_t);
71258945Srobertotypedef DWORD isc_thread_key_t;
72258945Sroberto
73258945Sroberto#define isc_thread_self (unsigned long)GetCurrentThreadId
74258945Sroberto
75258945SrobertoISC_LANG_BEGINDECLS
76258945Sroberto
77258945Srobertoisc_result_t
78258945Srobertoisc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *);
79258945Sroberto
80258945Srobertoisc_result_t
81258945Srobertoisc_thread_join(isc_thread_t, isc_threadresult_t *);
82258945Sroberto
83258945Srobertovoid
84258945Srobertoisc_thread_setconcurrency(unsigned int level);
85258945Sroberto
86258945Srobertoint
87258945Srobertoisc_thread_key_create(isc_thread_key_t *key, void (*func)(void *));
88258945Sroberto
89258945Srobertoint
90258945Srobertoisc_thread_key_delete(isc_thread_key_t key);
91258945Sroberto
92258945Srobertovoid *
93258945Srobertoisc_thread_key_getspecific(isc_thread_key_t);
94258945Sroberto
95258945Srobertoint
96258945Srobertoisc_thread_key_setspecific(isc_thread_key_t key, void *value);
97258945Sroberto
98258945SrobertoISC_LANG_ENDDECLS
99258945Sroberto
100258945Sroberto#endif /* ISC_THREAD_H */
101