OLD | NEW |
---|---|
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 Loading... | |
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 internal::SharedPtr<mojo::Callback<void(MojoResult)> > self = | |
sdefresne
2014/09/18 08:37:01
Why don't you DCHECK that wait_id_ is in callbacks
qsr
2014/09/18 08:56:10
Done.
| |
99 (*callbacks_)[wait_id_]; | |
100 callbacks_->erase(wait_id_); | |
101 | |
102 ScopedGIL acquire_gil; | |
103 PyObject* empty_tuple = Py_BuildValue("(i)", mojo_result); | |
sdefresne
2014/09/18 08:37:00
nit: empty_tuple is not empty, maybe rename to arg
qsr
2014/09/18 08:56:10
Done.
| |
104 if (!empty_tuple) { | |
105 mojo::RunLoop::current()->Quit(); | |
106 return; | |
107 } | |
108 | |
109 PyObject* result = PyObject_CallObject(callable_, empty_tuple); | |
110 Py_DECREF(empty_tuple); | |
111 if (result) { | |
112 Py_DECREF(result); | |
113 } else { | |
114 mojo::RunLoop::current()->Quit(); | |
115 return; | |
116 } | |
117 } | |
118 | |
119 private: | |
120 MojoAsyncWaitID wait_id_; | |
121 PyObject* callable_; | |
122 CallbackMap* callbacks_; | |
123 | |
124 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable); | |
125 }; | |
70 | 126 |
71 Closure BuildClosure(PyObject* callable) { | 127 Closure BuildClosure(PyObject* callable) { |
72 if (!PyCallable_Check(callable)) | 128 if (!PyCallable_Check(callable)) |
73 return Closure(); | 129 return Closure(); |
74 | 130 |
75 return Closure( | 131 return Closure( |
76 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable))); | 132 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable))); |
77 } | 133 } |
78 | 134 |
135 PythonAsyncWaiter::PythonAsyncWaiter() { | |
136 async_waiter_ = Environment::GetDefaultAsyncWaiter(); | |
137 } | |
138 | |
139 PythonAsyncWaiter::~PythonAsyncWaiter() { | |
140 for (CallbackMap::const_iterator it = callbacks_.begin(); | |
141 it != callbacks_.end(); | |
142 ++it) { | |
143 async_waiter_->CancelWait(it->first); | |
144 } | |
145 } | |
146 | |
147 MojoAsyncWaitID PythonAsyncWaiter::AsyncWait(MojoHandle handle, | |
148 MojoHandleSignals signals, | |
149 MojoDeadline deadline, | |
150 PyObject* callable) { | |
151 AsyncWaiterRunnable* runner = new AsyncWaiterRunnable(callable, &callbacks_); | |
152 internal::SharedPtr<mojo::Callback<void(MojoResult)> > callback( | |
153 new mojo::Callback<void(MojoResult)>( | |
154 static_cast<mojo::Callback<void(MojoResult)>::Runnable*>(runner))); | |
155 MojoAsyncWaitID wait_id = async_waiter_->AsyncWait( | |
156 handle, signals, deadline, &AsyncCallbackForwarder, callback.get()); | |
157 callbacks_[wait_id] = callback; | |
158 runner->set_wait_id(wait_id); | |
159 return wait_id; | |
160 } | |
161 | |
162 void PythonAsyncWaiter::CancelWait(MojoAsyncWaitID wait_id) { | |
163 if (callbacks_.find(wait_id) != callbacks_.end()) { | |
164 async_waiter_->CancelWait(wait_id); | |
165 callbacks_.erase(wait_id); | |
166 } | |
167 } | |
168 | |
169 } // namespace python | |
79 } // namespace mojo | 170 } // namespace mojo |
OLD | NEW |