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 #include <queue> 24 25 #include "absl/synchronization/mutex.h" 26 #include "tensorflow/stream_executor/lib/threadpool.h" 27 #include "tensorflow/stream_executor/stream_executor_internal.h" 28 29 namespace stream_executor { 30 namespace host { 31 32 class HostStream : public internal::StreamInterface { 33 public: 34 // stack_size_in_bytes may be '0', meaning "use the default thread stack 35 // size". 36 explicit HostStream(size_t stack_size_in_bytes); 37 ~HostStream() override; 38 39 bool EnqueueTask(std::function<void()> task); 40 GpuStreamHack()41 void *GpuStreamHack() override { return nullptr; } GpuStreamMemberHack()42 void **GpuStreamMemberHack() override { return nullptr; } 43 44 void BlockUntilDone(); 45 46 private: 47 bool WorkAvailable() TF_EXCLUSIVE_LOCKS_REQUIRED(mu_); 48 void WorkLoop(); 49 50 absl::Mutex mu_; 51 std::queue<std::function<void()>> work_queue_ TF_GUARDED_BY(mu_); 52 std::unique_ptr<port::Thread> thread_; 53 }; 54 55 } // namespace host 56 } // namespace stream_executor 57 58 #endif // TENSORFLOW_STREAM_EXECUTOR_HOST_HOST_STREAM_H_ 59