OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef MOJO_PUBLIC_PYTHON_SRC_COMMON_H | |
6 #define MOJO_PUBLIC_PYTHON_SRC_COMMON_H | |
7 | |
8 #include <Python.h> | |
9 | |
10 #include <map> | |
11 | |
12 #include "mojo/public/c/environment/async_waiter.h" | |
13 #include "mojo/public/cpp/bindings/callback.h" | |
14 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | |
15 #include "mojo/public/cpp/system/core.h" | |
16 | |
17 namespace mojo { | |
18 namespace python { | |
19 class ScopedGIL { | |
20 public: | |
21 ScopedGIL(); | |
22 | |
23 ~ScopedGIL(); | |
24 | |
25 private: | |
26 PyGILState_STATE state_; | |
27 | |
28 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL); | |
29 }; | |
30 | |
31 enum ScopedPyRefAcquire { | |
32 kAcquire, | |
33 }; | |
34 | |
35 class ScopedPyRef { | |
36 public: | |
37 ScopedPyRef(PyObject* object); | |
38 ScopedPyRef(PyObject* object, ScopedPyRefAcquire); | |
39 | |
40 ~ScopedPyRef(); | |
41 | |
42 operator PyObject*() const; | |
43 | |
44 private: | |
45 PyObject* object_; | |
46 | |
47 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedPyRef); | |
48 }; | |
49 | |
50 | |
51 class PythonClosure : public mojo::Closure::Runnable { | |
52 public: | |
53 PythonClosure(PyObject* callable, const mojo::Closure& quit); | |
54 ~PythonClosure(); | |
55 | |
56 void Run() const override; | |
57 | |
58 private: | |
59 ScopedPyRef callable_; | |
60 const mojo::Closure quit_; | |
61 | |
62 MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure); | |
63 }; | |
64 | |
65 // Create a mojo::Closure from a callable python object. | |
66 Closure::Runnable* NewRunnable(PyObject* callable, const Closure& quit_closure); | |
qsr
2014/12/10 15:38:57
I'm at fault, but this name is not very good -> wa
etiennej
2014/12/11 09:52:52
Done.
| |
67 | |
68 // AsyncWaiter for python, used to execute a python closure after waiting. If an | |
69 // error occurs while executing the closure, the current message loop will be | |
70 // exited. See |AsyncWaiter| in mojo/public/c/environment/async_waiter.h for | |
71 // more details. | |
72 class PythonAsyncWaiter { | |
73 public: | |
74 PythonAsyncWaiter(const mojo::Closure& quit_closure); | |
75 ~PythonAsyncWaiter(); | |
76 MojoAsyncWaitID AsyncWait(MojoHandle handle, | |
77 MojoHandleSignals signals, | |
78 MojoDeadline deadline, | |
79 PyObject* callable); | |
80 | |
81 void CancelWait(MojoAsyncWaitID wait_id); | |
82 | |
83 private: | |
84 class AsyncWaiterRunnable; | |
85 | |
86 typedef std::map<MojoAsyncWaitID, | |
87 internal::SharedPtr<mojo::Callback<void(MojoResult)> > > | |
88 CallbackMap; | |
89 | |
90 CallbackMap callbacks_; | |
91 const MojoAsyncWaiter* async_waiter_; | |
92 const mojo::Closure& quit_; | |
93 }; | |
94 | |
95 } // namespace python | |
96 } // namespace mojo | |
97 | |
98 #endif // MOJO_PUBLIC_PYTHON_SRC_COMMON_H_ | |
99 | |
OLD | NEW |