1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/public/python/src/python_system_helper.h"
9 #include "mojo/public/cpp/environment/environment.h"
10 #include "mojo/public/cpp/environment/logging.h"
11 #include "mojo/public/cpp/system/macros.h"
12 #include "mojo/public/cpp/utility/run_loop.h"
18 ScopedGIL() { state_ = PyGILState_Ensure(); }
20 ~ScopedGIL() { PyGILState_Release(state_); }
23 PyGILState_STATE state_;
25 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL);
28 class PythonClosure : public mojo::Closure::Runnable {
30 PythonClosure(PyObject* callable) : callable_(callable) {
31 MOJO_DCHECK(callable);
35 virtual ~PythonClosure() {
36 ScopedGIL acquire_gil;
40 virtual void Run() const MOJO_OVERRIDE {
41 ScopedGIL acquire_gil;
42 PyObject* empty_tuple = PyTuple_New(0);
44 mojo::RunLoop::current()->Quit();
48 PyObject* result = PyObject_CallObject(callable_, empty_tuple);
49 Py_DECREF(empty_tuple);
53 mojo::RunLoop::current()->Quit();
61 MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure);
64 void AsyncCallbackForwarder(void* closure, MojoResult result) {
65 mojo::Callback<void(MojoResult)>* callback =
66 static_cast<mojo::Callback<void(MojoResult)>*>(closure);
67 // callback will be deleted when it is run.
68 callback->Run(result);
76 class PythonAsyncWaiter::AsyncWaiterRunnable
77 : public mojo::Callback<void(MojoResult)>::Runnable {
79 AsyncWaiterRunnable(PyObject* callable, CallbackMap* callbacks)
80 : wait_id_(0), callable_(callable), callbacks_(callbacks) {
81 MOJO_DCHECK(callable);
82 MOJO_DCHECK(callbacks_);
86 virtual ~AsyncWaiterRunnable() {
87 ScopedGIL acquire_gil;
91 void set_wait_id(int wait_id) { wait_id_ = wait_id; }
93 virtual void Run(MojoResult mojo_result) const MOJO_OVERRIDE {
94 MOJO_DCHECK(wait_id_);
96 // Remove to reference to this object from PythonAsyncWaiter and ensure this
97 // object will be destroyed when this method exits.
98 MOJO_DCHECK(callbacks_->find(wait_id_) != callbacks_->end());
99 internal::SharedPtr<mojo::Callback<void(MojoResult)> > self =
100 (*callbacks_)[wait_id_];
101 callbacks_->erase(wait_id_);
103 ScopedGIL acquire_gil;
104 PyObject* args_tuple = Py_BuildValue("(i)", mojo_result);
106 mojo::RunLoop::current()->Quit();
110 PyObject* result = PyObject_CallObject(callable_, args_tuple);
111 Py_DECREF(args_tuple);
115 mojo::RunLoop::current()->Quit();
121 MojoAsyncWaitID wait_id_;
123 CallbackMap* callbacks_;
125 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable);
128 Closure BuildClosure(PyObject* callable) {
129 if (!PyCallable_Check(callable))
133 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable)));
136 PythonAsyncWaiter::PythonAsyncWaiter() {
137 async_waiter_ = Environment::GetDefaultAsyncWaiter();
140 PythonAsyncWaiter::~PythonAsyncWaiter() {
141 for (CallbackMap::const_iterator it = callbacks_.begin();
142 it != callbacks_.end();
144 async_waiter_->CancelWait(it->first);
148 MojoAsyncWaitID PythonAsyncWaiter::AsyncWait(MojoHandle handle,
149 MojoHandleSignals signals,
150 MojoDeadline deadline,
151 PyObject* callable) {
152 AsyncWaiterRunnable* runner = new AsyncWaiterRunnable(callable, &callbacks_);
153 internal::SharedPtr<mojo::Callback<void(MojoResult)> > callback(
154 new mojo::Callback<void(MojoResult)>(
155 static_cast<mojo::Callback<void(MojoResult)>::Runnable*>(runner)));
156 MojoAsyncWaitID wait_id = async_waiter_->AsyncWait(
157 handle, signals, deadline, &AsyncCallbackForwarder, callback.get());
158 callbacks_[wait_id] = callback;
159 runner->set_wait_id(wait_id);
163 void PythonAsyncWaiter::CancelWait(MojoAsyncWaitID wait_id) {
164 if (callbacks_.find(wait_id) != callbacks_.end()) {
165 async_waiter_->CancelWait(wait_id);
166 callbacks_.erase(wait_id);
170 } // namespace python