summaryrefslogtreecommitdiff
path: root/src/i_main.c
diff options
context:
space:
mode:
authorSimon Howard2009-03-12 22:20:07 +0000
committerSimon Howard2009-03-12 22:20:07 +0000
commit6e2c404f64f39e059848bc66b707dfe979f044ff (patch)
treecd0aba502696ceef12bd311d4576fd46c4dd5c02 /src/i_main.c
parent20cb00076d39bcb8a5c2e82912af8200c97034cb (diff)
parent535c64b336ce59b4e18819544cd2a1129ae97c61 (diff)
downloadchocolate-doom-6e2c404f64f39e059848bc66b707dfe979f044ff.tar.gz
chocolate-doom-6e2c404f64f39e059848bc66b707dfe979f044ff.tar.bz2
chocolate-doom-6e2c404f64f39e059848bc66b707dfe979f044ff.zip
Update from trunk.
Subversion-branch: /branches/raven-branch Subversion-revision: 1467
Diffstat (limited to 'src/i_main.c')
-rw-r--r--src/i_main.c93
1 files changed, 68 insertions, 25 deletions
diff --git a/src/i_main.c b/src/i_main.c
index d7cdcef6..a0b0bfd5 100644
--- a/src/i_main.c
+++ b/src/i_main.c
@@ -24,7 +24,6 @@
//
//-----------------------------------------------------------------------------
-
#include "config.h"
#include "SDL.h"
@@ -53,46 +52,90 @@
void D_DoomMain (void);
-#if !defined(_WIN32) && !defined(HAVE_SCHED_SETAFFINITY)
-#warning No known way to set processor affinity on this platform.
-#warning You may experience crashes due to SDL_mixer.
-#endif
+#if defined(_WIN32)
-int main(int argc, char **argv)
-{
- // save arguments
+typedef BOOL WINAPI (*SetAffinityFunc)(HANDLE hProcess, DWORD_PTR mask);
- myargc = argc;
- myargv = argv;
+// This is a bit more complicated than it really needs to be. We really
+// just need to call the SetProcessAffinityMask function, but that
+// function doesn't exist on systems before Windows 2000. Instead,
+// dynamically look up the function and call the pointer to it. This
+// way, the program will run on older versions of Windows (Win9x, etc.)
-#ifdef _WIN32
+static void LockCPUAffinity(void)
+{
+ HMODULE kernel32_dll;
+ SetAffinityFunc SetAffinity;
+
+ // Find the kernel interface DLL.
- // Set the process affinity mask so that all threads
- // run on the same processor. This is a workaround for a bug in
- // SDL_mixer that causes occasional crashes.
+ kernel32_dll = LoadLibrary("kernel32.dll");
- if (!SetProcessAffinityMask(GetCurrentProcess(), 1))
+ if (kernel32_dll == NULL)
{
- fprintf(stderr, "Failed to set process affinity mask (%d)\n",
- (int) GetLastError());
+ // This should never happen...
+
+ fprintf(stderr, "Failed to load kernel32.dll\n");
+ return;
}
-#endif
+ // Find the SetProcessAffinityMask function.
-#ifdef HAVE_SCHED_SETAFFINITY
+ SetAffinity = GetProcAddress(kernel32_dll, "SetProcessAffinityMask");
- // Linux version:
+ // If the function was not found, we are on an old (Win9x) system
+ // that doesn't have this function. That's no problem, because
+ // those systems don't support SMP anyway.
+ if (SetAffinity != NULL)
{
- cpu_set_t set;
+ if (!SetAffinity(GetCurrentProcess(), 1))
+ {
+ fprintf(stderr, "Failed to set process affinity (%d)\n",
+ (int) GetLastError());
+ }
+ }
+}
- CPU_ZERO(&set);
- CPU_SET(0, &set);
+#elif defined(HAVE_SCHED_SETAFFINITY)
- sched_setaffinity(getpid(), sizeof(set), &set);
- }
+// Unix (Linux) version:
+
+static void LockCPUAffinity(void)
+{
+ cpu_set_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(0, &set);
+
+ sched_setaffinity(getpid(), sizeof(set), &set);
+}
+
+#else
+
+#warning No known way to set processor affinity on this platform.
+#warning You may experience crashes due to SDL_mixer.
+
+static void LockCPUAffinity(void)
+{
+ fprintf(stderr,
+ "WARNING: No known way to set processor affinity on this platform.\n"
+ " You may experience crashes due to SDL_mixer.\n");
+}
#endif
+int main(int argc, char **argv)
+{
+ // save arguments
+
+ myargc = argc;
+ myargv = argv;
+
+ // Only schedule on a single core, if we have multiple
+ // cores. This is to work around a bug in SDL_mixer.
+
+ LockCPUAffinity();
+
// start doom
D_DoomMain ();