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