diff --git a/inc/shared/atomic.h b/inc/shared/atomic.h new file mode 100644 index 000000000..8385ef058 --- /dev/null +++ b/inc/shared/atomic.h @@ -0,0 +1,27 @@ +/* +Copyright (C) 2023 Andrey Nazarov + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#pragma once + +#ifdef _MSC_VER +typedef volatile int atomic_int; +#define atomic_load(p) (*(p)) +#define atomic_store(p, v) (*(p) = (v)) +#else +#include +#endif diff --git a/inc/shared/platform.h b/inc/shared/platform.h index 9288aa0c4..29152446c 100644 --- a/inc/shared/platform.h +++ b/inc/shared/platform.h @@ -142,10 +142,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #define q_malloc #define q_sentinel -#define q_likely(x) !!(x) -#define q_unlikely(x) !!(x) +#define q_likely(x) (x) +#define q_unlikely(x) (x) #define q_offsetof(t, m) ((size_t)&((t *)0)->m) +#ifdef _MSC_VER #define q_alignof(t) __alignof(t) +#else +#define q_alignof(t) 1 +#endif #define q_gameabi diff --git a/src/client/http.c b/src/client/http.c index ee799654c..882581605 100644 --- a/src/client/http.c +++ b/src/client/http.c @@ -21,14 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client.h" #include -#ifdef _MSC_VER -typedef volatile int atomic_int; -#define atomic_load(p) (*(p)) -#define atomic_store(p, v) (*(p) = (v)) -#else -#include -#endif - +#include "shared/atomic.h" #include "system/pthread.h" static cvar_t *cl_http_downloads; diff --git a/src/windows/system.c b/src/windows/system.c index bfaf9ee45..8288e4201 100644 --- a/src/windows/system.c +++ b/src/windows/system.c @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/cvar.h" #include "common/field.h" #include "common/prompt.h" +#include "shared/atomic.h" #if USE_WINSVC #include @@ -33,8 +34,8 @@ static SERVICE_STATUS_HANDLE statusHandle; static jmp_buf exitBuf; #endif -static volatile BOOL shouldExit; -static volatile BOOL errorEntered; +static atomic_int shouldExit; +static atomic_int errorEntered; static LARGE_INTEGER timer_freq; @@ -593,10 +594,10 @@ void Sys_SetConsoleTitle(const char *title) static BOOL WINAPI Sys_ConsoleCtrlHandler(DWORD dwCtrlType) { - if (errorEntered) { + if (atomic_load(&errorEntered)) { exit(1); } - shouldExit = TRUE; + atomic_store(&shouldExit, TRUE); Sleep(INFINITE); return TRUE; } @@ -825,9 +826,9 @@ void Sys_Error(const char *error, ...) longjmp(exitBuf, 1); #endif - errorEntered = TRUE; + atomic_store(&errorEntered, TRUE); - if (shouldExit || (sys_exitonerror && sys_exitonerror->integer)) + if (atomic_load(&shouldExit) || (sys_exitonerror && sys_exitonerror->integer)) exit(1); #if USE_SYSCON @@ -1229,7 +1230,7 @@ static int Sys_Main(int argc, char **argv) Qcommon_Init(argc, argv); // main program loop - while (!shouldExit) + while (!atomic_load(&shouldExit)) Qcommon_Frame(); Com_Quit(NULL, ERR_DISCONNECT); @@ -1238,43 +1239,6 @@ static int Sys_Main(int argc, char **argv) #if USE_CLIENT -#define MAX_LINE_TOKENS 128 - -static char *sys_argv[MAX_LINE_TOKENS]; -static int sys_argc; - -/* -=============== -Sys_ParseCommandLine - -=============== -*/ -static void Sys_ParseCommandLine(char *line) -{ - sys_argc = 1; - sys_argv[0] = APPLICATION; - while (*line) { - while (*line && *line <= 32) { - line++; - } - if (*line == 0) { - break; - } - sys_argv[sys_argc++] = line; - while (*line > 32) { - line++; - } - if (*line == 0) { - break; - } - *line = 0; - if (sys_argc == MAX_LINE_TOKENS) { - break; - } - line++; - } -} - /* ================== WinMain @@ -1290,9 +1254,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine hGlobalInstance = hInstance; - Sys_ParseCommandLine(lpCmdLine); - - return Sys_Main(sys_argc, sys_argv); + return Sys_Main(__argc, __argv); } #else // USE_CLIENT @@ -1305,7 +1267,7 @@ static int sys_argc; static void WINAPI ServiceHandler(DWORD fdwControl) { if (fdwControl == SERVICE_CONTROL_STOP) { - shouldExit = TRUE; + atomic_store(&shouldExit, TRUE); } }