-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_context.h
More file actions
74 lines (60 loc) · 2.06 KB
/
thread_context.h
File metadata and controls
74 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include "memory_pool.h"
struct Mutex;
struct ThreadFunctionTable
{
bool (*LockMutex)( Mutex* /*mutex*/, DWORD /*msTimeout*/ );
void (*Sleep)( DWORD /*ms*/ );
};
struct ThreadContext
{
ThreadContext();
void popMemoryPool(MemoryPool* pool)
{
assert( memoryPoolStackIndex > 0 ); // Can't pop the default pool
if ( memoryPoolStackIndex > 0 )
{
assert( pool == memoryPoolStack[ memoryPoolStackIndex ] ); // Popping a pool that is not the current top of the stack
--memoryPoolStackIndex;
}
}
void pushMemoryPool( MemoryPool* pool )
{
assert( ( memoryPoolStackIndex + 1 ) < c_maxMemoryPoolStackSize ); // Out of space in the stack
if ( ( memoryPoolStackIndex + 1 ) < c_maxMemoryPoolStackSize )
{
memoryPoolStack[ ++memoryPoolStackIndex ] = pool;
}
}
static size_t const c_maxMemoryPoolStackSize = 16;
ThreadFunctionTable threadFunctions;
MemoryPool* memoryPoolStack[ c_maxMemoryPoolStackSize ];
size_t memoryPoolStackIndex;
};
__declspec(thread) ThreadContext g_threadContext;
// Helper functions to wrap usage of the thread context
__forceinline void ThreadPopMemoryPool( MemoryPool* pool )
{
g_threadContext.popMemoryPool( pool );
}
__forceinline void ThreadPushMemoryPool( MemoryPool* pool )
{
g_threadContext.pushMemoryPool( pool );
}
__forceinline void ThreadLockMutex( Mutex* mutex, unsigned long msTimeout )
{
g_threadContext.threadFunctions.LockMutex( mutex, msTimeout );
}
__forceinline void ThreadSleep( unsigned long msTimeout )
{
g_threadContext.threadFunctions.Sleep( msTimeout );
}
// Overload defaule new/delete to use the thread context
void* operator new( size_t size )
{
return g_threadContext.memoryPoolStack[ g_threadContext.memoryPoolStackIndex ]->allocate( size );
}
void operator delete( void* allocation )
{
return g_threadContext.memoryPoolStack[ g_threadContext.memoryPoolStackIndex ]->free( allocation );
}