summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2016-08-29 19:40:24 -0700
committerThiago Macieira <thiago.macieira@intel.com>2016-08-30 21:52:04 +0000
commit84830fc07d65d58fce9b24b5ec8f1224b0969ac0 (patch)
tree0be365109637e660f8d964fc1defd409460e4c5c /src
parenteebdb16c63376bf7b8087cce82517a01abd573a3 (diff)
QLockFile::tryLock: prevent over-sleeping in certain cases
The sleep time increases exponentially, but we never checked whether the time to sleep was less than the remaining time. For example, if timeout was 4000 ms on entry, we'd progressively sleep 100 ms, 200, 400, 800, 1600 ms. At this point, the accummulated sleep time would be 3100 ms and the next sleep should be no more than 900 ms. Prior to this change, the tryLock() would then proceed to sleep 3200 ms, for a total wait time of 6300 ms, or 57.5% above the timeout provided by the user. Change-Id: Ifc295639c8cf4ddcaa69fffd146f7586a7ee95e4 Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/io/qlockfile.cpp7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp
index e7275eeaed..ae3a7c6abc 100644
--- a/src/corelib/io/qlockfile.cpp
+++ b/src/corelib/io/qlockfile.cpp
@@ -233,8 +233,13 @@ bool QLockFile::tryLock(int timeout)
}
break;
}
- if (timer.hasExpired())
+
+ int remainingTime = timer.remainingTime();
+ if (remainingTime == 0)
return false;
+ else if (uint(sleepTime) > uint(remainingTime))
+ sleepTime = remainingTime;
+
QThread::msleep(sleepTime);
if (sleepTime < 5 * 1000)
sleepTime *= 2;