xref: /freebsd/lib/libc/resolv/mtctxres.c (revision a4128aad8503277614f2d214011ef60a19447b83)
1 #include <port_before.h>
2 #ifdef DO_PTHREADS
3 #include <pthread.h>
4 #ifdef _LIBC
5 #include <pthread_np.h>
6 #endif
7 #endif
8 #include <errno.h>
9 #include <netdb.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <resolv_mt.h>
13 #include <port_after.h>
14 
15 #ifdef DO_PTHREADS
16 static pthread_key_t	key;
17 static int		mt_key_initialized = 0;
18 
19 static int		__res_init_ctx(void);
20 static void		__res_destroy_ctx(void *);
21 
22 #if defined(sun) && !defined(__GNUC__)
23 #pragma init	(_mtctxres_init)
24 #endif
25 #endif
26 
27 static mtctxres_t	sharedctx;
28 
29 #ifdef DO_PTHREADS
30 /*
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.
34  */
35 static void
36 _mtctxres_init(void) {
37 	int pthread_keycreate_ret;
38 
39 	pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx);
40 	if (pthread_keycreate_ret == 0)
41 		mt_key_initialized = 1;
42 }
43 #endif
44 
45 #ifndef _LIBC
46 /*
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.
50  */
51 int
52 __res_enable_mt(void) {
53 	return (-1);
54 }
55 
56 int
57 __res_disable_mt(void) {
58 	return (0);
59 }
60 #endif
61 
62 #ifdef DO_PTHREADS
63 static int
64 __res_init_ctx(void) {
65 
66 	mtctxres_t	*mt;
67 	int		ret;
68 
69 
70 	if (pthread_getspecific(key) != 0) {
71 		/* Already exists */
72 		return (0);
73 	}
74 
75 	if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
76 		errno = ENOMEM;
77 		return (-1);
78 	}
79 
80 	memset(mt, 0, sizeof (mtctxres_t));
81 
82 	if ((ret = pthread_setspecific(key, mt)) != 0) {
83 		free(mt);
84 		errno = ret;
85 		return (-1);
86 	}
87 
88 	return (0);
89 }
90 
91 static void
92 __res_destroy_ctx(void *value) {
93 
94 	free(value);
95 }
96 #endif
97 
98 mtctxres_t *
99 ___mtctxres(void) {
100 #ifdef DO_PTHREADS
101 	mtctxres_t	*mt;
102 
103 #ifdef _LIBC
104 	if (pthread_main_np() != 0)
105 		return (&sharedctx);
106 #endif
107 
108 	/*
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.
112 	 */
113 	if (!mt_key_initialized) {
114 		static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
115                 if (pthread_mutex_lock(&keylock) == 0) {
116 			_mtctxres_init();
117 			(void) pthread_mutex_unlock(&keylock);
118 		}
119 	}
120 
121 	/*
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.
125 	 */
126 	if (mt_key_initialized) {
127 		if (((mt = pthread_getspecific(key)) != NULL) ||
128 		    (__res_init_ctx() == 0 &&
129 		     (mt = pthread_getspecific(key)) != NULL)) {
130 			return (mt);
131 		}
132 	}
133 #endif
134 	return (&sharedctx);
135 }
136