Commit e59b309 Ticket 49887: Clean thread local usage

5 files Authored and Committed by firstyear 6 days ago
Ticket 49887: Clean thread local usage

Clean the thread local usage of the logging system to be more inline with
pthread's intent, remove bad practices of case/switch get setters, and change
the types to be more effecient struct types. Generally make it better

https://pagure.io/389-ds-base/issue/49941

Author: William Brown <william@blackhats.net.au>

    
 1 @@ -1514,8 +1514,6 @@
 2       SIGNAL(SIGPIPE, SIG_IGN);
 3   #endif
 4   
 5 -     slapi_td_init_internal_logging();
 6 - 
 7       while (1) {
 8           int is_timedout = 0;
 9           time_t curtime = 0;
10 @@ -1525,7 +1523,6 @@
11                             "op_thread received shutdown signal\n");
12               slapi_pblock_destroy(pb);
13               g_decr_active_threadcnt();
14 -             slapi_td_free_internal_logging();
15               return;
16           }
17   
18 @@ -1547,7 +1544,6 @@
19                                 "op_thread received shutdown signal\n");
20                   slapi_pblock_destroy(pb);
21                   g_decr_active_threadcnt();
22 -                 slapi_td_free_internal_logging();
23                   return;
24               case CONN_FOUND_WORK_TO_DO:
25                   /* note - don't need to lock here - connection should only
26 @@ -1560,7 +1556,6 @@
27                       slapi_log_err(SLAPI_LOG_ERR, "connection_threadmain", "pb_conn is NULL\n");
28                       slapi_pblock_destroy(pb);
29                       g_decr_active_threadcnt();
30 -                     slapi_td_free_internal_logging();
31                       return;
32                   }
33   
34 @@ -1627,7 +1622,6 @@
35               slapi_log_err(SLAPI_LOG_ERR, "connection_threadmain", "NULL param: conn (0x%p) op (0x%p)\n", conn, op);
36               slapi_pblock_destroy(pb);
37               g_decr_active_threadcnt();
38 -             slapi_td_free_internal_logging();
39               return;
40           }
41           maxthreads = config_get_maxthreadsperconn();
42 @@ -1806,7 +1800,6 @@
43               PR_ExitMonitor(conn->c_mutex);
44               signal_listner();
45               slapi_pblock_destroy(pb);
46 -             slapi_td_free_internal_logging();
47               return;
48           }
49           /*
50 @@ -1896,7 +1889,6 @@
51               PR_ExitMonitor(conn->c_mutex);
52           }
53       } /* while (1) */
54 -     slapi_td_free_internal_logging();
55   }
56   
57   /* thread need to hold conn->c_mutex before calling this function */
 1 @@ -925,6 +925,17 @@
 2       }
 3   
 4       /*
 5 +      * init the thread data indexes. Nothing should be creating their
 6 +      * own thread data, and should be using this function instead
 7 +      * as we may swap to context based storage in the future rather
 8 +      * than direct thread-local accesses (especially important with
 9 +      * consideration of rust etc)
10 +      *
11 +      * DOES THIS NEED TO BE BEFORE OR AFTER NS?
12 +      */
13 +     slapi_td_init();
14 + 
15 +     /*
16        * Create our thread pool here for tasks to utilise.
17        */
18       main_create_ns(&tp);
19 @@ -1112,10 +1123,6 @@
20            */
21           task_cleanup();
22   
23 -         /* init the thread data indexes - need to initialize internal logging TD here for bootstrap startup */
24 -         slapi_td_init();
25 -         slapi_td_init_internal_logging();
26 - 
27           /*
28            * Initialize password storage in entry extension.
29            * Need to be initialized before plugin_startall in case stucked
30 @@ -1194,7 +1201,6 @@
31       vattr_cleanup();
32       sasl_map_done();
33   cleanup:
34 -     slapi_td_free_internal_logging();
35       compute_terminate();
36       SSL_ShutdownServerSessionIDCache();
37       SSL_ClearSessionCache();
 1 @@ -1172,30 +1172,35 @@
 2   char *slapd_get_tmp_dir(void);
 3   
 4   /* thread-data.c */
 5 + /* defines for internal logging */
 6 + typedef enum _slapi_op_nest_state {
 7 +     OP_STATE_NOTNESTED = 0,
 8 +     OP_STATE_NESTED = 1,
 9 +     OP_STATE_UNNESTED = 2,
10 + } slapi_log_nest_state;
11 + 
12 + 
13 + struct slapi_td_log_op_state_t {
14 +     int32_t op_id;
15 +     int32_t op_int_id;
16 +     int32_t op_nest_count;
17 +     slapi_log_nest_state op_nest_state;
18 +     int64_t conn_id;
19 + };
20 + 
21   int slapi_td_init(void);
22 - int slapi_td_set_val(int indexType, void *value);
23 - void slapi_td_get_val(int indexType, void **value);
24 - int slapi_td_dn_init(void);
25   int slapi_td_set_dn(char *dn);
26   void slapi_td_get_dn(char **dn);
27   int slapi_td_plugin_lock_init(void);
28   int slapi_td_get_plugin_locked(void);
29   int slapi_td_set_plugin_locked(void);
30   int slapi_td_set_plugin_unlocked(void);
31 + struct slapi_td_log_op_state_t * slapi_td_get_log_op_state(void);
32   void slapi_td_internal_op_start(void);
33   void slapi_td_internal_op_finish(void);
34 - void slapi_td_init_internal_logging(void);
35   void slapi_td_reset_internal_logging(uint64_t conn_id, int32_t op_id);
36 - void slapi_td_free_internal_logging(void);
37   
38   /*  Thread Local Storage Index Types - thread_data.c */
39 - #define SLAPI_TD_REQUESTOR_DN 1
40 - #define SLAPI_TD_PLUGIN_LIST_LOCK 2
41 - #define SLAPI_TD_CONN_ID 3
42 - #define SLAPI_TD_OP_ID 4
43 - #define SLAPI_TD_OP_INTERNAL_ID 5
44 - #define SLAPI_TD_OP_NESTED_COUNT 6
45 - #define SLAPI_TD_OP_NESTED_STATE 7
46   
47   /* util.c */
48   #include <stdio.h> /* GGOODREPL - For BUFSIZ, below, gak */
  1 @@ -10,276 +10,95 @@
  2    *   Thread Local Storage Functions
  3    */
  4   #include "slap.h"
  5 - #include <prthread.h>
  6 - 
  7 - void td_dn_destructor(void *priv);
  8 + #include <pthread.h>
  9   
 10   /*
 11    * Thread Local Storage Indexes
 12    */
 13 - static PRUintn td_requestor_dn; /* TD_REQUESTOR_DN */
 14 - static PRUintn td_plugin_list;  /* SLAPI_TD_PLUGIN_LIST_LOCK - integer set to 1 or zero */
 15 - static PRUintn td_conn_id;
 16 - static PRUintn td_op_id;
 17 - static PRUintn td_op_internal_id;
 18 - static PRUintn td_op_internal_nested_state;
 19 - static PRUintn td_op_internal_nested_count;
 20 - 
 21 - /* defines for internal logging */
 22 - #define NOTNESTED 0
 23 - #define NESTED 1
 24 - #define UNNESTED 2
 25 + static pthread_key_t td_requestor_dn; /* TD_REQUESTOR_DN */
 26 + static pthread_key_t td_plugin_list;  /* SLAPI_TD_PLUGIN_LIST_LOCK - integer set to 1 or zero */
 27 + static pthread_key_t td_op_state;
 28   
 29   /*
 30 -  *   Index types defined in slapi-plugin.h
 31 -  *
 32 -  *   #define  SLAPI_TD_REQUESTOR_DN   1
 33 -  *   ...
 34 -  *   ...
 35 +  *   Destructor Functions
 36    */
 37   
 38 + static void
 39 + td_dn_destructor(void *priv)
 40 + {
 41 +     slapi_ch_free((void **)&priv);
 42 + }
 43   
 44 - /*
 45 -  *  The Process:
 46 -  *
 47 -  *   [1]  Create new index type macro in slapi-plugin.h
 48 -  *   [2]  Create new static "PRUintn" index
 49 -  *   [3]  Update these functions with the new index:
 50 -  *          slapi_td_init()
 51 -  *          slapi_td_set_val()
 52 -  *          slapi_td_get_val()
 53 -  *   [4]  Create wrapper functions if so desired, and update slapi_plugin.h
 54 -  *   [5]  Create destructor (if necessary)
 55 -  */
 56 + static void
 57 + td_op_state_destroy(void *priv) {
 58 +     slapi_ch_free((void **)&priv);
 59 + }
 60   
 61 - int
 62 + int32_t
 63   slapi_td_init(void)
 64   {
 65 -     if (PR_NewThreadPrivateIndex(&td_requestor_dn, td_dn_destructor) == PR_FAILURE) {
 66 +     if (pthread_key_create(&td_requestor_dn, td_dn_destructor) != 0) {
 67           slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_requestor_dn/td_dn_destructor\n");
 68           return PR_FAILURE;
 69       }
 70   
 71 -     if (PR_NewThreadPrivateIndex(&td_plugin_list, NULL) == PR_FAILURE) {
 72 +     if (pthread_key_create(&td_plugin_list, NULL) != 0) {
 73           slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_plugin_list\n");
 74           return PR_FAILURE;
 75       }
 76   
 77 -     if(PR_NewThreadPrivateIndex(&td_conn_id, NULL) == PR_FAILURE){
 78 -         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_conn_id\n");
 79 -         return PR_FAILURE;
 80 -     }
 81 - 
 82 -     if(PR_NewThreadPrivateIndex(&td_op_id, NULL) == PR_FAILURE){
 83 -         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_op_id\n");
 84 -         return PR_FAILURE;
 85 -     }
 86 - 
 87 -     if(PR_NewThreadPrivateIndex(&td_op_internal_id, NULL) == PR_FAILURE){
 88 -         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_op_internal_id\n");
 89 -         return PR_FAILURE;
 90 -     }
 91 - 
 92 -     if(PR_NewThreadPrivateIndex(&td_op_internal_nested_count, NULL) == PR_FAILURE){
 93 -         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_op_internal_nested_count\n");
 94 -         return PR_FAILURE;
 95 -     }
 96 - 
 97 - 
 98 -     if(PR_NewThreadPrivateIndex(&td_op_internal_nested_state, NULL) == PR_FAILURE){
 99 -         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_op_internal_nested_state\n");
100 -         return PR_FAILURE;
101 -     }
102 - 
103 -     return PR_SUCCESS;
104 - }
105 - 
106 - 
107 - 
108 - /*
109 -  *  Caller needs to cast value to (void *)
110 -  */
111 - int
112 - slapi_td_set_val(int indexType, void *value)
113 - {
114 -     switch (indexType) {
115 -     case SLAPI_TD_REQUESTOR_DN:
116 -         if (td_requestor_dn) {
117 -             if (PR_SetThreadPrivate(td_requestor_dn, value) == PR_FAILURE) {
118 -                 return PR_FAILURE;
119 -             }
120 -         } else {
121 -             return PR_FAILURE;
122 -         }
123 -         break;
124 -     case SLAPI_TD_PLUGIN_LIST_LOCK:
125 -         if (td_plugin_list) {
126 -             if (PR_SetThreadPrivate(td_plugin_list, value) == PR_FAILURE) {
127 -                 return PR_FAILURE;
128 -             }
129 -         } else {
130 -             return PR_FAILURE;
131 -         }
132 -         break;
133 -     case SLAPI_TD_CONN_ID:
134 -         if(td_conn_id){
135 -             if(PR_SetThreadPrivate(td_conn_id, value) == PR_FAILURE){
136 -                 return PR_FAILURE;
137 -             }
138 -         } else {
139 -             return PR_FAILURE;
140 -         }
141 -         break;
142 -     case SLAPI_TD_OP_ID:
143 -         if(td_op_id){
144 -             if(PR_SetThreadPrivate(td_op_id, value) == PR_FAILURE){
145 -                 return PR_FAILURE;
146 -             }
147 -         } else {
148 -             return PR_FAILURE;
149 -         }
150 -         break;
151 -     case SLAPI_TD_OP_INTERNAL_ID:
152 -         if(td_op_internal_id){
153 -             if(PR_SetThreadPrivate(td_op_internal_id, value) == PR_FAILURE){
154 -                 return PR_FAILURE;
155 -             }
156 -         } else {
157 -             return PR_FAILURE;
158 -         }
159 -         break;
160 -     case SLAPI_TD_OP_NESTED_COUNT:
161 -         if(td_op_internal_nested_count){
162 -             if(PR_SetThreadPrivate(td_op_internal_nested_count, value) == PR_FAILURE){
163 -                 return PR_FAILURE;
164 -             }
165 -         } else {
166 -             return PR_FAILURE;
167 -         }
168 -         break;
169 -     case SLAPI_TD_OP_NESTED_STATE:
170 -         if(td_op_internal_nested_state){
171 -             if(PR_SetThreadPrivate(td_op_internal_nested_state, value) == PR_FAILURE){
172 -                 return PR_FAILURE;
173 -             }
174 -         } else {
175 -             return PR_FAILURE;
176 -         }
177 -         break;
178 -     default:
179 +     if(pthread_key_create(&td_op_state, td_op_state_destroy) != 0){
180 +         slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_init", "Failed it create private thread index for td_op_state\n");
181           return PR_FAILURE;
182       }
183   
184       return PR_SUCCESS;
185   }
186   
187 - /*
188 -  *  Caller needs to cast value to (void **)
189 -  */
190 - void
191 - slapi_td_get_val(int indexType, void **value)
192 - {
193 -     switch (indexType) {
194 -     case SLAPI_TD_REQUESTOR_DN:
195 -         if (td_requestor_dn) {
196 -             *value = PR_GetThreadPrivate(td_requestor_dn);
197 -         } else {
198 -             *value = NULL;
199 -         }
200 -         break;
201 -     case SLAPI_TD_PLUGIN_LIST_LOCK:
202 -         if (td_plugin_list) {
203 -             *value = PR_GetThreadPrivate(td_plugin_list);
204 -         } else {
205 -             *value = 0;
206 -         }
207 -         break;
208 -     case SLAPI_TD_CONN_ID:
209 -         if(td_conn_id){
210 -             *value = PR_GetThreadPrivate(td_conn_id);
211 -         } else {
212 -             *value = 0;
213 -         }
214 -         break;
215 -     case SLAPI_TD_OP_ID:
216 -         if(td_op_id){
217 -             *value = PR_GetThreadPrivate(td_op_id);
218 -         } else {
219 -             *value = 0;
220 -         }
221 -         break;
222 -     case SLAPI_TD_OP_INTERNAL_ID:
223 -         if(td_op_internal_id){
224 -             *value = PR_GetThreadPrivate(td_op_internal_id);
225 -         } else {
226 -             *value = 0;
227 -         }
228 -         break;
229 -     case SLAPI_TD_OP_NESTED_COUNT:
230 -         if(td_op_internal_nested_count){
231 -             *value = PR_GetThreadPrivate(td_op_internal_nested_count);
232 -         } else {
233 -             *value = 0;
234 -         }
235 -         break;
236 -     case SLAPI_TD_OP_NESTED_STATE:
237 -         if(td_op_internal_nested_state){
238 -             *value = PR_GetThreadPrivate(td_op_internal_nested_state);
239 -         } else {
240 -             *value = 0;
241 -         }
242 -         break;
243 -     default:
244 -         *value = NULL;
245 -         return;
246 -     }
247 - }
248   
249   /*
250    *  Wrapper Functions
251    */
252   
253   /* plugin list locking */
254 - int
255 + int32_t
256   slapi_td_set_plugin_locked()
257   {
258 -     int val = 12345;
259 +     int32_t val = 12345;
260   
261 -     if (slapi_td_set_val(SLAPI_TD_PLUGIN_LIST_LOCK, (void *)&val) == PR_FAILURE) {
262 +     if (pthread_setspecific(td_plugin_list, (void *)&val) != 0) {
263           return PR_FAILURE;
264       }
265   
266       return PR_SUCCESS;
267   }
268   
269 - int
270 + int32_t
271   slapi_td_set_plugin_unlocked()
272   {
273 -     if (slapi_td_set_val(SLAPI_TD_PLUGIN_LIST_LOCK, NULL) == PR_FAILURE) {
274 +     if (pthread_setspecific(td_plugin_list, NULL) != 0) {
275           return PR_FAILURE;
276       }
277   
278       return PR_SUCCESS;
279   }
280   
281 - int
282 + int32_t
283   slapi_td_get_plugin_locked()
284   {
285 -     int *value = 0;
286 +     int32_t *value = pthread_getspecific(td_plugin_list);
287   
288 -     slapi_td_get_val(SLAPI_TD_PLUGIN_LIST_LOCK, (void **)&value);
289 -     if (value) {
290 -         return 1;
291 -     } else {
292 +     if (value == NULL) {
293           return 0;
294       }
295 +     return 1;
296   }
297   
298   /* requestor dn */
299 - int
300 + int32_t
301   slapi_td_set_dn(char *value)
302   {
303 -     if (slapi_td_set_val(SLAPI_TD_REQUESTOR_DN, (void *)value) == PR_FAILURE) {
304 +     if (pthread_setspecific(td_requestor_dn, value) != 0) {
305           return PR_FAILURE;
306       }
307   
308 @@ -289,9 +108,18 @@
309   void
310   slapi_td_get_dn(char **value)
311   {
312 -     slapi_td_get_val(SLAPI_TD_REQUESTOR_DN, (void **)value);
313 +     if (value) {
314 +         *value = pthread_getspecific(td_requestor_dn);
315 +     }
316 + }
317 + 
318 + /* Worker op-state */
319 + struct slapi_td_log_op_state_t *
320 + slapi_td_get_log_op_state() {
321 +     return pthread_getspecific(td_op_state);
322   }
323   
324 + 
325   /*
326    * Increment the internal operation count.  Since internal operations
327    * can be nested via plugins calling plugins we need to keep track of
328 @@ -302,43 +130,41 @@
329   void
330   slapi_td_internal_op_start(void)
331   {
332 -     int32_t *id_count_ptr = NULL;
333 -     int32_t *nested_state_ptr = NULL;
334 -     int32_t *nested_count_ptr = NULL;
335 -     uint64_t *connid = NULL;
336 - 
337 -     slapi_td_get_val(SLAPI_TD_CONN_ID, (void **)&connid);
338 -     if (connid == NULL){
339 -         /* No connection id, just return */
340 -         return;
341 +     struct slapi_td_log_op_state_t *op_state = pthread_getspecific(td_op_state);
342 + 
343 +     /* Allocate if needed */
344 +     if (op_state == NULL) {
345 +         op_state = (struct slapi_td_log_op_state_t *)slapi_ch_calloc(1, sizeof(struct slapi_td_log_op_state_t));
346 +         if (pthread_setspecific(td_op_state, op_state) != 0) {
347 +             slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_internal_op_start",
348 +                           "Failed to set op_state to td_op_state. OOM?\n");
349 +             return;
350 +         }
351       }
352   
353       /* increment the internal op id counter */
354 -     slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID, (void **)&id_count_ptr);
355 -     (*id_count_ptr)++;
356 +     op_state->op_int_id += 1;
357   
358       /*
359        * Bump the nested count so we can maintain our counts after plugins call
360        * plugins, etc.
361        */
362 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_COUNT, (void **)&nested_count_ptr);
363 -     (*nested_count_ptr)++;
364 +     op_state->op_nest_count += 1;
365   
366       /* Now check for special cases in the nested count */
367 -     if (*nested_count_ptr == 2){
368 +     if (op_state->op_nest_count == 2){
369           /* We are now nested, mark it as so */
370 -         slapi_td_get_val(SLAPI_TD_OP_NESTED_STATE, (void **)&nested_state_ptr);
371 -         *nested_state_ptr = NESTED;
372 -     } else if (*nested_count_ptr == 1) {
373 +         /* THERE IS A BETTER WAY! We should track parent op structs instead! */
374 +         op_state->op_nest_state = OP_STATE_NESTED;
375 +     } else if (op_state->op_nest_count == 1) {
376           /*
377            * Back to the beginning, but if we were previously nested then the
378            * internal op id count is off
379            */
380 -         slapi_td_get_val(SLAPI_TD_OP_NESTED_STATE, (void **)&nested_state_ptr);
381 -         if (*nested_state_ptr == UNNESTED){
382 +         if (op_state->op_nest_state == OP_STATE_UNNESTED){
383               /* We were nested but anymore, need to bump the internal id count again */
384 -             *nested_state_ptr = NOTNESTED;  /* reset nested state */
385 -             (*id_count_ptr)++;
386 +             op_state->op_nest_state = OP_STATE_NOTNESTED; /* reset nested state */
387 +             op_state->op_int_id += 1;
388           }
389       }
390   }
391 @@ -352,24 +178,24 @@
392   void
393   slapi_td_internal_op_finish(void)
394   {
395 -     int32_t *nested_count_ptr = NULL;
396 -     int32_t *nested_state_ptr = NULL;
397 -     int32_t *id_count_ptr = NULL;
398 -     uint64_t *connid = NULL;
399 - 
400 -     slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID, (void **)&connid);
401 -     if (connid == NULL){
402 -         /* No connection id, just return */
403 -         return;
404 +     struct slapi_td_log_op_state_t *op_state = pthread_getspecific(td_op_state);
405 + 
406 +     /* Allocate if needed - should be unreachable!*/
407 +     PR_ASSERT(op_state);
408 +     if (op_state == NULL) {
409 +         op_state = (struct slapi_td_log_op_state_t *)slapi_ch_calloc(1, sizeof(struct slapi_td_log_op_state_t));
410 +         if (pthread_setspecific(td_op_state, op_state) != 0) {
411 +             slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_internal_op_finish",
412 +                           "Failed to set op_state to td_op_state. OOM?\n");
413 +             return;
414 +         }
415       }
416   
417 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_COUNT, (void **)&nested_count_ptr);
418 -     if ( *nested_count_ptr > 1 ){
419 +     if ( op_state->op_nest_count > 1 ){
420           /* Nested op just finished, decr op id */
421 -         slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID, (void **)&id_count_ptr);
422 -         (*id_count_ptr)--;
423 +         op_state->op_int_id -= 1;
424   
425 -         if ( (*nested_count_ptr - 1) == 1 ){
426 +         if ( (op_state->op_nest_count - 1) == 1 ){
427               /*
428                * Okay we are back to the beginning, We were nested but not
429                * anymore.  So when we start the next internal op on this
430 @@ -377,81 +203,30 @@
431                * maintain the correct op id sequence.  Set the nested state
432                * to "unnested".
433                */
434 -             slapi_td_get_val(SLAPI_TD_OP_NESTED_STATE, (void **)&nested_state_ptr);
435 -             *nested_state_ptr = UNNESTED;
436 +             op_state->op_nest_state = OP_STATE_UNNESTED;
437           }
438       }
439       /* decrement nested count */
440 -     (*nested_count_ptr)--;
441 - }
442 - 
443 - void
444 - slapi_td_init_internal_logging(void)
445 - {
446 -     uint64_t *conn_id = (uint64_t *)slapi_ch_calloc(1, sizeof(uint64_t));
447 -     int32_t *op_id = (int32_t *)slapi_ch_calloc(1, sizeof(int32_t));
448 -     int32_t *internal_op_id = (int32_t *)slapi_ch_calloc(1, sizeof(int32_t));
449 -     int32_t *nested_count = (int32_t *)slapi_ch_calloc(1, sizeof(int32_t));
450 -     int32_t *nested_state = (int32_t *)slapi_ch_calloc(1, sizeof(int32_t));
451 - 
452 -     slapi_td_set_val(SLAPI_TD_CONN_ID, (void *)conn_id);
453 -     slapi_td_set_val(SLAPI_TD_OP_ID, (void *)op_id);
454 -     slapi_td_set_val(SLAPI_TD_OP_INTERNAL_ID, (void *)internal_op_id);
455 -     slapi_td_set_val(SLAPI_TD_OP_NESTED_COUNT, (void *)nested_count);
456 -     slapi_td_set_val(SLAPI_TD_OP_NESTED_STATE, (void *)nested_state);
457 +     op_state->op_nest_count -= 1;
458   }
459   
460   void
461   slapi_td_reset_internal_logging(uint64_t new_conn_id, int32_t new_op_id)
462   {
463 -     uint64_t *conn_id;
464 -     int32_t *op_id;
465 -     int32_t *internal_op_id;
466 -     int32_t *nested_count;
467 -     int32_t *nested_state;
468 - 
469 -     slapi_td_get_val(SLAPI_TD_CONN_ID, (void **)&conn_id);
470 -     slapi_td_get_val(SLAPI_TD_OP_ID, (void **)&op_id);
471 -     slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID, (void **)&internal_op_id);
472 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_COUNT, (void **)&nested_count);
473 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_STATE, (void **)&nested_state);
474 - 
475 -     *conn_id = new_conn_id;
476 -     *op_id = new_op_id;
477 -     *internal_op_id = 0;
478 -     *nested_count = 0;
479 -     *nested_state = 0;
480 - }
481 - 
482 - void
483 - slapi_td_free_internal_logging(void)
484 - {
485 -     uint64_t *conn_id = NULL;
486 -     int32_t *val = NULL;
487 - 
488 -     slapi_td_get_val(SLAPI_TD_CONN_ID, (void **)&conn_id);
489 -     slapi_ch_free((void **)&conn_id);
490 - 
491 -     slapi_td_get_val(SLAPI_TD_OP_ID, (void **)&val);
492 -     slapi_ch_free((void **)&val);
493 - 
494 -     slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID, (void **)&val);
495 -     slapi_ch_free((void **)&val);
496 - 
497 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_COUNT, (void **)&val);
498 -     slapi_ch_free((void **)&val);
499 - 
500 -     slapi_td_get_val(SLAPI_TD_OP_NESTED_STATE, (void **)&val);
501 -     slapi_ch_free((void **)&val);
502 - }
503 - 
504 - /*
505 -  *   Destructor Functions
506 -  */
507 - 
508 - void
509 - td_dn_destructor(void *priv)
510 - {
511 -     slapi_ch_free((void **)&priv);
512 +     struct slapi_td_log_op_state_t *op_state = pthread_getspecific(td_op_state);
513 + 
514 +     /* Allocate if needed */
515 +     if (op_state == NULL) {
516 +         op_state = (struct slapi_td_log_op_state_t *)slapi_ch_calloc(1, sizeof(struct slapi_td_log_op_state_t));
517 +         if (pthread_setspecific(td_op_state, op_state) != 0) {
518 +             slapi_log_err(SLAPI_LOG_CRIT, "slapi_td_internal_op_finish",
519 +                           "Failed to set op_state to td_op_state. OOM?\n");
520 +             return;
521 +         }
522 +     }
523 +     op_state->conn_id = new_conn_id;
524 +     op_state->op_id = new_op_id;
525 +     op_state->op_int_id = 0;
526 +     op_state->op_nest_count = 0;
527 +     op_state->op_nest_state = OP_STATE_NOTNESTED;
528   }
529 - 
 1 @@ -1591,31 +1591,16 @@
 2   }
 3   
 4   void
 5 - get_internal_conn_op (uint64_t *connid, int32_t *op_id, int32_t *op_internal_id)
 6 - {
 7 -     uint64_t default_id = 0;
 8 -     uint64_t *td_conn;
 9 - 
10 -     slapi_td_get_val(SLAPI_TD_CONN_ID,(void **)&td_conn);
11 -     if (td_conn) {
12 -         int32_t *td_op;
13 -         int32_t *td_internal_op;
14 -         *connid = *td_conn;
15 -         slapi_td_get_val(SLAPI_TD_OP_ID,(void **)&td_op);
16 -         if (td_op) {
17 -             *op_id = *td_op;
18 -         } else {
19 -             *op_id = default_id;
20 -         }
21 -         slapi_td_get_val(SLAPI_TD_OP_INTERNAL_ID,(void **)&td_internal_op);
22 -         if (td_internal_op){
23 -             *op_internal_id = *td_internal_op;
24 -         } else {
25 -             *op_internal_id = default_id;
26 -         }
27 + get_internal_conn_op (uint64_t *connid, int32_t *op_id, int32_t *op_internal_id) {
28 +     struct slapi_td_log_op_state_t *op_state = slapi_td_get_log_op_state();
29 + 
30 +     if (op_state != NULL) {
31 +         *connid = op_state->conn_id;
32 +         *op_id = op_state->op_id;
33 +         *op_internal_id = op_state->op_int_id;
34       } else {
35 -         *connid = default_id;
36 -         *op_id = default_id;
37 -         *op_internal_id = default_id;
38 +         *connid = 0;
39 +         *op_id = 0;
40 +         *op_internal_id = 0;
41       }
42   }