Anonymous user
C++: Difference between revisions
Jump to navigation
Jump to search
Added "Local Static Variables"
[unchecked revision] | [unchecked revision] |
m (Added link to cross compile libsupcxx) |
(Added "Local Static Variables") |
||
Line 219:
__declspec(allocate(".CRT$XIB")) static _PIFV pinit = onexitinit;
#pragma data_seg()
</pre>
===Local Static Variables===
When you declare local static variable, at least GCC compiler, puts a guard around variable's constructor call. This ensures that only one thread can call constructor at the same time to initialize it.
====GCC (Don't know from what version. Mine is 4.1.1)====
Note, that these are only stubs to get the code compiled, and you should implement them yourself. Simply add a mutex like guard with test and set primitive.
<pre>
namespace __cxxabiv1
{
/* guard variables */
/* The ABI requires a 64-bit type. */
__extension__ typedef int __guard __attribute__((mode (__DI__)));
extern "C" int __cxa_guard_acquire (__guard *);
extern "C" void __cxa_guard_release (__guard *);
extern "C" void __cxa_guard_abort (__guard *);
extern "C" int __cxa_guard_acquire (__guard *g)
{
return !*(char *)(g);
}
extern "C" void __cxa_guard_release (__guard *g)
{
*(char *)g = 1;
}
extern "C" void __cxa_guard_abort (__guard *)
{
}
}
</pre>
Actual code, emited by GCC, to call local static variable's constructor looks something like this:
<pre>
static <type> guard;
if (!guard.first_byte) {
if (__cxa_guard_acquire (&guard)) {
bool flag = false;
try {
// Do initialization.
flag = true;
__cxa_guard_release (&guard);
// Register variable for destruction at end of program.
} catch {
if (!flag) {
__cxa_guard_abort (&guard);
}
}
}
}
</pre>
===new and delete===
|