1 #include <port_before.h>
5 #include <pthread_np.h>
12 #include <resolv_mt.h>
13 #include <port_after.h>
16 static pthread_key_t key;
17 static int mt_key_initialized = 0;
19 static int __res_init_ctx(void);
20 static void __res_destroy_ctx(void *);
22 #if defined(sun) && !defined(__GNUC__)
23 #pragma init (_mtctxres_init)
27 static mtctxres_t sharedctx;
31 * Initialize the TSD key. By doing this at library load time, we're
32 * implicitly running without interference from other threads, so there's
33 * no need for locking.
36 _mtctxres_init(void) {
37 int pthread_keycreate_ret;
39 pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
40 if (pthread_keycreate_ret == 0)
41 mt_key_initialized = 1;
47 * To support binaries that used the private MT-safe interface in
48 * Solaris 8, we still need to provide the __res_enable_mt()
49 * and __res_disable_mt() entry points. They're do-nothing routines.
52 __res_enable_mt(void) {
57 __res_disable_mt(void) {
64 __res_init_ctx(void) {
70 if (pthread_getspecific(key) != 0) {
75 if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
80 memset(mt, 0, sizeof (mtctxres_t));
82 if ((ret = pthread_setspecific(key, mt)) != 0) {
92 __res_destroy_ctx(void *value) {
104 if (pthread_main_np() != 0)
109 * This if clause should only be executed if we are linking
110 * statically. When linked dynamically _mtctxres_init() should
111 * be called at binding time due the #pragma above.
113 if (!mt_key_initialized) {
114 static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
115 if (pthread_mutex_lock(&keylock) == 0) {
117 (void) pthread_mutex_unlock(&keylock);
122 * If we have already been called in this thread return the existing
123 * context. Otherwise recreat a new context and return it. If
124 * that fails return a global context.
126 if (mt_key_initialized) {
127 if (((mt = pthread_getspecific(key)) != NULL) ||
128 (__res_init_ctx() == 0 &&
129 (mt = pthread_getspecific(key)) != NULL)) {