1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1998-2001 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: thread.c,v 1.24 2007/06/19 23:47:19 tbox Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert #include <config.h>
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <process.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <isc/thread.h>
25*a466cc55SCy Schubert
26*a466cc55SCy Schubert isc_result_t
isc_thread_create(isc_threadfunc_t start,isc_threadarg_t arg,isc_thread_t * threadp)27*a466cc55SCy Schubert isc_thread_create(isc_threadfunc_t start, isc_threadarg_t arg,
28*a466cc55SCy Schubert isc_thread_t *threadp)
29*a466cc55SCy Schubert {
30*a466cc55SCy Schubert isc_thread_t thread;
31*a466cc55SCy Schubert unsigned int id;
32*a466cc55SCy Schubert
33*a466cc55SCy Schubert thread = (isc_thread_t)_beginthreadex(NULL, 0, start, arg, 0, &id);
34*a466cc55SCy Schubert if (thread == NULL) {
35*a466cc55SCy Schubert /* XXX */
36*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
37*a466cc55SCy Schubert }
38*a466cc55SCy Schubert
39*a466cc55SCy Schubert *threadp = thread;
40*a466cc55SCy Schubert
41*a466cc55SCy Schubert return (ISC_R_SUCCESS);
42*a466cc55SCy Schubert }
43*a466cc55SCy Schubert
44*a466cc55SCy Schubert isc_result_t
isc_thread_join(isc_thread_t thread,isc_threadresult_t * rp)45*a466cc55SCy Schubert isc_thread_join(isc_thread_t thread, isc_threadresult_t *rp) {
46*a466cc55SCy Schubert DWORD result;
47*a466cc55SCy Schubert DWORD threadrc;
48*a466cc55SCy Schubert
49*a466cc55SCy Schubert result = WaitForSingleObject(thread, INFINITE);
50*a466cc55SCy Schubert if (result != WAIT_OBJECT_0) {
51*a466cc55SCy Schubert /* XXX */
52*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
53*a466cc55SCy Schubert }
54*a466cc55SCy Schubert if (rp != NULL) {
55*a466cc55SCy Schubert if(!GetExitCodeThread(thread, &threadrc)) {
56*a466cc55SCy Schubert /* XXX */
57*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
58*a466cc55SCy Schubert }
59*a466cc55SCy Schubert *rp = threadrc;
60*a466cc55SCy Schubert }
61*a466cc55SCy Schubert (void)CloseHandle(thread);
62*a466cc55SCy Schubert
63*a466cc55SCy Schubert return (ISC_R_SUCCESS);
64*a466cc55SCy Schubert }
65*a466cc55SCy Schubert
66*a466cc55SCy Schubert void
isc_thread_setconcurrency(unsigned int level)67*a466cc55SCy Schubert isc_thread_setconcurrency(unsigned int level) {
68*a466cc55SCy Schubert /*
69*a466cc55SCy Schubert * This is unnecessary on Win32 systems, but is here so that the
70*a466cc55SCy Schubert * call exists
71*a466cc55SCy Schubert */
72*a466cc55SCy Schubert }
73*a466cc55SCy Schubert
74*a466cc55SCy Schubert void *
isc_thread_key_getspecific(isc_thread_key_t key)75*a466cc55SCy Schubert isc_thread_key_getspecific(isc_thread_key_t key) {
76*a466cc55SCy Schubert return(TlsGetValue(key));
77*a466cc55SCy Schubert }
78*a466cc55SCy Schubert
79*a466cc55SCy Schubert int
isc_thread_key_setspecific(isc_thread_key_t key,void * value)80*a466cc55SCy Schubert isc_thread_key_setspecific(isc_thread_key_t key, void *value) {
81*a466cc55SCy Schubert return (TlsSetValue(key, value) ? 0 : GetLastError());
82*a466cc55SCy Schubert }
83*a466cc55SCy Schubert
84*a466cc55SCy Schubert int
isc_thread_key_create(isc_thread_key_t * key,void (* func)(void *))85*a466cc55SCy Schubert isc_thread_key_create(isc_thread_key_t *key, void (*func)(void *)) {
86*a466cc55SCy Schubert *key = TlsAlloc();
87*a466cc55SCy Schubert
88*a466cc55SCy Schubert return ((*key != -1) ? 0 : GetLastError());
89*a466cc55SCy Schubert }
90*a466cc55SCy Schubert
91*a466cc55SCy Schubert int
isc_thread_key_delete(isc_thread_key_t key)92*a466cc55SCy Schubert isc_thread_key_delete(isc_thread_key_t key) {
93*a466cc55SCy Schubert return (TlsFree(key) ? 0 : GetLastError());
94*a466cc55SCy Schubert }
95