Browse Source

added set_cpu_affinity

Darren Ranalli 18 years ago
parent
commit
da78fd9940
2 changed files with 44 additions and 10 deletions
  1. 42 10
      panda/src/express/trueClock.cxx
  2. 2 0
      panda/src/express/trueClock.h

+ 42 - 10
panda/src/express/trueClock.cxx

@@ -123,12 +123,41 @@ get_short_raw_time() {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: TrueClock::Constructor, Win32 implementation
-//       Access: Protected
+//     Function: TrueClock::set_cpu_affinity, Win32 implementation
+//       Access: Published
 //  Description: 
 //  Description: 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 typedef BOOL (WINAPI * PFNSETPROCESSAFFINITYMASK)(HANDLE, DWORD_PTR);
 typedef BOOL (WINAPI * PFNSETPROCESSAFFINITYMASK)(HANDLE, DWORD_PTR);
+typedef BOOL (WINAPI * PFNGETPROCESSAFFINITYMASK)(HANDLE, PDWORD_PTR, PDWORD_PTR);
+
+bool TrueClock::
+set_cpu_affinity(PN_uint32 mask) const {
+  HMODULE hker = GetModuleHandle("kernel32");
+  if (hker != 0) {
+    PFNGETPROCESSAFFINITYMASK gp = (PFNGETPROCESSAFFINITYMASK)
+      GetProcAddress(hker, "GetProcessAffinityMask");
+    PFNSETPROCESSAFFINITYMASK sp = (PFNSETPROCESSAFFINITYMASK)
+      GetProcAddress(hker, "SetProcessAffinityMask");
+    if (gp != 0 && sp != 0) {
+      DWORD proc_mask;
+      DWORD sys_mask;
+      if (gp(GetCurrentProcess(), &proc_mask, &sys_mask)) {
+        // make sure we don't reference CPUs that don't exist
+        proc_mask = mask & sys_mask;
+        if (proc_mask) {
+          return sp(GetCurrentProcess(), proc_mask);
+        }
+      }
+    }
+  }
+  return false;
+}
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: TrueClock::Constructor, Win32 implementation
+//       Access: Protected
+//  Description: 
+////////////////////////////////////////////////////////////////////
 TrueClock::
 TrueClock::
 TrueClock() {
 TrueClock() {
   _error_count = 0;
   _error_count = 0;
@@ -142,14 +171,7 @@ TrueClock() {
   _report_time_scale_time = 0.0;
   _report_time_scale_time = 0.0;
 
 
   if (lock_to_one_cpu) {
   if (lock_to_one_cpu) {
-    HMODULE hker = GetModuleHandle("kernel32");
-    if (hker != 0) {
-      PFNSETPROCESSAFFINITYMASK sp = (PFNSETPROCESSAFFINITYMASK)
-        GetProcAddress(hker, "SetProcessAffinityMask");
-      if (sp != 0) {
-        sp(GetCurrentProcess(), 1);
-      }
-    }
+    set_cpu_affinity(0x01);
   }
   }
   
   
   if (get_use_high_res_clock()) {
   if (get_use_high_res_clock()) {
@@ -555,6 +577,16 @@ get_short_raw_time() {
   return (double)(tv.tv_sec - _init_sec) + (double)tv.tv_usec / 1000000.0;
   return (double)(tv.tv_sec - _init_sec) + (double)tv.tv_usec / 1000000.0;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: TrueClock::set_cpu_affinity, Posix implementation
+//       Access: Published
+//  Description: 
+////////////////////////////////////////////////////////////////////
+bool TrueClock::
+set_cpu_affinity(PN_int32 mask) {
+  return false;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TrueClock::Constructor, Posix implementation
 //     Function: TrueClock::Constructor, Posix implementation
 //       Access: Protected
 //       Access: Protected

+ 2 - 0
panda/src/express/trueClock.h

@@ -62,6 +62,8 @@ PUBLISHED:
 
 
   INLINE static TrueClock *get_global_ptr();
   INLINE static TrueClock *get_global_ptr();
 
 
+  bool set_cpu_affinity(PN_uint32 mask) const;
+
 protected:
 protected:
   TrueClock();
   TrueClock();
   INLINE ~TrueClock();
   INLINE ~TrueClock();