Look up SetProcessAffinityMask function at runtime, so that the program should work...
[chocolate-doom.git] / src / i_main.c
blob7c5b16e78fef43a26f1bb9606568460ffe027f24
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 // 02111-1307, USA.
22 // DESCRIPTION:
23 // Main program, simply calls D_DoomMain high level loop.
25 //-----------------------------------------------------------------------------
28 #include "config.h"
30 #include "SDL.h"
32 #include <signal.h>
34 #ifdef _WIN32
35 #define WIN32_LEAN_AND_MEAN
36 #include <windows.h>
37 #endif
39 #ifdef HAVE_SCHED_SETAFFINITY
40 #include <unistd.h>
41 #include <sched.h>
42 #endif
44 #include "doomdef.h"
45 #include "i_system.h"
46 #include "m_argv.h"
47 #include "d_main.h"
49 #if defined(_WIN32)
51 typedef BOOL WINAPI (*SetAffinityFunc)(HANDLE hProcess, DWORD_PTR mask);
53 // This is a bit more complicated than it really needs to be. We really
54 // just need to call the SetProcessAffinityMask function, but that
55 // function doesn't exist on systems before Windows 2000. Instead,
56 // dynamically look up the function and call the pointer to it. This
57 // way, the program will run on older versions of Windows (Win9x, etc.)
59 static void LockCPUAffinity(void)
61 HMODULE kernel32_dll;
62 SetAffinityFunc SetAffinity;
64 // Find the kernel interface DLL.
66 kernel32_dll = LoadLibrary("kernel32.dll");
68 if (kernel32_dll == NULL)
70 // This should never happen...
72 fprintf(stderr, "Failed to load kernel32.dll\n");
73 return;
76 // Find the SetProcessAffinityMask function.
78 SetAffinity = GetProcAddress(kernel32_dll, "SetProcessAffinityMask");
80 // If the function was not found, we are on an old (Win9x) system
81 // that doesn't have this function. That's no problem, because
82 // those systems don't support SMP anyway.
84 if (SetAffinity != NULL)
86 if (!SetAffinity(GetCurrentProcess(), 1))
88 fprintf(stderr, "Failed to set process affinity (%d)\n",
89 (int) GetLastError());
94 #elif defined(HAVE_SCHED_SETAFFINITY)
96 // Unix (Linux) version:
98 static void LockCPUAffinity(void)
100 cpu_set_t set;
102 CPU_ZERO(&set);
103 CPU_SET(0, &set);
105 sched_setaffinity(getpid(), sizeof(set), &set);
108 #else
110 #warning No known way to set processor affinity on this platform.
111 #warning You may experience crashes due to SDL_mixer.
113 static void LockCPUAffinity(void)
115 fprintf(stderr,
116 "WARNING: No known way to set processor affinity on this platform.\n"
117 " You may experience crashes due to SDL_mixer.\n");
120 #endif
122 int main(int argc, char **argv)
124 // save arguments
126 myargc = argc;
127 myargv = argv;
129 // Only schedule on a single core, if we have multiple
130 // cores. This is to work around a bug in SDL_mixer.
132 LockCPUAffinity();
134 // start doom
136 D_DoomMain ();
138 return 0;