------- Comment #19 from solar-gcc at openwall dot com 2010-08-24 12:18 ------- (In reply to comment #18) > Then, at the start of the spinning libgomp could initialize that flag and > check > it from time to time (say every few hundred or thousand iterations) whether it > has lost the CPU.
Without a kernel API like that, you can achieve a similar effect by issuing the rdtsc instruction (or its equivalents for non-x86 archs) and seeing if the cycle counter changes unexpectedly (say, by 1000 or more for a single loop iteration), which would indicate that there was a context switch. For an arch-independent implementation, you could also use a syscall such as times(2) or gettimeofday(2), but then you'd need to do it very infrequently (e.g., maybe just to see if there's a context switch between 10k to 20k spins). -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43706