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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 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/common.h ('k') | mojo/public/python/src/python_system_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <mojo/environment/async_waiter.h>
8 #include <Python.h>
9
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/macros.h"
14 #include "mojo/public/cpp/utility/run_loop.h"
15
16 namespace {
17
18 void AsyncCallbackForwarder(void* closure, MojoResult result) {
19 mojo::Callback<void(MojoResult)>* callback =
20 static_cast<mojo::Callback<void(MojoResult)>*>(closure);
21 // callback will be deleted when it is run.
22 callback->Run(result);
23 }
24
25 } // namespace
26
27 namespace mojo {
28 namespace python {
29
30 ScopedGIL::ScopedGIL() {
31 state_ = PyGILState_Ensure();
32 }
33
34 ScopedGIL::~ScopedGIL() {
35 PyGILState_Release(state_);
36 }
37
38 ScopedPyRef::ScopedPyRef(PyObject* object) : object_(object) {
39 }
40
41 ScopedPyRef::ScopedPyRef(PyObject* object, ScopedPyRefAcquire)
42 : object_(object) {
43 if (object_)
44 Py_XINCREF(object_);
45 }
46
47 ScopedPyRef::ScopedPyRef(const ScopedPyRef& other)
48 : ScopedPyRef(other, kAcquire) {
49 }
50
51 PyObject* ScopedPyRef::Release() {
52 PyObject* object = object_;
53 object_ = nullptr;
54 return object;
55 }
56
57 ScopedPyRef::~ScopedPyRef() {
58 if (object_) {
59 ScopedGIL acquire_gil;
60 Py_DECREF(object_);
61 }
62 }
63
64 ScopedPyRef& ScopedPyRef::operator=(const ScopedPyRef& other) {
65 if (other)
66 Py_XINCREF(other);
67 PyObject* old = object_;
68 object_ = other;
69 if (old)
70 Py_DECREF(old);
71 return *this;
72 }
73
74 PythonClosure::PythonClosure(PyObject* callable, const mojo::Closure& quit)
75 : callable_(callable, kAcquire), quit_(quit) {
76 MOJO_DCHECK(callable);
77 }
78
79 PythonClosure::~PythonClosure() {}
80
81 void PythonClosure::Run() const {
82 ScopedGIL acquire_gil;
83 ScopedPyRef empty_tuple(PyTuple_New(0));
84 if (!empty_tuple) {
85 quit_.Run();
86 return;
87 }
88
89 ScopedPyRef result(PyObject_CallObject(callable_, empty_tuple));
90 if (!result) {
91 quit_.Run();
92 return;
93 }
94 }
95
96 Closure::Runnable* NewRunnableFromCallable(PyObject* callable,
97 const mojo::Closure& quit_closure) {
98 MOJO_DCHECK(PyCallable_Check(callable));
99
100 return new PythonClosure(callable, quit_closure);
101 }
102
103 class PythonAsyncWaiter::AsyncWaiterRunnable
104 : public mojo::Callback<void(MojoResult)>::Runnable {
105 public:
106 AsyncWaiterRunnable(PyObject* callable,
107 CallbackMap* callbacks,
108 const mojo::Closure& quit)
109 : wait_id_(0),
110 callable_(callable, kAcquire),
111 callbacks_(callbacks),
112 quit_(quit) {
113 MOJO_DCHECK(callable_);
114 MOJO_DCHECK(callbacks_);
115 }
116
117 void set_wait_id(MojoAsyncWaitID wait_id) { wait_id_ = wait_id; }
118
119 void Run(MojoResult mojo_result) const override {
120 MOJO_DCHECK(wait_id_);
121
122 // Remove to reference to this object from PythonAsyncWaiter and ensure this
123 // object will be destroyed when this method exits.
124 MOJO_DCHECK(callbacks_->find(wait_id_) != callbacks_->end());
125 internal::SharedPtr<mojo::Callback<void(MojoResult)>> self =
126 (*callbacks_)[wait_id_];
127 callbacks_->erase(wait_id_);
128
129 ScopedGIL acquire_gil;
130 ScopedPyRef args_tuple(Py_BuildValue("(i)", mojo_result));
131 if (!args_tuple) {
132 quit_.Run();
133 return;
134 }
135
136 ScopedPyRef result(PyObject_CallObject(callable_, args_tuple));
137 if (!result) {
138 quit_.Run();
139 return;
140 }
141 }
142
143 private:
144 MojoAsyncWaitID wait_id_;
145 ScopedPyRef callable_;
146 CallbackMap* callbacks_;
147 const mojo::Closure quit_;
148
149 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable);
150 };
151
152 PythonAsyncWaiter::PythonAsyncWaiter(const mojo::Closure& quit_closure)
153 : quit_(quit_closure) {
154 async_waiter_ = Environment::GetDefaultAsyncWaiter();
155 }
156
157 PythonAsyncWaiter::~PythonAsyncWaiter() {
158 for (CallbackMap::const_iterator it = callbacks_.begin();
159 it != callbacks_.end();
160 ++it) {
161 async_waiter_->CancelWait(it->first);
162 }
163 }
164
165 MojoAsyncWaitID PythonAsyncWaiter::AsyncWait(MojoHandle handle,
166 MojoHandleSignals signals,
167 MojoDeadline deadline,
168 PyObject* callable) {
169 AsyncWaiterRunnable* runner =
170 new AsyncWaiterRunnable(callable, &callbacks_, quit_);
171 internal::SharedPtr<mojo::Callback<void(MojoResult)>> callback(
172 new mojo::Callback<void(MojoResult)>(
173 static_cast<mojo::Callback<void(MojoResult)>::Runnable*>(runner)));
174 MojoAsyncWaitID wait_id = async_waiter_->AsyncWait(
175 handle, signals, deadline, &AsyncCallbackForwarder, callback.get());
176 callbacks_[wait_id] = callback;
177 runner->set_wait_id(wait_id);
178 return wait_id;
179 }
180
181 void PythonAsyncWaiter::CancelWait(MojoAsyncWaitID wait_id) {
182 if (callbacks_.find(wait_id) != callbacks_.end()) {
183 async_waiter_->CancelWait(wait_id);
184 callbacks_.erase(wait_id);
185 }
186 }
187
188 } // namespace python
189 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/python/src/common.h ('k') | mojo/public/python/src/python_system_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698