Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Side by Side Diff: mojo/public/python/src/python_system_helper.cc

Issue 573253002: mojo: Add async waiter for python bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compilation due to pre-processor variable Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/python/src/python_system_helper.h ('k') | mojo/python/tests/async_wait_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/python/src/python_system_helper.h" 5 #include "mojo/public/python/src/python_system_helper.h"
6 6
7 #include "Python.h" 7 #include "Python.h"
8 8
9 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/cpp/environment/logging.h" 10 #include "mojo/public/cpp/environment/logging.h"
10 #include "mojo/public/cpp/system/macros.h" 11 #include "mojo/public/cpp/system/macros.h"
11 #include "mojo/public/cpp/utility/run_loop.h" 12 #include "mojo/public/cpp/utility/run_loop.h"
12 13
13 namespace { 14 namespace {
14 15
15 class ScopedGIL { 16 class ScopedGIL {
16 public: 17 public:
17 ScopedGIL() { 18 ScopedGIL() { state_ = PyGILState_Ensure(); }
18 state_ = PyGILState_Ensure();
19 }
20 19
21 ~ScopedGIL() { 20 ~ScopedGIL() { PyGILState_Release(state_); }
22 PyGILState_Release(state_);
23 }
24 21
25 private: 22 private:
26 PyGILState_STATE state_; 23 PyGILState_STATE state_;
27 24
28 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL); 25 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL);
29 }; 26 };
30 27
31 class PythonClosure : public mojo::Closure::Runnable { 28 class PythonClosure : public mojo::Closure::Runnable {
32 public: 29 public:
33 PythonClosure(PyObject* callable) : callable_(callable) { 30 PythonClosure(PyObject* callable) : callable_(callable) {
34 MOJO_CHECK(callable); 31 MOJO_DCHECK(callable);
35 Py_XINCREF(callable); 32 Py_XINCREF(callable);
36 } 33 }
37 34
38 virtual ~PythonClosure() { 35 virtual ~PythonClosure() {
39 ScopedGIL acquire_gil; 36 ScopedGIL acquire_gil;
40 Py_DECREF(callable_); 37 Py_DECREF(callable_);
41 } 38 }
42 39
43 virtual void Run() const MOJO_OVERRIDE { 40 virtual void Run() const MOJO_OVERRIDE {
44 ScopedGIL acquire_gil; 41 ScopedGIL acquire_gil;
(...skipping 12 matching lines...) Expand all
57 return; 54 return;
58 } 55 }
59 } 56 }
60 57
61 private: 58 private:
62 PyObject* callable_; 59 PyObject* callable_;
63 60
64 MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure); 61 MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure);
65 }; 62 };
66 63
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);
69 }
70
67 } // namespace 71 } // namespace
68 72
69 namespace mojo { 73 namespace mojo {
74 namespace python {
75
76 class PythonAsyncWaiter::AsyncWaiterRunnable
77 : public mojo::Callback<void(MojoResult)>::Runnable {
78 public:
79 AsyncWaiterRunnable(PyObject* callable, CallbackMap* callbacks)
80 : wait_id_(0), callable_(callable), callbacks_(callbacks) {
81 MOJO_DCHECK(callable);
82 MOJO_DCHECK(callbacks_);
83 Py_XINCREF(callable);
84 }
85
86 virtual ~AsyncWaiterRunnable() {
87 ScopedGIL acquire_gil;
88 Py_DECREF(callable_);
89 }
90
91 void set_wait_id(int wait_id) { wait_id_ = wait_id; }
92
93 virtual void Run(MojoResult mojo_result) const MOJO_OVERRIDE {
94 MOJO_DCHECK(wait_id_);
95
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_);
102
103 ScopedGIL acquire_gil;
104 PyObject* args_tuple = Py_BuildValue("(i)", mojo_result);
105 if (!args_tuple) {
106 mojo::RunLoop::current()->Quit();
107 return;
108 }
109
110 PyObject* result = PyObject_CallObject(callable_, args_tuple);
111 Py_DECREF(args_tuple);
112 if (result) {
113 Py_DECREF(result);
114 } else {
115 mojo::RunLoop::current()->Quit();
116 return;
117 }
118 }
119
120 private:
121 MojoAsyncWaitID wait_id_;
122 PyObject* callable_;
123 CallbackMap* callbacks_;
124
125 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable);
126 };
70 127
71 Closure BuildClosure(PyObject* callable) { 128 Closure BuildClosure(PyObject* callable) {
72 if (!PyCallable_Check(callable)) 129 if (!PyCallable_Check(callable))
73 return Closure(); 130 return Closure();
74 131
75 return Closure( 132 return Closure(
76 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable))); 133 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable)));
77 } 134 }
78 135
136 PythonAsyncWaiter::PythonAsyncWaiter() {
137 async_waiter_ = Environment::GetDefaultAsyncWaiter();
138 }
139
140 PythonAsyncWaiter::~PythonAsyncWaiter() {
141 for (CallbackMap::const_iterator it = callbacks_.begin();
142 it != callbacks_.end();
143 ++it) {
144 async_waiter_->CancelWait(it->first);
145 }
146 }
147
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);
160 return wait_id;
161 }
162
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);
167 }
168 }
169
170 } // namespace python
79 } // namespace mojo 171 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/python/src/python_system_helper.h ('k') | mojo/python/tests/async_wait_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698