2 #include <port_before.h>
6 #include <pthread_np.h>
13 #include "resolv_mt.h"
17 #include <port_after.h>
18 #include "un-namespace.h"
21 static pthread_key_t key;
22 static int mt_key_initialized = 0;
24 static int __res_init_ctx(void);
25 static void __res_destroy_ctx(void *);
27 #if defined(sun) && !defined(__GNUC__)
28 #pragma init (_mtctxres_init)
32 static mtctxres_t sharedctx;
36 * Initialize the TSD key. By doing this at library load time, we're
37 * implicitly running without interference from other threads, so there's
38 * no need for locking.
41 _mtctxres_init(void) {
42 int pthread_keycreate_ret;
44 pthread_keycreate_ret = _pthread_key_create(&key, __res_destroy_ctx);
45 if (pthread_keycreate_ret == 0)
46 mt_key_initialized = 1;
52 * To support binaries that used the private MT-safe interface in
53 * Solaris 8, we still need to provide the __res_enable_mt()
54 * and __res_disable_mt() entry points. They're do-nothing routines.
57 __res_enable_mt(void) {
63 __res_disable_mt(void) {
69 __res_init_ctx(void) {
75 if (_pthread_getspecific(key) != 0) {
80 if ((mt = malloc(sizeof (mtctxres_t))) == NULL) {
85 memset(mt, 0, sizeof (mtctxres_t));
87 if ((ret = _pthread_setspecific(key, mt)) != 0) {
97 __res_destroy_ctx(void *value) {
99 mtctxres_t *mt = (mtctxres_t *)value;
112 if (_pthread_main_np() != 0)
118 * This if clause should only be executed if we are linking
119 * statically. When linked dynamically _mtctxres_init() should
120 * be called at binding time due the #pragma above.
122 if (!mt_key_initialized) {
123 static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
124 if (_pthread_mutex_lock(&keylock) == 0) {
126 (void) _pthread_mutex_unlock(&keylock);
131 * If we have already been called in this thread return the existing
132 * context. Otherwise recreat a new context and return it. If
133 * that fails return a global context.
135 if (mt_key_initialized) {
136 if (((mt = _pthread_getspecific(key)) != NULL) ||
137 (__res_init_ctx() == 0 &&
138 (mt = _pthread_getspecific(key)) != NULL)) {