Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: mojo/public/python/mojo/system.pyx

Issue 644773002: mojo: Add RunLoop::Current() and refactor unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/python/mojo/c_environment.pxd ('k') | mojo/python/tests/async_wait_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 # distutils language = c++ 5 # distutils language = c++
6 6
7 cimport c_core 7 cimport c_core
8 cimport c_environment 8 cimport c_environment
9 9
10 10
11 from cpython.buffer cimport PyBUF_CONTIG 11 from cpython.buffer cimport PyBUF_CONTIG
12 from cpython.buffer cimport PyBUF_CONTIG_RO 12 from cpython.buffer cimport PyBUF_CONTIG_RO
13 from cpython.buffer cimport Py_buffer 13 from cpython.buffer cimport Py_buffer
14 from cpython.buffer cimport PyBuffer_FillInfo 14 from cpython.buffer cimport PyBuffer_FillInfo
15 from cpython.buffer cimport PyBuffer_Release 15 from cpython.buffer cimport PyBuffer_Release
16 from cpython.buffer cimport PyObject_GetBuffer 16 from cpython.buffer cimport PyObject_GetBuffer
17 from cpython.mem cimport PyMem_Malloc, PyMem_Free 17 from cpython.mem cimport PyMem_Malloc, PyMem_Free
18 from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t, uintptr_t 18 from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t, uintptr_t
19 19
20 import ctypes
21 import threading
22
20 def SetSystemThunks(system_thunks_as_object): 23 def SetSystemThunks(system_thunks_as_object):
21 """Bind the basic Mojo Core functions. 24 """Bind the basic Mojo Core functions.
22 25
23 This should only be used by the embedder. 26 This should only be used by the embedder.
24 """ 27 """
25 cdef const c_core.MojoSystemThunks* system_thunks = ( 28 cdef const c_core.MojoSystemThunks* system_thunks = (
26 <const c_core.MojoSystemThunks*><uintptr_t>system_thunks_as_object) 29 <const c_core.MojoSystemThunks*><uintptr_t>system_thunks_as_object)
27 c_core.MojoSetSystemThunks(system_thunks) 30 c_core.MojoSetSystemThunks(system_thunks)
28 31
29 HANDLE_INVALID = c_core.MOJO_HANDLE_INVALID 32 HANDLE_INVALID = c_core.MOJO_HANDLE_INVALID
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 """Options for duplicating a shared buffer. 707 """Options for duplicating a shared buffer.
705 708
706 See mojo/public/c/system/buffer.h 709 See mojo/public/c/system/buffer.h
707 """ 710 """
708 FLAG_NONE = c_core.MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE 711 FLAG_NONE = c_core.MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE
709 712
710 def __init__(self): 713 def __init__(self):
711 self.flags = DuplicateSharedBufferOptions.FLAG_NONE 714 self.flags = DuplicateSharedBufferOptions.FLAG_NONE
712 715
713 716
717 _RUN_LOOPS = threading.local()
sdefresne 2014/10/10 12:29:44 IIUC, _RUN_LOOPS is used to keep a weak-ref to the
qsr 2014/10/10 12:34:07 Done.
718
719
714 cdef class RunLoop(object): 720 cdef class RunLoop(object):
715 """RunLoop to use when using asynchronous operations on handles.""" 721 """RunLoop to use when using asynchronous operations on handles."""
716 722
717 cdef c_environment.CRunLoop c_run_loop 723 cdef c_environment.CRunLoop* c_run_loop
724
725 def __init__(self):
726 assert not <uintptr_t>(c_environment.CRunLoopCurrent())
727 self.c_run_loop = new c_environment.CRunLoop()
728 _RUN_LOOPS.loop = id(self)
729
730 def __dealloc__(self):
731 del _RUN_LOOPS.loop
732 del self.c_run_loop
733
734 @staticmethod
735 def Current():
736 if hasattr(_RUN_LOOPS, 'loop'):
737 return ctypes.cast(_RUN_LOOPS.loop, ctypes.py_object).value
738 return None
718 739
719 def Run(self): 740 def Run(self):
720 """Run the runloop until Quit is called.""" 741 """Run the runloop until Quit is called."""
721 self.c_run_loop.Run() 742 self.c_run_loop.Run()
722 743
723 def RunUntilIdle(self): 744 def RunUntilIdle(self):
724 """Run the runloop until Quit is called or no operation is waiting.""" 745 """Run the runloop until Quit is called or no operation is waiting."""
725 self.c_run_loop.RunUntilIdle() 746 self.c_run_loop.RunUntilIdle()
726 747
727 def Quit(self): 748 def Quit(self):
728 """Quit the runloop.""" 749 """Quit the runloop."""
729 self.c_run_loop.Quit() 750 self.c_run_loop.Quit()
730 751
731 def PostDelayedTask(self, runnable, delay=0): 752 def PostDelayedTask(self, runnable, delay=0):
732 """ 753 """
733 Post a task on the runloop. This must be called from the thread owning the 754 Post a task on the runloop. This must be called from the thread owning the
734 runloop. 755 runloop.
735 """ 756 """
736 cdef c_environment.CClosure closure = c_environment.BuildClosure(runnable) 757 cdef c_environment.CClosure closure = c_environment.BuildClosure(runnable)
737 self.c_run_loop.PostDelayedTask(closure, delay) 758 self.c_run_loop.PostDelayedTask(closure, delay)
738 759
739 760
740 cdef c_environment.CEnvironment* _ENVIRONMENT = new c_environment.CEnvironment() 761 cdef c_environment.CEnvironment* _ENVIRONMENT = new c_environment.CEnvironment()
741 cdef c_environment.PythonAsyncWaiter* _ASYNC_WAITER = new c_environment.PythonAs yncWaiter() 762 cdef c_environment.PythonAsyncWaiter* _ASYNC_WAITER = new c_environment.PythonAs yncWaiter()
OLDNEW
« no previous file with comments | « mojo/public/python/mojo/c_environment.pxd ('k') | mojo/python/tests/async_wait_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698