workqueue.cc 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "paddle/fluid/framework/new_executor/workqueue.h"
#include "paddle/fluid/framework/new_executor/nonblocking_threadpool.h"

namespace paddle {
namespace framework {

class SingleThreadedWorkQueue : public WorkQueue {
 public:
  SingleThreadedWorkQueue() : queue_(1) {}

  SingleThreadedWorkQueue(const SingleThreadedWorkQueue&) = delete;

  SingleThreadedWorkQueue& operator=(const SingleThreadedWorkQueue&) = delete;

  virtual ~SingleThreadedWorkQueue() = default;

  void AddTask(std::function<void()> fn) override {
    queue_.AddTask(std::move(fn));
  }

  void WaitQueueEmpty() override { queue_.WaitQueueEmpty(); }

  size_t NumThreads() override { return queue_.NumThreads(); }

 private:
  NonblockingThreadPool queue_;
};

std::unique_ptr<WorkQueue> CreateSingleThreadedWorkQueue() {
  std::unique_ptr<WorkQueue> ptr(new SingleThreadedWorkQueue);
  return std::move(ptr);
}

}  // namespace framework
}  // namespace paddle