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