-
Notifications
You must be signed in to change notification settings - Fork 1
/
win32_threads.c
3021 lines (2711 loc) · 104 KB
/
win32_threads.c
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996 by Silicon Graphics. All rights reserved.
* Copyright (c) 1998 by Fergus Henderson. All rights reserved.
* Copyright (c) 2000-2008 by Hewlett-Packard Development Company.
* All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*/
#include "private/gc_priv.h"
#if defined(GC_WIN32_THREADS)
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
#endif
#define NOSERVICE
#include <windows.h>
#ifdef THREAD_LOCAL_ALLOC
# include "private/thread_local_alloc.h"
#endif /* THREAD_LOCAL_ALLOC */
/* Allocation lock declarations. */
#if !defined(USE_PTHREAD_LOCKS)
GC_INNER CRITICAL_SECTION GC_allocate_ml;
# ifdef GC_ASSERTIONS
GC_INNER DWORD GC_lock_holder = NO_THREAD;
/* Thread id for current holder of allocation lock */
# endif
#else
GC_INNER pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
# ifdef GC_ASSERTIONS
GC_INNER unsigned long GC_lock_holder = NO_THREAD;
# endif
#endif
#undef CreateThread
#undef ExitThread
#undef _beginthreadex
#undef _endthreadex
#ifdef GC_PTHREADS
# include <errno.h> /* for EAGAIN */
/* Cygwin-specific forward decls */
# undef pthread_create
# undef pthread_join
# undef pthread_detach
# ifndef GC_NO_PTHREAD_SIGMASK
# undef pthread_sigmask
# endif
STATIC void * GC_pthread_start(void * arg);
STATIC void GC_thread_exit_proc(void *arg);
# include <pthread.h>
# ifdef CAN_CALL_ATFORK
# include <unistd.h>
# endif
#elif !defined(MSWINCE)
# include <process.h> /* For _beginthreadex, _endthreadex */
# include <errno.h> /* for errno, EAGAIN */
#endif /* !GC_PTHREADS && !MSWINCE */
/* DllMain-based thread registration is currently incompatible */
/* with thread-local allocation, pthreads and WinCE. */
#if (defined(GC_DLL) || defined(GC_INSIDE_DLL)) \
&& !defined(GC_NO_THREADS_DISCOVERY) && !defined(MSWINCE) \
&& !defined(THREAD_LOCAL_ALLOC) && !defined(GC_PTHREADS)
/* This code operates in two distinct modes, depending on */
/* the setting of GC_win32_dll_threads. */
/* If GC_win32_dll_threads is set, all threads in the process */
/* are implicitly registered with the GC by DllMain. */
/* No explicit registration is required, and attempts at */
/* explicit registration are ignored. This mode is */
/* very different from the Posix operation of the collector. */
/* In this mode access to the thread table is lock-free. */
/* Hence there is a static limit on the number of threads. */
# ifdef GC_DISCOVER_TASK_THREADS
/* GC_DISCOVER_TASK_THREADS should be used if DllMain-based */
/* thread registration is required but it is impossible to */
/* call GC_use_threads_discovery before other GC routines. */
# define GC_win32_dll_threads TRUE
# else
STATIC GC_bool GC_win32_dll_threads = FALSE;
/* GC_win32_dll_threads must be set (if needed) at the */
/* application initialization time, i.e. before any */
/* collector or thread calls. We make it a "dynamic" */
/* option only to avoid multiple library versions. */
# endif
#else
/* If GC_win32_dll_threads is FALSE (or the collector is */
/* built without GC_DLL defined), things operate in a way */
/* that is very similar to Posix platforms, and new threads */
/* must be registered with the collector, e.g. by using */
/* preprocessor-based interception of the thread primitives. */
/* In this case, we use a real data structure for the thread */
/* table. Note that there is no equivalent of linker-based */
/* call interception, since we don't have ELF-like */
/* facilities. The Windows analog appears to be "API */
/* hooking", which really seems to be a standard way to */
/* do minor binary rewriting (?). I'd prefer not to have */
/* the basic collector rely on such facilities, but an */
/* optional package that intercepts thread calls this way */
/* would probably be nice. */
# ifndef GC_NO_THREADS_DISCOVERY
# define GC_NO_THREADS_DISCOVERY
# endif
# define GC_win32_dll_threads FALSE
# undef MAX_THREADS
# define MAX_THREADS 1 /* dll_thread_table[] is always empty. */
#endif /* GC_NO_THREADS_DISCOVERY */
/* We have two versions of the thread table. Which one */
/* we us depends on whether or not GC_win32_dll_threads */
/* is set. Note that before initialization, we don't */
/* add any entries to either table, even if DllMain is */
/* called. The main thread will be added on */
/* initialization. */
/* The type of the first argument to InterlockedExchange. */
/* Documented to be LONG volatile *, but at least gcc likes */
/* this better. */
typedef LONG * IE_t;
STATIC GC_bool GC_thr_initialized = FALSE;
#ifndef GC_ALWAYS_MULTITHREADED
GC_INNER GC_bool GC_need_to_lock = FALSE;
#endif
static GC_bool parallel_initialized = FALSE;
/* GC_use_threads_discovery() is currently incompatible with pthreads */
/* and WinCE. It might be possible to get DllMain-based thread */
/* registration to work with Cygwin, but if you try it then you are on */
/* your own. */
GC_API void GC_CALL GC_use_threads_discovery(void)
{
# ifdef GC_NO_THREADS_DISCOVERY
ABORT("GC DllMain-based thread registration unsupported");
# else
/* Turn on GC_win32_dll_threads. */
GC_ASSERT(!parallel_initialized);
/* Note that GC_use_threads_discovery is expected to be called by */
/* the client application (not from DllMain) at start-up. */
# ifndef GC_DISCOVER_TASK_THREADS
GC_win32_dll_threads = TRUE;
# endif
GC_init_parallel();
# endif
}
STATIC DWORD GC_main_thread = 0;
#define ADDR_LIMIT ((ptr_t)GC_WORD_MAX)
struct GC_Thread_Rep {
union {
# ifndef GC_NO_THREADS_DISCOVERY
volatile AO_t in_use;
/* Updated without lock. */
/* We assert that unused */
/* entries have invalid ids of */
/* zero and zero stack fields. */
/* Used only with GC_win32_dll_threads. */
LONG long_in_use; /* The same but of the type that */
/* matches the first argument of */
/* InterlockedExchange(); volatile is */
/* omitted because the ancient version */
/* of the prototype lacks the qualifier.*/
# endif
struct GC_Thread_Rep * next;
/* Hash table link without */
/* GC_win32_dll_threads. */
/* More recently allocated threads */
/* with a given pthread id come */
/* first. (All but the first are */
/* guaranteed to be dead, but we may */
/* not yet have registered the join.) */
} tm; /* table_management */
DWORD id;
# ifdef MSWINCE
/* According to MSDN specs for WinCE targets: */
/* - DuplicateHandle() is not applicable to thread handles; and */
/* - the value returned by GetCurrentThreadId() could be used as */
/* a "real" thread handle (for SuspendThread(), ResumeThread() and */
/* GetThreadContext()). */
# define THREAD_HANDLE(t) (HANDLE)(word)(t)->id
# else
HANDLE handle;
# define THREAD_HANDLE(t) (t)->handle
# endif
ptr_t stack_base; /* The cold end of the stack. */
/* 0 ==> entry not valid. */
/* !in_use ==> stack_base == 0 */
ptr_t last_stack_min; /* Last known minimum (hottest) address */
/* in stack or ADDR_LIMIT if unset */
# ifdef IA64
ptr_t backing_store_end;
ptr_t backing_store_ptr;
# endif
ptr_t thread_blocked_sp; /* Protected by GC lock. */
/* NULL value means thread unblocked. */
/* If set to non-NULL, thread will */
/* acquire GC lock before doing any */
/* pointer manipulations. Thus it does */
/* not need to stop this thread. */
struct GC_traced_stack_sect_s *traced_stack_sect;
/* Points to the "stack section" data */
/* held in stack by the innermost */
/* GC_call_with_gc_active() of this */
/* thread. May be NULL. */
unsigned short finalizer_skipped;
unsigned char finalizer_nested;
/* Used by GC_check_finalizer_nested() */
/* to minimize the level of recursion */
/* when a client finalizer allocates */
/* memory (initially both are 0). */
unsigned char suspended; /* really of GC_bool type */
# ifdef GC_PTHREADS
unsigned char flags; /* Protected by GC lock. */
# define FINISHED 1 /* Thread has exited. */
# define DETACHED 2 /* Thread is intended to be detached. */
# define KNOWN_FINISHED(t) (((t) -> flags) & FINISHED)
pthread_t pthread_id;
void *status; /* hold exit value until join in case it's a pointer */
# else
# define KNOWN_FINISHED(t) 0
# endif
# ifdef THREAD_LOCAL_ALLOC
struct thread_local_freelists tlfs;
# endif
};
typedef struct GC_Thread_Rep * GC_thread;
typedef volatile struct GC_Thread_Rep * GC_vthread;
#ifndef GC_NO_THREADS_DISCOVERY
/* We assumed that volatile ==> memory ordering, at least among */
/* volatiles. This code should consistently use atomic_ops. */
STATIC volatile GC_bool GC_please_stop = FALSE;
#elif defined(GC_ASSERTIONS)
STATIC GC_bool GC_please_stop = FALSE;
#endif
/*
* We track thread attachments while the world is supposed to be stopped.
* Unfortunately, we can't stop them from starting, since blocking in
* DllMain seems to cause the world to deadlock. Thus we have to recover
* If we notice this in the middle of marking.
*/
#ifndef GC_NO_THREADS_DISCOVERY
STATIC volatile AO_t GC_attached_thread = FALSE;
#endif
#if defined(WRAP_MARK_SOME) && !defined(GC_PTHREADS)
/* Return TRUE if an thread was attached since we last asked or */
/* since GC_attached_thread was explicitly reset. */
GC_INNER GC_bool GC_started_thread_while_stopped(void)
{
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
# ifdef AO_HAVE_compare_and_swap_release
if (AO_compare_and_swap_release(&GC_attached_thread, TRUE,
FALSE /* stored */))
return TRUE;
# else
AO_nop_full(); /* Prior heap reads need to complete earlier. */
if (AO_load(&GC_attached_thread)) {
AO_store(&GC_attached_thread, FALSE);
return TRUE;
}
# endif
}
# endif
return FALSE;
}
#endif /* WRAP_MARK_SOME */
/* Thread table used if GC_win32_dll_threads is set. */
/* This is a fixed size array. */
/* Since we use runtime conditionals, both versions */
/* are always defined. */
# ifndef MAX_THREADS
# define MAX_THREADS 512
# endif
/* Things may get quite slow for large numbers of threads, */
/* since we look them up with sequential search. */
volatile struct GC_Thread_Rep dll_thread_table[MAX_THREADS];
STATIC volatile LONG GC_max_thread_index = 0;
/* Largest index in dll_thread_table */
/* that was ever used. */
/* And now the version used if GC_win32_dll_threads is not set. */
/* This is a chained hash table, with much of the code borrowed */
/* From the Posix implementation. */
#ifndef THREAD_TABLE_SZ
# define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */
#endif
#define THREAD_TABLE_INDEX(id) /* id is of DWORD type */ \
(int)((((id) >> 8) ^ (id)) % THREAD_TABLE_SZ)
STATIC GC_thread GC_threads[THREAD_TABLE_SZ];
/* It may not be safe to allocate when we register the first thread. */
/* Thus we allocated one statically. It does not contain any field we */
/* need to push ("next" and "status" fields are unused). */
static struct GC_Thread_Rep first_thread;
static GC_bool first_thread_used = FALSE;
/* Add a thread to GC_threads. We assume it wasn't already there. */
/* Caller holds allocation lock. */
/* Unlike the pthreads version, the id field is set by the caller. */
STATIC GC_thread GC_new_thread(DWORD id)
{
int hv = THREAD_TABLE_INDEX(id);
GC_thread result;
# ifdef DEBUG_THREADS
GC_log_printf("Creating thread 0x%lx\n", (long)id);
if (GC_threads[hv] != NULL)
GC_log_printf("Hash collision at GC_threads[%d]\n", hv);
# endif
GC_ASSERT(I_HOLD_LOCK());
if (!EXPECT(first_thread_used, TRUE)) {
result = &first_thread;
first_thread_used = TRUE;
GC_ASSERT(NULL == GC_threads[hv]);
} else {
GC_ASSERT(!GC_win32_dll_threads);
result = (struct GC_Thread_Rep *)
GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL);
if (result == 0) return(0);
}
/* result -> id = id; Done by caller. */
result -> tm.next = GC_threads[hv];
GC_threads[hv] = result;
# ifdef GC_PTHREADS
GC_ASSERT(result -> flags == 0);
# endif
GC_ASSERT(result -> thread_blocked_sp == NULL);
if (EXPECT(result != &first_thread, TRUE))
GC_dirty(result);
return(result);
}
GC_INNER GC_bool GC_in_thread_creation = FALSE;
/* Protected by allocation lock. */
GC_INLINE void GC_record_stack_base(GC_vthread me,
const struct GC_stack_base *sb)
{
me -> stack_base = (ptr_t)sb->mem_base;
# ifdef IA64
me -> backing_store_end = (ptr_t)sb->reg_base;
# endif
if (me -> stack_base == NULL)
ABORT("Bad stack base in GC_register_my_thread");
}
/* This may be called from DllMain, and hence operates under unusual */
/* constraints. In particular, it must be lock-free if */
/* GC_win32_dll_threads is set. Always called from the thread being */
/* added. If GC_win32_dll_threads is not set, we already hold the */
/* allocation lock except possibly during single-threaded startup code. */
STATIC GC_thread GC_register_my_thread_inner(const struct GC_stack_base *sb,
DWORD thread_id)
{
GC_vthread me;
/* The following should be a no-op according to the win32 */
/* documentation. There is empirical evidence that it */
/* isn't. - HB */
# if defined(MPROTECT_VDB) && !defined(CYGWIN32)
if (GC_auto_incremental
# ifdef GWW_VDB
&& !GC_gww_dirty_init()
# endif
)
GC_set_write_fault_handler();
# endif
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
int i;
/* It appears to be unsafe to acquire a lock here, since this */
/* code is apparently not preemptible on some systems. */
/* (This is based on complaints, not on Microsoft's official */
/* documentation, which says this should perform "only simple */
/* initialization tasks".) */
/* Hence we make do with nonblocking synchronization. */
/* It has been claimed that DllMain is really only executed with */
/* a particular system lock held, and thus careful use of locking */
/* around code that doesn't call back into the system libraries */
/* might be OK. But this hasn't been tested across all win32 */
/* variants. */
for (i = 0;
InterlockedExchange(&dll_thread_table[i].tm.long_in_use, 1) != 0;
i++) {
/* Compare-and-swap would make this cleaner, but that's not */
/* supported before Windows 98 and NT 4.0. In Windows 2000, */
/* InterlockedExchange is supposed to be replaced by */
/* InterlockedExchangePointer, but that's not really what I */
/* want here. */
/* FIXME: We should eventually declare Win95 dead and use AO_ */
/* primitives here. */
if (i == MAX_THREADS - 1)
ABORT("Too many threads");
}
/* Update GC_max_thread_index if necessary. The following is */
/* safe, and unlike CompareExchange-based solutions seems to work */
/* on all Windows95 and later platforms. */
/* Unfortunately, GC_max_thread_index may be temporarily out of */
/* bounds, so readers have to compensate. */
while (i > GC_max_thread_index) {
InterlockedIncrement((IE_t)&GC_max_thread_index);
}
if (GC_max_thread_index >= MAX_THREADS) {
/* We overshot due to simultaneous increments. */
/* Setting it to MAX_THREADS-1 is always safe. */
GC_max_thread_index = MAX_THREADS - 1;
}
me = dll_thread_table + i;
} else
# endif
/* else */ /* Not using DllMain */ {
GC_ASSERT(I_HOLD_LOCK());
GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */
me = GC_new_thread(thread_id);
GC_in_thread_creation = FALSE;
if (me == 0)
ABORT("Failed to allocate memory for thread registering");
}
# ifdef GC_PTHREADS
/* me can be NULL -> segfault */
me -> pthread_id = pthread_self();
# endif
# ifndef MSWINCE
/* GetCurrentThread() returns a pseudohandle (a const value). */
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(),
(HANDLE*)&(me -> handle),
0 /* dwDesiredAccess */, FALSE /* bInheritHandle */,
DUPLICATE_SAME_ACCESS)) {
ABORT_ARG1("DuplicateHandle failed",
": errcode= 0x%X", (unsigned)GetLastError());
}
# endif
me -> last_stack_min = ADDR_LIMIT;
GC_record_stack_base(me, sb);
/* Up until this point, GC_push_all_stacks considers this thread */
/* invalid. */
/* Up until this point, this entry is viewed as reserved but invalid */
/* by GC_delete_thread. */
me -> id = thread_id;
# if defined(THREAD_LOCAL_ALLOC)
GC_init_thread_local((GC_tlfs)(&(me->tlfs)));
# endif
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
if (GC_please_stop) {
AO_store(&GC_attached_thread, TRUE);
AO_nop_full(); /* Later updates must become visible after this. */
}
/* We'd like to wait here, but can't, since waiting in DllMain */
/* provokes deadlocks. */
/* Thus we force marking to be restarted instead. */
} else
# endif
/* else */ {
GC_ASSERT(!GC_please_stop);
/* Otherwise both we and the thread stopping code would be */
/* holding the allocation lock. */
}
return (GC_thread)(me);
}
/*
* GC_max_thread_index may temporarily be larger than MAX_THREADS.
* To avoid subscript errors, we check on access.
*/
GC_INLINE LONG GC_get_max_thread_index(void)
{
LONG my_max = GC_max_thread_index;
if (my_max >= MAX_THREADS) return MAX_THREADS - 1;
return my_max;
}
/* Return the GC_thread corresponding to a thread id. May be called */
/* without a lock, but should be called in contexts in which the */
/* requested thread cannot be asynchronously deleted, e.g. from the */
/* thread itself. */
/* This version assumes that either GC_win32_dll_threads is set, or */
/* we hold the allocator lock. */
/* Also used (for assertion checking only) from thread_local_alloc.c. */
STATIC GC_thread GC_lookup_thread_inner(DWORD thread_id)
{
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
int i;
LONG my_max = GC_get_max_thread_index();
for (i = 0; i <= my_max &&
(!AO_load_acquire(&dll_thread_table[i].tm.in_use)
|| dll_thread_table[i].id != thread_id);
/* Must still be in_use, since nobody else can store our */
/* thread_id. */
i++) {
/* empty */
}
return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL;
} else
# endif
/* else */ {
GC_thread p = GC_threads[THREAD_TABLE_INDEX(thread_id)];
GC_ASSERT(I_HOLD_LOCK());
while (p != 0 && p -> id != thread_id) p = p -> tm.next;
return(p);
}
}
#ifdef LINT2
# define CHECK_LOOKUP_MY_THREAD(me) \
if (!(me)) ABORT("GC_lookup_thread_inner(GetCurrentThreadId) failed")
#else
# define CHECK_LOOKUP_MY_THREAD(me) /* empty */
#endif
/* Called by GC_finalize() (in case of an allocation failure observed). */
/* GC_reset_finalizer_nested() is the same as in pthread_support.c. */
GC_INNER void GC_reset_finalizer_nested(void)
{
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId());
CHECK_LOOKUP_MY_THREAD(me);
me->finalizer_nested = 0;
}
/* Checks and updates the thread-local level of finalizers recursion. */
/* Returns NULL if GC_invoke_finalizers() should not be called by the */
/* collector (to minimize the risk of a deep finalizers recursion), */
/* otherwise returns a pointer to the thread-local finalizer_nested. */
/* Called by GC_notify_or_invoke_finalizers() only (the lock is held). */
/* GC_check_finalizer_nested() is the same as in pthread_support.c. */
GC_INNER unsigned char *GC_check_finalizer_nested(void)
{
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId());
unsigned nesting_level;
CHECK_LOOKUP_MY_THREAD(me);
nesting_level = me->finalizer_nested;
if (nesting_level) {
/* We are inside another GC_invoke_finalizers(). */
/* Skip some implicitly-called GC_invoke_finalizers() */
/* depending on the nesting (recursion) level. */
if (++me->finalizer_skipped < (1U << nesting_level)) return NULL;
me->finalizer_skipped = 0;
}
me->finalizer_nested = (unsigned char)(nesting_level + 1);
return &me->finalizer_nested;
}
#if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
/* This is called from thread-local GC_malloc(). */
GC_bool GC_is_thread_tsd_valid(void *tsd)
{
GC_thread me;
DCL_LOCK_STATE;
LOCK();
me = GC_lookup_thread_inner(GetCurrentThreadId());
UNLOCK();
return (word)tsd >= (word)(&me->tlfs)
&& (word)tsd < (word)(&me->tlfs) + sizeof(me->tlfs);
}
#endif /* GC_ASSERTIONS && THREAD_LOCAL_ALLOC */
GC_API int GC_CALL GC_thread_is_registered(void)
{
DWORD thread_id = GetCurrentThreadId();
GC_thread me;
DCL_LOCK_STATE;
LOCK();
me = GC_lookup_thread_inner(thread_id);
UNLOCK();
return me != NULL;
}
GC_API void GC_CALL GC_register_altstack(void *stack GC_ATTR_UNUSED,
GC_word stack_size GC_ATTR_UNUSED,
void *altstack GC_ATTR_UNUSED,
GC_word altstack_size GC_ATTR_UNUSED)
{
/* TODO: Implement */
}
/* Make sure thread descriptor t is not protected by the VDB */
/* implementation. */
/* Used to prevent write faults when the world is (partially) stopped, */
/* since it may have been stopped with a system lock held, and that */
/* lock may be required for fault handling. */
#if defined(MPROTECT_VDB)
# define UNPROTECT_THREAD(t) \
if (!GC_win32_dll_threads && GC_auto_incremental \
&& t != &first_thread) { \
GC_ASSERT(SMALL_OBJ(GC_size(t))); \
GC_remove_protection(HBLKPTR(t), 1, FALSE); \
} else (void)0
#else
# define UNPROTECT_THREAD(t) (void)0
#endif
#ifdef CYGWIN32
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id
#elif defined(GC_WIN32_PTHREADS) || defined(GC_PTHREADS_PARAMARK)
# include <pthread.h> /* to check for winpthreads */
# if defined(__WINPTHREADS_VERSION_MAJOR)
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id
# else
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id.p
# endif
#endif
/* If a thread has been joined, but we have not yet */
/* been notified, then there may be more than one thread */
/* in the table with the same win32 id. */
/* This is OK, but we need a way to delete a specific one. */
/* Assumes we hold the allocation lock unless */
/* GC_win32_dll_threads is set. Does not actually free */
/* GC_thread entry (only unlinks it). */
/* If GC_win32_dll_threads is set it should be called from the */
/* thread being deleted. */
STATIC void GC_delete_gc_thread_no_free(GC_vthread t)
{
# ifndef MSWINCE
CloseHandle(t->handle);
# endif
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
/* This is intended to be lock-free. */
/* It is either called synchronously from the thread being */
/* deleted, or by the joining thread. */
/* In this branch asynchronous changes to (*t) are possible. */
/* It's not allowed to call GC_printf (and the friends) here, */
/* see GC_stop_world() for the information. */
t -> stack_base = 0;
t -> id = 0;
AO_store_release(&t->tm.in_use, FALSE);
} else
# endif
/* else */ {
DWORD id = ((GC_thread)t) -> id;
/* Cast away volatile qualifier, since we have lock. */
int hv = THREAD_TABLE_INDEX(id);
GC_thread p = GC_threads[hv];
GC_thread prev = NULL;
GC_ASSERT(I_HOLD_LOCK());
while (p != (GC_thread)t) {
prev = p;
p = p -> tm.next;
}
if (prev == 0) {
GC_threads[hv] = p -> tm.next;
} else {
GC_ASSERT(prev != &first_thread);
prev -> tm.next = p -> tm.next;
GC_dirty(prev);
}
}
}
/* Delete a thread from GC_threads. We assume it is there. */
/* (The code intentionally traps if it wasn't.) Assumes we */
/* hold the allocation lock unless GC_win32_dll_threads is set. */
/* If GC_win32_dll_threads is set then it should be called from */
/* the thread being deleted. It is also safe to delete the */
/* main thread (unless GC_win32_dll_threads). */
STATIC void GC_delete_thread(DWORD id)
{
if (GC_win32_dll_threads) {
GC_vthread t = GC_lookup_thread_inner(id);
if (0 == t) {
WARN("Removing nonexistent thread, id = %" WARN_PRIdPTR "\n", id);
} else {
GC_delete_gc_thread_no_free(t);
}
} else {
int hv = THREAD_TABLE_INDEX(id);
GC_thread p = GC_threads[hv];
GC_thread prev = NULL;
GC_ASSERT(I_HOLD_LOCK());
while (p -> id != id) {
prev = p;
p = p -> tm.next;
}
# ifndef MSWINCE
CloseHandle(p->handle);
# endif
if (prev == 0) {
GC_threads[hv] = p -> tm.next;
} else {
GC_ASSERT(prev != &first_thread);
prev -> tm.next = p -> tm.next;
GC_dirty(prev);
}
if (EXPECT(p != &first_thread, TRUE)) {
GC_INTERNAL_FREE(p);
}
}
}
GC_API void GC_CALL GC_allow_register_threads(void)
{
/* Check GC is initialized and the current thread is registered. */
GC_ASSERT(GC_lookup_thread_inner(GetCurrentThreadId()) != 0);
# if !defined(GC_ALWAYS_MULTITHREADED) && !defined(PARALLEL_MARK) \
&& !defined(GC_NO_THREADS_DISCOVERY)
/* GC_init() does not call GC_init_parallel() in this case. */
parallel_initialized = TRUE;
# endif
set_need_to_lock();
}
GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *sb)
{
GC_thread me;
DWORD thread_id = GetCurrentThreadId();
DCL_LOCK_STATE;
if (GC_need_to_lock == FALSE)
ABORT("Threads explicit registering is not previously enabled");
/* We lock here, since we want to wait for an ongoing GC. */
LOCK();
me = GC_lookup_thread_inner(thread_id);
if (me == 0) {
# ifdef GC_PTHREADS
me = GC_register_my_thread_inner(sb, thread_id);
me -> flags |= DETACHED;
/* Treat as detached, since we do not need to worry about */
/* pointer results. */
# else
GC_register_my_thread_inner(sb, thread_id);
# endif
UNLOCK();
return GC_SUCCESS;
} else
# ifdef GC_PTHREADS
/* else */ if ((me -> flags & FINISHED) != 0) {
GC_record_stack_base(me, sb);
me -> flags &= ~FINISHED; /* but not DETACHED */
# ifdef THREAD_LOCAL_ALLOC
GC_init_thread_local((GC_tlfs)(&me->tlfs));
# endif
UNLOCK();
return GC_SUCCESS;
} else
# endif
/* else */ {
UNLOCK();
return GC_DUPLICATE;
}
}
/* Similar to that in pthread_support.c. */
STATIC void GC_wait_for_gc_completion(GC_bool wait_for_all)
{
GC_ASSERT(I_HOLD_LOCK());
if (GC_incremental && GC_collection_in_progress()) {
word old_gc_no = GC_gc_no;
/* Make sure that no part of our stack is still on the mark stack, */
/* since it's about to be unmapped. */
do {
ENTER_GC();
GC_in_thread_creation = TRUE;
GC_collect_a_little_inner(1);
GC_in_thread_creation = FALSE;
EXIT_GC();
UNLOCK();
Sleep(0); /* yield */
LOCK();
} while (GC_incremental && GC_collection_in_progress()
&& (wait_for_all || old_gc_no == GC_gc_no));
}
}
GC_API int GC_CALL GC_unregister_my_thread(void)
{
DCL_LOCK_STATE;
# ifdef DEBUG_THREADS
GC_log_printf("Unregistering thread 0x%lx\n", (long)GetCurrentThreadId());
# endif
if (GC_win32_dll_threads) {
# if defined(THREAD_LOCAL_ALLOC)
/* Can't happen: see GC_use_threads_discovery(). */
GC_ASSERT(FALSE);
# else
/* FIXME: Should we just ignore this? */
GC_delete_thread(GetCurrentThreadId());
# endif
} else {
# if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
GC_thread me;
# endif
DWORD thread_id = GetCurrentThreadId();
LOCK();
GC_wait_for_gc_completion(FALSE);
# if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me);
GC_ASSERT(!KNOWN_FINISHED(me));
# endif
# if defined(THREAD_LOCAL_ALLOC)
GC_ASSERT(GC_getspecific(GC_thread_key) == &me->tlfs);
GC_destroy_thread_local(&(me->tlfs));
# endif
# ifdef GC_PTHREADS
if ((me -> flags & DETACHED) == 0) {
me -> flags |= FINISHED;
} else
# endif
/* else */ {
GC_delete_thread(thread_id);
}
# if defined(THREAD_LOCAL_ALLOC)
/* It is required to call remove_specific defined in specific.c. */
GC_remove_specific(GC_thread_key);
# endif
UNLOCK();
}
return GC_SUCCESS;
}
/* Wrapper for functions that are likely to block for an appreciable */
/* length of time. */
/* GC_do_blocking_inner() is nearly the same as in pthread_support.c */
GC_INNER void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED)
{
struct blocking_data * d = (struct blocking_data *) data;
DWORD thread_id = GetCurrentThreadId();
GC_thread me;
# ifdef IA64
ptr_t stack_ptr = GC_save_regs_in_stack();
# endif
DCL_LOCK_STATE;
LOCK();
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me);
GC_ASSERT(me -> thread_blocked_sp == NULL);
# ifdef IA64
me -> backing_store_ptr = stack_ptr;
# endif
me -> thread_blocked_sp = (ptr_t) &d; /* save approx. sp */
/* Save context here if we want to support precise stack marking */
UNLOCK();
d -> client_data = (d -> fn)(d -> client_data);
LOCK(); /* This will block if the world is stopped. */
# if defined(CPPCHECK)
GC_noop1((word)me->thread_blocked_sp);
# endif
me -> thread_blocked_sp = NULL;
UNLOCK();
}
/* GC_call_with_gc_active() has the opposite to GC_do_blocking() */
/* functionality. It might be called from a user function invoked by */
/* GC_do_blocking() to temporarily back allow calling any GC function */
/* and/or manipulating pointers to the garbage collected heap. */
GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn,
void * client_data)
{
struct GC_traced_stack_sect_s stacksect;
DWORD thread_id = GetCurrentThreadId();
GC_thread me;
DCL_LOCK_STATE;
LOCK(); /* This will block if the world is stopped. */
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me);
/* Adjust our stack base value (this could happen unless */
/* GC_get_stack_base() was used which returned GC_SUCCESS). */
GC_ASSERT(me -> stack_base != NULL);
if ((word)me->stack_base < (word)(&stacksect))
me -> stack_base = (ptr_t)(&stacksect);
if (me -> thread_blocked_sp == NULL) {
/* We are not inside GC_do_blocking() - do nothing more. */
UNLOCK();
client_data = fn(client_data);
/* Prevent treating the above as a tail call. */
GC_noop1(COVERT_DATAFLOW(&stacksect));
return client_data; /* result */
}
/* Setup new "stack section". */
stacksect.saved_stack_ptr = me -> thread_blocked_sp;
# ifdef IA64
/* This is the same as in GC_call_with_stack_base(). */
stacksect.backing_store_end = GC_save_regs_in_stack();
/* Unnecessarily flushes register stack, */
/* but that probably doesn't hurt. */
stacksect.saved_backing_store_ptr = me -> backing_store_ptr;
# endif
stacksect.prev = me -> traced_stack_sect;
me -> thread_blocked_sp = NULL;
me -> traced_stack_sect = &stacksect;
UNLOCK();
client_data = fn(client_data);
GC_ASSERT(me -> thread_blocked_sp == NULL);
GC_ASSERT(me -> traced_stack_sect == &stacksect);
/* Restore original "stack section". */
LOCK();
# if defined(CPPCHECK)
GC_noop1((word)me->traced_stack_sect);
# endif
me -> traced_stack_sect = stacksect.prev;
# ifdef IA64
me -> backing_store_ptr = stacksect.saved_backing_store_ptr;
# endif
me -> thread_blocked_sp = stacksect.saved_stack_ptr;
UNLOCK();
return client_data; /* result */
}
#ifdef GC_PTHREADS
/* A quick-and-dirty cache of the mapping between pthread_t */
/* and win32 thread id. */
# define PTHREAD_MAP_SIZE 512
DWORD GC_pthread_map_cache[PTHREAD_MAP_SIZE] = {0};
# define PTHREAD_MAP_INDEX(pthread_id) \
((NUMERIC_THREAD_ID(pthread_id) >> 5) % PTHREAD_MAP_SIZE)
/* It appears pthread_t is really a pointer type ... */
# define SET_PTHREAD_MAP_CACHE(pthread_id, win32_id) \
(void)(GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)] = (win32_id))
# define GET_PTHREAD_MAP_CACHE(pthread_id) \
GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)]
/* Return a GC_thread corresponding to a given pthread_t. */
/* Returns 0 if it's not there. */
/* We assume that this is only called for pthread ids that */
/* have not yet terminated or are still joinable, and */
/* cannot be concurrently terminated. */
/* Assumes we do NOT hold the allocation lock. */
STATIC GC_thread GC_lookup_pthread(pthread_t id)
{
# ifndef GC_NO_THREADS_DISCOVERY
if (GC_win32_dll_threads) {
int i;
LONG my_max = GC_get_max_thread_index();
for (i = 0; i <= my_max &&
(!AO_load_acquire(&dll_thread_table[i].tm.in_use)
|| THREAD_EQUAL(dll_thread_table[i].pthread_id, id));
/* Must still be in_use, since nobody else can */
/* store our thread_id. */
i++) {
/* empty */
}
return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL;
} else
# endif