Commits

Anonymous committed 8016a59

first import

  • Participants

Comments (0)

Files changed (3)

+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by 
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import ctypes
+_lib = ctypes.cdll.LoadLibrary('libdispatch.so')
+
+QUEUE_PRIORITY_HIGH = ctypes.c_long(2)
+QUEUE_PRIORITY_DEFAULT = ctypes.c_long(0)
+QUEUE_PRIORITY_LOW = ctypes.c_long(-2)
+
+if 0:
+    
+    class dispatch_queue_t(ctypes.Structure):
+        pass
+
+    class dispatch_object_t(ctypes.Structure):
+        pass
+
+    class dispatch_function_f(ctypes.Structure):
+        pass
+
+    class dispatch_function_t(ctypes.Structure):
+        pass
+else:
+    dispatch_queue_t = ctypes.c_void_p
+    dispatch_object_t = ctypes.c_void_p
+    dispatch_function_t = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
+    dispatch_queue_attr_t = ctypes.c_void_p
+    dispatch_time_t = ctypes.c_void_p
+    
+    dispatch_function_f = ctypes.c_void_p
+    
+dispatch_release = _lib.dispatch_release
+dispatch_release.argtypes = [dispatch_object_t]
+
+dispatch_get_context = _lib.dispatch_get_context
+dispatch_get_context.argtypes = [dispatch_object_t]
+dispatch_get_context.restype = ctypes.c_voidp
+
+dispatch_set_context = _lib.dispatch_set_context
+dispatch_set_context.argtypes = [dispatch_object_t, ctypes.c_voidp]
+
+dispatch_set_finalizer_f = _lib.dispatch_set_finalizer_f
+dispatch_set_context.argtypes = [dispatch_object_t, dispatch_function_f]
+
+dispatch_suspend = _lib.dispatch_suspend
+dispatch_suspend.argtypes = [dispatch_object_t]
+
+dispatch_resume = _lib.dispatch_resume
+dispatch_resume.argtypes = [dispatch_object_t]
+
+dispatch_async_f = _lib.dispatch_async_f
+dispatch_async_f.argtypes = [dispatch_queue_t, ctypes.c_voidp, dispatch_function_t]
+
+dispatch_sync_f = _lib.dispatch_sync_f
+dispatch_sync_f.argtypes = [dispatch_queue_t, ctypes.c_voidp, dispatch_function_t]
+
+dispatch_get_main_queue = _lib.dispatch_get_main_queue
+dispatch_get_main_queue.argtypes = []
+dispatch_get_main_queue.restype = dispatch_queue_t
+
+dispatch_get_global_queue = _lib.dispatch_get_global_queue
+dispatch_get_global_queue.argtypes = [ctypes.c_long, ctypes.c_ulong]
+dispatch_get_global_queue.restype = dispatch_queue_t
+
+dispatch_queue_create = _lib.dispatch_queue_create
+dispatch_queue_create.argtypes = [ctypes.c_char_p, dispatch_queue_attr_t]
+dispatch_queue_create.restype = dispatch_queue_t
+
+dispatch_queue_get_label = _lib.dispatch_queue_get_label
+dispatch_queue_get_label.argtypes = [dispatch_queue_t]
+dispatch_queue_get_label.restype = ctypes.c_char_p
+
+dispatch_set_target_queue = _lib.dispatch_set_target_queue
+dispatch_set_target_queue.argtypes = [dispatch_object_t, dispatch_queue_t]
+
+dispatch_main = _lib.dispatch_main
+dispatch_main.argtypes = []
+#dispatch_main.restype = ctypes.c_void
+
+dispatch_after_f = _lib.dispatch_after_f
+dispatch_after_f.argtypes = [dispatch_time_t,
+                             dispatch_queue_t,
+                             ctypes.c_void_p,
+                             dispatch_function_t]
+#!/usr/bin/env python2
+import user
+import libdispatch
+import ctypes
+
+main_q = libdispatch.dispatch_get_main_queue()
+
+q1 = libdispatch.dispatch_queue_create("foo", None)
+print libdispatch.dispatch_queue_get_label(q1)
+
+def test1():
+    print "==== hello-world ===="
+
+    main_q = libdispatch.dispatch_get_main_queue()
+
+    import sys
+    fcts = []
+    for i in range(1,11):
+        def fct(i):
+            print "hello %s" % (i,)
+        if i % 2:
+            print "-->",i,"lambda"
+            fcts.append(libdispatch.dispatch_function_t(lambda x: sys.stdout.write("hello -- %s --\n"%x)))
+        else:
+            fcts.append(libdispatch.dispatch_function_t(fct))
+
+        libdispatch.dispatch_async_f(main_q,
+                                     i,
+                                     fcts[-1])
+
+    # add a last item to the main queue
+    def last_fct(*args):
+        print "goodbye"
+        import sys
+        sys.exit(0)
+    libdispatch.dispatch_async_f(main_q, None,
+                                libdispatch.dispatch_function_t(last_fct))
+
+    libdispatch.dispatch_main()
+    return
+
+