OLD | NEW |
| (Empty) |
1 // Copyright 2013 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. | |
4 | |
5 #include "mojo/public/python/src/common.h" | |
6 | |
7 #include <Python.h> | |
8 | |
9 #include "mojo/public/c/environment/async_waiter.h" | |
10 #include "mojo/public/cpp/bindings/callback.h" | |
11 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | |
12 #include "mojo/public/cpp/environment/logging.h" | |
13 #include "mojo/public/cpp/system/core.h" | |
14 #include "mojo/public/cpp/system/macros.h" | |
15 #include "mojo/public/cpp/utility/run_loop.h" | |
16 | |
17 namespace { | |
18 | |
19 void AsyncCallbackForwarder(void* closure, MojoResult result) { | |
20 mojo::Callback<void(MojoResult)>* callback = | |
21 static_cast<mojo::Callback<void(MojoResult)>*>(closure); | |
22 // callback will be deleted when it is run. | |
23 callback->Run(result); | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 namespace mojo { | |
29 namespace python { | |
30 | |
31 ScopedGIL::ScopedGIL() { | |
32 state_ = PyGILState_Ensure(); | |
33 } | |
34 | |
35 ScopedGIL::~ScopedGIL() { | |
36 PyGILState_Release(state_); | |
37 } | |
38 | |
39 ScopedPyRef::ScopedPyRef(PyObject* object) : object_(object) { | |
40 } | |
41 | |
42 ScopedPyRef::ScopedPyRef(PyObject* object, ScopedPyRefAcquire) | |
43 : object_(object) { | |
44 Py_XINCREF(object_); | |
45 } | |
46 | |
47 ScopedPyRef::~ScopedPyRef() { | |
48 if (object_) { | |
49 ScopedGIL acquire_gil; | |
50 Py_DECREF(object_); | |
51 } | |
52 } | |
53 | |
54 ScopedPyRef::operator PyObject*() const { | |
55 return object_; | |
56 } | |
57 | |
58 PythonClosure::PythonClosure(PyObject* callable, const mojo::Closure& quit) | |
59 : callable_(callable, kAcquire), quit_(quit) { | |
60 MOJO_DCHECK(callable); | |
61 } | |
62 | |
63 PythonClosure::~PythonClosure() {} | |
64 | |
65 void PythonClosure::Run() const { | |
66 ScopedGIL acquire_gil; | |
67 ScopedPyRef empty_tuple(PyTuple_New(0)); | |
68 if (!empty_tuple) { | |
69 quit_.Run(); | |
70 return; | |
71 } | |
72 | |
73 ScopedPyRef result(PyObject_CallObject(callable_, empty_tuple)); | |
74 if (!result) { | |
75 quit_.Run(); | |
76 return; | |
77 } | |
78 } | |
79 | |
80 Closure::Runnable* NewRunnableFromCallable(PyObject* callable, | |
81 const mojo::Closure& quit_closure) { | |
82 MOJO_DCHECK(PyCallable_Check(callable)); | |
83 | |
84 return new PythonClosure(callable, quit_closure); | |
85 } | |
86 | |
87 class PythonAsyncWaiter::AsyncWaiterRunnable | |
88 : public mojo::Callback<void(MojoResult)>::Runnable { | |
89 public: | |
90 AsyncWaiterRunnable(PyObject* callable, | |
91 CallbackMap* callbacks, | |
92 const mojo::Closure& quit) | |
93 : wait_id_(0), | |
94 callable_(callable, kAcquire), | |
95 callbacks_(callbacks), | |
96 quit_(quit) { | |
97 MOJO_DCHECK(callable_); | |
98 MOJO_DCHECK(callbacks_); | |
99 } | |
100 | |
101 void set_wait_id(MojoAsyncWaitID wait_id) { wait_id_ = wait_id; } | |
102 | |
103 void Run(MojoResult mojo_result) const override { | |
104 MOJO_DCHECK(wait_id_); | |
105 | |
106 // Remove to reference to this object from PythonAsyncWaiter and ensure this | |
107 // object will be destroyed when this method exits. | |
108 MOJO_DCHECK(callbacks_->find(wait_id_) != callbacks_->end()); | |
109 internal::SharedPtr<mojo::Callback<void(MojoResult)>> self = | |
110 (*callbacks_)[wait_id_]; | |
111 callbacks_->erase(wait_id_); | |
112 | |
113 ScopedGIL acquire_gil; | |
114 ScopedPyRef args_tuple(Py_BuildValue("(i)", mojo_result)); | |
115 if (!args_tuple) { | |
116 quit_.Run(); | |
117 return; | |
118 } | |
119 | |
120 ScopedPyRef result(PyObject_CallObject(callable_, args_tuple)); | |
121 if (!result) { | |
122 quit_.Run(); | |
123 return; | |
124 } | |
125 } | |
126 | |
127 private: | |
128 MojoAsyncWaitID wait_id_; | |
129 ScopedPyRef callable_; | |
130 CallbackMap* callbacks_; | |
131 const mojo::Closure quit_; | |
132 | |
133 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable); | |
134 }; | |
135 | |
136 PythonAsyncWaiter::PythonAsyncWaiter(const mojo::Closure& quit_closure) | |
137 : quit_(quit_closure) { | |
138 async_waiter_ = Environment::GetDefaultAsyncWaiter(); | |
139 } | |
140 | |
141 PythonAsyncWaiter::~PythonAsyncWaiter() { | |
142 for (CallbackMap::const_iterator it = callbacks_.begin(); | |
143 it != callbacks_.end(); | |
144 ++it) { | |
145 async_waiter_->CancelWait(it->first); | |
146 } | |
147 } | |
148 | |
149 MojoAsyncWaitID PythonAsyncWaiter::AsyncWait(MojoHandle handle, | |
150 MojoHandleSignals signals, | |
151 MojoDeadline deadline, | |
152 PyObject* callable) { | |
153 AsyncWaiterRunnable* runner = | |
154 new AsyncWaiterRunnable(callable, &callbacks_, quit_); | |
155 internal::SharedPtr<mojo::Callback<void(MojoResult)>> callback( | |
156 new mojo::Callback<void(MojoResult)>( | |
157 static_cast<mojo::Callback<void(MojoResult)>::Runnable*>(runner))); | |
158 MojoAsyncWaitID wait_id = async_waiter_->AsyncWait( | |
159 handle, signals, deadline, &AsyncCallbackForwarder, callback.get()); | |
160 callbacks_[wait_id] = callback; | |
161 runner->set_wait_id(wait_id); | |
162 return wait_id; | |
163 } | |
164 | |
165 void PythonAsyncWaiter::CancelWait(MojoAsyncWaitID wait_id) { | |
166 if (callbacks_.find(wait_id) != callbacks_.end()) { | |
167 async_waiter_->CancelWait(wait_id); | |
168 callbacks_.erase(wait_id); | |
169 } | |
170 } | |
171 | |
172 } // namespace python | |
173 } // namespace mojo | |
OLD | NEW |