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