2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A#include <port_before.h>
2N/A#ifdef DO_PTHREADS
2N/A#include <pthread.h>
2N/A#endif
2N/A#include <errno.h>
2N/A#include <netdb.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <resolv_mt.h>
2N/A#include <irs.h>
2N/A#include <port_after.h>
2N/A
2N/A#ifdef DO_PTHREADS
2N/Astatic pthread_key_t key;
2N/Astatic int mt_key_initialized = 0;
2N/A
2N/Astatic int __res_init_ctx(void);
2N/Astatic void __res_destroy_ctx(void *);
2N/A
2N/A#if defined(sun) && !defined(__GNUC__)
2N/A#pragma init (_mtctxres_init)
2N/A#endif
2N/A#endif
2N/A
2N/Astatic mtctxres_t sharedctx;
2N/A
2N/A#ifdef DO_PTHREADS
2N/A/*
2N/A * Initialize the TSD key. By doing this at library load time, we're
2N/A * implicitly running without interference from other threads, so there's
2N/A * no need for locking.
2N/A */
2N/Astatic void
2N/A_mtctxres_init(void) {
2N/A int pthread_keycreate_ret;
2N/A
2N/A pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
2N/A if (pthread_keycreate_ret == 0)
2N/A mt_key_initialized = 1;
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A * To support binaries that used the private MT-safe interface in
2N/A * Solaris 8, we still need to provide the __res_enable_mt()
2N/A * and __res_disable_mt() entry points. They're do-nothing routines.
2N/A */
2N/Aint
2N/A__res_enable_mt(void) {
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/A__res_disable_mt(void) {
2N/A return (0);
2N/A}
2N/A
2N/A#ifdef DO_PTHREADS
2N/Astatic int
2N/A__res_init_ctx(void) {
2N/A
2N/A mtctxres_t *mt;
2N/A int ret;
2N/A
2N/A
2N/A if (pthread_getspecific(key) != 0) {
2N/A /* Already exists */
2N/A return (0);
2N/A }
2N/A
2N/A if ((mt = malloc(sizeof (mtctxres_t))) == 0) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A }
2N/A
2N/A memset(mt, 0, sizeof (mtctxres_t));
2N/A
2N/A if ((ret = pthread_setspecific(key, mt)) != 0) {
2N/A free(mt);
2N/A errno = ret;
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/A__res_destroy_ctx(void *value) {
2N/A
2N/A mtctxres_t *mt = (mtctxres_t *)value;
2N/A
2N/A if (mt != 0)
2N/A free(mt);
2N/A}
2N/A#endif
2N/A
2N/Amtctxres_t *
2N/A___mtctxres(void) {
2N/A#ifdef DO_PTHREADS
2N/A mtctxres_t *mt;
2N/A
2N/A /*
2N/A * This if clause should only be executed if we are linking
2N/A * statically. When linked dynamically _mtctxres_init() should
2N/A * be called at binding time due the #pragma above.
2N/A */
2N/A if (!mt_key_initialized) {
2N/A static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
2N/A if (pthread_mutex_lock(&keylock) == 0) {
2N/A _mtctxres_init();
2N/A (void) pthread_mutex_unlock(&keylock);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If we have already been called in this thread return the existing
2N/A * context. Otherwise recreat a new context and return it. If
2N/A * that fails return a global context.
2N/A */
2N/A if (mt_key_initialized) {
2N/A if (((mt = pthread_getspecific(key)) != 0) ||
2N/A (__res_init_ctx() == 0 &&
2N/A (mt = pthread_getspecific(key)) != 0)) {
2N/A return (mt);
2N/A }
2N/A }
2N/A#endif
2N/A return (&sharedctx);
2N/A}