| 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.exception : onOutOfMemoryError; |
| 17 |
import core.stdc.stdlib : free, malloc; |
| 18 |
static import 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 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialization hook, called FROM each thread. No assumptions about |
| 30 |
* module initialization state should be made. |
| 31 |
*/ |
| 32 |
void* init() nothrow @nogc |
| 33 |
{ |
| 34 |
auto data = cast(Data*).malloc(Data.sizeof); |
| 35 |
if ( data is null ) onOutOfMemoryError(); |
| 36 |
*data = Data.init; |
| 37 |
|
| 38 |
// do module specific initialization |
| 39 |
data.tlsRanges = rt.sections.initTLSRanges(); |
| 40 |
return data; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Finalization hook, called FOR each thread. No assumptions about |
| 45 |
* module initialization state should be made. |
| 46 |
*/ |
| 47 |
void destroy(void* data) nothrow @nogc |
| 48 |
{ |
| 49 |
// do module specific finalization |
| 50 |
rt.sections.finiTLSRanges((cast(Data*)data).tlsRanges); |
| 51 |
|
| 52 |
.free(data); |
| 53 |
} |
| 54 |
|
| 55 |
alias void delegate(void* pstart, void* pend) nothrow ScanDg; |
| 56 |
|
| 57 |
/** |
| 58 |
* GC scan hook, called FOR each thread. Can be used to scan |
| 59 |
* additional thread local memory. |
| 60 |
*/ |
| 61 |
void scan(void* data, scope ScanDg dg) nothrow |
| 62 |
{ |
| 63 |
// do module specific marking |
| 64 |
rt.sections.scanTLSRanges((cast(Data*)data).tlsRanges, dg); |
| 65 |
} |