1156956Sume#include <sys/cdefs.h>
2156956Sume__FBSDID("$FreeBSD$");
3156956Sume
4156952Sume#include <port_before.h>
5156952Sume#ifdef DO_PTHREADS
6156952Sume#include <pthread.h>
7156956Sume#ifdef _LIBC
8156956Sume#include <pthread_np.h>
9156952Sume#endif
10156956Sume#endif
11156952Sume#include <errno.h>
12156952Sume#include <netdb.h>
13156952Sume#include <stdlib.h>
14156952Sume#include <string.h>
15156952Sume#include <resolv_mt.h>
16156952Sume#include <port_after.h>
17156952Sume
18156952Sume#ifdef DO_PTHREADS
19156952Sumestatic pthread_key_t	key;
20156952Sumestatic int		mt_key_initialized = 0;
21156952Sume
22156952Sumestatic int		__res_init_ctx(void);
23156952Sumestatic void		__res_destroy_ctx(void *);
24156952Sume
25156952Sume#if defined(sun) && !defined(__GNUC__)
26156952Sume#pragma init	(_mtctxres_init)
27156952Sume#endif
28156952Sume#endif
29156952Sume
30156952Sumestatic mtctxres_t	sharedctx;
31156952Sume
32156952Sume#ifdef DO_PTHREADS
33156952Sume/*
34156952Sume * Initialize the TSD key. By doing this at library load time, we're
35156952Sume * implicitly running without interference from other threads, so there's
36156952Sume * no need for locking.
37156952Sume */
38156952Sumestatic void
39156952Sume_mtctxres_init(void) {
40156952Sume	int pthread_keycreate_ret;
41156952Sume
42156952Sume	pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
43156952Sume	if (pthread_keycreate_ret == 0)
44156952Sume		mt_key_initialized = 1;
45156952Sume}
46156952Sume#endif
47156952Sume
48156956Sume#ifndef _LIBC
49156952Sume/*
50156952Sume * To support binaries that used the private MT-safe interface in
51156952Sume * Solaris 8, we still need to provide the __res_enable_mt()
52156952Sume * and __res_disable_mt() entry points. They're do-nothing routines.
53156952Sume */
54156952Sumeint
55156952Sume__res_enable_mt(void) {
56156952Sume	return (-1);
57156952Sume}
58156952Sume
59156952Sumeint
60156952Sume__res_disable_mt(void) {
61156952Sume	return (0);
62156952Sume}
63156956Sume#endif
64156952Sume
65156952Sume#ifdef DO_PTHREADS
66156952Sumestatic int
67156952Sume__res_init_ctx(void) {
68156952Sume
69156952Sume	mtctxres_t	*mt;
70156952Sume	int		ret;
71156952Sume
72156952Sume
73156952Sume	if (pthread_getspecific(key) != 0) {
74156952Sume		/* Already exists */
75156952Sume		return (0);
76156952Sume	}
77156952Sume
78297790Spfg	if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
79156952Sume		errno = ENOMEM;
80156952Sume		return (-1);
81156952Sume	}
82156952Sume
83156952Sume	memset(mt, 0, sizeof (mtctxres_t));
84156952Sume
85156952Sume	if ((ret = pthread_setspecific(key, mt)) != 0) {
86156952Sume		free(mt);
87156952Sume		errno = ret;
88156952Sume		return (-1);
89156952Sume	}
90156952Sume
91156952Sume	return (0);
92156952Sume}
93156952Sume
94156952Sumestatic void
95156952Sume__res_destroy_ctx(void *value) {
96156952Sume
97297790Spfg	free(value);
98156952Sume}
99156952Sume#endif
100156952Sume
101156952Sumemtctxres_t *
102156952Sume___mtctxres(void) {
103156952Sume#ifdef DO_PTHREADS
104156952Sume	mtctxres_t	*mt;
105156952Sume
106156956Sume#ifdef _LIBC
107156956Sume	if (pthread_main_np() != 0)
108156956Sume		return (&sharedctx);
109156956Sume#endif
110156956Sume
111156952Sume	/*
112156952Sume	 * This if clause should only be executed if we are linking
113156952Sume	 * statically.  When linked dynamically _mtctxres_init() should
114156952Sume	 * be called at binding time due the #pragma above.
115156952Sume	 */
116156952Sume	if (!mt_key_initialized) {
117156952Sume		static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
118165258Sume                if (pthread_mutex_lock(&keylock) == 0) {
119165258Sume			_mtctxres_init();
120165258Sume			(void) pthread_mutex_unlock(&keylock);
121165258Sume		}
122156952Sume	}
123156952Sume
124156952Sume	/*
125156952Sume	 * If we have already been called in this thread return the existing
126156952Sume	 * context.  Otherwise recreat a new context and return it.  If
127156952Sume	 * that fails return a global context.
128156952Sume	 */
129156952Sume	if (mt_key_initialized) {
130297790Spfg		if (((mt = pthread_getspecific(key)) != NULL) ||
131156952Sume		    (__res_init_ctx() == 0 &&
132297790Spfg		     (mt = pthread_getspecific(key)) != NULL)) {
133156952Sume			return (mt);
134156952Sume		}
135156952Sume	}
136156952Sume#endif
137156952Sume	return (&sharedctx);
138156952Sume}
139