threads.c revision 505d05c73a6e56769f263d4803b22eddd168ee24
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*
* Copyright 2004 by the Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
*
* Preliminary thread support.
*/
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <k5-thread.h>
#include <k5-platform.h>
#ifndef ENABLE_THREADS /* no thread support */
static void (*destructors[K5_KEY_MAX])(void *);
static struct tsd_block tsd_no_threads;
static unsigned char destructors_set[K5_KEY_MAX];
static CRITICAL_SECTION key_lock;
struct tsd_block {
void *values[K5_KEY_MAX];
};
static void (*destructors[K5_KEY_MAX])(void *);
static unsigned char destructors_set[K5_KEY_MAX];
void krb5int_thread_detach_hook (void)
{
/* XXX Memory leak here!
Need to destroy all TLS objects we know about for this thread. */
struct tsd_block *t;
int i, err;
if (err)
return;
t = TlsGetValue(tls_idx);
if (t == NULL)
return;
for (i = 0; i < K5_KEY_MAX; i++) {
void *v = t->values[i];
t->values[i] = 0;
(*destructors[i])(v);
}
}
}
#else /* POSIX threads */
loaded so this support code stays in the process, and gssapi is
loaded, unloaded, and loaded again. */
static void (*destructors[K5_KEY_MAX])(void *);
static unsigned char destructors_set[K5_KEY_MAX];
/* This is not safe yet!
Thread termination concurrent with key deletion can cause two
threads to interfere. It's a bit tricky, since one of the threads
will want to remove this structure from the list being walked by
the other.
Other cases, like looking up data while the library owning the key
is in the process of being unloaded, we don't worry about. */
struct tsd_block {
void *values[K5_KEY_MAX];
};
#ifdef HAVE_PRAGMA_WEAK_REF
# pragma weak pthread_getspecific
# pragma weak pthread_setspecific
# pragma weak pthread_key_create
# pragma weak pthread_key_delete
static struct tsd_block tsd_if_single;
# define GET_NO_PTHREAD_TSD() (&tsd_if_single)
#else
#endif
static pthread_key_t key;
static void thread_termination(void *);
static void thread_termination (void *tptr)
{
int i, pass, none_found;
/* Make multiple passes in case, for example, a libkrb5 cleanup
function wants to print out an error message, which causes
com_err to allocate a thread-specific buffer, after we just
freed up the old one.
Shouldn't actually happen, if we're careful, but check just in
case. */
pass = 0;
none_found = 0;
none_found = 1;
for (i = 0; i < K5_KEY_MAX; i++) {
void *v = t->values[i];
t->values[i] = 0;
(*destructors[i])(v);
none_found = 0;
}
}
}
/* remove thread from global linked list */
}
#endif /* no threads vs Win32 vs POSIX */
{
struct tsd_block *t;
int err;
if (err)
return NULL;
#ifndef ENABLE_THREADS
t = &tsd_no_threads;
t = TlsGetValue(tls_idx);
#else /* POSIX */
if (K5_PTHREADS_LOADED)
t = pthread_getspecific(key);
else
t = GET_NO_PTHREAD_TSD();
#endif
if (t == NULL)
return NULL;
}
{
struct tsd_block *t;
int err;
if (err)
return err;
#ifndef ENABLE_THREADS
t = &tsd_no_threads;
t = TlsGetValue(tls_idx);
if (t == NULL) {
int i;
t = malloc(sizeof(*t));
if (t == NULL)
return errno;
for (i = 0; i < K5_KEY_MAX; i++)
t->values[i] = 0;
/* add to global linked list */
/* t->next = 0; */
if (err) {
free(t);
return err;
}
}
#else /* POSIX */
if (K5_PTHREADS_LOADED) {
t = pthread_getspecific(key);
if (t == NULL) {
int i;
t = malloc(sizeof(*t));
if (t == NULL)
return errno;
for (i = 0; i < K5_KEY_MAX; i++)
t->values[i] = 0;
/* add to global linked list */
t->next = 0;
if (err) {
free(t);
return err;
}
}
} else {
t = GET_NO_PTHREAD_TSD();
}
#endif
return 0;
}
{
int err;
if (err)
return err;
#ifndef ENABLE_THREADS
err = 0;
/* XXX: This can raise EXCEPTION_POSSIBLE_DEADLOCK. */
err = 0;
#else /* POSIX */
if (err == 0) {
}
#endif
return 0;
}
{
#ifndef ENABLE_THREADS
destructors[keynum] = 0;
destructors_set[keynum] = 0;
/* XXX: This can raise EXCEPTION_POSSIBLE_DEADLOCK. */
/* XXX Memory leak here!
Need to destroy the associated data for all threads.
But watch for race conditions in case threads are going away too. */
#else /* POSIX */
/* Not written yet. */
abort();
#endif
return 0;
}
int krb5int_call_thread_support_init (void)
{
}
extern int krb5int_init_fac(void);
extern void krb5int_fini_fac(void);
int krb5int_thread_support_init (void)
{
int err;
#ifndef ENABLE_THREADS
/* Nothing to do for TLS initialization. */
/* XXX This can raise an exception if memory is low! */
#else /* POSIX */
if (err)
return err;
if (K5_PTHREADS_LOADED) {
if (err)
return err;
}
#endif
err = krb5int_init_fac();
if (err)
return err;
return 0;
}
void krb5int_thread_support_fini (void)
{
return;
#ifndef ENABLE_THREADS
/* Do nothing. */
/* ... free stuff ... */
#else /* POSIX */
return;
if (K5_PTHREADS_LOADED)
/* ... delete stuff ... */
#endif
}