unbuffered_channel.h 5.5 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
16
#include <atomic>
17 18 19 20 21 22 23 24 25
#include <condition_variable>
#include <mutex>

#include "paddle/framework/channel.h"

namespace paddle {
namespace framework {
namespace details {

26 27 28 29 30 31 32
// Four of the properties of UnBuffered Channel:
// - A send to a channel blocks temporarily until a receive from the
// channel or the channel is closed.
// - A receive from a channel blocks temporarily until a send to the
// channel or the channel is closed.
// - A send to a closed channel returns false immediately.
// - A receive from a closed channel returns false immediately.
33 34 35 36 37 38
template <typename T>
class UnBuffered : public paddle::framework::Channel<T> {
  friend Channel<T>* paddle::framework::MakeChannel<T>(size_t);
  friend void paddle::framework::CloseChannel<T>(Channel<T>*);

 public:
C
chengduo 已提交
39 40
  virtual bool Send(T*);
  virtual bool Receive(T*);
41
  virtual size_t Cap() { return 0; }
C
chengduo 已提交
42 43
  virtual void Close();
  virtual ~UnBuffered();
44 45

 private:
46 47 48 49 50 51 52 53 54
  std::mutex mu_ch_;
  // Mutex for readers and writers who are waiting for other reader
  // and writer to complete execution
  std::recursive_mutex mu_read_, mu_write_;
  // reader_found_ is set true when a reader is ready to accept data
  // writer_found_ is set true when a writer is ready to send data
  // A transaction occurs only when both are true
  std::atomic<bool> reader_found_{false}, writer_found_{false};
  std::condition_variable cv_channel_;
55
  std::condition_variable_any cv_reader_, cv_writer_, cv_destructor_;
56 57
  T* item{nullptr};
  std::atomic<bool> closed_{false};
58 59
  std::atomic<unsigned> send_ctr{0};
  std::atomic<unsigned> recv_ctr{0};
60 61 62 63

  UnBuffered() : closed_(false) {}

  void NotifyAllParticipants(std::unique_lock<std::mutex>*);
64 65
};

66 67 68
// This function implements the concept of how data should
// be sent from a writer to a reader.
template <typename T>
C
chengduo 已提交
69
bool UnBuffered<T>::Send(T* data) {
70 71 72 73
  bool ret = false;
  if (closed_) {
    return ret;
  }
74
  send_ctr++;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  // Prevent other writers from entering
  std::unique_lock<std::recursive_mutex> writer_lock(mu_write_);
  writer_found_ = true;
  std::unique_lock<std::recursive_mutex> cv_lock(mu_write_);
  // If writer comes first, it should wait till a reader arrives
  cv_writer_.wait(cv_lock,
                  [this]() { return reader_found_ == true || closed_; });
  cv_reader_.notify_one();
  if (!closed_) {
    std::unique_lock<std::mutex> channel_lock(mu_ch_);
    item = data;
    channel_lock.unlock();
    cv_channel_.notify_one();
    channel_lock.lock();
    cv_channel_.wait(channel_lock,
                     [this]() { return item == nullptr || closed_; });
C
chengduo 已提交
91
    ret = true;
92 93
  }
  writer_found_ = false;
94 95
  send_ctr--;
  cv_destructor_.notify_one();
C
chengduo 已提交
96
  return ret;
97 98 99 100
}

// This function implements the concept of how
// data that was sent by a writer is read from a reader.
101
template <typename T>
C
chengduo 已提交
102
bool UnBuffered<T>::Receive(T* data) {
103 104 105 106 107 108
  bool ret = false;
  // If channel is closed, we don't even want any reader to enter.
  // Unlike a buffered channel, an unbuffered channel does not allow
  // readers to read after closing because there is no buffer to be consumed.
  if (closed_) return ret;
  recv_ctr++;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  // Prevent other readers from entering
  std::unique_lock<std::recursive_mutex> read_lock{mu_read_};
  reader_found_ = true;
  std::unique_lock<std::recursive_mutex> cv_lock{mu_read_};
  // If reader comes first, it should wait till a writer arrives
  cv_reader_.wait(cv_lock,
                  [this]() { return writer_found_ == true || closed_; });
  cv_writer_.notify_one();
  if (!closed_) {
    std::unique_lock<std::mutex> lock_ch{mu_ch_};
    // Reader should wait for the writer to first write its data
    cv_channel_.wait(lock_ch, [this]() { return item != nullptr || closed_; });
    if (!closed_) {
      *data = std::move(*item);
      item = nullptr;
      lock_ch.unlock();
C
chengduo 已提交
125
      ret = true;
126 127 128 129
    }
    cv_channel_.notify_one();
  }
  reader_found_ = false;
130 131
  recv_ctr--;
  cv_destructor_.notify_one();
C
chengduo 已提交
132
  return ret;
133
}
134

135 136
// This function implements the sequence of events
// that take place once the channel is closed.
137
template <typename T>
138
void UnBuffered<T>::Close() {
139 140 141
  if (closed_) {
    return;
  }
142 143 144 145 146
  std::unique_lock<std::mutex> lock(mu_ch_);
  item = nullptr;
  closed_ = true;
  NotifyAllParticipants(&lock);
}
147

148 149 150
// This function implements the sequence of events
// that are executed once the object of an UnBuffered
// channel is destroyed.
C
chengduo 已提交
151
template <typename T>
152 153 154 155 156
UnBuffered<T>::~UnBuffered() {
  std::unique_lock<std::mutex> lock(mu_ch_);
  item = nullptr;
  closed_ = true;
  NotifyAllParticipants(&lock);
157 158 159
  lock.lock();
  cv_destructor_.wait(lock,
                      [this]() { return send_ctr == 0 && recv_ctr == 0; });
160
}
C
chengduo 已提交
161

162 163
// This function notifies all the readers, writers and
// the channel condition variables.
164
template <typename T>
165 166 167 168 169 170
void UnBuffered<T>::NotifyAllParticipants(std::unique_lock<std::mutex>* lock) {
  lock->unlock();
  cv_writer_.notify_all();
  cv_channel_.notify_all();
  cv_reader_.notify_all();
}
171 172 173 174

}  // namespace details
}  // namespace framework
}  // namespace paddle