4 #include <port_before.h>
8 #include <pthread_np.h>
15 #include <resolv_mt.h>
16 #include <port_after.h>
19 static pthread_key_t key;
20 static int mt_key_initialized = 0;
22 static int __res_init_ctx(void);
23 static void __res_destroy_ctx(void *);
25 #if defined(sun) && !defined(__GNUC__)
26 #pragma init (_mtctxres_init)
30 static mtctxres_t sharedctx;
34 * Initialize the TSD key. By doing this at library load time, we're
35 * implicitly running without interference from other threads, so there's
36 * no need for locking.
39 _mtctxres_init(void) {
40 int pthread_keycreate_ret;
42 pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
43 if (pthread_keycreate_ret == 0)
44 mt_key_initialized = 1;
50 * To support binaries that used the private MT-safe interface in
51 * Solaris 8, we still need to provide the __res_enable_mt()
52 * and __res_disable_mt() entry points. They're do-nothing routines.
55 __res_enable_mt(void) {
60 __res_disable_mt(void) {
67 __res_init_ctx(void) {
73 if (pthread_getspecific(key) != 0) {
78 if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
83 memset(mt, 0, sizeof (mtctxres_t));
85 if ((ret = pthread_setspecific(key, mt)) != 0) {
95 __res_destroy_ctx(void *value) {
107 if (pthread_main_np() != 0)
112 * This if clause should only be executed if we are linking
113 * statically. When linked dynamically _mtctxres_init() should
114 * be called at binding time due the #pragma above.
116 if (!mt_key_initialized) {
117 static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
118 if (pthread_mutex_lock(&keylock) == 0) {
120 (void) pthread_mutex_unlock(&keylock);
125 * If we have already been called in this thread return the existing
126 * context. Otherwise recreat a new context and return it. If
127 * that fails return a global context.
129 if (mt_key_initialized) {
130 if (((mt = pthread_getspecific(key)) != NULL) ||
131 (__res_init_ctx() == 0 &&
132 (mt = pthread_getspecific(key)) != NULL)) {