Skip to content

Commit 435e365

Browse files
committed
Reduced spins on semaphore wait for improved CPU usage; updated semaphore code with latest changes from concurrentqueue project
1 parent e791886 commit 435e365

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

atomicops.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ namespace moodycamel
537537
const int usecs_in_1_sec = 1000000;
538538
const int nsecs_in_1_sec = 1000000000;
539539
clock_gettime(CLOCK_REALTIME, &ts);
540-
ts.tv_sec += usecs / usecs_in_1_sec;
541-
ts.tv_nsec += (usecs % usecs_in_1_sec) * 1000;
540+
ts.tv_sec += (time_t)(usecs / usecs_in_1_sec);
541+
ts.tv_nsec += (long)(usecs % usecs_in_1_sec) * 1000;
542542
// sem_timedwait bombs if you have more than 1e9 in tv_nsec
543543
// so we have to clean things up before passing it in
544544
if (ts.tv_nsec >= nsecs_in_1_sec) {
@@ -588,7 +588,7 @@ namespace moodycamel
588588
// Is there a better way to set the initial spin count?
589589
// If we lower it to 1000, testBenaphore becomes 15x slower on my Core i7-5930K Windows PC,
590590
// as threads start hitting the kernel semaphore.
591-
int spin = 10000;
591+
int spin = 1024;
592592
while (--spin >= 0)
593593
{
594594
if (m_count.load() > 0)
@@ -602,8 +602,11 @@ namespace moodycamel
602602
if (oldCount > 0)
603603
return true;
604604
if (timeout_usecs < 0)
605-
return m_sema.wait();
606-
if (m_sema.timed_wait(timeout_usecs))
605+
{
606+
if (m_sema.wait())
607+
return true;
608+
}
609+
if (timeout_usecs > 0 && m_sema.timed_wait(timeout_usecs))
607610
return true;
608611
// At this point, we've timed out waiting for the semaphore, but the
609612
// count is still decremented indicating we may still be waiting on

0 commit comments

Comments
 (0)