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 # distutils language = c++ | |
6 | |
7 cimport c_async_waiter | |
8 cimport c_environment | |
9 cimport c_export # needed so the init function gets exported | |
10 cimport c_thunks | |
11 | |
12 | |
13 from libc.stdint cimport uintptr_t | |
14 | |
15 | |
16 def SetSystemThunks(system_thunks_as_object): | |
17 """Bind the basic Mojo Core functions. | |
18 """ | |
19 cdef const c_thunks.MojoSystemThunks* system_thunks = ( | |
20 <const c_thunks.MojoSystemThunks*><uintptr_t>system_thunks_as_object) | |
21 c_thunks.MojoSetSystemThunks(system_thunks) | |
22 | |
23 | |
24 cdef class RunLoop(object): | |
25 """RunLoop to use when using asynchronous operations on handles.""" | |
26 | |
27 cdef c_environment.CRunLoop* c_run_loop | |
28 | |
29 def __init__(self): | |
30 assert not <uintptr_t>(c_environment.CRunLoopCurrent()) | |
31 self.c_run_loop = new c_environment.CRunLoop() | |
32 | |
33 def __dealloc__(self): | |
34 del self.c_run_loop | |
35 | |
36 def Run(self): | |
37 """Run the runloop until Quit is called.""" | |
38 self.c_run_loop.Run() | |
39 | |
40 def RunUntilIdle(self): | |
41 """Run the runloop until Quit is called or no operation is waiting.""" | |
42 self.c_run_loop.RunUntilIdle() | |
43 | |
44 def Quit(self): | |
45 """Quit the runloop.""" | |
46 self.c_run_loop.Quit() | |
47 | |
48 def PostDelayedTask(self, runnable, delay=0): | |
49 """ | |
50 Post a task on the runloop. This must be called from the thread owning the | |
51 runloop. | |
52 """ | |
53 cdef c_environment.CClosure closure = c_environment.BuildClosure(runnable) | |
54 self.c_run_loop.PostDelayedTask(closure, delay) | |
55 | |
56 | |
57 # We use a wrapping class to be able to call the C++ class PythonAsyncWaiter | |
58 # across module boundaries. | |
59 cdef class AsyncWaiter(object): | |
60 cdef c_environment.CEnvironment* _cenvironment | |
61 cdef c_async_waiter.PythonAsyncWaiter* _c_async_waiter | |
62 | |
63 def __init__(self): | |
64 self._cenvironment = new c_environment.CEnvironment() | |
65 self._c_async_waiter = c_environment.NewAsyncWaiter() | |
66 | |
67 def __dealloc__(self): | |
68 del self._c_async_waiter | |
69 del self._cenvironment | |
70 | |
71 def AsyncWait(self, handle, signals, deadline, callback): | |
72 return self._c_async_waiter.AsyncWait(handle, signals, deadline, callback) | |
73 | |
74 def CancelWait(self, wait_id): | |
75 self._c_async_waiter.CancelWait(wait_id) | |
OLD | NEW |