forked from mono/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_support.c
1924 lines (1715 loc) · 56.9 KB
/
pthread_support.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-2004 by Hewlett-Packard 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.
*/
/*
* Support code for LinuxThreads, the clone()-based kernel
* thread package for Linux which is included in libc6.
*
* This code relies on implementation details of LinuxThreads,
* (i.e. properties not guaranteed by the Pthread standard),
* though this version now does less of that than the other Pthreads
* support code.
*
* Note that there is a lot of code duplication between linux_threads.c
* and thread support for some of the other Posix platforms; any changes
* made here may need to be reflected there too.
*/
/* DG/UX ix86 support <[email protected]> */
/*
* Linux_threads.c now also includes some code to support HPUX and
* OSF1 (Compaq Tru64 Unix, really). The OSF1 support is based on Eric Benson's
* patch.
*
* Eric also suggested an alternate basis for a lock implementation in
* his code:
* + #elif defined(OSF1)
* + unsigned long GC_allocate_lock = 0;
* + msemaphore GC_allocate_semaphore;
* + # define GC_TRY_LOCK() \
* + ((msem_lock(&GC_allocate_semaphore, MSEM_IF_NOWAIT) == 0) \
* + ? (GC_allocate_lock = 1) \
* + : 0)
* + # define GC_LOCK_TAKEN GC_allocate_lock
*/
/*#define DEBUG_THREADS 1*/
/*#define GC_ASSERTIONS*/
# include "private/pthread_support.h"
# if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
&& !defined(GC_WIN32_THREADS)
# if defined(GC_HPUX_THREADS) && !defined(USE_PTHREAD_SPECIFIC) \
&& !defined(USE_COMPILER_TLS)
# ifdef __GNUC__
# define USE_PTHREAD_SPECIFIC
/* Empirically, as of gcc 3.3, USE_COMPILER_TLS doesn't work. */
# else
# define USE_COMPILER_TLS
# endif
# endif
# if defined USE_HPUX_TLS
--> Macro replaced by USE_COMPILER_TLS
# endif
#ifndef USE_COMPILER_TLS
# if (defined(GC_DGUX386_THREADS) || defined(GC_OSF1_THREADS) || \
defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS)) || \
defined(GC_NETBSD_THREADS) && !defined(USE_PTHREAD_SPECIFIC) || \
defined(GC_FREEBSD_THREADS) && !defined(USE_PTHREAD_SPECIFIC) || \
defined(GC_OPENBSD_THREADS)
# define USE_PTHREAD_SPECIFIC
# endif
#endif
# if defined(GC_DGUX386_THREADS) && !defined(_POSIX4A_DRAFT10_SOURCE)
# define _POSIX4A_DRAFT10_SOURCE 1
# endif
# if defined(GC_DGUX386_THREADS) && !defined(_USING_POSIX4A_DRAFT10)
# define _USING_POSIX4A_DRAFT10 1
# endif
# ifdef THREAD_LOCAL_ALLOC
# if !defined(USE_PTHREAD_SPECIFIC) && !defined(USE_COMPILER_TLS)
# include "private/specific.h"
# endif
/* Note that these macros should be used only to get/set the GC_thread pointer.
* We need to use both tls and pthread because we use the pthread_create function hook to
* free the data for foreign threads. When that doesn't happen, libgc could have old
* pthread_t that get reused...
*/
# if defined(USE_PTHREAD_SPECIFIC)
# define GC_getspecific pthread_getspecific
# define GC_setspecific pthread_setspecific
# define GC_key_create pthread_key_create
typedef pthread_key_t GC_key_t;
# endif
# if defined(USE_COMPILER_TLS)
/* Note sles9 gcc on powerpc gets confused by the define to set GC_thread_tls and pthread_setspecific
* so we actually use a static inline function decalred below that is equivalent to:
* define GC_setspecific(key, v) (GC_thread_tls = (v), pthread_setspecific ((key), (v)))
*/
# define GC_getspecific(x) (GC_thread_tls)
# define GC_key_create pthread_key_create
typedef pthread_key_t GC_key_t;
# endif
# endif
# include <stdlib.h>
# include <pthread.h>
# include <sched.h>
# include <time.h>
# include <errno.h>
# include <unistd.h>
# include <sys/mman.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <signal.h>
#if defined(GC_DARWIN_THREADS)
# include "private/darwin_semaphore.h"
#else
# include <semaphore.h>
#endif /* !GC_DARWIN_THREADS */
#if defined(GC_DARWIN_THREADS) || defined(GC_FREEBSD_THREADS)
# include <sys/sysctl.h>
#endif /* GC_DARWIN_THREADS */
#if defined(GC_NETBSD_THREADS) || defined(GC_OPENBSD_THREADS)
# include <sys/param.h>
# include <sys/sysctl.h>
#endif
#if defined(GC_DGUX386_THREADS)
# include <sys/dg_sys_info.h>
# include <sys/_int_psem.h>
/* sem_t is an uint in DG/UX */
typedef unsigned int sem_t;
#endif /* GC_DGUX386_THREADS */
#ifndef __GNUC__
# define __inline__
#endif
#ifdef GC_USE_LD_WRAP
# define WRAP_FUNC(f) __wrap_##f
# define REAL_FUNC(f) __real_##f
#else
# define WRAP_FUNC(f) GC_##f
# if !defined(GC_DGUX386_THREADS)
# define REAL_FUNC(f) f
# else /* GC_DGUX386_THREADS */
# define REAL_FUNC(f) __d10_##f
# endif /* GC_DGUX386_THREADS */
# undef pthread_create
# if !defined(GC_DARWIN_THREADS)
# undef pthread_sigmask
# endif
# undef pthread_join
# undef pthread_detach
# if defined(NACL)
# undef pthread_exit
# endif
# if defined(GC_OSF1_THREADS) && defined(_PTHREAD_USE_MANGLED_NAMES_) \
&& !defined(_PTHREAD_USE_PTDNAM_)
/* Restore the original mangled names on Tru64 UNIX. */
# define pthread_create __pthread_create
# define pthread_join __pthread_join
# define pthread_detach __pthread_detach
# endif
#endif
void GC_thr_init();
static GC_bool parallel_initialized = FALSE;
void GC_init_parallel();
static pthread_t main_pthread_self;
static void *main_stack, *main_altstack;
static int main_stack_size, main_altstack_size;
# if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
/* We don't really support thread-local allocation with DBG_HDRS_ALL */
/* work around a dlopen issue (bug #75390), undefs to avoid warnings with redefinitions */
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "mono/utils/mono-compiler.h"
static
GC_key_t GC_thread_key;
#ifdef USE_COMPILER_TLS
__thread MONO_TLS_FAST void* GC_thread_tls;
/*
* gcc errors out with /tmp/ccdPMFuq.s:2994: Error: symbol `.LTLS4' is already defined
* if the inline is added on powerpc
*/
#if !defined(__ppc__) && !defined(__powerpc__)
inline
#endif
static int GC_setspecific (GC_key_t key, void *value) {
GC_thread_tls = value;
return pthread_setspecific (key, value);
}
#endif
static GC_bool keys_initialized;
#ifdef MONO_DEBUGGER_SUPPORTED
#include "include/libgc-mono-debugger.h"
#endif
/* Recover the contents of the freelist array fl into the global one gfl.*/
/* Note that the indexing scheme differs, in that gfl has finer size */
/* resolution, even if not all entries are used. */
/* We hold the allocator lock. */
static void return_freelists(ptr_t *fl, ptr_t *gfl)
{
int i;
ptr_t q, *qptr;
size_t nwords;
for (i = 1; i < NFREELISTS; ++i) {
nwords = i * (GRANULARITY/sizeof(word));
qptr = fl + i;
q = *qptr;
if ((word)q >= HBLKSIZE) {
if (gfl[nwords] == 0) {
gfl[nwords] = q;
} else {
/* Concatenate: */
for (; (word)q >= HBLKSIZE; qptr = &(obj_link(q)), q = *qptr);
GC_ASSERT(0 == q);
*qptr = gfl[nwords];
gfl[nwords] = fl[i];
}
}
/* Clear fl[i], since the thread structure may hang around. */
/* Do it in a way that is likely to trap if we access it. */
fl[i] = (ptr_t)HBLKSIZE;
}
}
/* We statically allocate a single "size 0" object. It is linked to */
/* itself, and is thus repeatedly reused for all size 0 allocation */
/* requests. (Size 0 gcj allocation requests are incorrect, and */
/* we arrange for those to fault asap.) */
static ptr_t size_zero_object = (ptr_t)(&size_zero_object);
void GC_delete_gc_thread(pthread_t id, GC_thread gct);
void GC_destroy_thread_local(GC_thread p);
void GC_thread_deregister_foreign (void *data)
{
GC_thread me = (GC_thread)data;
/* GC_fprintf1( "\n\n\n\n --- Deregister %x ---\n\n\n\n\n", me->flags ); */
if (me -> flags & FOREIGN_THREAD) {
LOCK();
/* GC_fprintf0( "\n\n\n\n --- FOO ---\n\n\n\n\n" ); */
#if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
GC_destroy_thread_local (me);
#endif
GC_delete_gc_thread(me->id, me);
UNLOCK();
}
}
/* Each thread structure must be initialized. */
/* This call must be made from the new thread. */
/* Caller holds allocation lock. */
void GC_init_thread_local(GC_thread p)
{
int i;
if (!keys_initialized) {
if (0 != GC_key_create(&GC_thread_key, GC_thread_deregister_foreign)) {
ABORT("Failed to create key for local allocator");
}
keys_initialized = TRUE;
}
if (0 != GC_setspecific(GC_thread_key, p)) {
ABORT("Failed to set thread specific allocation pointers");
}
for (i = 1; i < NFREELISTS; ++i) {
p -> ptrfree_freelists[i] = (ptr_t)1;
p -> normal_freelists[i] = (ptr_t)1;
# ifdef GC_GCJ_SUPPORT
p -> gcj_freelists[i] = (ptr_t)1;
# endif
}
/* Set up the size 0 free lists. */
p -> ptrfree_freelists[0] = (ptr_t)(&size_zero_object);
p -> normal_freelists[0] = (ptr_t)(&size_zero_object);
# ifdef GC_GCJ_SUPPORT
p -> gcj_freelists[0] = (ptr_t)(-1);
# endif
}
#ifdef GC_GCJ_SUPPORT
extern ptr_t * GC_gcjobjfreelist;
#endif
/* We hold the allocator lock. */
void GC_destroy_thread_local(GC_thread p)
{
/* We currently only do this from the thread itself or from */
/* the fork handler for a child process. */
# ifndef HANDLE_FORK
GC_ASSERT(GC_getspecific(GC_thread_key) == (void *)p);
# endif
return_freelists(p -> ptrfree_freelists, GC_aobjfreelist);
return_freelists(p -> normal_freelists, GC_objfreelist);
# ifdef GC_GCJ_SUPPORT
return_freelists(p -> gcj_freelists, GC_gcjobjfreelist);
# endif
}
extern GC_PTR GC_generic_malloc_many();
GC_PTR GC_local_malloc(size_t bytes)
{
if (EXPECT(!SMALL_ENOUGH(bytes),0)) {
return(GC_malloc(bytes));
} else {
int index = INDEX_FROM_BYTES(bytes);
ptr_t * my_fl;
ptr_t my_entry;
# if defined(REDIRECT_MALLOC) && !defined(USE_PTHREAD_SPECIFIC)
GC_key_t k = GC_thread_key;
# endif
void * tsd;
# if defined(REDIRECT_MALLOC) && !defined(USE_PTHREAD_SPECIFIC)
if (EXPECT(0 == k, 0)) {
/* This can happen if we get called when the world is */
/* being initialized. Whether we can actually complete */
/* the initialization then is unclear. */
GC_init_parallel();
k = GC_thread_key;
}
# endif
tsd = GC_getspecific(GC_thread_key);
# ifdef GC_ASSERTIONS
LOCK();
GC_ASSERT(tsd == (void *)GC_lookup_thread(pthread_self()));
UNLOCK();
# endif
my_fl = ((GC_thread)tsd) -> normal_freelists + index;
my_entry = *my_fl;
if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
ptr_t next = obj_link(my_entry);
GC_PTR result = (GC_PTR)my_entry;
*my_fl = next;
obj_link(my_entry) = 0;
PREFETCH_FOR_WRITE(next);
return result;
} else if ((word)my_entry - 1 < DIRECT_GRANULES) {
*my_fl = my_entry + index + 1;
return GC_malloc(bytes);
} else {
GC_generic_malloc_many(BYTES_FROM_INDEX(index), NORMAL, my_fl);
if (*my_fl == 0) return GC_oom_fn(bytes);
return GC_local_malloc(bytes);
}
}
}
GC_PTR GC_local_malloc_atomic(size_t bytes)
{
if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
return(GC_malloc_atomic(bytes));
} else {
int index = INDEX_FROM_BYTES(bytes);
ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
-> ptrfree_freelists + index;
ptr_t my_entry = *my_fl;
if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
GC_PTR result = (GC_PTR)my_entry;
*my_fl = obj_link(my_entry);
return result;
} else if ((word)my_entry - 1 < DIRECT_GRANULES) {
*my_fl = my_entry + index + 1;
return GC_malloc_atomic(bytes);
} else {
GC_generic_malloc_many(BYTES_FROM_INDEX(index), PTRFREE, my_fl);
/* *my_fl is updated while the collector is excluded; */
/* the free list is always visible to the collector as */
/* such. */
if (*my_fl == 0) return GC_oom_fn(bytes);
return GC_local_malloc_atomic(bytes);
}
}
}
#ifdef GC_GCJ_SUPPORT
#include "include/gc_gcj.h"
#ifdef GC_ASSERTIONS
extern GC_bool GC_gcj_malloc_initialized;
#endif
extern int GC_gcj_kind;
GC_PTR GC_local_gcj_malloc(size_t bytes,
void * ptr_to_struct_containing_descr)
{
GC_ASSERT(GC_gcj_malloc_initialized);
if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
} else {
int index = INDEX_FROM_BYTES(bytes);
ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
-> gcj_freelists + index;
ptr_t my_entry = *my_fl;
if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
GC_PTR result = (GC_PTR)my_entry;
GC_ASSERT(!GC_incremental);
/* We assert that any concurrent marker will stop us. */
/* Thus it is impossible for a mark procedure to see the */
/* allocation of the next object, but to see this object */
/* still containing a free list pointer. Otherwise the */
/* marker might find a random "mark descriptor". */
*(volatile ptr_t *)my_fl = obj_link(my_entry);
/* We must update the freelist before we store the pointer. */
/* Otherwise a GC at this point would see a corrupted */
/* free list. */
/* A memory barrier is probably never needed, since the */
/* action of stopping this thread will cause prior writes */
/* to complete. */
GC_ASSERT(((void * volatile *)result)[1] == 0);
*(void * volatile *)result = ptr_to_struct_containing_descr;
return result;
} else if ((word)my_entry - 1 < DIRECT_GRANULES) {
if (!GC_incremental) *my_fl = my_entry + index + 1;
/* In the incremental case, we always have to take this */
/* path. Thus we leave the counter alone. */
return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
} else {
GC_generic_malloc_many(BYTES_FROM_INDEX(index), GC_gcj_kind, my_fl);
if (*my_fl == 0) return GC_oom_fn(bytes);
return GC_local_gcj_malloc(bytes, ptr_to_struct_containing_descr);
}
}
}
/* Similar to GC_local_gcj_malloc, but the size is in words, and we don't */
/* adjust it. The size is assumed to be such that it can be */
/* allocated as a small object. */
void * GC_local_gcj_fast_malloc(size_t lw, void * ptr_to_struct_containing_descr)
{
ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
-> gcj_freelists + lw;
ptr_t my_entry = *my_fl;
GC_ASSERT(GC_gcj_malloc_initialized);
if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
GC_PTR result = (GC_PTR)my_entry;
GC_ASSERT(!GC_incremental);
/* We assert that any concurrent marker will stop us. */
/* Thus it is impossible for a mark procedure to see the */
/* allocation of the next object, but to see this object */
/* still containing a free list pointer. Otherwise the */
/* marker might find a random "mark descriptor". */
*(volatile ptr_t *)my_fl = obj_link(my_entry);
/* We must update the freelist before we store the pointer. */
/* Otherwise a GC at this point would see a corrupted */
/* free list. */
/* A memory barrier is probably never needed, since the */
/* action of stopping this thread will cause prior writes */
/* to complete. */
GC_ASSERT(((void * volatile *)result)[1] == 0);
*(void * volatile *)result = ptr_to_struct_containing_descr;
return result;
} else if ((word)my_entry - 1 < DIRECT_GRANULES) {
if (!GC_incremental) *my_fl = my_entry + lw + 1;
/* In the incremental case, we always have to take this */
/* path. Thus we leave the counter alone. */
return GC_gcj_fast_malloc(lw, ptr_to_struct_containing_descr);
} else {
GC_generic_malloc_many(BYTES_FROM_INDEX(lw), GC_gcj_kind, my_fl);
if (*my_fl == 0) return GC_oom_fn(BYTES_FROM_INDEX(lw));
return GC_local_gcj_fast_malloc(lw, ptr_to_struct_containing_descr);
}
}
#endif /* GC_GCJ_SUPPORT */
# else /* !THREAD_LOCAL_ALLOC && !DBG_HDRS_ALL */
# define GC_destroy_thread_local(t)
# endif /* !THREAD_LOCAL_ALLOC */
#if 0
/*
To make sure that we're using LinuxThreads and not some other thread
package, we generate a dummy reference to `pthread_kill_other_threads_np'
(was `__pthread_initial_thread_bos' but that disappeared),
which is a symbol defined in LinuxThreads, but (hopefully) not in other
thread packages.
We no longer do this, since this code is now portable enough that it might
actually work for something else.
*/
void (*dummy_var_to_force_linux_threads)() = pthread_kill_other_threads_np;
#endif /* 0 */
long GC_nprocs = 1; /* Number of processors. We may not have */
/* access to all of them, but this is as good */
/* a guess as any ... */
#ifdef PARALLEL_MARK
# ifndef MAX_MARKERS
# define MAX_MARKERS 16
# endif
static ptr_t marker_sp[MAX_MARKERS] = {0};
void * GC_mark_thread(void * id)
{
word my_mark_no = 0;
marker_sp[(word)id] = GC_approx_sp();
for (;; ++my_mark_no) {
/* GC_mark_no is passed only to allow GC_help_marker to terminate */
/* promptly. This is important if it were called from the signal */
/* handler or from the GC lock acquisition code. Under Linux, it's */
/* not safe to call it from a signal handler, since it uses mutexes */
/* and condition variables. Since it is called only here, the */
/* argument is unnecessary. */
if (my_mark_no < GC_mark_no || my_mark_no > GC_mark_no + 2) {
/* resynchronize if we get far off, e.g. because GC_mark_no */
/* wrapped. */
my_mark_no = GC_mark_no;
}
# ifdef DEBUG_THREADS
GC_printf1("Starting mark helper for mark number %ld\n", my_mark_no);
# endif
GC_help_marker(my_mark_no);
}
}
extern long GC_markers; /* Number of mark threads we would */
/* like to have. Includes the */
/* initiating thread. */
pthread_t GC_mark_threads[MAX_MARKERS];
#define PTHREAD_CREATE REAL_FUNC(pthread_create)
static void start_mark_threads()
{
unsigned i;
pthread_attr_t attr;
if (GC_markers > MAX_MARKERS) {
WARN("Limiting number of mark threads\n", 0);
GC_markers = MAX_MARKERS;
}
if (0 != pthread_attr_init(&attr)) ABORT("pthread_attr_init failed");
if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
ABORT("pthread_attr_setdetachstate failed");
# if defined(HPUX) || defined(GC_DGUX386_THREADS)
/* Default stack size is usually too small: fix it. */
/* Otherwise marker threads or GC may run out of */
/* space. */
# define MIN_STACK_SIZE (8*HBLKSIZE*sizeof(word))
{
size_t old_size;
int code;
if (pthread_attr_getstacksize(&attr, &old_size) != 0)
ABORT("pthread_attr_getstacksize failed\n");
if (old_size < MIN_STACK_SIZE) {
if (pthread_attr_setstacksize(&attr, MIN_STACK_SIZE) != 0)
ABORT("pthread_attr_setstacksize failed\n");
}
}
# endif /* HPUX || GC_DGUX386_THREADS */
# ifdef CONDPRINT
if (GC_print_stats) {
GC_printf1("Starting %ld marker threads\n", GC_markers - 1);
}
# endif
for (i = 0; i < GC_markers - 1; ++i) {
if (0 != PTHREAD_CREATE(GC_mark_threads + i, &attr,
GC_mark_thread, (void *)(word)i)) {
WARN("Marker thread creation failed, errno = %ld.\n", errno);
}
}
}
#else /* !PARALLEL_MARK */
static __inline__ void start_mark_threads()
{
}
#endif /* !PARALLEL_MARK */
GC_bool GC_thr_initialized = FALSE;
volatile GC_thread GC_threads[THREAD_TABLE_SZ];
/*
* gcc-3.3.6 miscompiles the &GC_thread_key+sizeof(&GC_thread_key) expression so
* put it into a separate function.
*/
# if defined(__GNUC__) && defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
static __attribute__((noinline)) unsigned char* get_gc_thread_key_addr GC_PROTO((void))
{
return (unsigned char*)&GC_thread_key;
}
void GC_push_thread_structures GC_PROTO((void))
{
GC_push_all((ptr_t)(GC_threads), (ptr_t)(GC_threads)+sizeof(GC_threads));
# if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
GC_push_all((ptr_t)get_gc_thread_key_addr(),
(ptr_t)(get_gc_thread_key_addr())+sizeof(&GC_thread_key));
# endif
}
#else
void GC_push_thread_structures GC_PROTO((void))
{
GC_push_all((ptr_t)(GC_threads), (ptr_t)(GC_threads)+sizeof(GC_threads));
# if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
GC_push_all((ptr_t)(&GC_thread_key),
(ptr_t)(&GC_thread_key)+sizeof(&GC_thread_key));
# endif
}
#endif
#ifdef THREAD_LOCAL_ALLOC
/* We must explicitly mark ptrfree and gcj free lists, since the free */
/* list links wouldn't otherwise be found. We also set them in the */
/* normal free lists, since that involves touching less memory than if */
/* we scanned them normally. */
void GC_mark_thread_local_free_lists(void)
{
int i, j;
GC_thread p;
ptr_t q;
for (i = 0; i < THREAD_TABLE_SZ; ++i) {
for (p = GC_threads[i]; 0 != p; p = p -> next) {
for (j = 1; j < NFREELISTS; ++j) {
q = p -> ptrfree_freelists[j];
if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
q = p -> normal_freelists[j];
if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
# ifdef GC_GCJ_SUPPORT
q = p -> gcj_freelists[j];
if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
# endif /* GC_GCJ_SUPPORT */
}
}
}
}
#endif /* THREAD_LOCAL_ALLOC */
static struct GC_Thread_Rep first_thread;
#ifdef NACL
extern int nacl_thread_parked[MAX_NACL_GC_THREADS];
extern int nacl_thread_used[MAX_NACL_GC_THREADS];
extern int nacl_thread_parking_inited;
extern int nacl_num_gc_threads;
extern pthread_mutex_t nacl_thread_alloc_lock;
extern __thread int nacl_thread_idx;
extern __thread GC_thread nacl_gc_thread_self;
extern void nacl_pre_syscall_hook();
extern void nacl_post_syscall_hook();
extern void nacl_register_gc_hooks(void (*pre)(), void (*post)());
void nacl_initialize_gc_thread()
{
int i;
nacl_register_gc_hooks(nacl_pre_syscall_hook, nacl_post_syscall_hook);
pthread_mutex_lock(&nacl_thread_alloc_lock);
if (!nacl_thread_parking_inited)
{
for (i = 0; i < MAX_NACL_GC_THREADS; i++) {
nacl_thread_used[i] = 0;
nacl_thread_parked[i] = 0;
}
nacl_thread_parking_inited = 1;
}
GC_ASSERT(nacl_num_gc_threads <= MAX_NACL_GC_THREADS);
for (i = 0; i < MAX_NACL_GC_THREADS; i++) {
if (nacl_thread_used[i] == 0) {
nacl_thread_used[i] = 1;
nacl_thread_idx = i;
nacl_num_gc_threads++;
break;
}
}
pthread_mutex_unlock(&nacl_thread_alloc_lock);
}
void nacl_shutdown_gc_thread()
{
pthread_mutex_lock(&nacl_thread_alloc_lock);
GC_ASSERT(nacl_thread_idx >= 0 && nacl_thread_idx < MAX_NACL_GC_THREADS);
GC_ASSERT(nacl_thread_used[nacl_thread_idx] != 0);
nacl_thread_used[nacl_thread_idx] = 0;
nacl_thread_idx = -1;
nacl_num_gc_threads--;
pthread_mutex_unlock(&nacl_thread_alloc_lock);
}
#endif /* NACL */
/* Add a thread to GC_threads. We assume it wasn't already there. */
/* Caller holds allocation lock. */
GC_thread GC_new_thread(pthread_t id)
{
int hv = ((unsigned long)id) % THREAD_TABLE_SZ;
GC_thread result;
static GC_bool first_thread_used = FALSE;
if (!first_thread_used) {
result = &first_thread;
first_thread_used = TRUE;
} else {
result = (struct GC_Thread_Rep *)
GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL);
}
if (result == 0) return(0);
result -> id = id;
#ifdef PLATFORM_ANDROID
result -> kernel_id = gettid();
#endif
result -> next = GC_threads[hv];
GC_threads[hv] = result;
#ifdef NACL
nacl_gc_thread_self = result;
nacl_initialize_gc_thread();
#endif
GC_ASSERT(result -> flags == 0 && result -> thread_blocked == 0);
return(result);
}
/* Delete a thread from GC_threads. We assume it is there. */
/* (The code intentionally traps if it wasn't.) */
/* Caller holds allocation lock. */
void GC_delete_thread(pthread_t id)
{
int hv = ((unsigned long)id) % THREAD_TABLE_SZ;
register GC_thread p = GC_threads[hv];
register GC_thread prev = 0;
#ifdef NACL
nacl_shutdown_gc_thread();
nacl_gc_thread_self = NULL;
#endif
while (!pthread_equal(p -> id, id)) {
prev = p;
p = p -> next;
}
if (prev == 0) {
GC_threads[hv] = p -> next;
} else {
prev -> next = p -> next;
}
#ifdef MONO_DEBUGGER_SUPPORTED
if (gc_thread_vtable && gc_thread_vtable->thread_exited)
gc_thread_vtable->thread_exited (id, &p->stop_info.stack_ptr);
#endif
#ifdef GC_DARWIN_THREADS
mach_port_deallocate(mach_task_self(), p->stop_info.mach_thread);
#endif
GC_INTERNAL_FREE(p);
}
/* 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 pthread id. */
/* This is OK, but we need a way to delete a specific one. */
void GC_delete_gc_thread(pthread_t id, GC_thread gc_id)
{
int hv = ((unsigned long)id) % THREAD_TABLE_SZ;
register GC_thread p = GC_threads[hv];
register GC_thread prev = 0;
while (p != gc_id) {
prev = p;
p = p -> next;
}
if (prev == 0) {
GC_threads[hv] = p -> next;
} else {
prev -> next = p -> next;
}
#ifdef GC_DARWIN_THREADS
mach_port_deallocate(mach_task_self(), p->stop_info.mach_thread);
#endif
GC_INTERNAL_FREE(p);
}
/* Return a GC_thread corresponding to a given pthread_t. */
/* Returns 0 if it's not there. */
/* Caller holds allocation lock or otherwise inhibits */
/* updates. */
/* If there is more than one thread with the given id we */
/* return the most recent one. */
GC_thread GC_lookup_thread(pthread_t id)
{
int hv = ((unsigned long)id) % THREAD_TABLE_SZ;
register GC_thread p = GC_threads[hv];
while (p != 0 && !pthread_equal(p -> id, id)) p = p -> next;
return(p);
}
int GC_thread_is_registered (void)
{
void *ptr;
LOCK();
ptr = (void *)GC_lookup_thread(pthread_self());
UNLOCK();
return ptr ? 1 : 0;
}
void GC_register_altstack (void *stack, int stack_size, void *altstack, int altstack_size)
{
GC_thread thread;
LOCK();
thread = (void *)GC_lookup_thread(pthread_self());
if (thread) {
thread->stack = stack;
thread->stack_size = stack_size;
thread->altstack = altstack;
thread->altstack_size = altstack_size;
} else {
/*
* This happens if we are called before GC_thr_init ().
*/
main_pthread_self = pthread_self ();
main_stack = stack;
main_stack_size = stack_size;
main_altstack = altstack;
main_altstack_size = altstack_size;
}
UNLOCK();
}
#ifdef HANDLE_FORK
/* Remove all entries from the GC_threads table, except the */
/* one for the current thread. We need to do this in the child */
/* process after a fork(), since only the current thread */
/* survives in the child. */
void GC_remove_all_threads_but_me(void)
{
pthread_t self = pthread_self();
int hv;
GC_thread p, next, me;
for (hv = 0; hv < THREAD_TABLE_SZ; ++hv) {
me = 0;
for (p = GC_threads[hv]; 0 != p; p = next) {
next = p -> next;
if (p -> id == self) {
me = p;
p -> next = 0;
} else {
# ifdef THREAD_LOCAL_ALLOC
if (!(p -> flags & FINISHED)) {
GC_destroy_thread_local(p);
}
# endif /* THREAD_LOCAL_ALLOC */
if (p != &first_thread) GC_INTERNAL_FREE(p);
}
}
GC_threads[hv] = me;
}
GC_INTERNAL_FREE(p);
}
#endif /* HANDLE_FORK */
#ifdef USE_PROC_FOR_LIBRARIES
int GC_segment_is_thread_stack(ptr_t lo, ptr_t hi)
{
int i;
GC_thread p;
# ifdef PARALLEL_MARK
for (i = 0; i < GC_markers; ++i) {
if (marker_sp[i] > lo & marker_sp[i] < hi) return 1;
}
# endif
for (i = 0; i < THREAD_TABLE_SZ; i++) {
for (p = GC_threads[i]; p != 0; p = p -> next) {
if (0 != p -> stack_end) {
# ifdef STACK_GROWS_UP
if (p -> stack_end >= lo && p -> stack_end < hi) return 1;
# else /* STACK_GROWS_DOWN */
if (p -> stack_end > lo && p -> stack_end <= hi) return 1;
# endif
}
}
}
return 0;
}
#endif /* USE_PROC_FOR_LIBRARIES */
#ifdef GC_LINUX_THREADS
/* Return the number of processors, or i<= 0 if it can't be determined. */
int GC_get_nprocs()
{
/* Should be "return sysconf(_SC_NPROCESSORS_ONLN);" but that */
/* appears to be buggy in many cases. */
/* We look for lines "cpu<n>" in /proc/stat. */
# define STAT_BUF_SIZE 4096
# define STAT_READ read
/* If read is wrapped, this may need to be redefined to call */
/* the real one. */
char stat_buf[STAT_BUF_SIZE];
int f;
word result = 1;
/* Some old kernels only have a single "cpu nnnn ..." */
/* entry in /proc/stat. We identify those as */
/* uniprocessors. */
size_t i, len = 0;
f = open("/proc/stat", O_RDONLY);
if (f < 0 || (len = STAT_READ(f, stat_buf, STAT_BUF_SIZE)) < 100) {
WARN("Couldn't read /proc/stat\n", 0);
return -1;
}
for (i = 0; i < len - 100; ++i) {
if (stat_buf[i] == '\n' && stat_buf[i+1] == 'c'
&& stat_buf[i+2] == 'p' && stat_buf[i+3] == 'u') {
int cpu_no = atoi(stat_buf + i + 4);
if (cpu_no >= result) result = cpu_no + 1;
}
}
close(f);
return result;
}
#endif /* GC_LINUX_THREADS */
/* We hold the GC lock. Wait until an in-progress GC has finished. */
/* Repeatedly RELEASES GC LOCK in order to wait. */
/* If wait_for_all is true, then we exit with the GC lock held and no */
/* collection in progress; otherwise we just wait for the current GC */
/* to finish. */
extern GC_bool GC_collection_in_progress();
void GC_wait_for_gc_completion(GC_bool wait_for_all)
{
if (GC_incremental && GC_collection_in_progress()) {
int 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. */
while (GC_incremental && GC_collection_in_progress()
&& (wait_for_all || old_gc_no == GC_gc_no)) {
ENTER_GC();
GC_in_thread_creation = TRUE;
GC_collect_a_little_inner(1);
GC_in_thread_creation = FALSE;
EXIT_GC();
UNLOCK();
sched_yield();
LOCK();
}