xref: /freebsd/lib/libc/resolv/mtctxres.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 #include <sys/cdefs.h>
2 #include <port_before.h>
3 #ifdef DO_PTHREADS
4 #include <pthread.h>
5 #ifdef _LIBC
6 #include <pthread_np.h>
7 #endif
8 #endif
9 #include <errno.h>
10 #include <netdb.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <resolv_mt.h>
14 #include <port_after.h>
15 
16 #ifdef DO_PTHREADS
17 static pthread_key_t	key;
18 static int		mt_key_initialized = 0;
19 
20 static int		__res_init_ctx(void);
21 static void		__res_destroy_ctx(void *);
22 
23 #if defined(sun) && !defined(__GNUC__)
24 #pragma init	(_mtctxres_init)
25 #endif
26 #endif
27 
28 static mtctxres_t	sharedctx;
29 
30 #ifdef DO_PTHREADS
31 /*
32  * Initialize the TSD key. By doing this at library load time, we're
33  * implicitly running without interference from other threads, so there's
34  * no need for locking.
35  */
36 static void
37 _mtctxres_init(void) {
38 	int pthread_keycreate_ret;
39 
40 	pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
41 	if (pthread_keycreate_ret == 0)
42 		mt_key_initialized = 1;
43 }
44 #endif
45 
46 #ifndef _LIBC
47 /*
48  * To support binaries that used the private MT-safe interface in
49  * Solaris 8, we still need to provide the __res_enable_mt()
50  * and __res_disable_mt() entry points. They're do-nothing routines.
51  */
52 int
53 __res_enable_mt(void) {
54 	return (-1);
55 }
56 
57 int
58 __res_disable_mt(void) {
59 	return (0);
60 }
61 #endif
62 
63 #ifdef DO_PTHREADS
64 static int
65 __res_init_ctx(void) {
66 
67 	mtctxres_t	*mt;
68 	int		ret;
69 
70 
71 	if (pthread_getspecific(key) != 0) {
72 		/* Already exists */
73 		return (0);
74 	}
75 
76 	if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
77 		errno = ENOMEM;
78 		return (-1);
79 	}
80 
81 	memset(mt, 0, sizeof (mtctxres_t));
82 
83 	if ((ret = pthread_setspecific(key, mt)) != 0) {
84 		free(mt);
85 		errno = ret;
86 		return (-1);
87 	}
88 
89 	return (0);
90 }
91 
92 static void
93 __res_destroy_ctx(void *value) {
94 
95 	free(value);
96 }
97 #endif
98 
99 mtctxres_t *
100 ___mtctxres(void) {
101 #ifdef DO_PTHREADS
102 	mtctxres_t	*mt;
103 
104 #ifdef _LIBC
105 	if (pthread_main_np() != 0)
106 		return (&sharedctx);
107 #endif
108 
109 	/*
110 	 * This if clause should only be executed if we are linking
111 	 * statically.  When linked dynamically _mtctxres_init() should
112 	 * be called at binding time due the #pragma above.
113 	 */
114 	if (!mt_key_initialized) {
115 		static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
116                 if (pthread_mutex_lock(&keylock) == 0) {
117 			_mtctxres_init();
118 			(void) pthread_mutex_unlock(&keylock);
119 		}
120 	}
121 
122 	/*
123 	 * If we have already been called in this thread return the existing
124 	 * context.  Otherwise recreat a new context and return it.  If
125 	 * that fails return a global context.
126 	 */
127 	if (mt_key_initialized) {
128 		if (((mt = pthread_getspecific(key)) != NULL) ||
129 		    (__res_init_ctx() == 0 &&
130 		     (mt = pthread_getspecific(key)) != NULL)) {
131 			return (mt);
132 		}
133 	}
134 #endif
135 	return (&sharedctx);
136 }
137