Add CPU affinity function for Windows CE.
[chocolate-doom.git] / src / i_main.c
blobe1de58813a4bcfa95fb0609d41d84ba5b3f081e3
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 "doomdef.h"
33 #include "i_system.h"
34 #include "m_argv.h"
35 #include "d_main.h"
37 #if defined(_WIN32_WCE)
39 // Windows CE? I doubt it even supports SMP..
41 static void LockCPUAffinity(void)
45 #elif defined(_WIN32)
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
50 typedef BOOL WINAPI (*SetAffinityFunc)(HANDLE hProcess, DWORD_PTR mask);
52 // This is a bit more complicated than it really needs to be. We really
53 // just need to call the SetProcessAffinityMask function, but that
54 // function doesn't exist on systems before Windows 2000. Instead,
55 // dynamically look up the function and call the pointer to it. This
56 // way, the program will run on older versions of Windows (Win9x, etc.)
58 static void LockCPUAffinity(void)
60 HMODULE kernel32_dll;
61 SetAffinityFunc SetAffinity;
63 // Find the kernel interface DLL.
65 kernel32_dll = LoadLibrary("kernel32.dll");
67 if (kernel32_dll == NULL)
69 // This should never happen...
71 fprintf(stderr, "Failed to load kernel32.dll\n");
72 return;
75 // Find the SetProcessAffinityMask function.
77 SetAffinity = GetProcAddress(kernel32_dll, "SetProcessAffinityMask");
79 // If the function was not found, we are on an old (Win9x) system
80 // that doesn't have this function. That's no problem, because
81 // those systems don't support SMP anyway.
83 if (SetAffinity != NULL)
85 if (!SetAffinity(GetCurrentProcess(), 1))
87 fprintf(stderr, "Failed to set process affinity (%d)\n",
88 (int) GetLastError());
93 #elif defined(HAVE_SCHED_SETAFFINITY)
95 #include <unistd.h>
96 #include <sched.h>
98 // Unix (Linux) version:
100 static void LockCPUAffinity(void)
102 #ifdef CPU_SET
103 cpu_set_t set;
105 CPU_ZERO(&set);
106 CPU_SET(0, &set);
108 sched_setaffinity(getpid(), sizeof(set), &set);
109 #else
110 unsigned long mask = 1;
111 sched_setaffinity(getpid(), sizeof(mask), &mask);
112 #endif
115 #else
117 #warning No known way to set processor affinity on this platform.
118 #warning You may experience crashes due to SDL_mixer.
120 static void LockCPUAffinity(void)
122 fprintf(stderr,
123 "WARNING: No known way to set processor affinity on this platform.\n"
124 " You may experience crashes due to SDL_mixer.\n");
127 #endif
129 int main(int argc, char **argv)
131 // save arguments
133 myargc = argc;
134 myargv = argv;
136 // Only schedule on a single core, if we have multiple
137 // cores. This is to work around a bug in SDL_mixer.
139 LockCPUAffinity();
141 // start doom
143 D_DoomMain ();
145 return 0;