File: cl_thread.c

package info (click to toggle)
gridengine 6.2u5-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 57,132 kB
  • ctags: 56,142
  • sloc: ansic: 438,030; java: 66,252; sh: 36,376; jsp: 7,757; xml: 5,850; makefile: 5,514; csh: 4,571; cpp: 2,848; perl: 2,401; tcl: 692; lisp: 669; yacc: 668; ruby: 642; lex: 344
file content (863 lines) | stat: -rw-r--r-- 26,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>

#include "cl_lists.h"

/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2001 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/


#define CL_DO_THREAD_DEBUG 0


/* this global is used to set the thread configuration data for each thread */
static pthread_mutex_t global_thread_config_key_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_key_t global_thread_config_key;
static int global_thread_config_key_done = 0;

static int cl_thread_set_default_cancel_method(void);


#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_create_thread_condition()"
int cl_thread_create_thread_condition(cl_thread_condition_t** condition ) {
   cl_thread_condition_t* new_condition = NULL;
   int ret_val;
   if (condition == NULL) {
      /* no condition pointer pointer */
      return CL_RETVAL_PARAMS;
   }
   if (*condition != NULL) {
      /* pointer pointer is already initialized (not free) */
      return CL_RETVAL_PARAMS;
   }

   new_condition =  (cl_thread_condition_t*)malloc(sizeof(cl_thread_condition_t));
   if ( new_condition == NULL) {
      return CL_RETVAL_MALLOC;
   }

   new_condition->thread_mutex_lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
   if ( new_condition->thread_mutex_lock == NULL) {
      free(new_condition);
      return CL_RETVAL_MALLOC;
   }

   new_condition->trigger_count_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
   if ( new_condition->trigger_count_mutex == NULL) {
      free(new_condition->thread_mutex_lock);
      free(new_condition);
      return CL_RETVAL_MALLOC;
   }
   new_condition->trigger_count = 0;


   new_condition->thread_cond_var = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
   if (new_condition->thread_cond_var == NULL) {
      free(new_condition->trigger_count_mutex);
      free(new_condition->thread_mutex_lock);
      free(new_condition);
      return CL_RETVAL_MALLOC;
   }

   if (pthread_mutex_init(new_condition->thread_mutex_lock, NULL) != 0) {
      free(new_condition->trigger_count_mutex);
      free(new_condition->thread_mutex_lock);
      free(new_condition->thread_cond_var);
      free(new_condition);
      return CL_RETVAL_MUTEX_ERROR;
   }

   if (pthread_mutex_init(new_condition->trigger_count_mutex, NULL) != 0) {
      ret_val = pthread_mutex_destroy(new_condition->thread_mutex_lock);
      if (ret_val == EBUSY) {
         return CL_RETVAL_MUTEX_CLEANUP_ERROR;
      }
      free(new_condition->trigger_count_mutex);
      free(new_condition->thread_mutex_lock);
      free(new_condition->thread_cond_var);
      free(new_condition);
      return CL_RETVAL_MUTEX_ERROR;
   }

   if (pthread_cond_init(new_condition->thread_cond_var,NULL) != 0) {
      ret_val = pthread_mutex_destroy(new_condition->thread_mutex_lock);
      if (ret_val == EBUSY) {
         return CL_RETVAL_MUTEX_CLEANUP_ERROR;
      }
      ret_val = pthread_mutex_destroy(new_condition->trigger_count_mutex);
      if (ret_val == EBUSY) {
         return CL_RETVAL_MUTEX_CLEANUP_ERROR;
      }
      free(new_condition->trigger_count_mutex);
      free(new_condition->thread_mutex_lock);
      free(new_condition->thread_cond_var);
      free(new_condition);
      return CL_RETVAL_CONDITION_ERROR;
   }

   *condition = new_condition;
   return CL_RETVAL_OK;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_delete_thread_condition()"
int cl_thread_delete_thread_condition(cl_thread_condition_t** condition ) {
   int ret_val;
   if (condition == NULL) {
      return CL_RETVAL_PARAMS;
   }
   if (*condition == NULL) {
      return CL_RETVAL_PARAMS;
   }

   if ( (*condition)->thread_mutex_lock != NULL) {
      ret_val = pthread_mutex_destroy((*condition)->thread_mutex_lock);
      if (ret_val == EBUSY) {
         return CL_RETVAL_MUTEX_CLEANUP_ERROR;
      }
   }

   if ( (*condition)->trigger_count_mutex != NULL) {
      ret_val = pthread_mutex_destroy((*condition)->trigger_count_mutex);
      if (ret_val == EBUSY) {
         return CL_RETVAL_MUTEX_CLEANUP_ERROR;
      }
   }

   if ( (*condition)->thread_cond_var != NULL) {
      ret_val = pthread_cond_destroy((*condition)->thread_cond_var);
      if (ret_val == EBUSY) {
         return CL_RETVAL_CONDITION_CLEANUP_ERROR;
      }
   }

   if ( (*condition)->thread_mutex_lock != NULL) {
      free((*condition)->thread_mutex_lock);
   }
 
   if ( (*condition)->trigger_count_mutex != NULL) {
      free((*condition)->trigger_count_mutex);
   }

   if ( (*condition)->thread_cond_var != NULL) {
      free((*condition)->thread_cond_var);
   }

   free(*condition);
   *condition = NULL;
   return CL_RETVAL_OK;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_wait_for_thread_condition()"
int cl_thread_wait_for_thread_condition(cl_thread_condition_t* condition, long sec, long micro_sec) {
   int ret_val = CL_RETVAL_OK;

   if (condition == NULL) {
      CL_LOG(CL_LOG_ERROR, "thread condition is NULL");
      return CL_RETVAL_PARAMS;
   }


   /* lock condition mutex */
   if (pthread_mutex_lock(condition->thread_mutex_lock) != 0) {
      return CL_RETVAL_MUTEX_LOCK_ERROR;
   } 


   pthread_mutex_lock(condition->trigger_count_mutex);
#if CL_DO_THREAD_DEBUG
   CL_LOG_INT(CL_LOG_DEBUG,"Trigger count:", (int)condition->trigger_count );
#endif

   if ( condition->trigger_count == 0 ) {
      /* trigger count is zero, wait for trigger */

      pthread_mutex_unlock(condition->trigger_count_mutex);

      if (sec <= 0 && micro_sec <= 0) {
         /* do a not timed wait */
         if (pthread_cond_wait(condition->thread_cond_var,condition->thread_mutex_lock) != 0) {
            ret_val = CL_RETVAL_CONDITION_ERROR;
            pthread_mutex_lock(condition->trigger_count_mutex);
            condition->trigger_count = 0;
            pthread_mutex_unlock(condition->trigger_count_mutex);
         } else {
            /* triggered, do not count the awake trigger */
            pthread_mutex_lock(condition->trigger_count_mutex);
            if ( condition->trigger_count > 0) {
               condition->trigger_count = condition->trigger_count - 1;
            }
            pthread_mutex_unlock(condition->trigger_count_mutex);
         }
      } else {
         int retcode;
         struct timeval now;
         struct timespec timeout;
         long sec_now;
         long micro_sec_now;
         
         /* get current time */
         gettimeofday(&now,NULL);
     
         /* normalize timeout parameter */
         sec = sec + (micro_sec / 1000000);
         micro_sec = micro_sec % 1000000;

         /* append timeout time to current time */
         micro_sec_now = now.tv_usec + micro_sec;
         sec_now       = now.tv_sec  + sec;
         
         /* handle overrun */
         if (micro_sec_now >= 1000000) {
            micro_sec_now = micro_sec_now - 1000000; 
            sec_now++;
         }
   
         /* set timeout time */
         timeout.tv_sec = sec_now;
         timeout.tv_nsec = (micro_sec_now * 1000 );
   
         retcode = pthread_cond_timedwait(condition->thread_cond_var, condition->thread_mutex_lock , &timeout);
   
         if (retcode == ETIMEDOUT) {
            ret_val = CL_RETVAL_CONDITION_WAIT_TIMEOUT;   /* timeout */
         } else if (retcode != 0) {
            ret_val = CL_RETVAL_CONDITION_ERROR;
            pthread_mutex_lock(condition->trigger_count_mutex);
            condition->trigger_count = 0;
            pthread_mutex_unlock(condition->trigger_count_mutex);
         }

         if (ret_val != CL_RETVAL_CONDITION_WAIT_TIMEOUT && ret_val != CL_RETVAL_CONDITION_ERROR) {
            /* triggered, do not count the awake trigger */
            pthread_mutex_lock(condition->trigger_count_mutex);
            if ( condition->trigger_count > 0) {
               condition->trigger_count = condition->trigger_count - 1;
            }
            pthread_mutex_unlock(condition->trigger_count_mutex);
         }
      }
   } else {
      /* trigger count is > zero, do not trigger */
#if CL_DO_THREAD_DEBUG
      CL_LOG(CL_LOG_DEBUG,"Thread was triggerd before wait - continue");
      CL_LOG_INT(CL_LOG_DEBUG,"Trigger count:", (int)condition->trigger_count );
#endif
      condition->trigger_count = condition->trigger_count - 1;
      pthread_mutex_unlock(condition->trigger_count_mutex);
   }


   /* unlock condition mutex */
   if (pthread_mutex_unlock(condition->thread_mutex_lock) != 0) {
      if (ret_val == CL_RETVAL_OK) {
         ret_val = CL_RETVAL_MUTEX_UNLOCK_ERROR;
      }
   }

   return ret_val;
}



#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_clear_triggered_conditions()"
int cl_thread_clear_triggered_conditions(cl_thread_condition_t* condition) {
   if (condition == NULL) {
      return CL_RETVAL_PARAMS;
   }
   /* increase trigger count */
   if (pthread_mutex_lock(condition->trigger_count_mutex) == 0) {
      condition->trigger_count = 0;
#if CL_DO_THREAD_DEBUG
      CL_LOG(CL_LOG_DEBUG,"cleared trigger count");
#endif
      if (pthread_mutex_unlock(condition->trigger_count_mutex) != 0) {
         CL_LOG(CL_LOG_ERROR,"could not unlock trigger_count_mutex");
         return CL_RETVAL_MUTEX_UNLOCK_ERROR;
      }
   } else {
      CL_LOG(CL_LOG_ERROR,"could not lock trigger_count_mutex");
      return CL_RETVAL_MUTEX_LOCK_ERROR;
   }
   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_trigger_thread_condition()"
int cl_thread_trigger_thread_condition(cl_thread_condition_t* condition, int do_broadcast) {
   int ret_val = CL_RETVAL_OK;

   if (condition == NULL) {
      return CL_RETVAL_PARAMS;
   }

   /* first lock condition mutex */
   if (pthread_mutex_lock(condition->thread_mutex_lock) != 0) {
      return CL_RETVAL_MUTEX_LOCK_ERROR;
   }

   /* increase trigger count */
   if (pthread_mutex_lock(condition->trigger_count_mutex) == 0) {
      condition->trigger_count = condition->trigger_count + 1;
      if (pthread_mutex_unlock(condition->trigger_count_mutex) != 0) {
         CL_LOG(CL_LOG_ERROR,"could not unlock trigger_count_mutex");
      }
   } else {
      CL_LOG(CL_LOG_ERROR,"could not lock trigger_count_mutex");
   }

   if (do_broadcast != 0) {
      /* signal condition with broadcast */
      if (pthread_cond_broadcast(condition->thread_cond_var) != 0) {
         ret_val = CL_RETVAL_CONDITION_SIGNAL_ERROR;
      }
   } else {
      /* signal condition with signal */
      if (pthread_cond_signal(condition->thread_cond_var) != 0) {
         ret_val = CL_RETVAL_CONDITION_SIGNAL_ERROR;
      }
   }
   
   /* unlock condition mutex */
   if (pthread_mutex_unlock(condition->thread_mutex_lock) != 0) {
      if (ret_val == CL_RETVAL_OK) {
         ret_val = CL_RETVAL_MUTEX_UNLOCK_ERROR;
      }
   }
   return ret_val;
}


#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_cleanup_global_thread_config_key()"
void cl_thread_cleanup_global_thread_config_key()
{
   pthread_mutex_lock(&global_thread_config_key_mutex);
   if (global_thread_config_key_done == 1) {
      pthread_key_delete(global_thread_config_key);
      global_thread_config_key_done = 0;
   }
   pthread_mutex_unlock(&global_thread_config_key_mutex);
}


/* if no start_routine is given (=NULL) the cl_thread_settings_t struct is
   filled, but no thread is started */
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_setup()"
int cl_thread_setup(cl_thread_settings_t* thread_config, 
                    cl_raw_list_t* log_list,
                    const char* name,
                    int id,
                    void * (*start_routine)(void *), 
                    cl_thread_cleanup_func_t cleanup_func,
                    void* user_data,
                    cl_thread_type_t thread_type) {

   int retry = 0; 
   int ret_val;
   
   if ( thread_config == NULL || name == NULL ) {
      return CL_RETVAL_PARAMS;
   }

   /* clean thread structure */
   memset(thread_config, 0, sizeof(cl_thread_settings_t));

   thread_config->thread_name = strdup(name);    /* malloc */
   if (thread_config->thread_name == NULL) {
      return CL_RETVAL_MALLOC;
   }

   thread_config->thread_log_list = log_list;

   thread_config->thread_id = id;

   thread_config->thread_type = thread_type;

   ret_val = cl_thread_create_thread_condition(&(thread_config->thread_event_condition));
   if ( ret_val != CL_RETVAL_OK ) {
      return ret_val;
   }

   thread_config->thread_state = CL_THREAD_STARTING;

   thread_config->thread_cleanup_func = cleanup_func;
   thread_config->thread_user_data = user_data;

   if (start_routine != NULL) {
      thread_config->thread_pointer = (pthread_t*)malloc(sizeof(pthread_t));
      if (thread_config->thread_pointer == NULL) {
         return CL_RETVAL_MALLOC;
      }
   } else {
      thread_config->thread_pointer = NULL;
   }


   ret_val = cl_thread_create_thread_condition(&(thread_config->thread_startup_condition));
   if ( ret_val != CL_RETVAL_OK ) {
      return ret_val;
   }

   pthread_mutex_lock(&global_thread_config_key_mutex);
   if (global_thread_config_key_done == 0) {
      pthread_key_create(&global_thread_config_key, NULL);
      global_thread_config_key_done = 1;
   }
   pthread_mutex_unlock(&global_thread_config_key_mutex);

   if (start_routine != NULL) {
      /* startup thread */
      if (pthread_create(thread_config->thread_pointer,NULL,start_routine,thread_config ) != 0) {
         return CL_RETVAL_THREAD_CREATE_ERROR;
      }
      

      /* wait for thread startup condition variable set */
      while (thread_config->thread_state == CL_THREAD_STARTING) {
         cl_thread_wait_for_thread_condition(thread_config->thread_startup_condition,0,100 * 1000);
         retry++;
         if (retry > 60) {
            return CL_RETVAL_THREAD_START_TIMEOUT;
         }
      }
   } else {
      /* this is creator thread setting */
      thread_config->thread_state = CL_THREAD_CREATOR;
      if (cl_thread_set_thread_config(thread_config) != CL_RETVAL_OK) {
         CL_LOG_STR(CL_LOG_ERROR, "cl_thread_set_thre_config() error for thread ->",thread_config->thread_name); 
      }
   }

   CL_LOG_STR(CL_LOG_INFO, "setup complete for thread ->",thread_config->thread_name); 
   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_join()"
int cl_thread_join(cl_thread_settings_t* thread_config) {
   
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }

   cl_thread_trigger_event(thread_config);

   CL_LOG(CL_LOG_DEBUG,"cl_thread_join() waiting for thread ...");

   /* wait for thread's end of life */
   if (pthread_join(*(thread_config->thread_pointer),NULL) != 0) {
      return CL_RETVAL_THREAD_JOIN_ERROR;
   }
   CL_LOG(CL_LOG_DEBUG,"cl_thread_join() done");
   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_get_thread_config()"
cl_thread_settings_t* cl_thread_get_thread_config(void) {
   /* cl_thread_setup  will set the thread specific data */
   cl_thread_settings_t* settings = NULL;
   pthread_mutex_lock(&global_thread_config_key_mutex);
   if (global_thread_config_key_done != 0) {
      settings = (cl_thread_settings_t*) pthread_getspecific(global_thread_config_key);
   }
   pthread_mutex_unlock(&global_thread_config_key_mutex);
   return settings;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_cleanup()"
int cl_thread_cleanup(cl_thread_settings_t* thread_config) {
   /* free all malloc()'ed pointers in cl_thread_settings_t structure */
   int ret_val;
   
   /* on CL_RETVAL_MUTEX_CLEANUP_ERROR or CL_RETVAL_CONDITION_CLEANUP_ERROR 
      the struct is NOT freed  !!! */

   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }

   
   if (thread_config->thread_event_condition != NULL) {
      ret_val = cl_thread_delete_thread_condition(&(thread_config->thread_event_condition));
      if (ret_val != CL_RETVAL_OK) {
         return ret_val;
      }
   }
   
   if (thread_config->thread_startup_condition != NULL) {
      ret_val = cl_thread_delete_thread_condition(&(thread_config->thread_startup_condition));
      if (ret_val != CL_RETVAL_OK) {
         return ret_val;
      }
   }

   if (thread_config->thread_name) {
      CL_LOG_STR(CL_LOG_DEBUG,"cleanup for thread ->", thread_config->thread_name);
   }
   
   /* destroy thread name */
   if (thread_config->thread_name) {
      free(thread_config->thread_name);
      thread_config->thread_name = NULL;
   }
 
   /* destroy thread_pointer */
   if (thread_config->thread_pointer) {
      free(thread_config->thread_pointer);
      thread_config->thread_pointer = NULL;
   }

   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_shutdown()"
int cl_thread_shutdown(cl_thread_settings_t* thread_config) {
   int ret_val;

   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }

   ret_val = pthread_cancel(*(thread_config->thread_pointer)); 

   switch(ret_val) {
       case 0:
          return CL_RETVAL_OK;
       case ESRCH:
          return CL_RETVAL_THREAD_NOT_FOUND;
       default:
          return CL_RETVAL_UNKNOWN; 
   }
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_wait_for_event()"
int cl_thread_wait_for_event(cl_thread_settings_t *thread_config, long sec, long micro_sec) {

   int ret = CL_RETVAL_OK;
    
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }

   thread_config->thread_event_count = thread_config->thread_event_count + 1;
   thread_config->thread_state = CL_THREAD_WAITING;
#if CL_DO_THREAD_DEBUG
   CL_LOG(CL_LOG_DEBUG, "cl_thread_wait_for_event() start waiting ...");
#endif

   
   ret = cl_thread_wait_for_thread_condition(thread_config->thread_event_condition, sec, micro_sec);

   thread_config->thread_state = CL_THREAD_RUNNING;
#if CL_DO_THREAD_DEBUG
   CL_LOG(CL_LOG_DEBUG, "cl_thread_wait_for_event() wake up");
#endif


   return ret;
}



#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_get_state()"
const char* cl_thread_get_state(cl_thread_settings_t* thread_config) {

   if ( thread_config == NULL ) {
      return "got no thread config";
   }

   return cl_thread_convert_state_id(thread_config->thread_state);
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_convert_state_id()"
const char* cl_thread_convert_state_id(int thread_state) {


   switch (thread_state) {
      case CL_THREAD_RUNNING: 
         return "r";
      case CL_THREAD_WAITING:
         return "w";
      case CL_THREAD_EXIT:
         return "d";
      case CL_THREAD_STARTING:
         return "s";
      case CL_THREAD_CANCELED:
         return "c";
      case CL_THREAD_CREATOR:
         return "m"; /* m for Main thread */
      default:
         return "?";
   }
}


#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_clear_events()"
int cl_thread_clear_events(cl_thread_settings_t *thread_config) {
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }
   return cl_thread_clear_triggered_conditions(thread_config->thread_event_condition);
}


#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_trigger_event()"
int cl_thread_trigger_event(cl_thread_settings_t *thread_config) {
   int ret_val;
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }
   
   ret_val = cl_thread_trigger_thread_condition(thread_config->thread_event_condition, 0);
#if CL_DO_THREAD_DEBUG
   CL_LOG(CL_LOG_DEBUG, "cl_thread_trigger_event() called");
#endif
   return ret_val;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_func_testcancel()"
int cl_thread_func_testcancel(cl_thread_settings_t* thread_config) {
   int ret_val = 0;
   int execute_pop = 0;

   if (thread_config == NULL) {
      return CL_RETVAL_THREAD_CANCELSTATE_ERROR;
   }

   /* pthread_cleanup_push() and pthread_cleanup_pop() must be used in the
      same { ... } context */

#ifdef CL_DO_COMMLIB_DEBUG
   gettimeofday(&(thread_config->thread_last_cancel_test_time),NULL);
#endif

   if (thread_config->thread_cleanup_func != NULL) {
      /* push user cleanup function */
      pthread_cleanup_push( (void(*)(void*)) thread_config->thread_cleanup_func, thread_config );
   
      /* push default cleanup function */
      pthread_cleanup_push( (void(*)(void*)) cl_thread_default_cleanup_function, thread_config );
   
      ret_val = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
   
      if (ret_val == 0) {
         pthread_testcancel();
         ret_val = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
      }
      /* remove cleanup function from stack without execution */
      pthread_cleanup_pop(execute_pop);  /* client_thread_cleanup */
   
      /* remove user function from stack without execution */
      pthread_cleanup_pop(execute_pop);
   } else {
      /* push default cleanup function */
      pthread_cleanup_push( (void(*)(void*)) cl_thread_default_cleanup_function, thread_config );
      ret_val = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  
      if (ret_val == 0) {
         pthread_testcancel();
         ret_val = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
      }
      /* remove cleanup function from stack without execution */
      pthread_cleanup_pop(execute_pop);  /* client_thread_cleanup */
   }


   if (ret_val != 0) {
      return CL_RETVAL_THREAD_CANCELSTATE_ERROR;
   }
   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_set_default_cancel_method()"
static int cl_thread_set_default_cancel_method(void) {
   /*
    * Setting thread cancel state and type to default values.
    * Commlib threads have a cancelation point:
    * The threads * have to call cl_thread_func_testcancel() in their mainloop!
    */
   pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
   pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
   return CL_RETVAL_OK;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_func_startup()"
int cl_thread_func_startup(cl_thread_settings_t* thread_config) {
   int ret_val = CL_RETVAL_OK;
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }

   cl_thread_set_default_cancel_method();

   /* set thread config data */
   if (cl_thread_set_thread_config(thread_config) != CL_RETVAL_OK) {
      printf("cl_thread_set_thread_config() error\n");
   }
   thread_config->thread_event_count = 0;

   ret_val = cl_thread_trigger_thread_condition(thread_config->thread_startup_condition,0);

   if (ret_val == CL_RETVAL_OK) {
      thread_config->thread_state = CL_THREAD_RUNNING;
   }
   CL_LOG(CL_LOG_DEBUG, "cl_thread_func_startup() done");
   return ret_val;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_set_thread_config()"
int cl_thread_set_thread_config(cl_thread_settings_t* thread_config) {

   cl_thread_set_default_cancel_method();

   pthread_mutex_lock(&global_thread_config_key_mutex);
   if (global_thread_config_key_done != 0) {
      if (pthread_setspecific(global_thread_config_key, thread_config) != 0) {
         pthread_mutex_unlock(&global_thread_config_key_mutex);
         return CL_RETVAL_THREAD_SETSPECIFIC_ERROR;
      }
      pthread_mutex_unlock(&global_thread_config_key_mutex);
      return CL_RETVAL_OK;
   }
   pthread_mutex_unlock(&global_thread_config_key_mutex);
   return CL_RETVAL_NOT_THREAD_SPECIFIC_INIT;
}

#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_unset_thread_config()"
int cl_thread_unset_thread_config(void) {

   pthread_mutex_lock(&global_thread_config_key_mutex);
   if (global_thread_config_key_done != 0) {
      if (pthread_setspecific(global_thread_config_key, NULL) != 0) {
         pthread_mutex_unlock(&global_thread_config_key_mutex);
         return CL_RETVAL_THREAD_SETSPECIFIC_ERROR;
      }
      pthread_mutex_unlock(&global_thread_config_key_mutex);
      return CL_RETVAL_OK;
   } 
   pthread_mutex_unlock(&global_thread_config_key_mutex);
   return CL_RETVAL_NOT_THREAD_SPECIFIC_INIT;
}



#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_func_cleanup()"
int cl_thread_func_cleanup(cl_thread_settings_t* thread_config) {
   if (thread_config == NULL) {
      return CL_RETVAL_PARAMS;
   }
   thread_config->thread_state = CL_THREAD_EXIT;
   CL_LOG(CL_LOG_DEBUG, "cl_thread_func_cleanup() called");
   cl_thread_unset_thread_config();
   return CL_RETVAL_OK;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_thread_default_cleanup_function()"
void cl_thread_default_cleanup_function(cl_thread_settings_t* thread_config) {
   if (thread_config != NULL) {
      thread_config->thread_state = CL_THREAD_CANCELED;
      CL_LOG(CL_LOG_INFO,  "cl_thread_default_cleanup_function() called");
      /*  There is no need to unset thread config - This can result in
       *  unexpected cl_log_list - logging output.
       */ 
#if 0
      cl_thread_unset_thread_config();
#endif
   }
}