0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <stdlib.h>
0037 #include <stdio.h>
0038 #include <ndrstandard.h>
0039 #include <ubf_tls.h>
0040 #include <string.h>
0041 #include "thlock.h"
0042 #include "userlog.h"
0043 #include <ndebug.h>
0044
0045
0046
0047
0048
0049 __thread ubf_tls_t *G_ubf_tls = NULL;
0050
0051 exprivate pthread_key_t M_ubf_tls_key;
0052 exprivate MUTEX_LOCKDECL(M_thdata_init);
0053 exprivate int M_first = EXTRUE;
0054
0055
0056
0057
0058
0059
0060 expublic void * ndrx_ubf_tls_get(void)
0061 {
0062 ubf_tls_t *tmp = G_ubf_tls;
0063
0064 G_ubf_tls = NULL;
0065
0066 if (NULL!=tmp)
0067 {
0068
0069
0070
0071 if (tmp->is_auto)
0072 {
0073 pthread_setspecific( M_ubf_tls_key, NULL );
0074 }
0075
0076
0077 MUTEX_UNLOCK_V(tmp->mutex);
0078 }
0079
0080 return (void *)tmp;
0081 }
0082
0083
0084
0085
0086
0087 expublic int ndrx_ubf_tls_set(void *data)
0088 {
0089 ubf_tls_t *tls = (ubf_tls_t *)data;
0090
0091 if (NULL!=tls)
0092 {
0093
0094 if (UBF_TLS_MAGIG!=tls->magic)
0095 {
0096 userlog("ubf_tls_set: invalid magic! expected: %x got %x",
0097 UBF_TLS_MAGIG, tls->magic);
0098 }
0099
0100
0101 MUTEX_LOCK_V(tls->mutex);
0102
0103 G_ubf_tls = tls;
0104
0105
0106
0107
0108 if (tls->is_auto)
0109 {
0110 pthread_setspecific( M_ubf_tls_key, (void *)tls );
0111 }
0112 }
0113 else
0114 {
0115 G_ubf_tls = NULL;
0116 }
0117
0118 return EXSUCCEED;
0119
0120 }
0121
0122
0123
0124
0125
0126
0127 expublic void ndrx_ubf_tls_free(void *data)
0128 {
0129 if (NULL!=data)
0130 {
0131 ubf_tls_t *tls = (ubf_tls_t *)data;
0132
0133 if (data==G_ubf_tls)
0134 {
0135 if (G_ubf_tls->is_auto)
0136 {
0137 pthread_setspecific( M_ubf_tls_key, NULL );
0138 }
0139 G_ubf_tls=NULL;
0140 }
0141
0142
0143 if (NULL!=tls->str_buf_ptr)
0144 {
0145 NDRX_FREE(tls->str_buf_ptr);
0146 }
0147
0148 NDRX_FREE((char*)data);
0149 }
0150 }
0151
0152
0153
0154
0155
0156
0157 expublic void * ndrx_ubf_tls_new(int auto_destroy, int auto_set)
0158 {
0159 int ret = EXSUCCEED;
0160 ubf_tls_t *tls = NULL;
0161 char fn[] = "ubf_context_new";
0162
0163
0164 if (M_first)
0165 {
0166 MUTEX_LOCK_V(M_thdata_init);
0167 if (M_first)
0168 {
0169 pthread_key_create( &M_ubf_tls_key,
0170 &ndrx_ubf_tls_free );
0171 M_first = EXFALSE;
0172 }
0173 MUTEX_UNLOCK_V(M_thdata_init);
0174 }
0175
0176 if (NULL==(tls = (ubf_tls_t *)NDRX_MALLOC(sizeof(ubf_tls_t))))
0177 {
0178 userlog ("%s: failed to malloc", fn);
0179 EXFAIL_OUT(ret);
0180 }
0181
0182
0183 tls->magic = UBF_TLS_MAGIG;
0184
0185 tls->tbuf_s =0;
0186 tls->tbuf_l =0;
0187 tls->tbuf_c =0;
0188 tls->tbuf_f =0.0f;
0189 tls->tbuf_d =0.0f;
0190 tls->tbuf_p =0.0f;
0191
0192 tls->str_buf_ptr = NULL;
0193 tls->str_dat_len = 0;
0194
0195 tls->carray_buf_ptr= NULL;
0196 tls->carray_dat_len = 0;
0197
0198 tls->M_ubf_error_msg_buf[0]= EXEOS;
0199 tls->M_ubf_error = BMINVAL;
0200
0201
0202 pthread_mutex_init(&tls->mutex, NULL);
0203
0204
0205
0206
0207 if (auto_destroy)
0208 {
0209 tls->is_auto = EXTRUE;
0210 pthread_setspecific( M_ubf_tls_key, (void *)tls );
0211 }
0212 else
0213 {
0214 tls->is_auto = EXFALSE;
0215 }
0216
0217 if (auto_set)
0218 {
0219 ndrx_ubf_tls_set(tls);
0220 }
0221
0222 out:
0223
0224 if (EXSUCCEED!=ret && NULL!=tls)
0225 {
0226 ndrx_ubf_tls_free((char *)tls);
0227 }
0228
0229 return (void *)tls;
0230 }
0231
0232