Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with Q2PRO: Windows-, MSVC-related things #429

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions inc/shared/atomic.h
Original file line number Diff line number Diff line change
@@ -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 <stdatomic.h>
#endif
8 changes: 6 additions & 2 deletions inc/shared/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 1 addition & 8 deletions src/client/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client.h"
#include <curl/curl.h>

#ifdef _MSC_VER
typedef volatile int atomic_int;
#define atomic_load(p) (*(p))
#define atomic_store(p, v) (*(p) = (v))
#else
#include <stdatomic.h>
#endif

#include "shared/atomic.h"
#include "system/pthread.h"

static cvar_t *cl_http_downloads;
Expand Down
58 changes: 10 additions & 48 deletions src/windows/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <winsvc.h>
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down
Loading