12 #if HAVE_SCHED_SETAFFINITY
13 static bool didSaveAffinity
= false;
14 static cpu_set_t savedAffinity
;
18 void setAffinityTo(int cpu
)
20 #if HAVE_SCHED_SETAFFINITY
21 if (sched_getaffinity(0, sizeof(cpu_set_t
), &savedAffinity
) == -1) return;
22 didSaveAffinity
= true;
23 printMsg(lvlDebug
, format("locking this thread to CPU %1%") % cpu
);
24 cpu_set_t newAffinity
;
25 CPU_ZERO(&newAffinity
);
26 CPU_SET(cpu
, &newAffinity
);
27 if (sched_setaffinity(0, sizeof(cpu_set_t
), &newAffinity
) == -1)
28 printMsg(lvlError
, format("failed to lock thread to CPU %1%") % cpu
);
33 int lockToCurrentCPU()
35 #if HAVE_SCHED_SETAFFINITY
36 int cpu
= sched_getcpu();
37 if (cpu
!= -1) setAffinityTo(cpu
);
45 void restoreAffinity()
47 #if HAVE_SCHED_SETAFFINITY
48 if (!didSaveAffinity
) return;
49 if (sched_setaffinity(0, sizeof(cpu_set_t
), &savedAffinity
) == -1)
50 printMsg(lvlError
, "failed to restore affinity %1%");