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

Side by Side Diff: mojo/system/waiter.cc

Issue 23621056: Initial in-process implementation of some Mojo primitives. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: C++ wrappers Created 7 years, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/system/waiter.h"
6
7 #include <limits>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11
12 namespace mojo {
13 namespace system {
14
15 Waiter::Waiter()
16 : cv_(&lock_),
17 awoken_(false),
18 wait_result_(MOJO_RESULT_INTERNAL) {
19 }
20
21 Waiter::~Waiter() {
22 }
23
24 void Waiter::Init() {
25 awoken_ = false;
26 // NOTE(vtl): If performance ever becomes an issue, we can disable the setting
27 // of |wait_result_| (except the first one in |Awake()|) in Release builds.
28 wait_result_ = MOJO_RESULT_INTERNAL;
29 }
30
31 // TODO(vtl): Fast-path the |deadline == 0| case?
32 MojoResult Waiter::Wait(MojoDeadline deadline) {
33 base::AutoLock locker(lock_);
34
35 // Fast-path the already-awoken case:
36 if (awoken_) {
37 DCHECK_NE(wait_result_, MOJO_RESULT_INTERNAL);
38 return wait_result_;
39 }
40
41 // |MojoDeadline| is actually a |uint64_t|, but we need a signed quantity.
42 // Treat any out-of-range deadline as "forever" (which is wrong, but okay
43 // since 2^63 microseconds is ~300000 years). Note that this also takes care
44 // of the |MOJO_DEADLINE_INDEFINITE| (= 2^64 - 1) case.
45 if (deadline > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
46 do {
47 cv_.Wait();
48 } while (!awoken_);
49 } else {
50 // NOTE(vtl): This is very inefficient on POSIX, since pthreads condition
51 // variables take an absolute deadline.
52 const base::TimeTicks end_time = base::TimeTicks::HighResNow() +
53 base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline));
54 do {
55 base::TimeTicks now_time = base::TimeTicks::HighResNow();
56 if (now_time >= end_time)
57 return MOJO_RESULT_DEADLINE_EXCEEDED;
58
59 cv_.TimedWait(end_time - now_time);
60 } while (!awoken_);
61 }
62
63 DCHECK_NE(wait_result_, MOJO_RESULT_INTERNAL);
64 return wait_result_;
65 }
66
67 void Waiter::Awake(MojoResult wait_result) {
68 base::AutoLock locker(lock_);
69
70 if (awoken_)
71 return;
72
73 awoken_ = true;
74 wait_result_ = wait_result;
75 cv_.Signal();
darin (slow to review) 2013/09/27 15:34:47 You probably had this in mind at the time, but jus
viettrungluu 2013/09/27 17:58:01 Actually, |cv_.Wait()| only returns after |lock_|
76 }
77
78 } // namespace system
79 } // namespace mojo
OLDNEW
« mojo/system/limits.h ('K') | « mojo/system/waiter.h ('k') | mojo/system/waiter_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698