work around broken try_lock_for in gcc
authorNathan Bronson <ngbronson@fb.com>
Sun, 29 Dec 2013 05:35:56 +0000 (21:35 -0800)
committerSara Golemon <sgolemon@fb.com>
Mon, 6 Jan 2014 19:38:57 +0000 (11:38 -0800)
Summary:
timed_mutex::try_lock_for is broken in gcc 4.8 (see
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54562), so this diff adds
a workaround using try_lock_until.  The internal implementation of
try_lock_for converts to try_lock_until, but it uses the steady_clock
instead of the system_clock as its time base.  In some gcc versions
these are the same clock so it works okay, but not in all.

Test Plan: unit tests

Reviewed By: delong.j@fb.com

FB internal diff: D1108584

folly/Synchronized.h

index b5a3c5ba0ffa1c8af805a45eb0eeab678d2ebf2c..965372531db20caf7021934cfa7930c6429e36f0 100644 (file)
@@ -108,7 +108,12 @@ typename std::enable_if<
   IsOneOf<T, std::timed_mutex, std::recursive_timed_mutex>::value, bool>::type
 acquireReadWrite(T& mutex,
                  unsigned int milliseconds) {
   IsOneOf<T, std::timed_mutex, std::recursive_timed_mutex>::value, bool>::type
 acquireReadWrite(T& mutex,
                  unsigned int milliseconds) {
-  return mutex.try_lock_for(std::chrono::milliseconds(milliseconds));
+  // work around try_lock_for bug in some gcc versions, see
+  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54562
+  return mutex.try_lock()
+      || (milliseconds > 0 &&
+          mutex.try_lock_until(std::chrono::system_clock::now() +
+                               std::chrono::milliseconds(milliseconds)));
 }
 
 /**
 }
 
 /**