Home | History | Annotate | Download | only in host
      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 method definitions for HostStream, the Stream implementation for
     17 // the HostExecutor implementation.
     18 #include "tensorflow/stream_executor/host/host_stream.h"
     19 
     20 namespace perftools {
     21 namespace gputools {
     22 namespace host {
     23 
     24 HostStream::HostStream()
     25     : host_executor_(new port::ThreadPool(port::Env::Default(),
     26                                           port::ThreadOptions(),
     27                                           "host_executor", kExecutorThreads)) {}
     28 
     29 HostStream::~HostStream() {}
     30 
     31 bool HostStream::EnqueueTask(std::function<void()> task) {
     32   {
     33     mutex_lock lock(mu_);
     34     ++pending_tasks_;
     35   }
     36   host_executor_->Schedule([this, task]() {
     37     task();
     38     {
     39       mutex_lock lock(mu_);
     40       --pending_tasks_;
     41     }
     42     completion_condition_.notify_all();
     43   });
     44   return true;
     45 }
     46 
     47 void HostStream::BlockUntilDone() {
     48   mutex_lock lock(mu_);
     49   while (pending_tasks_ != 0) {
     50     completion_condition_.wait(lock);
     51   }
     52 }
     53 
     54 }  // namespace host
     55 
     56 }  // namespace gputools
     57 }  // namespace perftools
     58