Skip to content

Commit

Permalink
There is no std::thread and std::mutex under i686-w64-mingw32.
Browse files Browse the repository at this point in the history
  • Loading branch information
klei1984 committed Jun 2, 2024
1 parent 5f32cf4 commit 2a68ad4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
48 changes: 35 additions & 13 deletions src/ailog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@
#define AILOG_FILE_LIMIT UINT16_MAX

std::ofstream AiLog::AiLog_File;
std::mutex AiLog::AiLog_Mutex;
SDL_mutex* AiLog::AiLog_Mutex;
int32_t AiLog::AiLog_SectionCount;
int32_t AiLog::AiLog_EntryCount;

inline void AiLog::AiLog_InitMutex() {
if (!AiLog_Mutex) {
AiLog_Mutex = SDL_CreateMutex();

if (!AiLog_Mutex) {
ResourceManager_ExitGame(EXIT_CODE_INSUFFICIENT_MEMORY);
}
}
}

AiLog::AiLog(const char* format, ...) {
AiLog_Mutex.lock();
AiLog_InitMutex();

SDL_LockMutex(AiLog_Mutex);

if (AiLog_File.is_open()) {
time_stamp = timer_get();
Expand All @@ -47,11 +59,13 @@ AiLog::AiLog(const char* format, ...) {

++AiLog_SectionCount;

AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog_Mutex);
}

AiLog::~AiLog() {
AiLog_Mutex.lock();
AiLog_InitMutex();

SDL_LockMutex(AiLog_Mutex);

--AiLog_SectionCount;

Expand All @@ -74,7 +88,7 @@ AiLog::~AiLog() {
}
}

AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog_Mutex);
}

void AiLog::VSprintf(const char* format, va_list args) {
Expand All @@ -91,7 +105,9 @@ void AiLog::VSprintf(const char* format, va_list args) {
}

void AiLog::Log(const char* format, ...) {
AiLog::AiLog_Mutex.lock();
AiLog_InitMutex();

SDL_LockMutex(AiLog_Mutex);

if (AiLog_File.is_open()) {
va_list args;
Expand All @@ -101,7 +117,7 @@ void AiLog::Log(const char* format, ...) {
va_end(args);
}

AiLog::AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog_Mutex);
}

void AiLog::NoLockLog(const char* format, ...) {
Expand All @@ -113,17 +129,21 @@ void AiLog::NoLockLog(const char* format, ...) {
}

void AiLog::VLog(const char* format, va_list args) {
AiLog::AiLog_Mutex.lock();
AiLog_InitMutex();

SDL_LockMutex(AiLog_Mutex);

if (AiLog_File.is_open()) {
VSprintf(format, args);
}

AiLog::AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog_Mutex);
}

void AiLog_Open() {
AiLog::AiLog_Mutex.lock();
AiLog::AiLog_InitMutex();

SDL_LockMutex(AiLog::AiLog_Mutex);

if (!AiLog::AiLog_File.is_open()) {
auto filepath{(ResourceManager_FilePathGamePref / "ai_log.txt").lexically_normal()};
Expand All @@ -132,13 +152,15 @@ void AiLog_Open() {
AiLog::AiLog_EntryCount = 0;
}

AiLog::AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog::AiLog_Mutex);
}

void AiLog_Close() {
AiLog::AiLog_Mutex.lock();
AiLog::AiLog_InitMutex();

SDL_LockMutex(AiLog::AiLog_Mutex);

AiLog::AiLog_File.close();

AiLog::AiLog_Mutex.unlock();
SDL_UnlockMutex(AiLog::AiLog_Mutex);
}
6 changes: 4 additions & 2 deletions src/ailog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
#ifndef AILOG_HPP
#define AILOG_HPP

#include <SDL_mutex.h>

#include <cstdint>
#include <fstream>
#include <mutex>

class AiLog {
static std::ofstream AiLog_File;
static std::mutex AiLog_Mutex;
static SDL_mutex *AiLog_Mutex;
static int32_t AiLog_SectionCount;
static int32_t AiLog_EntryCount;

uint32_t time_stamp;

static void AiLog_InitMutex();
void VSprintf(const char *format, va_list args);
void NoLockLog(const char *format, ...);

Expand Down

0 comments on commit 2a68ad4

Please sign in to comment.