diff options
Diffstat (limited to 'mojo/public/python')
-rw-r--r-- | mojo/public/python/BUILD.gn | 32 | ||||
-rw-r--r-- | mojo/public/python/mojo/system.pyx | 40 | ||||
-rw-r--r-- | mojo/public/python/mojo/system_impl.pyx | 75 |
3 files changed, 118 insertions, 29 deletions
diff --git a/mojo/public/python/BUILD.gn b/mojo/public/python/BUILD.gn index 66f132f..70402e7 100644 --- a/mojo/public/python/BUILD.gn +++ b/mojo/public/python/BUILD.gn @@ -9,6 +9,7 @@ group("python") { ":base", ":bindings", ":system", + ":system_impl", ] } @@ -17,21 +18,38 @@ python_binary_module("system") { python_base_module = "mojo" sources = [ "mojo/c_core.pxd", - "mojo/c_environment.pxd", "mojo/system.pyx", ] + deps = [ + ":base", + "../c/environment", + "../c/system:for_shared_library", + "../cpp/environment:standalone", + "../cpp/system", + "../cpp/utility", + "../cpp/bindings:callback", + ] +} + +python_binary_module("system_impl") { + python_base_module = "mojo" + sources = [ + "mojo/c_core.pxd", + "mojo/c_environment.pxd", + "mojo/system_impl.pyx", + ] additional_sources = [ "src/python_system_helper.cc", "src/python_system_helper.h", ] deps = [ - "//mojo/public/c/environment", - "//mojo/public/c/system:for_shared_library", - "//mojo/public/cpp/environment:standalone", - "//mojo/public/cpp/system", - "//mojo/public/cpp/utility", - "//mojo/public/cpp/bindings:callback", ":base", + "../c/environment", + "../c/system:for_shared_library", + "../cpp/environment:standalone", + "../cpp/system", + "../cpp/utility", + "../cpp/bindings:callback", ] } diff --git a/mojo/public/python/mojo/system.pyx b/mojo/public/python/mojo/system.pyx index f0a814a..35b8093 100644 --- a/mojo/public/python/mojo/system.pyx +++ b/mojo/public/python/mojo/system.pyx @@ -5,7 +5,6 @@ # distutils language = c++ cimport c_core -cimport c_environment from cpython.buffer cimport PyBUF_CONTIG @@ -21,6 +20,8 @@ from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t, uintptr_t import ctypes import threading +import mojo.system_impl + def SetSystemThunks(system_thunks_as_object): """Bind the basic Mojo Core functions. @@ -29,6 +30,7 @@ def SetSystemThunks(system_thunks_as_object): cdef const c_core.MojoSystemThunks* system_thunks = ( <const c_core.MojoSystemThunks*><uintptr_t>system_thunks_as_object) c_core.MojoSetSystemThunks(system_thunks) + mojo.system_impl.SetSystemThunks(system_thunks_as_object) HANDLE_INVALID = c_core.MOJO_HANDLE_INVALID RESULT_OK = c_core.MOJO_RESULT_OK @@ -347,7 +349,7 @@ cdef class Handle(object): cdef c_core.MojoHandle handle = self._mojo_handle cdef c_core.MojoHandleSignals csignals = signals cdef c_core.MojoDeadline cdeadline = deadline - cdef c_environment.MojoAsyncWaitID wait_id = _ASYNC_WAITER.AsyncWait( + wait_id = _ASYNC_WAITER.AsyncWait( handle, csignals, cdeadline, @@ -734,46 +736,40 @@ class DuplicateSharedBufferOptions(object): _RUN_LOOPS = threading.local() -cdef class RunLoop(object): +class RunLoop(object): """RunLoop to use when using asynchronous operations on handles.""" - cdef c_environment.CRunLoop* c_run_loop - def __init__(self): - assert not <uintptr_t>(c_environment.CRunLoopCurrent()) - self.c_run_loop = new c_environment.CRunLoop() + self.__run_loop = mojo.system_impl.RunLoop() _RUN_LOOPS.loop = id(self) - def __dealloc__(self): + def __del__(self): del _RUN_LOOPS.loop - del self.c_run_loop - - @staticmethod - def Current(): - if hasattr(_RUN_LOOPS, 'loop'): - return ctypes.cast(_RUN_LOOPS.loop, ctypes.py_object).value - return None def Run(self): """Run the runloop until Quit is called.""" - self.c_run_loop.Run() + return self.__run_loop.Run() def RunUntilIdle(self): """Run the runloop until Quit is called or no operation is waiting.""" - self.c_run_loop.RunUntilIdle() + return self.__run_loop.RunUntilIdle() def Quit(self): """Quit the runloop.""" - self.c_run_loop.Quit() + return self.__run_loop.Quit() def PostDelayedTask(self, runnable, delay=0): """ Post a task on the runloop. This must be called from the thread owning the runloop. """ - cdef c_environment.CClosure closure = c_environment.BuildClosure(runnable) - self.c_run_loop.PostDelayedTask(closure, delay) + return self.__run_loop.PostDelayedTask(runnable, delay) + + @staticmethod + def Current(): + if hasattr(_RUN_LOOPS, 'loop'): + return ctypes.cast(_RUN_LOOPS.loop, ctypes.py_object).value + return None -cdef c_environment.CEnvironment* _ENVIRONMENT = new c_environment.CEnvironment() -cdef c_environment.PythonAsyncWaiter* _ASYNC_WAITER = new c_environment.PythonAsyncWaiter() +_ASYNC_WAITER = mojo.system_impl.ASYNC_WAITER diff --git a/mojo/public/python/mojo/system_impl.pyx b/mojo/public/python/mojo/system_impl.pyx new file mode 100644 index 0000000..6e79c51 --- /dev/null +++ b/mojo/public/python/mojo/system_impl.pyx @@ -0,0 +1,75 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# distutils language = c++ + +cimport c_core +cimport c_environment + + +from libc.stdint cimport uintptr_t + +def SetSystemThunks(system_thunks_as_object): + """Bind the basic Mojo Core functions. + """ + cdef const c_core.MojoSystemThunks* system_thunks = ( + <const c_core.MojoSystemThunks*><uintptr_t>system_thunks_as_object) + c_core.MojoSetSystemThunks(system_thunks) + + +cdef class RunLoop(object): + """RunLoop to use when using asynchronous operations on handles.""" + + cdef c_environment.CRunLoop* c_run_loop + + def __init__(self): + assert not <uintptr_t>(c_environment.CRunLoopCurrent()) + self.c_run_loop = new c_environment.CRunLoop() + + def __dealloc__(self): + del self.c_run_loop + + def Run(self): + """Run the runloop until Quit is called.""" + self.c_run_loop.Run() + + def RunUntilIdle(self): + """Run the runloop until Quit is called or no operation is waiting.""" + self.c_run_loop.RunUntilIdle() + + def Quit(self): + """Quit the runloop.""" + self.c_run_loop.Quit() + + def PostDelayedTask(self, runnable, delay=0): + """ + Post a task on the runloop. This must be called from the thread owning the + runloop. + """ + cdef c_environment.CClosure closure = c_environment.BuildClosure(runnable) + self.c_run_loop.PostDelayedTask(closure, delay) + + +# We use a wrapping class to be able to call the C++ class PythonAsyncWaiter +# across module boundaries. +cdef class _AsyncWaiter(object): + cdef c_environment.CEnvironment* _cenvironment + cdef c_environment.PythonAsyncWaiter* _c_async_waiter + + def __init__(self): + self._cenvironment = new c_environment.CEnvironment() + self._c_async_waiter = new c_environment.PythonAsyncWaiter() + + def __dealloc__(self): + del self._c_async_waiter + del self._cenvironment + + def AsyncWait(self, handle, signals, deadline, callback): + return self._c_async_waiter.AsyncWait(handle, signals, deadline, callback) + + def CancelWait(self, wait_id): + self._c_async_waiter.CancelWait(wait_id) + + +ASYNC_WAITER = _AsyncWaiter() |