aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/MultiThreadedHook/MultiThreadedHookSample.cpp
blob: 4c872680a9397b7a895d22c49e95e0018b326cfb (plain)
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
75
76
77
78
79
80
// =================================================================================================================================
// This sample is more of a show-(and test) case for HeapInspector's. It demonstrates:
// 1) That HeapInspector is multithread safe.
// 2) HeapInspector's ability to deal with allocations prior to Initialise (although those allocations will not be tracked).
// 3) HeapInspector's ability to deal with API calls during static initialisation phase.
//
// In this sample, multiple threads are started that perform allocations for a set period of time. The
// application will wait for those threads to finish. After the time is passed and the application calls Shutdown,
// the client will disconnect.
//
// To switch between launching the threads during the static initalisation phase and launching the treads
// in main, flip the INIT_IN_STATIC_PHASE define.
// 
// =================================================================================================================================

#include "IThread.h"
#include <stdlib.h>

void Wait(int a_MilliSeconds);

#define INIT_IN_STATIC_PHASE	0
const int g_NumThreads = 4;

class MultiThreadedAllocator
{
public:
	static void WorkerThread()
	{
		for (int i = 0; i != 1000; ++i)
		{
			void* mem1 = malloc(10);
			Wait(10);
			free(mem1);
			Wait(10);
		}
	}

	MultiThreadedAllocator()
	{
		for (int i = 0; i != g_NumThreads; ++i)
		{
			m_Threads[i] = CreateThread();
			m_Threads[i]->Fork(WorkerThread);
		}
	}

	~MultiThreadedAllocator()
	{
		WaitForThreads();
		for (int i = 0; i != g_NumThreads; ++i)
		{
			DestroyThread(m_Threads[i]);
		}
	}

private:
	void WaitForThreads()
	{
		for (int i = 0; i != g_NumThreads; ++i)
		{
			m_Threads[i]->Join();
		}
	}

private:
	IThread* m_Threads[g_NumThreads];
};

#if INIT_IN_STATIC_PHASE
static MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator();
#endif

void RunHeapInspectorServer()
{
#if !INIT_IN_STATIC_PHASE
	MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator();
#endif

	delete g_Allocator;
}