C client: Fix crashing when turning off music then back on again on linux

This commit is contained in:
UnknownShadow200 2018-09-11 07:55:05 +10:00
parent d8ed9b77a2
commit cd1961f588
10 changed files with 26 additions and 30 deletions

View File

@ -561,7 +561,7 @@ namespace ClassicalSharp.GraphicsAPI {
internal override void UpdateApiInfo() { internal override void UpdateApiInfo() {
float mem = VideoMemoryMB; float mem = VideoMemoryMB;
ApiInfo[3] = "Video memory: " + totalMem + " MB total, " + mem + " now"; ApiInfo[3] = "Video memory: " + totalMem + " MB total, " + mem + " free";
} }
public override void TakeScreenshot(Stream output, int width, int height) { public override void TakeScreenshot(Stream output, int width, int height) {

View File

@ -570,7 +570,7 @@ namespace ClassicalSharp.GraphicsAPI {
if (totalKb <= 0 || curKb <= 0) return; if (totalKb <= 0 || curKb <= 0) return;
float total = totalKb / 1024f, cur = curKb / 1024f; float total = totalKb / 1024f, cur = curKb / 1024f;
ApiInfo[4] = "Video memory: " + total + " MB total, " + cur + " now"; ApiInfo[4] = "Video memory: " + total + " MB total, " + cur + " free";
} }
public override bool WarnIfNecessary(Chat chat) { public override bool WarnIfNecessary(Chat chat) {

View File

@ -286,7 +286,7 @@ static void AsyncDownloader_Init(void) {
async_pendingMutex = Mutex_Create(); async_pendingMutex = Mutex_Create();
async_processedMutex = Mutex_Create(); async_processedMutex = Mutex_Create();
async_curRequestMutex = Mutex_Create(); async_curRequestMutex = Mutex_Create();
async_workerThread = Thread_Start(AsyncDownloader_WorkerFunc); async_workerThread = Thread_Start(AsyncDownloader_WorkerFunc, false);
} }
static void AsyncDownloader_Reset(void) { static void AsyncDownloader_Reset(void) {
@ -302,7 +302,6 @@ static void AsyncDownloader_Free(void) {
async_terminate = true; async_terminate = true;
AsyncDownloader_Reset(); AsyncDownloader_Reset();
Thread_Join(async_workerThread); Thread_Join(async_workerThread);
Thread_FreeHandle(async_workerThread);
AsyncRequestList_Free(&async_pending); AsyncRequestList_Free(&async_pending);
AsyncRequestList_Free(&async_processed); AsyncRequestList_Free(&async_processed);

View File

@ -438,7 +438,7 @@ static void Music_Init(void) {
if (music_thread) return; if (music_thread) return;
music_pendingStop = false; music_pendingStop = false;
Audio_Init(&music_out, AUDIO_MAX_CHUNKS); Audio_Init(&music_out, AUDIO_MAX_CHUNKS);
music_thread = Thread_Start(Music_RunLoop); music_thread = Thread_Start(Music_RunLoop, false);
} }
static void Music_Free(void) { static void Music_Free(void) {
@ -447,7 +447,6 @@ static void Music_Free(void) {
if (music_out == -1) return; if (music_out == -1) return;
Thread_Join(music_thread); Thread_Join(music_thread);
Thread_FreeHandle(music_thread);
Audio_Free(music_out); Audio_Free(music_out);
music_out = -1; music_out = -1;
music_thread = NULL; music_thread = NULL;

View File

@ -60,7 +60,7 @@ struct FontDesc { void* Handle; UInt16 Size, Style; };
#define UInt32_MaxValue ((UInt32)4294967295UL) #define UInt32_MaxValue ((UInt32)4294967295UL)
#define CC_BUILD_GL11 false #define CC_BUILD_GL11 false
#define CC_BUILD_D3D9 false #define CC_BUILD_D3D9 true
#define CC_BUILD_WIN true #define CC_BUILD_WIN true
#define CC_BUILD_OSX false #define CC_BUILD_OSX false

View File

@ -668,7 +668,7 @@ void Gfx_MakeApiInfo(void) {
void Gfx_UpdateApiInfo(void) { void Gfx_UpdateApiInfo(void) {
Real32 mem = IDirect3DDevice9_GetAvailableTextureMem(device) / (1024.0f * 1024.0f); Real32 mem = IDirect3DDevice9_GetAvailableTextureMem(device) / (1024.0f * 1024.0f);
Gfx_ApiInfo[3].length = 0; Gfx_ApiInfo[3].length = 0;
String_Format2(&Gfx_ApiInfo[3], "Video memory: %f2 MB total, %f2 now", &d3d9_totalMem, &mem); String_Format2(&Gfx_ApiInfo[3], "Video memory: %f2 MB total, %f2 free", &d3d9_totalMem, &mem);
} }
void Gfx_OnWindowResize(void) { void Gfx_OnWindowResize(void) {

View File

@ -569,7 +569,7 @@ void Gfx_UpdateApiInfo(void) {
if (totalKb <= 0 || curKb <= 0) return; if (totalKb <= 0 || curKb <= 0) return;
Gfx_ApiInfo[4].length = 0; Gfx_ApiInfo[4].length = 0;
Real32 total = totalKb / 1024.0f, cur = curKb / 1024.0f; Real32 total = totalKb / 1024.0f, cur = curKb / 1024.0f;
String_Format2(&Gfx_ApiInfo[4], "Video memory: %f2 MB total, %f2 now", &total, &cur); String_Format2(&Gfx_ApiInfo[4], "Video memory: %f2 MB total, %f2 free", &total, &cur);
} }
bool Gfx_WarnIfNecessary(void) { bool Gfx_WarnIfNecessary(void) {

View File

@ -506,23 +506,26 @@ DWORD WINAPI Thread_StartCallback(LPVOID lpParam) {
return 0; return 0;
} }
void* Thread_Start(Thread_StartFunc* func) { static void Thread_FreeHandle(void* handle) {
if (!CloseHandle((HANDLE)handle)) {
ErrorHandler_FailWithCode(GetLastError(), "Freeing thread handle");
}
}
void* Thread_Start(Thread_StartFunc* func, bool detach) {
DWORD threadID; DWORD threadID;
void* handle = CreateThread(NULL, 0, Thread_StartCallback, func, 0, &threadID); void* handle = CreateThread(NULL, 0, Thread_StartCallback, func, 0, &threadID);
if (!handle) { if (!handle) {
ErrorHandler_FailWithCode(GetLastError(), "Creating thread"); ErrorHandler_FailWithCode(GetLastError(), "Creating thread");
} }
if (detach) Thread_FreeHandle(handle);
return handle; return handle;
} }
void Thread_Join(void* handle) { void Thread_Join(void* handle) {
WaitForSingleObject((HANDLE)handle, INFINITE); WaitForSingleObject((HANDLE)handle, INFINITE);
} Thread_FreeHandle(handle);
void Thread_FreeHandle(void* handle) {
if (!CloseHandle((HANDLE)handle)) {
ErrorHandler_FailWithCode(GetLastError(), "Freeing thread handle");
}
} }
CRITICAL_SECTION mutexList[3]; Int32 mutexIndex; CRITICAL_SECTION mutexList[3]; Int32 mutexIndex;
@ -568,12 +571,16 @@ void* Thread_StartCallback(void* lpParam) {
} }
pthread_t threadList[3]; Int32 threadIndex; pthread_t threadList[3]; Int32 threadIndex;
void* Thread_Start(Thread_StartFunc* func) { void* Thread_Start(Thread_StartFunc* func, bool detach) {
if (threadIndex == Array_Elems(threadList)) ErrorHandler_Fail("Cannot allocate thread"); if (threadIndex == Array_Elems(threadList)) ErrorHandler_Fail("Cannot allocate thread");
pthread_t* ptr = &threadList[threadIndex]; pthread_t* ptr = &threadList[threadIndex];
int result = pthread_create(ptr, NULL, Thread_StartCallback, func); int result = pthread_create(ptr, NULL, Thread_StartCallback, func);
ErrorHandler_CheckOrFail(result, "Creating thread"); ErrorHandler_CheckOrFail(result, "Creating thread");
if (detach) {
result = pthread_detach(*ptr);
ErrorHandler_CheckOrFail(result, "Detaching thread");
}
threadIndex++; return ptr; threadIndex++; return ptr;
} }
@ -582,11 +589,6 @@ void Thread_Join(void* handle) {
ErrorHandler_CheckOrFail(result, "Joining thread"); ErrorHandler_CheckOrFail(result, "Joining thread");
} }
void Thread_FreeHandle(void* handle) {
int result = pthread_detach(*((pthread_t*)handle));
ErrorHandler_CheckOrFail(result, "Detaching thread");
}
pthread_mutex_t mutexList[3]; Int32 mutexIndex; pthread_mutex_t mutexList[3]; Int32 mutexIndex;
void* Mutex_Create(void) { void* Mutex_Create(void) {
if (mutexIndex == Array_Elems(mutexList)) ErrorHandler_Fail("Cannot allocate mutex"); if (mutexIndex == Array_Elems(mutexList)) ErrorHandler_Fail("Cannot allocate mutex");

View File

@ -74,10 +74,8 @@ ReturnCode File_Length(void* file, UInt32* length);
void Thread_Sleep(UInt32 milliseconds); void Thread_Sleep(UInt32 milliseconds);
typedef void Thread_StartFunc(void); typedef void Thread_StartFunc(void);
void* Thread_Start(Thread_StartFunc* func); void* Thread_Start(Thread_StartFunc* func, bool detach);
void Thread_Join(void* handle); void Thread_Join(void* handle);
/* Frees handle to thread - NOT THE THREAD ITSELF */
void Thread_FreeHandle(void* handle);
void* Mutex_Create(void); void* Mutex_Create(void);
void Mutex_Free(void* handle); void Mutex_Free(void* handle);

View File

@ -620,12 +620,10 @@ static void GeneratingScreen_Init(void* screen) {
void* threadHandle; void* threadHandle;
if (Gen_Vanilla) { if (Gen_Vanilla) {
threadHandle = Thread_Start(&NotchyGen_Generate); Thread_Start(&NotchyGen_Generate, true);
} else { } else {
threadHandle = Thread_Start(&FlatgrassGen_Generate); Thread_Start(&FlatgrassGen_Generate, true);
} }
/* don't leak thread handle here */
Thread_FreeHandle(threadHandle);
} }
static void GeneratingScreen_EndGeneration(void) { static void GeneratingScreen_EndGeneration(void) {