Commit 4853d94ca47a6f1ecaa7c24b9034ededcb7e5bff

Edward Thomson 2020-05-14T10:36:35

global: separate global state from thread-local state Our "global initialization" has accumulated some debris over the years. It was previously responsible for both running the various global initializers (that set up various subsystems) _and_ setting up the "global state", which is actually the thread-local state for things like error reporting. Separate the thread local state out into "threadstate". Use the normal subsystem initialization functions that we already have to set it up. This makes both the global initialization system and the threadstate system simpler to reason about.

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
diff --git a/src/errors.c b/src/errors.c
index 8570226..d4da50d 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -7,7 +7,7 @@
 
 #include "common.h"
 
-#include "global.h"
+#include "threadstate.h"
 #include "posix.h"
 #include "buffer.h"
 
@@ -22,18 +22,18 @@ static git_error g_git_oom_error = {
 
 static void set_error_from_buffer(int error_class)
 {
-	git_error *error = &GIT_GLOBAL->error_t;
-	git_buf *buf = &GIT_GLOBAL->error_buf;
+	git_error *error = &GIT_THREADSTATE->error_t;
+	git_buf *buf = &GIT_THREADSTATE->error_buf;
 
 	error->message = buf->ptr;
 	error->klass = error_class;
 
-	GIT_GLOBAL->last_error = error;
+	GIT_THREADSTATE->last_error = error;
 }
 
 static void set_error(int error_class, char *string)
 {
-	git_buf *buf = &GIT_GLOBAL->error_buf;
+	git_buf *buf = &GIT_THREADSTATE->error_buf;
 
 	git_buf_clear(buf);
 	if (string) {
@@ -46,7 +46,7 @@ static void set_error(int error_class, char *string)
 
 void git_error_set_oom(void)
 {
-	GIT_GLOBAL->last_error = &g_git_oom_error;
+	GIT_THREADSTATE->last_error = &g_git_oom_error;
 }
 
 void git_error_set(int error_class, const char *fmt, ...)
@@ -64,7 +64,7 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
 	DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
 #endif
 	int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
-	git_buf *buf = &GIT_GLOBAL->error_buf;
+	git_buf *buf = &GIT_THREADSTATE->error_buf;
 
 	git_buf_clear(buf);
 	if (fmt) {
@@ -97,7 +97,7 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
 
 int git_error_set_str(int error_class, const char *string)
 {
-	git_buf *buf = &GIT_GLOBAL->error_buf;
+	git_buf *buf = &GIT_THREADSTATE->error_buf;
 
 	assert(string);
 
@@ -118,9 +118,9 @@ int git_error_set_str(int error_class, const char *string)
 
 void git_error_clear(void)
 {
-	if (GIT_GLOBAL->last_error != NULL) {
+	if (GIT_THREADSTATE->last_error != NULL) {
 		set_error(0, NULL);
-		GIT_GLOBAL->last_error = NULL;
+		GIT_THREADSTATE->last_error = NULL;
 	}
 
 	errno = 0;
@@ -131,13 +131,13 @@ void git_error_clear(void)
 
 const git_error *git_error_last(void)
 {
-	return GIT_GLOBAL->last_error;
+	return GIT_THREADSTATE->last_error;
 }
 
 int git_error_state_capture(git_error_state *state, int error_code)
 {
-	git_error *error = GIT_GLOBAL->last_error;
-	git_buf *error_buf = &GIT_GLOBAL->error_buf;
+	git_error *error = GIT_THREADSTATE->last_error;
+	git_buf *error_buf = &GIT_THREADSTATE->error_buf;
 
 	memset(state, 0, sizeof(git_error_state));
 
diff --git a/src/global.c b/src/global.c
index f67811a..c4e925b 100644
--- a/src/global.c
+++ b/src/global.c
@@ -8,6 +8,7 @@
 #include "global.h"
 
 #include "alloc.h"
+#include "threadstate.h"
 #include "hash.h"
 #include "sysdir.h"
 #include "filter.h"
@@ -27,6 +28,7 @@ typedef int (*git_global_init_fn)(void);
 
 static git_global_init_fn git__init_callbacks[] = {
 	git_allocator_global_init,
+	git_threadstate_global_init,
 	git_threads_global_init,
 	git_hash_global_init,
 	git_sysdir_global_init,
@@ -53,15 +55,6 @@ void git__on_shutdown(git_global_shutdown_fn callback)
 	git__shutdown_callbacks[count - 1] = callback;
 }
 
-static void git__global_state_cleanup(git_global_st *st)
-{
-	if (!st)
-		return;
-
-	git__free(st->error_t.message);
-	st->error_t.message = NULL;
-}
-
 static int init_common(void)
 {
 	size_t i;
@@ -94,32 +87,6 @@ static void shutdown_common(void)
 	}
 }
 
-/**
- * Handle the global state with TLS
- *
- * If libgit2 is built with GIT_THREADS enabled,
- * the `git_libgit2_init()` function must be called
- * before calling any other function of the library.
- *
- * This function allocates a TLS index (using pthreads
- * or the native Win32 API) to store the global state
- * on a per-thread basis.
- *
- * Any internal method that requires global state will
- * then call `git__global_state()` which returns a pointer
- * to the global state structure; this pointer is lazily
- * allocated on each thread.
- *
- * Before shutting down the library, the
- * `git_libgit2_shutdown` method must be called to free
- * the previously reserved TLS index.
- *
- * If libgit2 is built without threading support, the
- * `git__global_statestate()` call returns a pointer to a single,
- * statically allocated global state. The `git_thread_`
- * functions are not available in that case.
- */
-
 /*
  * `git_libgit2_init()` allows subsystems to perform global setup,
  * which may take place in the global scope.  An explicit memory
@@ -130,42 +97,67 @@ static void shutdown_common(void)
  */
 #if defined(GIT_THREADS) && defined(GIT_WIN32)
 
-static DWORD _fls_index;
-static volatile LONG _mutex = 0;
+/*
+ * On Win32, we use a spinlock to provide locking semantics.  This is
+ * lighter-weight than a proper critical section.
+ */
+static volatile LONG init_spinlock = 0;
 
-static void WINAPI fls_free(void *st)
+GIT_INLINE(int) init_lock(void)
 {
-	git__global_state_cleanup(st);
-	git__free(st);
+	while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
+	return 0;
 }
 
-static int synchronized_threads_init(void)
+GIT_INLINE(int) init_unlock(void)
 {
-	int error;
+	InterlockedExchange(&init_spinlock, 0);
+	return 0;
+}
 
-	if ((_fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
-		return -1;
+#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
 
-	error = init_common();
+/*
+ * On POSIX, we need to use a proper mutex for locking.  We might prefer
+ * a spinlock here, too, but there's no static initializer for a
+ * pthread_spinlock_t.
+ */
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
-	return error;
+GIT_INLINE(int) init_lock(void)
+{
+	return pthread_mutex_lock(&mutex) == 0 ? 0 : -1;
 }
 
+GIT_INLINE(int) init_unlock(void)
+{
+	return pthread_mutex_unlock(&mutex) == 0 ? 0 : -1;
+}
+
+#elif defined(GIT_THREADS)
+# error unknown threading model
+#else
+
+# define init_lock() 0
+# define init_unlock() 0
+
+#endif
+
 int git_libgit2_init(void)
 {
 	int ret;
 
-	/* Enter the lock */
-	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
+	if (init_lock() < 0)
+		return -1;
 
 	/* Only do work on a 0 -> 1 transition of the refcount */
 	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
-		if (synchronized_threads_init() < 0)
+		if (init_common() < 0)
 			ret = -1;
 	}
 
-	/* Exit the lock */
-	InterlockedExchange(&_mutex, 0);
+	if (init_unlock() < 0)
+		return -1;
 
 	return ret;
 }
@@ -175,163 +167,16 @@ int git_libgit2_shutdown(void)
 	int ret;
 
 	/* Enter the lock */
-	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
+	if (init_lock() < 0)
+		return -1;
 
 	/* Only do work on a 1 -> 0 transition of the refcount */
-	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
+	if ((ret = git_atomic_dec(&git__n_inits)) == 0)
 		shutdown_common();
 
-		FlsFree(_fls_index);
-	}
-
 	/* Exit the lock */
-	InterlockedExchange(&_mutex, 0);
-
-	return ret;
-}
-
-git_global_st *git__global_state(void)
-{
-	git_global_st *ptr;
-
-	assert(git_atomic_get(&git__n_inits) > 0);
-
-	if ((ptr = FlsGetValue(_fls_index)) != NULL)
-		return ptr;
-
-	ptr = git__calloc(1, sizeof(git_global_st));
-	if (!ptr)
-		return NULL;
-
-	if (git_buf_init(&ptr->error_buf, 0) < 0)
-		return NULL;
-
-	FlsSetValue(_fls_index, ptr);
-	return ptr;
-}
-
-#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
-
-static pthread_key_t _tls_key;
-static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
-int init_error = 0;
-
-static void cb__free_status(void *st)
-{
-	git__global_state_cleanup(st);
-	git__free(st);
-}
-
-static void init_once(void)
-{
-	pthread_key_create(&_tls_key, &cb__free_status);
-	init_error = init_common();
-}
-
-int git_libgit2_init(void)
-{
-	int ret, err;
-
-	if ((err = pthread_mutex_lock(&_init_mutex)) != 0)
-		return err;
-
-	ret = git_atomic_inc(&git__n_inits);
-	err = pthread_once(&_once_init, init_once);
-	err |= pthread_mutex_unlock(&_init_mutex);
-
-	if (err || init_error)
-		return err | init_error;
-
-	return ret;
-}
-
-int git_libgit2_shutdown(void)
-{
-	void *ptr = NULL;
-	pthread_once_t new_once = PTHREAD_ONCE_INIT;
-	int error, ret;
-
-	if ((error = pthread_mutex_lock(&_init_mutex)) != 0)
-		return error;
-
-	if ((ret = git_atomic_dec(&git__n_inits)) != 0)
-		goto out;
-
-	/* Shut down any subsystems that have global state */
-	shutdown_common();
-
-	ptr = pthread_getspecific(_tls_key);
-	pthread_setspecific(_tls_key, NULL);
-
-	git__global_state_cleanup(ptr);
-	git__free(ptr);
-
-	pthread_key_delete(_tls_key);
-	_once_init = new_once;
-
-out:
-	if ((error = pthread_mutex_unlock(&_init_mutex)) != 0)
-		return error;
-
-	return ret;
-}
-
-git_global_st *git__global_state(void)
-{
-	git_global_st *ptr;
-
-	assert(git_atomic_get(&git__n_inits) > 0);
-
-	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
-		return ptr;
-
-	ptr = git__calloc(1, sizeof(git_global_st));
-	if (!ptr)
-		return NULL;
-
-	if (git_buf_init(&ptr->error_buf, 0) < 0)
-		return NULL;
-
-	pthread_setspecific(_tls_key, ptr);
-	return ptr;
-}
-
-#else
-
-static git_global_st __state;
-
-int git_libgit2_init(void)
-{
-	int ret;
-
-	/* Only init subsystems the first time */
-	if ((ret = git_atomic_inc(&git__n_inits)) != 1)
-		return ret;
-
-	if ((ret = init_common()) < 0)
-		return ret;
-
-	return 1;
-}
-
-int git_libgit2_shutdown(void)
-{
-	int ret;
-
-	/* Shut down any subsystems that have global state */
-	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
-		shutdown_common();
-		git__global_state_cleanup(&__state);
-		memset(&__state, 0, sizeof(__state));
-	}
+	if (init_unlock() < 0)
+		return -1;
 
 	return ret;
 }
-
-git_global_st *git__global_state(void)
-{
-	return &__state;
-}
-
-#endif /* GIT_THREADS */
diff --git a/src/global.h b/src/global.h
index f4e55eb..0364cab 100644
--- a/src/global.h
+++ b/src/global.h
@@ -9,23 +9,6 @@
 
 #include "common.h"
 
-typedef struct {
-	git_error *last_error;
-	git_error error_t;
-	git_buf error_buf;
-	char oid_fmt[GIT_OID_HEXSZ+1];
-
-	/* On Windows, this is the current child thread that was started by
-	 * `git_thread_create`.  This is used to set the thread's exit code
-	 * when terminated by `git_thread_exit`.  It is unused on POSIX.
-	 */
-	git_thread *current_thread;
-} git_global_st;
-
-git_global_st *git__global_state(void);
-
-#define GIT_GLOBAL (git__global_state())
-
 typedef void (*git_global_shutdown_fn)(void);
 
 extern void git__on_shutdown(git_global_shutdown_fn callback);
diff --git a/src/oid.c b/src/oid.c
index 7831aca..a8ad3d2 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -9,7 +9,7 @@
 
 #include "git2/oid.h"
 #include "repository.h"
-#include "global.h"
+#include "threadstate.h"
 #include <string.h>
 #include <limits.h>
 
@@ -107,7 +107,7 @@ int git_oid_pathfmt(char *str, const git_oid *oid)
 
 char *git_oid_tostr_s(const git_oid *oid)
 {
-	char *str = GIT_GLOBAL->oid_fmt;
+	char *str = GIT_THREADSTATE->oid_fmt;
 	git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
 	return str;
 }
diff --git a/src/threadstate.c b/src/threadstate.c
new file mode 100644
index 0000000..4aa1c03
--- /dev/null
+++ b/src/threadstate.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "threadstate.h"
+#include "global.h"
+
+static void threadstate_dispose(git_threadstate *threadstate);
+
+/**
+ * Handle the thread-local state
+ *
+ * `git_threadstate_global_init` will be called as part
+ * of `git_libgit2_init` (which itself must be called
+ * before calling any other function in the library).
+ *
+ * This function allocates a TLS index (using pthreads
+ * or fiber-local storage in Win32) to store the per-
+ * thread state.
+ *
+ * Any internal method that requires thread-local state
+ * will then call `git_threadstate_get()` which returns a
+ * pointer to the thread-local state structure; this
+ * structure is lazily allocated on each thread.
+ *
+ * This mechanism will register a shutdown handler
+ * (`git_threadstate_global_shutdown`) which will free the
+ * TLS index.  This shutdown handler will be called by
+ * `git_libgit2_shutdown`.
+ *
+ * If libgit2 is built without threading support, the
+ * `git_threadstate_get()` call returns a pointer to a single,
+ * statically allocated global state. The `git_thread_`
+ * functions are not available in that case.
+ */
+
+#if defined(GIT_THREADS) && defined(GIT_WIN32)
+
+static DWORD fls_index;
+
+static void git_threadstate_global_shutdown(void)
+{
+	FlsFree(fls_index);
+}
+
+static void WINAPI fls_free(void *threadstate)
+{
+	threadstate_dispose(threadstate);
+	git__free(threadstate);
+}
+
+int git_threadstate_global_init(void)
+{
+	if ((fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
+		return -1;
+
+	git__on_shutdown(git_threadstate_global_shutdown);
+
+	return 0;
+}
+
+git_threadstate *git_threadstate_get(void)
+{
+	git_threadstate *threadstate;
+
+	if ((threadstate = FlsGetValue(fls_index)) != NULL)
+		return threadstate;
+
+	if ((threadstate = git__calloc(1, sizeof(git_threadstate))) == NULL ||
+	    git_buf_init(&threadstate->error_buf, 0) < 0)
+		return NULL;
+
+	FlsSetValue(fls_index, threadstate);
+	return threadstate;
+}
+
+#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
+
+static pthread_key_t tls_key;
+
+static void git_threadstate_global_shutdown(void)
+{
+	git_threadstate *threadstate;
+
+	threadstate = pthread_getspecific(tls_key);
+	pthread_setspecific(tls_key, NULL);
+
+	threadstate_dispose(threadstate);
+	git__free(threadstate);
+
+	pthread_key_delete(tls_key);
+}
+
+static void tls_free(void *threadstate)
+{
+	threadstate_dispose(threadstate);
+	git__free(threadstate);
+}
+
+int git_threadstate_global_init(void)
+{
+	if (pthread_key_create(&tls_key, &tls_free) != 0)
+		return -1;
+
+	git__on_shutdown(git_threadstate_global_shutdown);
+
+	return 0;
+}
+
+git_threadstate *git_threadstate_get(void)
+{
+	git_threadstate *threadstate;
+
+	if ((threadstate = pthread_getspecific(tls_key)) != NULL)
+		return threadstate;
+
+	if ((threadstate = git__calloc(1, sizeof(git_threadstate))) == NULL ||
+	    git_buf_init(&threadstate->error_buf, 0) < 0)
+		return NULL;
+
+	pthread_setspecific(tls_key, threadstate);
+	return threadstate;
+}
+
+#elif defined(GIT_THREADS)
+# error unknown threading model
+#else
+
+static git_threadstate threadstate;
+
+static void git_threadstate_global_shutdown(void)
+{
+	threadstate_dispose(&threadstate);
+	memset(&threadstate, 0, sizeof(git_threadstate);
+}
+
+int git_threadstate_global_init(void)
+{
+	git__on_shutdown(git_threadstate_global_shutdown);
+
+	return 0;
+}
+
+git_threadstate *git_threadstate_get(void)
+{
+	return &threadstate;
+}
+
+#endif
+
+static void threadstate_dispose(git_threadstate *threadstate)
+{
+	if (!threadstate)
+		return;
+
+	git__free(threadstate->error_t.message);
+	threadstate->error_t.message = NULL;
+}
diff --git a/src/threadstate.h b/src/threadstate.h
new file mode 100644
index 0000000..9a4ef4d
--- /dev/null
+++ b/src/threadstate.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_threadstate_h__
+#define INCLUDE_threadstate_h__
+
+#include "common.h"
+
+typedef struct {
+	git_error *last_error;
+	git_error error_t;
+	git_buf error_buf;
+	char oid_fmt[GIT_OID_HEXSZ+1];
+
+	/* On Windows, this is the current child thread that was started by
+	 * `git_thread_create`.  This is used to set the thread's exit code
+	 * when terminated by `git_thread_exit`.  It is unused on POSIX.
+	 */
+	git_thread *current_thread;
+} git_threadstate;
+
+extern int git_threadstate_global_init(void);
+extern git_threadstate *git_threadstate_get(void);
+
+#define GIT_THREADSTATE (git_threadstate_get())
+
+#endif
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 0936c31..b9f8bc9 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -7,7 +7,7 @@
 
 #include "thread.h"
 
-#include "../global.h"
+#include "../tlsdata.h"
 
 #define CLEAN_THREAD_EXIT 0x6F012842
 
@@ -28,7 +28,7 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
 	git_thread *thread = lpParameter;
 
 	/* Set the current thread for `git_thread_exit` */
-	GIT_GLOBAL->current_thread = thread;
+	GIT_TLSDATA->current_thread = thread;
 
 	thread->result = thread->proc(thread->param);
 
@@ -99,8 +99,8 @@ int git_thread_join(
 
 void git_thread_exit(void *value)
 {
-	assert(GIT_GLOBAL->current_thread);
-	GIT_GLOBAL->current_thread->result = value;
+	assert(GIT_TLSDATA->current_thread);
+	GIT_TLSDATA->current_thread->result = value;
 	ExitThread(CLEAN_THREAD_EXIT);
 }