proxygen
folly::python::GILAwareManualExecutor Class Reference

#include <GILAwareManualExecutor.h>

Inheritance diagram for folly::python::GILAwareManualExecutor:
folly::DrivableExecutor folly::SequencedExecutor folly::Executor folly::Executor

Public Member Functions

void add (Func) override
 
void drive () override
 
- Public Member Functions inherited from folly::DrivableExecutor
 ~DrivableExecutor () override=default
 
- Public Member Functions inherited from folly::Executor
virtual ~Executor ()
 
virtual void addWithPriority (Func, int8_t priority)
 
virtual uint8_t getNumPriorities () const
 

Private Member Functions

void waitBeforeDrive ()
 
void driveImpl ()
 

Private Attributes

std::mutex lock_
 
std::queue< Funcfuncs_
 
std::condition_variable cv_
 

Additional Inherited Members

- Static Public Member Functions inherited from folly::Executor
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT *executor)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT &executor)
 
- Static Public Attributes inherited from folly::Executor
static const int8_t LO_PRI = SCHAR_MIN
 
static const int8_t MID_PRI = 0
 
static const int8_t HI_PRI = SCHAR_MAX
 
- Protected Member Functions inherited from folly::Executor
virtual bool keepAliveAcquire ()
 
virtual void keepAliveRelease ()
 
- Static Protected Member Functions inherited from folly::Executor
template<typename ExecutorT >
static bool isKeepAliveDummy (const KeepAlive< ExecutorT > &keepAlive)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > makeKeepAlive (ExecutorT *executor)
 

Detailed Description

A simple ManualExecutor intended to be run directly on a Python thread. It releases Python GIL while waiting for tasks to execute.

Definition at line 34 of file GILAwareManualExecutor.h.

Member Function Documentation

void folly::python::GILAwareManualExecutor::add ( Func  )
overridevirtual

Enqueue a function to executed by this executor. This and all variants must be threadsafe.

Implements folly::Executor.

Definition at line 26 of file GILAwareManualExecutor.cpp.

References cv_, funcs_, folly::lock(), lock_, and folly::gen::move.

26  {
27  {
28  std::lock_guard<std::mutex> lock(lock_);
29  funcs_.emplace(std::move(callback));
30  }
31  cv_.notify_one();
32 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
void folly::python::GILAwareManualExecutor::drive ( )
inlineoverridevirtual
void folly::python::GILAwareManualExecutor::driveImpl ( )
private

Definition at line 51 of file GILAwareManualExecutor.cpp.

References funcs_, folly::lock(), lock_, and folly::gen::move.

Referenced by drive().

51  {
52  Func func;
53  while (true) {
54  {
55  std::lock_guard<std::mutex> lock(lock_);
56  if (funcs_.empty()) {
57  break;
58  }
59 
60  func = std::move(funcs_.front());
61  funcs_.pop();
62  }
63  func();
64  }
65 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Function< void()> Func
Definition: Executor.h:27
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
void folly::python::GILAwareManualExecutor::waitBeforeDrive ( )
private

Definition at line 34 of file GILAwareManualExecutor.cpp.

References cv_, funcs_, folly::lock(), lock_, and SCOPE_EXIT.

Referenced by drive().

34  {
35  std::unique_lock<std::mutex> lock(lock_);
36  if (!funcs_.empty()) {
37  return;
38  }
39 
40  // Release GIL before waiting on lock
41  auto* pyThreadState = PyEval_SaveThread();
42  SCOPE_EXIT {
43  // Release lock before re-acquiring GIL,
44  // to avoid deadlock if another GIL-owning thread is calling add
45  lock.unlock();
46  PyEval_RestoreThread(pyThreadState);
47  };
48  cv_.wait(lock, [&] { return !funcs_.empty(); });
49 }
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
auto lock(Synchronized< D, M > &synchronized, Args &&...args)

Member Data Documentation

std::condition_variable folly::python::GILAwareManualExecutor::cv_
private

Definition at line 50 of file GILAwareManualExecutor.h.

Referenced by add(), and waitBeforeDrive().

std::queue<Func> folly::python::GILAwareManualExecutor::funcs_
private

Definition at line 49 of file GILAwareManualExecutor.h.

Referenced by add(), driveImpl(), and waitBeforeDrive().

std::mutex folly::python::GILAwareManualExecutor::lock_
private

Definition at line 48 of file GILAwareManualExecutor.h.

Referenced by add(), driveImpl(), and waitBeforeDrive().


The documentation for this class was generated from the following files: