xref: /netbsd-src/external/bsd/libbind/dist/resolv/mtctxres.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: mtctxres.c,v 1.1.1.1 2009/04/12 15:33:56 christos Exp $	*/
2 
3 #include <port_before.h>
4 #ifdef DO_PTHREADS
5 #include <pthread.h>
6 #endif
7 #include <errno.h>
8 #include <netdb.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <resolv_mt.h>
12 #include <irs.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
_mtctxres_init(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 /*
46  * To support binaries that used the private MT-safe interface in
47  * Solaris 8, we still need to provide the __res_enable_mt()
48  * and __res_disable_mt() entry points. They're do-nothing routines.
49  */
50 int
__res_enable_mt(void)51 __res_enable_mt(void) {
52 	return (-1);
53 }
54 
55 int
__res_disable_mt(void)56 __res_disable_mt(void) {
57 	return (0);
58 }
59 
60 #ifdef DO_PTHREADS
61 static int
__res_init_ctx(void)62 __res_init_ctx(void) {
63 
64 	mtctxres_t	*mt;
65 	int		ret;
66 
67 
68 	if (pthread_getspecific(key) != 0) {
69 		/* Already exists */
70 		return (0);
71 	}
72 
73 	if ((mt = malloc(sizeof (mtctxres_t))) == 0) {
74 		errno = ENOMEM;
75 		return (-1);
76 	}
77 
78 	memset(mt, 0, sizeof (mtctxres_t));
79 
80 	if ((ret = pthread_setspecific(key, mt)) != 0) {
81 		free(mt);
82 		errno = ret;
83 		return (-1);
84 	}
85 
86 	return (0);
87 }
88 
89 static void
__res_destroy_ctx(void * value)90 __res_destroy_ctx(void *value) {
91 
92 	mtctxres_t	*mt = (mtctxres_t *)value;
93 
94 	if (mt != 0)
95 		free(mt);
96 }
97 #endif
98 
99 mtctxres_t *
___mtctxres(void)100 ___mtctxres(void) {
101 #ifdef DO_PTHREADS
102 	mtctxres_t	*mt;
103 
104 	/*
105 	 * This if clause should only be executed if we are linking
106 	 * statically.  When linked dynamically _mtctxres_init() should
107 	 * be called at binding time due the #pragma above.
108 	 */
109 	if (!mt_key_initialized) {
110 		static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
111                 if (pthread_mutex_lock(&keylock) == 0) {
112 			_mtctxres_init();
113 			(void) pthread_mutex_unlock(&keylock);
114 		}
115 	}
116 
117 	/*
118 	 * If we have already been called in this thread return the existing
119 	 * context.  Otherwise recreat a new context and return it.  If
120 	 * that fails return a global context.
121 	 */
122 	if (mt_key_initialized) {
123 		if (((mt = pthread_getspecific(key)) != 0) ||
124 		    (__res_init_ctx() == 0 &&
125 		     (mt = pthread_getspecific(key)) != 0)) {
126 			return (mt);
127 		}
128 	}
129 #endif
130 	return (&sharedctx);
131 }
132