TensorFlow Serving C++ API Documentation
tensorflow_serving
batching
incremental_barrier.h
1
/* Copyright 2020 Google Inc. All Rights Reserved.
2
3
Licensed under the Apache License, Version 2.0 (the "License");
4
you may not use this file except in compliance with the License.
5
You may obtain a copy of the License at
6
7
http://www.apache.org/licenses/LICENSE-2.0
8
9
Unless required by applicable law or agreed to in writing, software
10
distributed under the License is distributed on an "AS IS" BASIS,
11
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
See the License for the specific language governing permissions and
13
limitations under the License.
14
==============================================================================*/
15
16
#ifndef TENSORFLOW_CORE_KERNELS_BATCHING_UTIL_INCREMENTAL_BARRIER_H_
17
#define TENSORFLOW_CORE_KERNELS_BATCHING_UTIL_INCREMENTAL_BARRIER_H_
18
19
#include <atomic>
20
#include <functional>
21
22
namespace
tensorflow {
23
namespace
serving {
24
25
class
InternalIncrementalBarrier;
26
27
// BarrierClosure (see
28
// https://github.com/chromium/chromium/blob/master/base/barrier_closure.h)
29
// executes a callback after it has been invoked |num_closures| times.
30
// Plus, `BarrierClosure` is a continuation-passing style abstraction and self-
31
// deleting.
32
33
// IncrementalBarrier is a convenience class to be used in place of a barrier
34
// closure, which is particularly helpful (e.g. simplify code) because callers
35
// don't need to calculate the |num_closures| beforehand.
36
//
37
// Example Usage:
38
// void MakeCalls() {
39
// typedef std::function<void()> Callback;
40
// typedef std::function<void(Callback)> OtherCallback;
41
// Callback done_callback = ...
42
// OtherCallback cb1 = ...
43
// OtherCallback cb2 = ...
44
// std::thread threads[2];
45
// {
46
// IncrementalBarrier barrier(done_callback);
47
// threads[0] = std::thread(cb1(barrier.Inc());
48
// threads[1] = std::thread(cb2(barrier.Inc());
49
// ... at this moment, `barrier` is incremented twice, and then
50
// destructed....
51
// }
52
// threads[0].join();
53
// threads[1].join();
54
// }
55
//
56
// `done_callback` will be called when both conditions are true:
57
// 1) after `barrier` is destructed.
58
// 2) Each `BarrierCallback` returned by `Inc` is called.
59
// This class is thread-safe.
60
class
IncrementalBarrier
{
61
public
:
62
typedef
std::function<void()> DoneCallback;
63
typedef
std::function<void()> BarrierCallback;
64
explicit
IncrementalBarrier
(DoneCallback callback);
65
66
~
IncrementalBarrier
();
67
68
// Returns a BarrierCallback (std::function) that individual task call to
69
// signal its completeness.
70
// The returned BarrierCallback outlives this `IncrementalBarrier` instance.
71
// Furthermore, each task should eventually call the returned function, or
72
// else done_callback wouldn't be called.
73
BarrierCallback Inc();
74
75
private
:
76
// self-deleting, thereby not owned by 'IncrementalBarrier'.
77
InternalIncrementalBarrier
* internal_barrier_;
78
};
79
80
}
// namespace serving
81
}
// namespace tensorflow
82
83
#endif
// TENSORFLOW_CORE_KERNELS_BATCHING_UTIL_INCREMENTAL_BARRIER_H_
tensorflow::serving::IncrementalBarrier
Definition:
incremental_barrier.h:60
tensorflow::serving::InternalIncrementalBarrier
Definition:
incremental_barrier.cc:28
Generated by
1.9.1