/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <type_traits>
#include <mutex>
#include <boost/thread.hpp>
-#include "folly/Preprocessor.h"
-#include "folly/Traits.h"
+#include <folly/Preprocessor.h>
+#include <folly/Traits.h>
namespace folly {
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)));
}
/**
return;
}
// Could not acquire the resource, pointer is null
- parent_ = NULL;
+ parent_ = nullptr;
}
/**
* SYNCHRONIZED below.
*/
T* operator->() {
- return parent_ ? &parent_->datum_ : NULL;
+ return parent_ ? &parent_->datum_ : nullptr;
}
/**
acquire();
}
ConstLockedPtr(const Synchronized* parent, unsigned int milliseconds) {
- if (parent->mutex_.timed_lock(
+ if (parent->mutex_.timed_lock_shared(
boost::posix_time::milliseconds(milliseconds))) {
parent_ = parent;
return;
}
// Could not acquire the resource, pointer is null
- parent_ = NULL;
+ parent_ = nullptr;
}
ConstLockedPtr& operator=(const ConstLockedPtr& rhs) {
}
const T* operator->() const {
- return parent_ ? &parent_->datum_ : NULL;
+ return parent_ ? &parent_->datum_ : nullptr;
}
struct Unsynchronizer {