Re: Anti-Jammer-Thread
Verfasst: 19.11.2013, 08:30
Wow, cool. Ist vorgemerkt.
Die deutsche Spieleentwickler-Community (seit 1999).
https://zfx.info/
Code: Alles auswählen
#pragma warning(disable : 4350 4514 4710 4711 4820)
#include <iostream>
int MyFunction()
{
// With VC 2013 RTM: warning C4640: 'i' : construction of local static object is not thread-safe
static int i = rand();
i++;
return i;
}
int main(int /*argc*/, char * /*argv*/[])
{
std::cout << MyFunction() << std::endl;
std::cout << MyFunction() << std::endl;
std::cout << MyFunction() << std::endl;
return 0;
}
Code: Alles auswählen
int MyFunction()
{
push rdi
sub rsp,20h
mov rdi,rsp
mov ecx,8
mov eax,0CCCCCCCCh
rep stos dword ptr [rdi]
// With VC 2013 RTM: warning C4640: 'i' : construction of local static object is not thread-safe
static int i = rand();
mov eax,104h
mov eax,eax
mov ecx,dword ptr [_tls_index (07FF797FA0444h)]
mov rdx,qword ptr gs:[58h]
mov rcx,qword ptr [rdx+rcx*8]
mov eax,dword ptr [rax+rcx]
cmp dword ptr [i+4h (07FF797FA03B8h)],eax
jle MyFunction+67h (07FF797F92477h)
lea rcx,[i+4h (07FF797FA03B8h)]
call _Init_thread_header (07FF797F94BB0h)
cmp dword ptr [i+4h (07FF797FA03B8h)],0FFFFFFFFh
jne MyFunction+67h (07FF797F92477h)
call qword ptr [__imp_rand (07FF797FA2378h)]
mov dword ptr [i (07FF797FA03B4h)],eax
lea rcx,[i+4h (07FF797FA03B8h)]
call _Init_thread_footer (07FF797F94B40h)
i++;
mov eax,dword ptr [i (07FF797FA03B4h)]
inc eax
mov dword ptr [i (07FF797FA03B4h)],eax
return i;
mov eax,dword ptr [i (07FF797FA03B4h)]
}
add rsp,20h
pop rdi
ret
Code: Alles auswählen
// Control access to the initialization expression. Only one thread may leave
// this function before the variable has completed initialization, this thread
// will perform initialization. All other threads are blocked until the
// initialization completes or fails due to an exception.
void __cdecl _Init_thread_header(int *pOnce)
{
_Init_thread_cs.lock();
if (*pOnce == Uninitialized)
{
*pOnce = BeingInitialized;
}
else
{
while (*pOnce == BeingInitialized)
{
_Init_thread_cv.wait(_Init_thread_cs);
if (*pOnce == Uninitialized)
{
*pOnce = BeingInitialized;
_Init_thread_cs.unlock();
return;
}
}
_Init_thread_epoch = _Init_global_epoch;
}
_Init_thread_cs.unlock();
}
Code: Alles auswählen
// Called by the thread that completes initialization of a variable.
// Increment the global and per thread counters, mark the variable as
// initialized, and release waiting threads.
void __cdecl _Init_thread_footer(int *pOnce)
{
_Init_thread_cs.lock();
++_Init_global_epoch;
*pOnce = _Init_global_epoch;
_Init_thread_cv.notify_all();
_Init_thread_epoch = _Init_global_epoch;
_Init_thread_cs.unlock();
}
Okay, das hört sich ja schon mal sehr, sehr gut an. Dann schauen wir uns mal diesen „fast-path“ mal an. Visual C++ 2013 RTM (alle mit x gekennzeichneten Zeilen werden ausgeführt):http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2382.html#AppendixHeader hat geschrieben:The fast-path for the inline function in the header has the following code as generated by gcc 3.2.2 on an x86:
mov %gs:(%esi),%eax # access to thread-local storage
cmp %eax, pthread_once_t_location # accesses the pthread_once_t variable
jg slow_path # decides whether to call the function
This code is one more instruction than a test that is not thread safe. This code touches two cache lines, while an unsafe version would touch only one. However, the additional cache line is thread-specific, and not especially likely to cause misses. As one might expect, repeated calls to the inline version achieve around 1 billion fast-path calls per second on a 2.8GHz processor with a hot cache.
Code: Alles auswählen
std::cout << MyFunction() << std::endl;
x mov eax,dword ptr [_fmode+8h (07FF74C5456E8h)]
x test al,1
x jne main+67h (07FF74C5412D7h)
or eax,1
mov dword ptr [_fmode+8h (07FF74C5456E8h)],eax
call qword ptr [__imp_rand (07FF74C543190h)]
jmp main+6Dh (07FF74C5412DDh)
x mov eax,dword ptr [_fmode+4h (07FF74C5456E4h)]
x mov rcx,qword ptr [__imp_std::cout (07FF74C543080h)]
x inc eax
x mov edx,eax
x mov dword ptr [_fmode+4h (07FF74C5456E4h)],eax
x call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF74C543048h)]
x lea rdx,[std::endl<char,std::char_traits<char> > (07FF74C5417A0h)]
x mov rcx,rax
x call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF74C543090h)]
Code: Alles auswählen
std::cout << MyFunction() << std::endl;
x mov eax,dword ptr [rbx]
x cmp dword ptr [piecewise_construct+3h (07FF6412E577Ch)],eax
x jle main+0B0h (07FF6412E1360h)
lea rcx,[piecewise_construct+3h (07FF6412E577Ch)]
call _Init_thread_header (07FF6412E18F0h)
cmp dword ptr [piecewise_construct+3h (07FF6412E577Ch)],0FFFFFFFFh
jne main+0B0h (07FF6412E1360h)
call qword ptr [__imp_rand (07FF6412E31D0h)]
lea rcx,[piecewise_construct+3h (07FF6412E577Ch)]
mov dword ptr [_fmode+4h (07FF6412E5774h)],eax
call _Init_thread_footer (07FF6412E1880h)
x mov edx,dword ptr [_fmode+4h (07FF6412E5774h)]
x mov rcx,qword ptr [__imp_std::cout (07FF6412E3080h)]
x inc edx
x mov dword ptr [_fmode+4h (07FF6412E5774h)],edx
x call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF6412E3048h)]
x lea rdx,[std::endl<char,std::char_traits<char> > (07FF6412E1840h)]
x mov rcx,rax
x call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF6412E3090h)]
Braucht Betriebssystemsupport? Kopiert Stackabschnitte auf den Heap? ... Angst?Limitations and Breaking Changes in Visual C++ Compiler November 2013 CTP hat geschrieben:Await
This feature utilizes mechanisms provided by the underlying operating system. It can only be used on machines running Windows 8.1 or later.
Und, bist du ein braver Junge und hörst darauf, was Mutti sagt?Krishty hat geschrieben:Mama Natur will, dass ich wieder mit Sky Rendering anfange
James Mickens hat geschrieben:A systems programmer has seen the terrors of the world and understood the intrinsic horror of existence.
The systems programmer has written drivers for buggy devices whose firmware was implemented by a drunken child or a sober goldfish. The systems programmer has traced a network problem acress eight machines, three time zones, and a brief diversion into Amish country, where the problem was transmitted in the front left hoof of a mule named Deliverance.
The systems programmer has read the kernel source, to better understand the deep ways of the universe, and the systems programmer has seen the comment in the scheduler that says "DOES THIS WORK LOL", and the systems programmer has wept instead of LOLed, and the systems programmer has submitted a kernel patch to restore balance to The Force and fix the priority inversion that was causing MySQL to hang.
A systems programmer will know what to do when society breaks down, because the systems programmer already lives in a world without law.
When you debug a distributed system or an OS kernel, you do it Texas-style. You gather some mean, stoic people, people who have seen things die, and you get some primitive tools, like a compass and a rucksack and a stick that’s pointed on one end, and you walk into the wilderness and you look for trouble, possibly while chewing tobacco.
(via The Old New Thing — Essays from the funniest man in Microsoft Research)Indeed, the common discovery mode for an impossibly large buffer error is that your program seems to be working fine, and then it tries to display a string that should say “Hello world,” but instead it prints “#a[5]:3!” or another syntactically correct Perl script, and you’re like WHAT THE HOW THE
Code: Alles auswählen
Main()
{
var evaluator = new
{
evaluate = (Func<string, double>)((expression) =>
{
var csc = new Microsoft.CSharp.CSharpCodeProvider();
var args = new System.CodeDom.Compiler.CompilerParameters(new string[]
{
"mscorlib.dll"
});
args.GenerateExecutable = false;
args.GenerateInMemory = true;
args.OutputAssembly = "Evaluator";
// Hier wird der Quellcode generiert. Wenn irgendwelche Methoden/Funktionen
// im Parser gewünscht sind, einfach in die Klasse einfügen und static markieren.
// Die Methoden sind dann im Parser aufrufbar.
// {{ wird zu { da string.Format {i} durch den i-ten Parameter ersetzt.
// Code kann durch {i} beliebig erweitert werden.
string src = string.Format(
@"using System;
public class Evaluator
{{
public static double Evaluate(double a, double b)
{{
return {0};
}}
}}", expression); // Ersetzt {0} durch expression.
var results = csc.CompileAssemblyFromSource(args, src);
if (results.Errors.Count > 0)
{
#if DEBUG
foreach (var error in results.Errors)
{
System.Diagnostics.Debug.WriteLine(error);
}
#endif
throw new ArgumentException("The expression is invalid.", "expression");
}
var asm = results.CompiledAssembly;
var type = asm.GetType("Evaluator");
var method = type.GetMethod("Evaluate", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
// In dem Array hier die Parameter von Evaluate angeben
return (double)method.Invoke(null, new object[] { 10.0, 25.0 });
})
};
Console.WriteLine("a = 10, b = 15");
do
{
Console.Write("=");
string expr = Console.ReadLine();
if (expr == "exit")
return;
Console.WriteLine("=> {0}", evaluator.evaluate(expr));
} while (true);
}
Und schnellen die Verkaufszahlen schon in die Höhe?Schrompf hat geschrieben:Splatter ist bei Steam angenommen worden.