1 /* Copyright 2016 The TensorFlow Authors. 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 // Class declaration for Stream type that enqueues tasks onto a host/CPU-based
17 // execution context (as opposed to a GPU device), HostExecutor.
18 #ifndef TENSORFLOW_STREAM_EXECUTOR_HOST_HOST_STREAM_H_
19 #define TENSORFLOW_STREAM_EXECUTOR_HOST_HOST_STREAM_H_
20 
21 #include <functional>
22 #include <memory>
23 
24 #include "tensorflow/stream_executor/lib/threadpool.h"
25 #include "tensorflow/stream_executor/stream_executor_internal.h"
26 
27 namespace stream_executor {
28 namespace host {
29 
30 class HostStream : public internal::StreamInterface {
31  public:
32   HostStream();
33   ~HostStream() override;
34 
35   bool EnqueueTask(std::function<void()> task);
36 
GpuStreamHack()37   void *GpuStreamHack() override { return nullptr; }
GpuStreamMemberHack()38   void **GpuStreamMemberHack() override { return nullptr; }
39 
40   void BlockUntilDone();
41 
42  private:
43   // Use only one thread and own task queue to preserve FIFO ordering
44   // for the operations enqueued by any given stream.
45   static const int kExecutorThreads = 1;
46   std::unique_ptr<port::ThreadPool> host_executor_;
47 
48   mutex mu_;
49   int pending_tasks_ GUARDED_BY(mu_) = 0;
50   condition_variable completion_condition_;
51 };
52 
53 }  // namespace host
54 }  // namespace stream_executor
55 
56 #endif  // TENSORFLOW_STREAM_EXECUTOR_HOST_HOST_STREAM_H_
57