Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Unified Diff: util/synchronization/semaphore_posix.cc

Issue 909263002: Add Semaphore::TimedWait() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/synchronization/semaphore_mac.cc ('k') | util/synchronization/semaphore_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/synchronization/semaphore_posix.cc
diff --git a/util/misc/clock_posix.cc b/util/synchronization/semaphore_posix.cc
similarity index 50%
copy from util/misc/clock_posix.cc
copy to util/synchronization/semaphore_posix.cc
index 24171091db1d41db1c34b420eb197b8291220214..973f0a5d503df575e3f0b24f3397abc3e6a5168e 100644
--- a/util/misc/clock_posix.cc
+++ b/util/synchronization/semaphore_posix.cc
@@ -12,40 +12,46 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "util/misc/clock.h"
Robert Sesek 2015/02/10 21:57:07 The find-copies here sucks.
Mark Mentovai 2015/02/10 22:01:45 Robert Sesek wrote:
+#include "util/synchronization/semaphore.h"
-#include <time.h>
+#include <errno.h>
+
+#include <cmath>
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
-#include "build/build_config.h"
-
-namespace {
-
-const uint64_t kNanosecondsPerSecond = 1E9;
-
-} // namespace
namespace crashpad {
#if !defined(OS_MACOSX)
-uint64_t ClockMonotonicNanoseconds() {
- timespec now;
- int rv = clock_gettime(CLOCK_MONOTONIC, &now);
- DPCHECK(rv == 0) << "clock_gettime";
+Semaphore::Semaphore(int value) {
+ PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init";
+}
- return now.tv_sec * kNanosecondsPerSecond + now.tv_nsec;
+Semaphore::~Semaphore() {
+ PCHECK(sem_destroy(&semaphore_) == 0) << "sem_destroy";
}
-#endif
+void Semaphore::Wait() {
+ PCHECK(HANDLE_EINTR(sem_wait(&semaphore_)) == 0) << "sem_wait";
+}
+
+bool Semaphore::TimedWait(double seconds) {
+ DCHECK_GE(seconds, 0.0);
+ timespec timeout;
+ timeout.tv_sec = seconds;
+ timeout.tv_nsec = (seconds - trunc(seconds)) * 1E9;
+
+ int rv = HANDLE_EINTR(sem_timedwait(&semaphore_, &timeout));
+ PCHECK(rv == 0 || errno == ETIMEDOUT) << "sem_timedwait";
+ return rv == 0;
+}
-void SleepNanoseconds(uint64_t nanoseconds) {
- timespec sleep_time;
- sleep_time.tv_sec = nanoseconds / kNanosecondsPerSecond;
- sleep_time.tv_nsec = nanoseconds % kNanosecondsPerSecond;
- int rv = HANDLE_EINTR(nanosleep(&sleep_time, &sleep_time));
- DPCHECK(rv == 0) << "nanosleep";
+void Semaphore::Signal() {
+ PCHECK(sem_post(&semaphore_) == 0) << "sem_post";
}
+#endif
+
} // namespace crashpad
« no previous file with comments | « util/synchronization/semaphore_mac.cc ('k') | util/synchronization/semaphore_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698