Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions aephysics/native/aephysics_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ static inline void aephysics_prefetch(const void *p)
#endif
}

// Which worker the calling thread is (aephysics_worker_index in
// aephysics_native.c), without a call where that can be had. On 64-bit
// Windows the worker's slot is a native TLS index, and TlsGetValue for an
// index under 64 reads the thread's TEB.TlsSlots[index]: read here in place,
// one load from gs (the offset is checked against winternl.h in
// aephysics_native.c). Every per-worker scratch block is chosen through
// this, tens of millions of times a run of the convex pile, where the call
// into kernel32 was 2% of the step. A slot not yet made, an index past 64
// or a thread that has not claimed one goes through the function.
int aephysics_worker_index(void);
#if defined(_WIN32) && defined(__x86_64__)
extern unsigned long aephysics_tls_slot;
static inline int aephysics_worker_index_inline(void)
{
unsigned long slot = aephysics_tls_slot;
if (slot < 64) {
unsigned long long stored;
__asm__ __volatile__("movq %%gs:0x1480(,%1,8), %0" : "=r"(stored) : "r"((unsigned long long)slot));
if (stored != 0) return (int)(stored - 1);
}
return aephysics_worker_index();
}
#else
static inline int aephysics_worker_index_inline(void) { return aephysics_worker_index(); }
#endif

// A spin's pause: the processor's hint that the thread is waiting.
static inline void aephysics_pause(void)
{
Expand Down
12 changes: 11 additions & 1 deletion aephysics/native/aephysics_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ void aephysics_release_slots(int start, int count)
// exits. Elsewhere the compiler's TLS is native already, and a pthread
// key's destructor gives the slot back.
#ifdef _WIN32
static DWORD g_worker_slot = TLS_OUT_OF_INDEXES;
// The slot's index, read by aephysics_worker_index_inline
// (aephysics_inline.h) as well, which takes the thread's value straight
// from its TEB.TlsSlots, the array TlsGetValue reads for an index under 64:
// the offset it uses is checked here against winternl.h.
#include <stddef.h>
#include <winternl.h>
#if defined(__x86_64__)
_Static_assert(offsetof(TEB, TlsSlots) == 0x1480, "TEB.TlsSlots moved: aephysics_worker_index_inline reads it at gs:0x1480");
#endif
DWORD aephysics_tls_slot = TLS_OUT_OF_INDEXES;
#define g_worker_slot aephysics_tls_slot
static DWORD g_owned_slot = FLS_OUT_OF_INDEXES;

static void WINAPI release_owned_slot(void *value)
Expand Down
5 changes: 3 additions & 2 deletions aephysics/native/module.ae
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports (
const MAX_WORKERS = 64

extern aephysics_worker_index() -> int
extern aephysics_worker_index_inline() -> int @c_import
extern aephysics_set_worker_index(index: int)
extern aephysics_claim_slots(count: int) -> int
extern aephysics_release_slots(start: int, count: int)
Expand Down Expand Up @@ -77,7 +78,7 @@ name_text(name: ptr) -> string { return aephysics_name_text(name) }
// world's step, else the slot the thread claimed the first time it asked
// (given back when it exits). Every module's scratch is a block per slot
// chosen by this, so threads querying a world at once never share one.
worker_index() -> int { return aephysics_worker_index() }
worker_index() -> int { return aephysics_worker_index_inline() }
set_worker_index(index: int) { aephysics_set_worker_index(index) }

// A run of `count` worker slots claimed (the first, or -1 when none is
Expand Down Expand Up @@ -120,4 +121,4 @@ semaphore_signal(s: ptr, count: int) { aephysics_semaphore_signal(s, count) }
// MAX_WORKERS blocks of `bytes`: what the reference keeps on the stack
// of each thread, kept here as one block per worker since Aether has
// neither stack arrays nor thread-local variables.
scratch(blocks: @into ptr, bytes: int) -> ptr { return blocks + aephysics_worker_index() * bytes }
scratch(blocks: @into ptr, bytes: int) -> ptr { return blocks + aephysics_worker_index_inline() * bytes }
Loading