YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
89.6k
  Promise() : latch_(1) {}
_ZN2yb7PromiseINS_6StatusEEC2Ev
Line
Count
Source
48
89.6k
  Promise() : latch_(1) {}
_ZN2yb7PromiseIiEC2Ev
Line
Count
Source
48
1
  Promise() : latch_(1) {}
49
47.9k
  ~Promise() {}
_ZN2yb7PromiseINS_6StatusEED2Ev
Line
Count
Source
49
47.9k
  ~Promise() {}
_ZN2yb7PromiseIiED2Ev
Line
Count
Source
49
1
  ~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
96.9k
  void Reset() {
56
96.9k
    latch_.Reset(1);
57
96.9k
    val_ = T();
58
96.9k
  }
59
60
  // Block until a value is available, and return a reference to it.
61
97.0k
  const T& Get() const {
62
97.0k
    latch_.Wait();
63
97.0k
    return val_;
64
97.0k
  }
_ZNK2yb7PromiseINS_6StatusEE3GetEv
Line
Count
Source
61
97.0k
  const T& Get() const {
62
97.0k
    latch_.Wait();
63
97.0k
    return val_;
64
97.0k
  }
_ZNK2yb7PromiseIiE3GetEv
Line
Count
Source
61
1
  const T& Get() const {
62
1
    latch_.Wait();
63
1
    return val_;
64
1
  }
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
97.0k
  void Set(const T& val) {
82
0
    DCHECK_EQ(latch_.count(), 1) << "Already set!";
83
97.0k
    val_ = val;
84
97.0k
    latch_.CountDown();
85
97.0k
  }
_ZN2yb7PromiseINS_6StatusEE3SetERKS1_
Line
Count
Source
81
97.0k
  void Set(const T& val) {
82
0
    DCHECK_EQ(latch_.count(), 1) << "Already set!";
83
97.0k
    val_ = val;
84
97.0k
    latch_.CountDown();
85
97.0k
  }
_ZN2yb7PromiseIiE3SetERKi
Line
Count
Source
81
1
  void Set(const T& val) {
82
0
    DCHECK_EQ(latch_.count(), 1) << "Already set!";
83
1
    val_ = val;
84
1
    latch_.CountDown();
85
1
  }
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 */