aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PS3/PS3Extras/ShutdownManager.cpp
blob: a04e45d9f53e56679a458e8e917e55c439a349f9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "stdafx.h"
#include "ShutdownManager.h"
#include "..\..\Common\Leaderboards\LeaderboardManager.h"
#include "..\..\MinecraftServer.h"
#ifdef __PS3__
#include "C4JSpursJob.h"


bool ShutdownManager::s_threadShouldRun[ShutdownManager::eThreadIdCount];
int ShutdownManager::s_threadRunning[ShutdownManager::eThreadIdCount];
CRITICAL_SECTION ShutdownManager::s_threadRunningCS;
C4JThread::EventArray *ShutdownManager::s_eventArray[eThreadIdCount];
#endif

// Initialises the shutdown manager - this needs to be called as soon as the game is started so it can respond as quickly as possible to shut down requests
void ShutdownManager::Initialise()
{
#ifdef __PS3__
	cellSysutilRegisterCallback( 1, SysUtilCallback, nullptr );
	for( int i = 0; i < eThreadIdCount; i++ )
	{
		s_threadShouldRun[i] = true;
		s_threadRunning[i] = 0;
		s_eventArray[i] = nullptr;
	}
	// Special case for storage manager, which we will manually set now to be considered as running - this will be unset by StorageManager.ExitRequest if required
	s_threadRunning[eStorageManagerThreads] = true;
	InitializeCriticalSection(&s_threadRunningCS);
#endif
}

// Called in response to a system request to exit the game. This just requests that the main thread should stop, and then the main thread is responsible for calling MainThreadHandleShutdown which
// starts the rest of the shut down process, then waits until it is complete.
void ShutdownManager::StartShutdown()
{
#ifdef __PS3__
	s_threadShouldRun[ eMainThread ] = false;
#endif
}

// This should be called from the main thread after it has been requested to shut down (ShouldRun for main thread returns false), and before it returns control to the kernel. This is responsible for
// signalling to all other threads to stop, and wait until their completion before returning.
void ShutdownManager::MainThreadHandleShutdown()
{
#ifdef __PS3__
	// Set flags for each thread which will be reset when they are complete
	s_threadRunning[ eMainThread ] = false;

	// Second wave of things we would like to shut down (after main)
	LeaderboardManager::Instance()->CancelOperation();
	RequestThreadToStop( eLeaderboardThread );
	RequestThreadToStop( eCommerceThread );
	RequestThreadToStop( ePostProcessThread );
	RequestThreadToStop( eRunUpdateThread );
	RequestThreadToStop( eRenderChunkUpdateThread );
	RequestThreadToStop( eConnectionReadThreads );
	RequestThreadToStop( eConnectionWriteThreads );
	RequestThreadToStop( eEventQueueThreads );
	app.DebugPrintf("Shutdown manager: waiting on first batch of threads requested to terminate...\n");
	WaitForSignalledToComplete();
	app.DebugPrintf("Shutdown manager: terminated.\n");

	// Now shut down the server thread
	MinecraftServer::HaltServer();
	RequestThreadToStop( eServerThread );
	app.DebugPrintf("Shutdown manager: waiting on server to terminate...\n");
	WaitForSignalledToComplete();
	app.DebugPrintf("Shutdown manager: terminated.\n");

	//And shut down the storage manager
	RequestThreadToStop( eStorageManagerThreads );
	StorageManager.ExitRequest(&StorageManagerCompleteFn);
	app.DebugPrintf("Shutdown manager: waiting on storage manager to terminate...\n");
	WaitForSignalledToComplete();
	app.DebugPrintf("Shutdown manager: terminated.\n");

	// Audio system shutdown
	app.DebugPrintf("Shutdown manager: Audio shutdown.\n");
	AIL_shutdown();

	// Trophy system shutdown
	app.DebugPrintf("Shutdown manager: Trophy system shutdown.\n");
	ProfileManager.Terminate();

	// Network manager shutdown
	app.DebugPrintf("Shutdown manager: Network manager shutdown.\n");
	g_NetworkManager.Terminate();

	// Finally shut down the spurs job queue - leaving until last so there should be nothing else dependent on this still running
	app.DebugPrintf("Shutdown manager: SPURS shutdown.\n");
	C4JSpursJobQueue::getMainJobQueue().shutdown();
	app.DebugPrintf("Shutdown manager: Complete.\n");
#endif
}

