1505d05cgtb/*
2505d05cgtb * util/support/threads.c
3505d05cgtb *
4159d09aMark Phalan * Copyright 2004,2005,2006 by the Massachusetts Institute of Technology.
5505d05cgtb * All Rights Reserved.
6505d05cgtb *
7505d05cgtb * Export of this software from the United States of America may
8505d05cgtb *   require a specific license from the United States Government.
9505d05cgtb *   It is the responsibility of any person or organization contemplating
10505d05cgtb *   export to obtain such a license before exporting.
11505d05cgtb *
12505d05cgtb * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13505d05cgtb * distribute this software and its documentation for any purpose and
14505d05cgtb * without fee is hereby granted, provided that the above copyright
15505d05cgtb * notice appear in all copies and that both that copyright notice and
16505d05cgtb * this permission notice appear in supporting documentation, and that
17505d05cgtb * the name of M.I.T. not be used in advertising or publicity pertaining
18505d05cgtb * to distribution of the software without specific, written prior
19505d05cgtb * permission.  Furthermore if you modify this software you must label
20505d05cgtb * your software as modified software and not distribute it in such a
21505d05cgtb * fashion that it might be confused with the original M.I.T. software.
22505d05cgtb * M.I.T. makes no representations about the suitability of
23505d05cgtb * this software for any purpose.  It is provided "as is" without express
24505d05cgtb * or implied warranty.
25505d05cgtb *
26505d05cgtb *
27505d05cgtb * Preliminary thread support.
28505d05cgtb */
29505d05cgtb
30505d05cgtb#include <assert.h>
31505d05cgtb#include <stdlib.h>
32505d05cgtb#include <errno.h>
33159d09aMark Phalan#include "k5-thread.h"
34159d09aMark Phalan#include "k5-platform.h"
35159d09aMark Phalan#include "supp-int.h"
36505d05cgtb
37505d05cgtbMAKE_INIT_FUNCTION(krb5int_thread_support_init);
38505d05cgtbMAKE_FINI_FUNCTION(krb5int_thread_support_fini);
39505d05cgtb
40505d05cgtb#ifndef ENABLE_THREADS /* no thread support */
41505d05cgtb
42505d05cgtbstatic void (*destructors[K5_KEY_MAX])(void *);
43505d05cgtbstruct tsd_block { void *values[K5_KEY_MAX]; };
44505d05cgtbstatic struct tsd_block tsd_no_threads;
45505d05cgtbstatic unsigned char destructors_set[K5_KEY_MAX];
46505d05cgtb
47159d09aMark Phalanint krb5int_pthread_loaded (void)
48159d09aMark Phalan{
49159d09aMark Phalan    return 0;
50159d09aMark Phalan}
51159d09aMark Phalan
52505d05cgtb#elif defined(_WIN32)
53505d05cgtb
54505d05cgtbstatic DWORD tls_idx;
55505d05cgtbstatic CRITICAL_SECTION key_lock;
56505d05cgtbstruct tsd_block {
57505d05cgtb  void *values[K5_KEY_MAX];
58505d05cgtb};
59505d05cgtbstatic void (*destructors[K5_KEY_MAX])(void *);
60505d05cgtbstatic unsigned char destructors_set[K5_KEY_MAX];
61505d05cgtb
62505d05cgtbvoid krb5int_thread_detach_hook (void)
63505d05cgtb{
64505d05cgtb    /* XXX Memory leak here!
65505d05cgtb       Need to destroy all TLS objects we know about for this thread.  */
66505d05cgtb    struct tsd_block *t;
67505d05cgtb    int i, err;
68505d05cgtb
69505d05cgtb    err = CALL_INIT_FUNCTION(krb5int_thread_support_init);
70505d05cgtb    if (err)
71505d05cgtb	return;
72505d05cgtb
73505d05cgtb    t = TlsGetValue(tls_idx);
74