Skip to content

Commit 7cf1054

Browse files
committed
Alleviate lock contention in the load balancing mechanism
Add a small sleep in LoadReadjusted to allow threads to aquire the lock. This enormously speeds up some simple tform tests under valgrind.
1 parent 1d5e40b commit 7cf1054

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

sources/threads.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,8 +3309,20 @@ restart:;
33093309
}
33103310
goto intercepted;
33113311
}
3312+
/* This has always been commented. Indeed no lock is held here. */
33123313
/* UNLOCK(thr->lock); */
3313-
if ( numbusy > 0 ) return(1); /* Wait a bit.... */
3314+
if ( numbusy > 0 ) {
3315+
/* JD: this avoids large runtimes for tform tests under valgrind.
3316+
What seems to happen is we return from here, goto Finalize, and
3317+
end up in LoadReadjusted again without the threads having a
3318+
chance to update their busy status. Then we end up here again.
3319+
Sleep the thread for, say, 1us to allow threads to aquire the lock. */
3320+
struct timespec sleeptime;
3321+
sleeptime.tv_sec = 0;
3322+
sleeptime.tv_nsec = 1000L;
3323+
nanosleep(&sleeptime, NULL);
3324+
return(1); /* Wait a bit.... */
3325+
}
33143326
return(0);
33153327
intercepted:;
33163328
/*
@@ -3319,8 +3331,9 @@ intercepted:;
33193331
2: find the first untreated term.
33203332
3: put the terms in the free buckets.
33213333
3322-
Remember: we have the lock to avoid interference from the thread
3323-
that is being robbed.
3334+
Remember: we still have the lock to avoid interference from the thread
3335+
that is being robbed. We were holding it and then jumped here with
3336+
goto intercepted.
33243337
*/
33253338
numinput = thr->firstterm + thr->usenum;
33263339
nperbucket = numtogo / numfree;

0 commit comments

Comments
 (0)