void ShutdownManager::HasStarted(ShutdownManager::EThreadId threadId)
{
#ifdef __PS3__
	EnterCriticalSection(&s_threadRunningCS);
	s_threadRunning[threadId]++;
	LeaveCriticalSection(&s_threadRunningCS);
#endif
}

void ShutdownManager::HasStarted(ShutdownManager::EThreadId threadId, C4JThread::EventArray *eventArray)
{
#ifdef __PS3__
	EnterCriticalSection(&s_threadRunningCS);
	s_threadRunning[threadId]++;
	LeaveCriticalSection(&s_threadRunningCS);
	s_eventArray[threadId] = eventArray;
#endif
}

bool ShutdownManager::ShouldRun(ShutdownManager::EThreadId threadId)
{
#ifdef __PS3__
	return s_threadShouldRun[threadId];
#else
	return true;
#endif
}

void ShutdownManager::HasFinished(ShutdownManager::EThreadId threadId)
{
#ifdef __PS3__
	EnterCriticalSection(&s_threadRunningCS);
	s_threadRunning[threadId]--;
	LeaveCriticalSection(&s_threadRunningCS);
#endif
}

#ifdef __PS3__
void ShutdownManager::SysUtilCallback(uint64_t status, uint64_t param, void *userdata)
{
	Minecraft *minecraft = Minecraft::GetInstance();
	switch(status)
	{
	case CELL_SYSUTIL_REQUEST_EXITGAME:
		app.DebugPrintf("CELL_SYSUTIL_REQUEST_EXITGAME\n");
		StartShutdown();
		break;
	case CELL_SYSUTIL_SYSTEM_MENU_OPEN:
		// Tell the game UI to stop processing
		StorageManager.SetSystemUIDisplaying(true);
		break;
	case CELL_SYSUTIL_DRAWING_END:
		StorageManager.SetSystemUIDisplaying(false);
		break;
	case CELL_SYSUTIL_DRAWING_BEGIN:
	case CELL_SYSUTIL_SYSTEM_MENU_CLOSE:
		break;
	case CELL_SYSUTIL_BGMPLAYBACK_PLAY:
		if( minecraft )
		{
			minecraft->soundEngine->updateSystemMusicPlaying(true);
		}
		app.DebugPrintf("BGM playing\n");
		break;
	case CELL_SYSUTIL_BGMPLAYBACK_STOP:
		if( minecraft )
		{
			minecraft->soundEngine->updateSystemMusicPlaying(false);
		}
		app.DebugPrintf("BGM stopped\n");
		break;
	}
}
#endif

#ifdef __PS3__
void ShutdownManager::WaitForSignalledToComplete()
{
	bool allComplete;
	do
	{
		cellSysutilCheckCallback();
		Sleep(10);
		allComplete = true;
		for( int i = 0; i < eThreadIdCount; i++ )
		{
			if( !s_threadShouldRun[i] )
			{
				if( s_threadRunning[i] != 0 ) allComplete = false;
			}
		}
	} while( !allComplete);
	
}

void ShutdownManager::RequestThreadToStop(int i)
{
	s_threadShouldRun[i] = false;
	if( s_eventArray[i] )
	{
		s_eventArray[i]->Cancel();
	}
}

void ShutdownManager::StorageManagerCompleteFn()
{
	HasFinished(eStorageManagerThreads);
}
#endif