1 /**
2  *
3  * Copyright: Copyright Digital Mars 2011 - 2012.
4  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
5  * Authors:   Martin Nowak
6  * Source: $(DRUNTIMESRC rt/tlsgc.d)
7  */
8 
9 /*          Copyright Digital Mars 2011.
10  * Distributed under the Boost Software License, Version 1.0.
11  *    (See accompanying file LICENSE or copy at
12  *          http://www.boost.org/LICENSE_1_0.txt)
13  */
14 module rt.tlsgc;
15 
16 import core.stdc.stdlib;
17 
18 static import rt.lifetime, rt.sections;
19 
20 /**
21  * Per thread record to store thread associated data for garbage collection.
22  */
23 struct Data
24 {
25     typeof(rt.sections.initTLSRanges()) tlsRanges;
26     rt.lifetime.BlkInfo** blockInfoCache;
27 }
28 
29 /**
30  * Initialization hook, called FROM each thread. No assumptions about
31  * module initialization state should be made.
32  */
33 void* init() nothrow @nogc
34 {
35     auto data = cast(Data*).malloc(Data.sizeof);
36     import core.exception;
37     if ( data is null ) core.exception.onOutOfMemoryError();
38     *data = Data.init;
39 
40     // do module specific initialization
41     data.tlsRanges = rt.sections.initTLSRanges();
42     data.blockInfoCache = &rt.lifetime.__blkcache_storage;
43 
44     return data;
45 }
46 
47 /**
48  * Finalization hook, called FOR each thread. No assumptions about
49  * module initialization state should be made.
50  */
51 void destroy(void* data) nothrow @nogc
52 {
53     // do module specific finalization
54     rt.sections.finiTLSRanges((cast(Data*)data).tlsRanges);
55 
56     .free(data);
57 }
58 
59 alias void delegate(void* pstart, void* pend) nothrow ScanDg;
60 
61 /**
62  * GC scan hook, called FOR each thread. Can be used to scan
63  * additional thread local memory.
64  */
65 void scan(void* data, scope ScanDg dg) nothrow
66 {
67     // do module specific marking
68     rt.sections.scanTLSRanges((cast(Data*)data).tlsRanges, dg);
69 }
70 
71 alias int delegate(void* addr) nothrow IsMarkedDg;
72 
73 /**
74  * GC sweep hook, called FOR each thread. Can be used to free
75  * additional thread local memory or associated data structures. Note
76  * that only memory allocated from the GC can have marks.
77  */
78 void processGCMarks(void* data, scope IsMarkedDg dg) nothrow
79 {
80     // do module specific sweeping
81     rt.lifetime.processGCMarks(*(cast(Data*)data).blockInfoCache, dg);
82 }