/Users/deen/code/yugabyte-db/src/yb/util/promise.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_UTIL_PROMISE_H |
33 | | #define YB_UTIL_PROMISE_H |
34 | | |
35 | | #include "yb/gutil/macros.h" |
36 | | #include "yb/util/countdown_latch.h" |
37 | | |
38 | | namespace yb { |
39 | | |
40 | | // A promise boxes a value which is to be provided at some time in the future. |
41 | | // A single producer calls Set(...), and any number of consumers can call Get() |
42 | | // to retrieve the produced value. |
43 | | // |
44 | | // In Guava terms, this is a SettableFuture<T>. |
45 | | template<typename T> |
46 | | class Promise { |
47 | | public: |
48 | 151k | Promise() : latch_(1) {} |
49 | 75.0k | ~Promise() {} |
50 | | |
51 | | // Reset the promise to be used again. |
52 | | // For this to be safe, there must be some kind of external synchronization |
53 | | // ensuring that no threads are still accessing the value from the previous |
54 | | // incarnation of the promise. |
55 | 159k | void Reset() { |
56 | 159k | latch_.Reset(1); |
57 | 159k | val_ = T(); |
58 | 159k | } |
59 | | |
60 | | // Block until a value is available, and return a reference to it. |
61 | 160k | const T& Get() const { |
62 | 160k | latch_.Wait(); |
63 | 160k | return val_; |
64 | 160k | } |
65 | | |
66 | | // Wait for the promised value to become available with the given timeout. |
67 | | // |
68 | | // Returns NULL if the timeout elapses before a value is available. |
69 | | // Otherwise returns a pointer to the value. This pointer's lifetime is |
70 | | // tied to the lifetime of the Promise object. |
71 | | const T* WaitFor(const MonoDelta& delta) const { |
72 | | if (latch_.WaitFor(delta)) { |
73 | | return &val_; |
74 | | } else { |
75 | | return NULL; |
76 | | } |
77 | | } |
78 | | |
79 | | // Set the value of this promise. |
80 | | // This may be called at most once. |
81 | 160k | void Set(const T& val) { |
82 | 160k | DCHECK_EQ(latch_.count(), 1) << "Already set!"0 ; |
83 | 160k | val_ = val; |
84 | 160k | latch_.CountDown(); |
85 | 160k | } |
86 | | |
87 | | private: |
88 | | CountDownLatch latch_; |
89 | | T val_; |
90 | | DISALLOW_COPY_AND_ASSIGN(Promise); |
91 | | }; |
92 | | |
93 | | } // namespace yb |
94 | | #endif /* YB_UTIL_PROMISE_H */ |