├── BUGS ├── Makefile.OLD ├── Makefile.am ├── README ├── autogen.sh ├── configure.ac ├── configure.rb ├── libdispatch-197 ├── INSTALL ├── LICENSE ├── Makefile.am ├── autogen.sh ├── configure.ac ├── dispatch │ ├── Makefile.am │ ├── base.h │ ├── benchmark.h │ ├── dispatch.h │ ├── group.h │ ├── object.h │ ├── once.h │ ├── queue.h │ ├── semaphore.h │ ├── source.h │ └── time.h ├── libdispatch.plist ├── libdispatch.xcodeproj │ └── project.pbxproj ├── m4 │ ├── atomic.m4 │ ├── blocks.m4 │ ├── pkg.m4 │ └── private-extern.m4 ├── man │ ├── Makefile.am │ ├── dispatch.3 │ ├── dispatch_after.3 │ ├── dispatch_api.3 │ ├── dispatch_apply.3 │ ├── dispatch_async.3 │ ├── dispatch_benchmark.3 │ ├── dispatch_group_create.3 │ ├── dispatch_object.3 │ ├── dispatch_once.3 │ ├── dispatch_queue_create.3 │ ├── dispatch_semaphore_create.3 │ ├── dispatch_source_create.3 │ └── dispatch_time.3 ├── src │ ├── Makefile.am │ ├── apply.c │ ├── benchmark.c │ ├── hw_shims.h │ ├── internal.h │ ├── kevent_internal.h │ ├── legacy.c │ ├── legacy.h │ ├── object.c │ ├── object_internal.h │ ├── once.c │ ├── os_shims.h │ ├── private.h │ ├── protocol.defs │ ├── queue.c │ ├── queue_internal.h │ ├── queue_kevent.c │ ├── queue_private.h │ ├── semaphore.c │ ├── semaphore_internal.h │ ├── shims │ │ ├── getprogname.h │ │ ├── mach.c │ │ ├── malloc_zone.h │ │ ├── perfmon.h │ │ ├── time.c │ │ ├── time.h │ │ ├── tsd.c │ │ └── tsd.h │ ├── source.c │ ├── source_internal.h │ ├── source_kevent.c │ ├── source_private.h │ └── time.c └── testing │ ├── Makefile.am │ ├── Makefile.darwin │ ├── bench.mm │ ├── dispatch_after.c │ ├── dispatch_api.c │ ├── dispatch_apply.c │ ├── dispatch_c99.c │ ├── dispatch_cascade.c │ ├── dispatch_cffd.c │ ├── dispatch_debug.c │ ├── dispatch_drift.c │ ├── dispatch_group.c │ ├── dispatch_pingpong.c │ ├── dispatch_plusplus.cpp │ ├── dispatch_priority.c │ ├── dispatch_proc.c │ ├── dispatch_read.c │ ├── dispatch_readsync.c │ ├── dispatch_sema.c │ ├── dispatch_starfish.c │ ├── dispatch_test.c │ ├── dispatch_test.h │ ├── dispatch_timer_bit31.c │ ├── dispatch_timer_bit63.c │ ├── dispatch_timer_set_time.c │ ├── fd_stress.c │ ├── func.c │ ├── harness.c │ ├── leaks-wrapper │ ├── nsoperation.m │ ├── os_shims.h │ ├── queue_finalizer.c │ ├── shims │ ├── arc4random.c │ ├── arc4random.h │ ├── asprintf.c │ ├── asprintf.h │ ├── fgetln.c │ └── fgetln.h │ └── summarize.c ├── libdispatch.tgz ├── libdispatch_0~svn197.orig.tar.gz └── patch ├── disable_dispatch_read.patch ├── dispatch-atomic.diff ├── dispatch-blocks.diff ├── dispatch-internal.diff ├── dispatch-private_extern.diff ├── dispatch-semaphore.diff ├── dispatch-spawn.diff ├── dispatch-workaround.diff ├── getloadavg.diff ├── kqueue-private.diff ├── kqueue-test.diff ├── kqueue-timer.diff ├── kqueue-tls.diff ├── libdispatch-r197_v2.patch └── libdispatch-r215_v1.diff /BUGS: -------------------------------------------------------------------------------- 1 | Known bugs for the Android port: 2 | 3 | * The configure script will fail if clang is not installed, even though 4 | it is not needed for the Android build. 5 | The workaround is to make sure clang is available in your $PATH. 6 | 7 | * The configure script for libdispatch will call pkg-config to search 8 | for libkqueue. As a workaround, you can run 9 | 'sudo aptitude install libkqueue-dev' to bring in the .pc file. 10 | This might also happen with libpthread_workqueue. 11 | 12 | Known bugs for the Solaris port: 13 | 14 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | # FIXME - Only supports Linux for now 3 | 4 | SUBDIRS = blocks-runtime libkqueue libpwq 5 | 6 | 7 | ##lib_LTLIBRARIES = libdispatch.la 8 | # 9 | ##libBlocksRuntime_la_CFLAGS = -stg=c99 -Wall -Wextra -W -pedantic 10 | # 11 | #libBlocksRuntime_la_SOURCES = runtime.c data.c 12 | # 13 | ##check_PROGRAMS = blockrt-test 14 | # 15 | ##TESTS = blockrt-test 16 | ## 17 | #blockrt_test_SOURCES = test/main.c 18 | # 19 | #blockrt_test_CFLAGS = -I. -fblocks 20 | # 21 | #blockrt_test_LDADD = libBlocksRuntime.la 22 | 23 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ---- 2 | 3 | # *IMPORTANT NOTE*: This project is no longer being maintained. 4 | 5 | This repository was designed to be used with an older version of libdispatch, and is not compatible with newer versions. 6 | 7 | ---- 8 | 9 | Building OpenGCD for Linux 10 | -------------------------- 11 | 12 | Run the following commands to build OpenGCD for Linux. 13 | 14 | ./autogen.sh 15 | ./configure --prefix=/usr 16 | make 17 | sudo make install 18 | cd libdispatch 19 | autoreconf -fvi 20 | ./configure 21 | make 22 | make check 23 | sudo make install 24 | 25 | 26 | Building OpenGCD for Android 27 | ---------------------------- 28 | 29 | Make sure to read the BUGS file for any bugs pertaining to the Android platform. 30 | 31 | 1. Download the Android NDK revision 8e from here: 32 | http://developer.android.com/tools/sdk/ndk/index.html 33 | 34 | 2. Install the Android NDK under /opt. 35 | 36 | 3. Download the Android Developer Tools (ADT) bundle version 20130219: 37 | http://developer.android.com/sdk/index.html 38 | 39 | 4. Install the ADT bundle under /opt. 40 | 41 | 5. Run the 'autogen' script to download needed dependencies and 42 | generate the ./configure script. 43 | 44 | ./autogen.sh 45 | 46 | 6. Run the OpenGCD configure script and provide the paths to your NDK and SDK. 47 | Example: 48 | 49 | ./configure --host=arm-linux-androideabi \ 50 | --with-ndk=/opt/android-ndk-r8e \ 51 | --with-sdk=/opt/adt-bundle-linux-x86_64-20130219/sdk 52 | 53 | **NOTE** The path to the SDK depends on what OS you are using, 54 | the above example is for a 64-bit Linux host. 55 | 56 | 7. Build libdispatch 57 | 58 | make 59 | 60 | 8. Run the unit tests 61 | 62 | make check 63 | 64 | Debugging 65 | --------- 66 | 67 | To run one of the test programs under the debugger, follow these steps: 68 | 69 | 1. Run the "make debug" target and set the BINARY variable to the 70 | path to the binary. Example: 71 | 72 | make debug BINARY=obj/local/armeabi/dispatch-api 73 | 74 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/bash -ex 3 | 4 | test -d blocks-runtime || \ 5 | git clone https://github.com/mheily/blocks-runtime.git 6 | 7 | test -d libkqueue || \ 8 | git clone https://github.com/mheily/libkqueue.git 9 | 10 | test -d libpwq || \ 11 | svn co svn://svn.code.sf.net/p/libpwq/code/trunk libpwq 12 | 13 | test -d libdispatch 14 | if [ $? -ne 0 ] ; then 15 | svn co http://svn.macosforge.org/repository/libdispatch/trunk@197 libdispatch 16 | cd libdispatch 17 | patch -p0 < ../patch/disable_dispatch_read.patch 18 | patch -p0 < ../patch/libdispatch-r197_v2.patch 19 | cd .. 20 | fi 21 | 22 | autoreconf -fvi 23 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_INIT([opengcd], [0.4]) 5 | AC_PROG_CC([clang]) 6 | LT_INIT 7 | AM_INIT_AUTOMAKE([foreign subdir-objects]) 8 | AC_CONFIG_SRCDIR([configure.ac]) 9 | AC_CONFIG_HEADERS([config.h]) 10 | AC_CONFIG_SUBDIRS([blocks-runtime libkqueue libpwq]) 11 | AC_CONFIG_FILES([Makefile]) 12 | AC_OUTPUT 13 | -------------------------------------------------------------------------------- /libdispatch-197/INSTALL: -------------------------------------------------------------------------------- 1 | Grand Central Dispatch (GCD) 2 | 3 | GCD is a concurrent programming framework first shipped with Mac OS X Snow 4 | Leopard. This package is an open source bundling of libdispatch, the core 5 | user space library implementing GCD. At the time of writing, support for 6 | the BSD kqueue API, and specifically extensions introduced in Mac OS X Snow 7 | Leopard and FreeBSD 9-CURRENT, are required to use libdispatch. Other 8 | systems are currently unsupported. 9 | 10 | Configuring and installing libdispatch 11 | 12 | GCD is built using autoconf, automake, and libtool, and has a number of 13 | compile-time configuration options that should be reviewed before starting. 14 | An uncustomized install requires: 15 | 16 | ./configure 17 | make 18 | make install 19 | 20 | The following configure options may be of general interest: 21 | 22 | --with-apple-libc-source 23 | 24 | Specify the path to Apple's Libc package, so that appropriate headers 25 | can be found and used. 26 | 27 | --with-apple-xnu-source 28 | 29 | Specify the path to Apple's XNU package, so that appropriate headers 30 | can be found and used. 31 | 32 | --with-blocks-runtime 33 | 34 | On systems where -fblocks is supported, specify an additional library 35 | path in which libBlocksRuntime can be found. This is not required on 36 | Mac OS X, where the Blocks runtime is included in libSystem, but is 37 | required on FreeBSD. 38 | 39 | Some sites will wish to build using a non-default C compiler; for example, 40 | this is desirable on FreeBSD so that libdispatch is built with clang and 41 | blocks support. A typically FreeBSD configuration will use: 42 | 43 | CC=clang ./configure --with-blocks-runtime=/usr/local/lib 44 | make 45 | make install 46 | 47 | The following options are likely only to be required if building libdispatch 48 | as part of Mac OS X's libSystem: 49 | 50 | --enable-legacy-api 51 | 52 | Enable a legacy (deprecated) API used by some early GCD applications. 53 | 54 | --disable-libdispatch-init-constructor 55 | 56 | Do not tag libdispatch's init routine as __constructor, in which case 57 | it must be run manually before libdispatch routines can be called. 58 | For the libdispatch code compiled directly into libSystem, the init 59 | routine is called automatically during process start. 60 | 61 | --enable-apple-crashreporter-info 62 | 63 | Set global variables during a libdispatch crash to provide additional 64 | debugging information for CrashReporter. 65 | 66 | --enable-apple-tsd-optimizations 67 | 68 | Use a non-portable allocation scheme for pthread per-thread data 69 | (TSD) keys when built into libSystem on Mac OS X. This should not be 70 | used on other OS's, nor on Mac OS X when building as a stand-alone 71 | library. 72 | 73 | --enable-apple-semaphore-optimizations 74 | 75 | libdispatch contains hand-optimized assembly for use with libdispatch 76 | semaphores. 77 | 78 | Typical configuration commands 79 | 80 | The following command lines create the default config.h required to build 81 | libdispatch with libSystem in Mac OS X Snow Leopard: 82 | 83 | sh autogen.sh 84 | ./configure \ 85 | --with-apple-libc-source=/path/to/10.6.0/Libc-583 \ 86 | --with-apple-xnu-source=/path/to/10.6.0/xnu-1456.1.26 \ 87 | --enable-legacy-api \ 88 | --disable-libdispatch-init-constructor \ 89 | --enable-apple-crashreporter-info \ 90 | --enable-apple-tsd-optimizations \ 91 | --enable-apple-semaphore-optimizations 92 | 93 | Typical configuration line for FreeBSD 8.x and 9.x: 94 | 95 | sh autogen.sh 96 | CC=clang ./configure --with-blocks-runtime=/usr/local/lib 97 | -------------------------------------------------------------------------------- /libdispatch-197/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # 4 | ACLOCAL_AMFLAGS = -I m4 5 | 6 | SUBDIRS= \ 7 | dispatch \ 8 | man \ 9 | src \ 10 | testing 11 | -------------------------------------------------------------------------------- /libdispatch-197/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | autoreconf -fvi 3 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # 4 | 5 | dispatchdir=$(includedir)/dispatch 6 | 7 | dispatch_HEADERS= \ 8 | base.h \ 9 | dispatch.h \ 10 | group.h \ 11 | object.h \ 12 | once.h \ 13 | queue.h \ 14 | semaphore.h \ 15 | source.h \ 16 | time.h 17 | 18 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_BASE__ 22 | #define __DISPATCH_BASE__ 23 | 24 | #ifndef __DISPATCH_INDIRECT__ 25 | #error "Please #include instead of this file directly." 26 | #endif 27 | 28 | #ifdef __cplusplus 29 | /* 30 | * Dispatch objects are NOT C++ objects. Nevertheless, we can at least keep C++ 31 | * aware of type compatibility. 32 | */ 33 | typedef struct dispatch_object_s { 34 | private: 35 | dispatch_object_s(); 36 | ~dispatch_object_s(); 37 | dispatch_object_s(const dispatch_object_s &); 38 | void operator=(const dispatch_object_s &); 39 | } *dispatch_object_t; 40 | #else 41 | typedef union { 42 | struct dispatch_object_s *_do; 43 | struct dispatch_continuation_s *_dc; 44 | struct dispatch_queue_s *_dq; 45 | struct dispatch_queue_attr_s *_dqa; 46 | struct dispatch_group_s *_dg; 47 | struct dispatch_source_s *_ds; 48 | struct dispatch_source_attr_s *_dsa; 49 | struct dispatch_semaphore_s *_dsema; 50 | } dispatch_object_t __attribute__((transparent_union)); 51 | #endif 52 | 53 | typedef void (*dispatch_function_t)(void *); 54 | 55 | #ifdef __cplusplus 56 | #define DISPATCH_DECL(name) typedef struct name##_s : public dispatch_object_s {} *name##_t 57 | #else 58 | /*! @parseOnly */ 59 | #define DISPATCH_DECL(name) typedef struct name##_s *name##_t 60 | #endif 61 | 62 | #ifdef __GNUC__ 63 | #define DISPATCH_NORETURN __attribute__((__noreturn__)) 64 | #define DISPATCH_NOTHROW __attribute__((__nothrow__)) 65 | #define DISPATCH_NONNULL1 __attribute__((__nonnull__(1))) 66 | #define DISPATCH_NONNULL2 __attribute__((__nonnull__(2))) 67 | #define DISPATCH_NONNULL3 __attribute__((__nonnull__(3))) 68 | #define DISPATCH_NONNULL4 __attribute__((__nonnull__(4))) 69 | #define DISPATCH_NONNULL5 __attribute__((__nonnull__(5))) 70 | #define DISPATCH_NONNULL6 __attribute__((__nonnull__(6))) 71 | #define DISPATCH_NONNULL7 __attribute__((__nonnull__(7))) 72 | #if __clang__ 73 | // rdar://problem/6857843 74 | #define DISPATCH_NONNULL_ALL 75 | #else 76 | #define DISPATCH_NONNULL_ALL __attribute__((__nonnull__)) 77 | #endif 78 | #define DISPATCH_SENTINEL __attribute__((__sentinel__)) 79 | #define DISPATCH_PURE __attribute__((__pure__)) 80 | #define DISPATCH_WARN_RESULT __attribute__((__warn_unused_result__)) 81 | #define DISPATCH_MALLOC __attribute__((__malloc__)) 82 | #define DISPATCH_FORMAT(...) __attribute__((__format__(__VA_ARGS__))) 83 | #else 84 | /*! @parseOnly */ 85 | #define DISPATCH_NORETURN 86 | /*! @parseOnly */ 87 | #define DISPATCH_NOTHROW 88 | /*! @parseOnly */ 89 | #define DISPATCH_NONNULL1 90 | /*! @parseOnly */ 91 | #define DISPATCH_NONNULL2 92 | /*! @parseOnly */ 93 | #define DISPATCH_NONNULL3 94 | /*! @parseOnly */ 95 | #define DISPATCH_NONNULL4 96 | /*! @parseOnly */ 97 | #define DISPATCH_NONNULL5 98 | /*! @parseOnly */ 99 | #define DISPATCH_NONNULL6 100 | /*! @parseOnly */ 101 | #define DISPATCH_NONNULL7 102 | /*! @parseOnly */ 103 | #define DISPATCH_NONNULL_ALL 104 | /*! @parseOnly */ 105 | #define DISPATCH_SENTINEL 106 | /*! @parseOnly */ 107 | #define DISPATCH_PURE 108 | /*! @parseOnly */ 109 | #define DISPATCH_WARN_RESULT 110 | /*! @parseOnly */ 111 | #define DISPATCH_MALLOC 112 | /*! @parseOnly */ 113 | #define DISPATCH_FORMAT(...) 114 | #endif 115 | 116 | #if __GNUC__ 117 | #define DISPATCH_EXPORT extern __attribute__((visibility("default"))) 118 | #else 119 | #define DISPATCH_EXPORT extern 120 | #endif 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/benchmark.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_BENCHMARK__ 28 | #define __DISPATCH_BENCHMARK__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #include // for HeaderDoc 33 | #endif 34 | 35 | __DISPATCH_BEGIN_DECLS 36 | 37 | /*! 38 | * @function dispatch_benchmark 39 | * 40 | * @abstract 41 | * Count the average number of cycles a given block takes to execute. 42 | * 43 | * @param count 44 | * The number of times to serially execute the given block. 45 | * 46 | * @param block 47 | * The block to execute. 48 | * 49 | * @result 50 | * The approximate number of cycles the block takes to execute. 51 | * 52 | * @discussion 53 | * This function is for debugging and performance analysis work. For the best 54 | * results, pass a high count value to dispatch_benchmark(). When benchmarking 55 | * concurrent code, please compare the serial version of the code against the 56 | * concurrent version, and compare the concurrent version on different classes 57 | * of hardware. Please look for inflection points with various data sets and 58 | * keep the following facts in mind: 59 | * 60 | * 1) Code bound by computational bandwidth may be inferred by proportional 61 | * changes in performance as concurrency is increased. 62 | * 2) Code bound by memory bandwidth may be inferred by negligible changes in 63 | * performance as concurrency is increased. 64 | * 3) Code bound by critical sections may be inferred by retrograde changes in 65 | * performance as concurrency is increased. 66 | * 3a) Intentional: locks, mutexes, and condition variables. 67 | * 3b) Accidental: unrelated and frequently modified data on the same cache-line. 68 | */ 69 | #ifdef __BLOCKS__ 70 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 71 | DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW 72 | uint64_t 73 | dispatch_benchmark(size_t count, void (^block)(void)); 74 | #endif 75 | 76 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 77 | DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NOTHROW 78 | uint64_t 79 | dispatch_benchmark_f(size_t count, void *ctxt, void (*func)(void *)); 80 | 81 | __DISPATCH_END_DECLS 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/dispatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_PUBLIC__ 22 | #define __DISPATCH_PUBLIC__ 23 | 24 | #ifdef __APPLE__ 25 | #include 26 | #include 27 | #endif 28 | #if HAVE_SYS_CDEFS_H 29 | #include 30 | #endif 31 | #include 32 | #include 33 | #include 34 | #include 35 | #if HAVE_UNISTD_H 36 | #include 37 | #endif 38 | 39 | #if defined(__cplusplus) 40 | #define __DISPATCH_BEGIN_DECLS extern "C" { 41 | #define __DISPATCH_END_DECLS } 42 | #else 43 | #define __DISPATCH_BEGIN_DECLS 44 | #define __DISPATCH_END_DECLS 45 | #endif 46 | 47 | #ifndef __OSX_AVAILABLE_STARTING 48 | #define __OSX_AVAILABLE_STARTING(x, y) 49 | #endif 50 | 51 | #define DISPATCH_API_VERSION 20090501 52 | 53 | #ifndef __DISPATCH_BUILDING_DISPATCH__ 54 | 55 | #ifndef __DISPATCH_INDIRECT__ 56 | #define __DISPATCH_INDIRECT__ 57 | #endif 58 | 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | #include 65 | #include 66 | #include 67 | 68 | #undef __DISPATCH_INDIRECT__ 69 | 70 | #endif /* !__DISPATCH_BUILDING_DISPATCH__ */ 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/once.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_ONCE__ 22 | #define __DISPATCH_ONCE__ 23 | 24 | #ifndef __DISPATCH_INDIRECT__ 25 | #error "Please #include instead of this file directly." 26 | #include // for HeaderDoc 27 | #endif 28 | 29 | __DISPATCH_BEGIN_DECLS 30 | 31 | /*! 32 | * @typedef dispatch_once_t 33 | * 34 | * @abstract 35 | * A predicate for use with dispatch_once(). It must be initialized to zero. 36 | * Note: static and global variables default to zero. 37 | */ 38 | typedef long dispatch_once_t; 39 | 40 | /*! 41 | * @function dispatch_once 42 | * 43 | * @abstract 44 | * Execute a block once and only once. 45 | * 46 | * @param predicate 47 | * A pointer to a dispatch_once_t that is used to test whether the block has 48 | * completed or not. 49 | * 50 | * @param block 51 | * The block to execute once. 52 | * 53 | * @discussion 54 | * Always call dispatch_once() before using or testing any variables that are 55 | * initialized by the block. 56 | */ 57 | #ifdef __BLOCKS__ 58 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 59 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 60 | void 61 | dispatch_once(dispatch_once_t *predicate, dispatch_block_t block); 62 | #ifdef __GNUC__ 63 | #define dispatch_once(x, ...) do { if (__builtin_expect(*(x), ~0l) != ~0l) dispatch_once((x), (__VA_ARGS__)); } while (0) 64 | #endif 65 | #endif 66 | 67 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 68 | DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW 69 | void 70 | dispatch_once_f(dispatch_once_t *predicate, void *context, void (*function)(void *)); 71 | #ifdef __GNUC__ 72 | #define dispatch_once_f(x, y, z) do { if (__builtin_expect(*(x), ~0l) != ~0l) dispatch_once_f((x), (y), (z)); } while (0) 73 | #endif 74 | 75 | __DISPATCH_END_DECLS 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/semaphore.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_SEMAPHORE__ 22 | #define __DISPATCH_SEMAPHORE__ 23 | 24 | #ifndef __DISPATCH_INDIRECT__ 25 | #error "Please #include instead of this file directly." 26 | #include // for HeaderDoc 27 | #endif 28 | 29 | /*! 30 | * @typedef dispatch_semaphore_t 31 | * 32 | * @abstract 33 | * A counting semaphore. 34 | */ 35 | DISPATCH_DECL(dispatch_semaphore); 36 | 37 | __DISPATCH_BEGIN_DECLS 38 | 39 | /*! 40 | * @function dispatch_semaphore_create 41 | * 42 | * @abstract 43 | * Creates new counting semaphore with an initial value. 44 | * 45 | * @discussion 46 | * Passing zero for the value is useful for when two threads need to reconcile 47 | * the completion of a particular event. Passing a value greather than zero is 48 | * useful for managing a finite pool of resources, where the pool size is equal 49 | * to the value. 50 | * 51 | * @param value 52 | * The starting value for the semaphore. Passing a value less than zero will 53 | * cause NULL to be returned. 54 | * 55 | * @result 56 | * The newly created semaphore, or NULL on failure. 57 | */ 58 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 59 | DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_NOTHROW 60 | dispatch_semaphore_t 61 | dispatch_semaphore_create(long value); 62 | 63 | /*! 64 | * @function dispatch_semaphore_wait 65 | * 66 | * @abstract 67 | * Wait (decrement) for a semaphore. 68 | * 69 | * @discussion 70 | * Decrement the counting semaphore. If the resulting value is less than zero, 71 | * this function waits in FIFO order for a signal to occur before returning. 72 | * 73 | * @param dsema 74 | * The semaphore. The result of passing NULL in this parameter is undefined. 75 | * 76 | * @param timeout 77 | * When to timeout (see dispatch_time). As a convenience, there are the 78 | * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants. 79 | * 80 | * @result 81 | * Returns zero on success, or non-zero if the timeout occurred. 82 | */ 83 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 84 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 85 | long 86 | dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout); 87 | 88 | /*! 89 | * @function dispatch_semaphore_signal 90 | * 91 | * @abstract 92 | * Signal (increment) a semaphore. 93 | * 94 | * @discussion 95 | * Increment the counting semaphore. If the previous value was less than zero, 96 | * this function wakes a waiting thread before returning. 97 | * 98 | * @param dsema The counting semaphore. 99 | * The result of passing NULL in this parameter is undefined. 100 | * 101 | * @result 102 | * This function returns non-zero if a thread is woken. Otherwise, zero is 103 | * returned. 104 | */ 105 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 106 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 107 | long 108 | dispatch_semaphore_signal(dispatch_semaphore_t dsema); 109 | 110 | __DISPATCH_END_DECLS 111 | 112 | #endif /* __DISPATCH_SEMAPHORE__ */ 113 | -------------------------------------------------------------------------------- /libdispatch-197/dispatch/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_TIME__ 22 | #define __DISPATCH_TIME__ 23 | 24 | #ifndef __DISPATCH_INDIRECT__ 25 | #error "Please #include instead of this file directly." 26 | #include // for HeaderDoc 27 | #endif 28 | 29 | #include 30 | 31 | __DISPATCH_BEGIN_DECLS 32 | 33 | struct timespec; 34 | 35 | // 6368156 36 | #ifdef NSEC_PER_SEC 37 | #undef NSEC_PER_SEC 38 | #endif 39 | #ifdef USEC_PER_SEC 40 | #undef USEC_PER_SEC 41 | #endif 42 | #ifdef NSEC_PER_USEC 43 | #undef NSEC_PER_USEC 44 | #endif 45 | #ifdef NSEC_PER_MSEC 46 | #undef NSEC_PER_MSEC 47 | #endif 48 | #define NSEC_PER_SEC 1000000000ull 49 | #define NSEC_PER_MSEC 1000000ull 50 | #define USEC_PER_SEC 1000000ull 51 | #define NSEC_PER_USEC 1000ull 52 | 53 | /*! 54 | * @typedef dispatch_time_t 55 | * 56 | * @abstract 57 | * An somewhat abstract representation of time; where zero means "now" and 58 | * DISPATCH_TIME_FOREVER means "infinity" and every value in between is an 59 | * opaque encoding. 60 | */ 61 | typedef uint64_t dispatch_time_t; 62 | 63 | #define DISPATCH_TIME_NOW 0 64 | #define DISPATCH_TIME_FOREVER (~0ull) 65 | 66 | /*! 67 | * @function dispatch_time 68 | * 69 | * @abstract 70 | * Create dispatch_time_t relative to the default clock or modify an existing 71 | * dispatch_time_t. 72 | * 73 | * @discussion 74 | * On Mac OS X the default clock is based on mach_absolute_time(). 75 | * 76 | * @param when 77 | * An optional dispatch_time_t to add nanoseconds to. If zero is passed, then 78 | * dispatch_time() will use the result of mach_absolute_time(). 79 | * 80 | * @param delta 81 | * Nanoseconds to add. 82 | * 83 | * @result 84 | * A new dispatch_time_t. 85 | */ 86 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 87 | DISPATCH_EXPORT DISPATCH_NOTHROW 88 | dispatch_time_t 89 | dispatch_time(dispatch_time_t when, int64_t delta); 90 | 91 | /*! 92 | * @function dispatch_walltime 93 | * 94 | * @abstract 95 | * Create a dispatch_time_t using the wall clock. 96 | * 97 | * @discussion 98 | * On Mac OS X the wall clock is based on gettimeofday(3). 99 | * 100 | * @param when 101 | * A struct timespect to add time to. If NULL is passed, then 102 | * dispatch_walltime() will use the result of gettimeofday(3). 103 | * 104 | * @param delta 105 | * Nanoseconds to add. 106 | * 107 | * @result 108 | * A new dispatch_time_t. 109 | */ 110 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 111 | DISPATCH_EXPORT DISPATCH_NOTHROW 112 | dispatch_time_t 113 | dispatch_walltime(const struct timespec *when, int64_t delta); 114 | 115 | __DISPATCH_END_DECLS 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /libdispatch-197/libdispatch.plist: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | build = libdispatch; 4 | inherits = 10C540; 5 | projects = { 6 | libdispatch = { 7 | branch = "/trunk"; 8 | dependencies = { 9 | build = ( 10 | bootstrap_cmds, 11 | cctools, 12 | file_cmds, 13 | gcc, 14 | gcc_select, 15 | shell_cmds, 16 | system_cmds, 17 | text_cmds, 18 | ); 19 | lib = ( 20 | ICU, 21 | Libsystem, 22 | xnu, 23 | ); 24 | header = ( 25 | CarbonHeaders, 26 | Libc_headers, 27 | Libsyscall, 28 | libclosure, 29 | ); 30 | }; 31 | source_sites = ( 32 | "http://svn.macosforge.org/repository/libdispatch/", 33 | ); 34 | version = 1; 35 | }; 36 | }; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /libdispatch-197/m4/atomic.m4: -------------------------------------------------------------------------------- 1 | AC_DEFUN([DISPATCH_C_ATOMIC_BUILTINS], [ 2 | # 3 | # This is a bit subtle: on i386 systems without at least -march=i486 defined, 4 | # certain built-in atomics fall back to depending on undefined symbols if 5 | # their return values are used. 6 | # 7 | AC_CACHE_CHECK([for gcc atomic builtins],[dispatch_cv_atomic], 8 | [AC_LINK_IFELSE([AC_LANG_PROGRAM([],[[ 9 | int i, x =0; 10 | i = __sync_add_and_fetch(&x,1); 11 | return x;]])],[dispatch_cv_atomic=yes], 12 | [saveCFLAGS="$CFLAGS" 13 | CFLAGS="$CFLAGS -march=i486" 14 | AC_LINK_IFELSE([AC_LANG_PROGRAM([],[[ 15 | int i, x =0; 16 | i = __sync_add_and_fetch(&x,1); 17 | return x;]])],[CFLAGS="$saveCFLAGS" 18 | dispatch_cv_atomic="-march=i486" 19 | ])])]) 20 | 21 | ]) 22 | -------------------------------------------------------------------------------- /libdispatch-197/m4/blocks.m4: -------------------------------------------------------------------------------- 1 | AC_DEFUN([DISPATCH_C_BLOCKS], [ 2 | # 3 | # Allow configure to be passed a path to the directory where it should look 4 | # for the Blocks runtime library, if any. 5 | # 6 | AC_ARG_WITH([blocks-runtime], 7 | [AS_HELP_STRING([--with-blocks-runtime], 8 | [Specify path to the blocks runtime])], 9 | [blocks_runtime=${withval} 10 | LIBS="$LIBS -L$blocks_runtime"] 11 | ) 12 | 13 | # 14 | # Detect compiler support for Blocks; perhaps someday -fblocks won't be 15 | # required, in which case we'll need to change this. 16 | # 17 | AC_CACHE_CHECK([for C Blocks support], [dispatch_cv_cblocks], [ 18 | saveCFLAGS="$CFLAGS" 19 | CFLAGS="$CFLAGS -fblocks" 20 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[(void)^{int i; i = 0; }();])], [ 21 | CFLAGS="$saveCFLAGS" 22 | dispatch_cv_cblocks="-fblocks" 23 | ], [ 24 | CFLAGS="$saveCFLAGS" 25 | dispatch_cv_cblocks="no" 26 | ]) 27 | ]) 28 | 29 | AS_IF([test "x$dispatch_cv_cblocks" != "xno"], [ 30 | CBLOCKS_FLAGS="$dispatch_cv_cblocks" 31 | 32 | # 33 | # It may be necessary to directly link the Blocks runtime on some 34 | # systems, so give it a try if we can't link a C program that uses 35 | # Blocks. We will want to remove this at somepoint, as really -fblocks 36 | # should force that linkage already. 37 | # 38 | saveCFLAGS="$CFLAGS" 39 | CFLAGS="$CFLAGS -fblocks -O0" 40 | AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime]) 41 | AC_TRY_LINK([], [ 42 | ^{ int j; j=0; }(); 43 | ], [ 44 | AC_MSG_RESULT([no]); 45 | ], [ 46 | saveLIBS="$LIBS" 47 | LIBS="$LIBS -lBlocksRuntime" 48 | AC_TRY_LINK([], [ 49 | ^{ int k; k=0; }(); 50 | ], [ 51 | AC_MSG_RESULT([-lBlocksRuntime]) 52 | ], [ 53 | AC_MSG_RESULT([-lBlocksRuntime]) 54 | #AC_MSG_ERROR([can't find Blocks runtime]) 55 | ]) 56 | ]) 57 | CFLAGS="$saveCFLAGS" 58 | have_cblocks=true 59 | ], [ 60 | CBLOCKS_FLAGS="" 61 | have_cblocks=false 62 | ]) 63 | AM_CONDITIONAL(HAVE_CBLOCKS, $have_cblocks) 64 | AC_SUBST([CBLOCKS_FLAGS]) 65 | 66 | # 67 | # Because a different C++ compiler may be specified than C compiler, we have 68 | # to do it again for C++. 69 | # 70 | AC_LANG_PUSH([C++]) 71 | AC_CACHE_CHECK([for C++ Blocks support], [dispatch_cv_cxxblocks], [ 72 | saveCXXFLAGS="$CXXFLAGS" 73 | CXXFLAGS="$CXXFLAGS -fblocks" 74 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[(void)^{int i; i = 0; }();])], [ 75 | CXXFLAGS="$saveCXXFLAGS" 76 | dispatch_cv_cxxblocks="-fblocks" 77 | ], [ 78 | CXXFLAGS="$saveCXXFLAGS" 79 | dispatch_cv_cxxblocks="no" 80 | ]) 81 | ]) 82 | 83 | AS_IF([test "x$dispatch_cv_cxxblocks" != "xno"], [ 84 | CXXBLOCKS_FLAGS="$dispatch_cv_cxxblocks" 85 | 86 | saveCXXFLAGS="$CXXFLAGS" 87 | CXXFLAGS="$CXXFLAGS -fblocks -O0" 88 | AC_MSG_CHECKING([whether additional libraries are required for the Blocks runtime]) 89 | AC_TRY_LINK([], [ 90 | ^{ int j; j=0; }(); 91 | ], [ 92 | AC_MSG_RESULT([no]); 93 | ], [ 94 | saveLIBS="$LIBS" 95 | LIBS="$LIBS -lBlocksRuntime" 96 | AC_TRY_LINK([], [ 97 | ^{ int k; k=0; }(); 98 | ], [ 99 | AC_MSG_RESULT([-lBlocksRuntime]) 100 | ], [ 101 | AC_MSG_RESULT([-lBlocksRuntime]) 102 | # AC_MSG_ERROR([can't find Blocks runtime]) 103 | ]) 104 | ]) 105 | CXXFLAGS="$saveCXXFLAGS" 106 | have_cxxblocks=true 107 | ], [ 108 | CXXBLOCKS_FLAGS="" 109 | have_cxxblocks=false 110 | ]) 111 | AC_LANG_POP([C++]) 112 | AM_CONDITIONAL(HAVE_CXXBLOCKS, $have_cxxblocks) 113 | AC_SUBST([CXXBLOCKS_FLAGS]) 114 | ]) 115 | -------------------------------------------------------------------------------- /libdispatch-197/m4/private-extern.m4: -------------------------------------------------------------------------------- 1 | # 2 | # Does the platform support __private_extern__? There is surely a better way 3 | # to check this. 4 | # 5 | AC_DEFUN([DISPATCH_C_PRIVATE_EXTERN], [ 6 | 7 | AC_LANG_WERROR 8 | 9 | AC_CACHE_CHECK([for __attribute__((visibility("hidden")))], 10 | dispatch_cv_hidden_visibility_attribute, [ 11 | AC_TRY_LINK([int foo; extern __attribute__ ((visibility ("hidden"))) int foo;], 12 | [foo = 0;], 13 | [dispatch_cv_hidden_visibility_attribute=yes], 14 | [dispatch_cv_hidden_visibility_attribute=no])]) 15 | 16 | AC_CACHE_CHECK([for __private_extern__], 17 | dispatch_cv_private_extern, [ 18 | AC_TRY_LINK([], [__private_extern__ int foo;], 19 | [dispatch_cv_private_extern=yes], 20 | [dispatch_cv_private_extern=no])]) 21 | 22 | if test $host_os = "linux-gnu" ; then 23 | # 24 | # WORKAROUND: Building with clang and the GNU linker causes this error: 25 | # 26 | # /usr/bin/ld: .libs/libdispatch_la-apply.o: relocation R_X86_64_PC32 against symbol `_dispatch_hw_config' can not be used when making a shared object; recompile with -fPIC 27 | # 28 | AC_DEFINE([__private_extern__], [extern], 29 | [Define to a replacement for __private_extern]) 30 | elif test $dispatch_cv_private_extern = yes; then 31 | AC_DEFINE(HAVE_PRIVATE_EXTERN, 1, Define if __private_extern__ present) 32 | elif test $dispatch_cv_hidden_visibility_attribute = yes; then 33 | AC_DEFINE(HAVE_PRIVATE_EXTERN, 1, Define if __private_extern__ present) 34 | AC_DEFINE([__private_extern__], [extern __attribute__ ((visibility ("hidden")))], 35 | [Define to a replacement for __private_extern]) 36 | else 37 | AC_DEFINE([__private_extern__], [extern], 38 | [Define to a replacement for __private_extern]) 39 | fi 40 | 41 | ]) 42 | -------------------------------------------------------------------------------- /libdispatch-197/man/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # 4 | 5 | man3_MANS= \ 6 | dispatch.3 \ 7 | dispatch_after.3 \ 8 | dispatch_api.3 \ 9 | dispatch_apply.3 \ 10 | dispatch_async.3 \ 11 | dispatch_benchmark.3 \ 12 | dispatch_group_create.3 \ 13 | dispatch_object.3 \ 14 | dispatch_once.3 \ 15 | dispatch_queue_create.3 \ 16 | dispatch_semaphore_create.3 \ 17 | dispatch_source_create.3 \ 18 | dispatch_time.3 19 | 20 | # 21 | # Install man page symlinks. Is there a better way to do this in automake? 22 | # 23 | install-data-hook: 24 | cd $(DESTDIR)$(mandir)/man3 && \ 25 | $(LN_S) -f dispatch_after.3 dispatch_after_f.3 26 | cd $(DESTDIR)$(mandir)/man3 && \ 27 | $(LN_S) -f dispatch_apply.3 dispatch_apply_f.3 28 | cd $(DESTDIR)$(mandir)/man3 && \ 29 | $(LN_S) -f dispatch_async.3 dispatch_sync.3 30 | cd $(DESTDIR)$(mandir)/man3 && \ 31 | $(LN_S) -f dispatch_async.3 dispatch_async_f.3 32 | cd $(DESTDIR)$(mandir)/man3 && \ 33 | $(LN_S) -f dispatch_async.3 dispatch_sync_f.3 34 | cd $(DESTDIR)$(mandir)/man3 && \ 35 | $(LN_S) -f dispatch_benchmark.3 dispatch_benchmark_f.3 36 | cd $(DESTDIR)$(mandir)/man3 && \ 37 | $(LN_S) -f dispatch_group_create.3 dispatch_group_enter.3 38 | cd $(DESTDIR)$(mandir)/man3 && \ 39 | $(LN_S) -f dispatch_group_create.3 dispatch_group_leave.3 40 | cd $(DESTDIR)$(mandir)/man3 && \ 41 | $(LN_S) -f dispatch_group_create.3 dispatch_group_wait.3 42 | cd $(DESTDIR)$(mandir)/man3 && \ 43 | $(LN_S) -f dispatch_group_create.3 dispatch_group_notify.3 44 | cd $(DESTDIR)$(mandir)/man3 && \ 45 | $(LN_S) -f dispatch_group_create.3 dispatch_group_notify_f.3 46 | cd $(DESTDIR)$(mandir)/man3 && \ 47 | $(LN_S) -f dispatch_group_create.3 dispatch_group_async.3 48 | cd $(DESTDIR)$(mandir)/man3 && \ 49 | $(LN_S) -f dispatch_group_create.3 dispatch_group_async_f.3 50 | cd $(DESTDIR)$(mandir)/man3 && \ 51 | $(LN_S) -f dispatch_object.3 dispatch_retain.3 52 | cd $(DESTDIR)$(mandir)/man3 && \ 53 | $(LN_S) -f dispatch_object.3 dispatch_release.3 54 | cd $(DESTDIR)$(mandir)/man3 && \ 55 | $(LN_S) -f dispatch_object.3 dispatch_suspend.3 56 | cd $(DESTDIR)$(mandir)/man3 && \ 57 | $(LN_S) -f dispatch_object.3 dispatch_resume.3 58 | cd $(DESTDIR)$(mandir)/man3 && \ 59 | $(LN_S) -f dispatch_object.3 dispatch_get_context.3 60 | cd $(DESTDIR)$(mandir)/man3 && \ 61 | $(LN_S) -f dispatch_object.3 dispatch_set_context.3 62 | cd $(DESTDIR)$(mandir)/man3 && \ 63 | $(LN_S) -f dispatch_object.3 dispatch_set_finalizer_f.3 64 | cd $(DESTDIR)$(mandir)/man3 && \ 65 | $(LN_S) -f dispatch_once.3 dispatch_once_f.3 66 | cd $(DESTDIR)$(mandir)/man3 && \ 67 | $(LN_S) -f dispatch_queue_create.3 dispatch_queue_get_label.3 68 | cd $(DESTDIR)$(mandir)/man3 && \ 69 | $(LN_S) -f dispatch_queue_create.3 dispatch_get_current_queue.3 70 | cd $(DESTDIR)$(mandir)/man3 && \ 71 | $(LN_S) -f dispatch_queue_create.3 dispatch_get_global_queue.3 72 | cd $(DESTDIR)$(mandir)/man3 && \ 73 | $(LN_S) -f dispatch_queue_create.3 dispatch_get_main_queue.3 74 | cd $(DESTDIR)$(mandir)/man3 && \ 75 | $(LN_S) -f dispatch_queue_create.3 dispatch_main.3 76 | cd $(DESTDIR)$(mandir)/man3 && \ 77 | $(LN_S) -f dispatch_queue_create.3 dispatch_set_target_queue.3 78 | cd $(DESTDIR)$(mandir)/man3 && \ 79 | $(LN_S) -f dispatch_semaphore_create.3 \ 80 | dispatch_semaphore_signal.3 81 | cd $(DESTDIR)$(mandir)/man3 && \ 82 | $(LN_S) -f dispatch_semaphore_create.3 \ 83 | dispatch_semaphore_wait.3 84 | cd $(DESTDIR)$(mandir)/man3 && \ 85 | $(LN_S) -f dispatch_source_create.3 \ 86 | dispatch_source_set_event_handler.3 87 | cd $(DESTDIR)$(mandir)/man3 && \ 88 | $(LN_S) -f dispatch_source_create.3 \ 89 | dispatch_source_set_event_handler_f.3 90 | cd $(DESTDIR)$(mandir)/man3 && \ 91 | $(LN_S) -f dispatch_source_create.3 \ 92 | dispatch_source_set_cancel_handler.3 93 | cd $(DESTDIR)$(mandir)/man3 && \ 94 | $(LN_S) -f dispatch_source_create.3 \ 95 | dispatch_source_set_cancel_handler_f.3 96 | cd $(DESTDIR)$(mandir)/man3 && \ 97 | $(LN_S) -f dispatch_source_create.3 \ 98 | dispatch_source_cancel.3 99 | cd $(DESTDIR)$(mandir)/man3 && \ 100 | $(LN_S) -f dispatch_source_create.3 \ 101 | dispatch_source_testcancel.3 102 | cd $(DESTDIR)$(mandir)/man3 && \ 103 | $(LN_S) -f dispatch_source_create.3 \ 104 | dispatch_source_get_handle.3 105 | cd $(DESTDIR)$(mandir)/man3 && \ 106 | $(LN_S) -f dispatch_source_create.3 dispatch_source_get_mask.3 107 | cd $(DESTDIR)$(mandir)/man3 && \ 108 | $(LN_S) -f dispatch_source_create.3 \ 109 | dispatch_source_get_data.3 110 | cd $(DESTDIR)$(mandir)/man3 && \ 111 | $(LN_S) -f dispatch_source_create.3 \ 112 | dispatch_source_merge_data.3 113 | cd $(DESTDIR)$(mandir)/man3 && \ 114 | $(LN_S) -f dispatch_source_create.3 \ 115 | dispatch_source_set_timer.3 116 | cd $(DESTDIR)$(mandir)/man3 && \ 117 | $(LN_S) -f dispatch_time.3 dispatch_walltime.3 118 | 119 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch 7 | .Nd the dispatch framework 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Sh DESCRIPTION 11 | The dispatch framework allows blocks to be scheduled for asynchronous and 12 | concurrent execution via the core functions described in 13 | .Xr dispatch_async 3 and 14 | .Xr dispatch_apply 3 . 15 | .Pp 16 | Dispatch queues are the basic units of organization of blocks. Several queues 17 | are created by default, and applications may create additional queues for their 18 | own use. See 19 | .Xr dispatch_queue_create 3 20 | for more information. 21 | .Pp 22 | Dispatch groups allow applications to track the progress of blocks submitted to 23 | queues and take action when the blocks complete. See 24 | .Xr dispatch_group_create 3 25 | for more information. 26 | .Pp 27 | The dispatch framework also provides functions to monitor underlying system 28 | events and automatically submit event handler blocks to dispatch queues. 29 | .Sh SEE ALSO 30 | .Xr dispatch_after 3 , 31 | .Xr dispatch_api 3 , 32 | .Xr dispatch_apply 3 , 33 | .Xr dispatch_async 3 , 34 | .Xr dispatch_benchmark 3 , 35 | .Xr dispatch_group_create 3 , 36 | .Xr dispatch_object 3 , 37 | .Xr dispatch_once 3 , 38 | .Xr dispatch_queue_create 3 , 39 | .Xr dispatch_semaphore_create 3 , 40 | .Xr dispatch_source_create 3 , 41 | .Xr dispatch_time 3 42 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_after.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_after 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_after 7 | .Nd schedule blocks for deferred execution 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Ft void 11 | .Fo dispatch_after 12 | .Fa "dispatch_time_t when" "dispatch_queue_t queue" "void (^block)(void)" 13 | .Fc 14 | .Ft void 15 | .Fo dispatch_after_f 16 | .Fa "dispatch_time_t when" "dispatch_queue_t queue" "void *context" "void (*function)(void *)" 17 | .Fc 18 | .Sh DESCRIPTION 19 | The 20 | .Fn dispatch_after 21 | function submits the 22 | .Fa block 23 | to the given 24 | .Fa queue 25 | at the time specified by the 26 | .Fa when 27 | parameter. 28 | The 29 | .Fa when 30 | parameter is a value created by 31 | .Fn dispatch_time 32 | or 33 | .Fn dispatch_walltime . 34 | .Pp 35 | For a more detailed description about submitting blocks to queues, see 36 | .Xr dispatch_async 3 . 37 | .Sh CAVEATS 38 | Specifying 39 | .Vt DISPATCH_TIME_NOW 40 | as the 41 | .Fa when 42 | parameter 43 | is supported, but is not as efficient as calling 44 | .Fn dispatch_async . 45 | The result of passing 46 | .Vt DISPATCH_TIME_FOREVER 47 | as the 48 | .Fa when 49 | parameter is undefined. 50 | .Sh FUNDAMENTALS 51 | The 52 | .Fn dispatch_after 53 | function is a wrapper around 54 | .Fn dispatch_after_f . 55 | .Sh SEE ALSO 56 | .Xr dispatch 3 , 57 | .Xr dispatch_async 3 , 58 | .Xr dispatch_time 3 59 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_api.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_api 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_api 7 | .Nd Designing API using dispatch 8 | .Sh DESCRIPTION 9 | The following is a brief summary of some of the common design patterns to 10 | consider when designing and implementing API in terms of dispatch queues 11 | and blocks. 12 | .Pp 13 | A general recommendation is to allow both a callback block and target dispatch 14 | queue to be specified. This gives the application the greatest flexibility in 15 | handling asynchronous events. 16 | .Pp 17 | It's also recommended that interfaces take only a single block as the last 18 | parameter. This is both for consistency across projects, as well as the visual 19 | aesthetics of multiline blocks that are declared inline. The dispatch queue to 20 | which the block will be submitted should immediately precede the block argument 21 | (second-to-last argument). For example: 22 | .Pp 23 | .Bd -literal -offset indent 24 | read_async(file, callback_queue, ^{ 25 | printf("received callback.\\n"); 26 | }); 27 | .Ed 28 | .Pp 29 | When function pointer alternatives to interfaces that take blocks are provided, 30 | the argument order of the function signature should be identical to the block 31 | variant; with the exception that the block argument is replaced with a context 32 | pointer, and a new last parameter is added, which is the function to call. 33 | .Pp 34 | The function based callback should pass the context pointer as the first 35 | argument, and the subsequent arguments should be identical to the block based 36 | variant (albeit offset by one in order). 37 | .Pp 38 | It is also important to use consistent naming. The dispatch API, for example, 39 | uses the suffix "_f" for function based variants. 40 | .Pp 41 | .Sh SEE ALSO 42 | .Xr dispatch 3 , 43 | .Xr dispatch_async 3 , 44 | .Xr dispatch_queue_create 3 45 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_apply.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_apply 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_apply 7 | .Nd schedule blocks for iterative execution 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Ft void 11 | .Fo dispatch_apply 12 | .Fa "size_t iterations" "dispatch_queue_t queue" "void (^block)(size_t)" 13 | .Fc 14 | .Ft void 15 | .Fo dispatch_apply_f 16 | .Fa "size_t iterations" "dispatch_queue_t queue" "void *context" "void (*function)(void *, size_t)" 17 | .Fc 18 | .Sh DESCRIPTION 19 | The 20 | .Fn dispatch_apply 21 | function provides data-level concurrency through a "for (;;)" loop like primitive: 22 | .Bd -literal 23 | dispatch_queue_t the_queue = dispatch_get_concurrent_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT); 24 | size_t iterations = 10; 25 | 26 | // 'idx' is zero indexed, just like: 27 | // for (idx = 0; idx < iterations; idx++) 28 | 29 | dispatch_apply(iterations, the_queue, ^(size_t idx) { 30 | printf("%zu\\n", idx); 31 | }); 32 | .Ed 33 | .Pp 34 | Like a "for (;;)" loop, the 35 | .Fn dispatch_apply 36 | function is synchronous. 37 | If asynchronous behavior is desired, please wrap the call to 38 | .Fn dispatch_apply 39 | with a call to 40 | .Fn dispatch_async 41 | against another queue. 42 | .Pp 43 | Sometimes, when the block passed to 44 | .Fn dispatch_apply 45 | is simple, the use of striding can tune performance. 46 | Calculating the optimal stride is best left to experimentation. 47 | Start with a stride of one and work upwards until the desired performance is 48 | achieved (perhaps using a power of two search): 49 | .Bd -literal 50 | #define STRIDE 3 51 | 52 | dispatch_apply(count / STRIDE, queue, ^(size_t idx) { 53 | size_t j = idx * STRIDE; 54 | size_t j_stop = j + STRIDE; 55 | do { 56 | printf("%zu\\n", j++); 57 | } while (j < j_stop); 58 | }); 59 | 60 | size_t i; 61 | for (i = count - (count % STRIDE); i < count; i++) { 62 | printf("%zu\\n", i); 63 | } 64 | .Ed 65 | .Sh FUNDAMENTALS 66 | Conceptually, 67 | .Fn dispatch_apply 68 | is a convenient wrapper around 69 | .Fn dispatch_async 70 | and a semaphore to wait for completion. 71 | In practice, the dispatch library optimizes this function. 72 | .Pp 73 | The 74 | .Fn dispatch_apply 75 | function is a wrapper around 76 | .Fn dispatch_apply_f . 77 | .Sh SEE ALSO 78 | .Xr dispatch 3 , 79 | .Xr dispatch_async 3 , 80 | .Xr dispatch_queue_create 3 , 81 | .Xr dispatch_semaphore_create 3 82 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_benchmark.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_benchmark 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_benchmark 7 | .Nd Measures block execution time 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Ft uint64_t 11 | .Fo dispatch_benchmark 12 | .Fa "size_t count" "void (^block)(void)" 13 | .Fc 14 | .Ft uint64_t 15 | .Fo dispatch_benchmark_f 16 | .Fa "size_t count" "void *context" "void (*function)(void *)" 17 | .Fc 18 | .Sh DESCRIPTION 19 | The 20 | .Fn dispatch_benchmark 21 | function executes the given 22 | .Fa block 23 | multiple times according to the 24 | .Fa count 25 | variable and then returns the average number of nanoseconds per execution. 26 | This function is for debugging and performance analysis work. 27 | For the best 28 | results, pass a high count value to 29 | .Fn dispatch_benchmark . 30 | When benchmarking concurrent code, please compare the 31 | serial version of the code against the concurrent version, and compare the 32 | concurrent version on different classes of hardware. 33 | Please look for inflection 34 | points with various data sets and keep the following facts in mind: 35 | .Pp 36 | .Bl -bullet -offset indent -compact 37 | .It 38 | Code bound by computational bandwidth may be inferred by proportional 39 | changes in performance as concurrency is increased. 40 | .It 41 | Code bound by memory bandwidth may be inferred by negligible changes in 42 | performance as concurrency is increased. 43 | .It 44 | Code bound by critical sections may be inferred by retrograde changes in 45 | performance as concurrency is increased. 46 | .Bl -bullet -offset indent -compact 47 | .It 48 | Intentional: locks, mutexes, and condition variables. 49 | .It 50 | Accidental: unrelated and frequently modified data on the same cache-line. 51 | .El 52 | .El 53 | .Sh RETURN VALUE 54 | The 55 | .Fn dispatch_benchmark 56 | function returns the average number of nanoseconds the given block takes to 57 | execute. 58 | .Sh SEE ALSO 59 | .Xr dispatch 3 60 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_group_create.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_group_create 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_group_create , 7 | .Nm dispatch_group_async , 8 | .Nm dispatch_group_wait , 9 | .Nm dispatch_group_notify 10 | .Nd group blocks submitted to queues 11 | .Sh SYNOPSIS 12 | .Fd #include 13 | .Ft dispatch_group_t 14 | .Fo dispatch_group_create 15 | .Fa void 16 | .Fc 17 | .Ft void 18 | .Fo dispatch_group_enter 19 | .Fa "dispatch_group_t group" 20 | .Fc 21 | .Ft void 22 | .Fo dispatch_group_leave 23 | .Fa "dispatch_group_t group" 24 | .Fc 25 | .Ft long 26 | .Fo dispatch_group_wait 27 | .Fa "dispatch_group_t group" "dispatch_time_t timeout" 28 | .Fc 29 | .Ft void 30 | .Fo dispatch_group_notify 31 | .Fa "dispatch_group_t group" "dispatch_queue_t queue" "void (^block)(void)" 32 | .Fc 33 | .Ft void 34 | .Fo dispatch_group_notify_f 35 | .Fa "dispatch_group_t group" "dispatch_queue_t queue" "void *context" "void (*function)(void *)" 36 | .Fc 37 | .Ft void 38 | .Fo dispatch_group_async 39 | .Fa "dispatch_group_t group" "dispatch_queue_t queue" "void (^block)(void)" 40 | .Fc 41 | .Ft void 42 | .Fo dispatch_group_async_f 43 | .Fa "dispatch_group_t group" "dispatch_queue_t queue" "void *context" "void (*function)(void *)" 44 | .Fc 45 | .Sh DESCRIPTION 46 | A dispatch group is an association of one or more blocks submitted to dispatch 47 | queues for asynchronous invocation. 48 | Applications may use dispatch groups to 49 | wait for the completion of blocks associated with the group. 50 | .Pp 51 | The 52 | .Fn dispatch_group_create 53 | function returns a new and empty dispatch group. 54 | .Pp 55 | The 56 | .Fn dispatch_group_enter 57 | and 58 | .Fn dispatch_group_leave 59 | functions update the number of blocks running within a group. 60 | .Pp 61 | The 62 | .Fn dispatch_group_wait 63 | function waits until all blocks associated with the 64 | .Fa group 65 | have completed, or until the specified 66 | .Fa timeout 67 | has elapsed. 68 | If the 69 | .Fa group 70 | becomes empty within the specified amount of time, the function will return zero 71 | indicating success. Otherwise, a non-zero return code will be returned. 72 | When 73 | .Va DISPATCH_TIME_FOREVER 74 | is passed as the 75 | .Fa timeout , 76 | calls to this function will wait an unlimited amount of time until the group 77 | becomes empty and the return value is always zero. 78 | .Pp 79 | The 80 | .Fn dispatch_group_notify 81 | function provides asynchronous notification of the completion of the blocks 82 | associated with the 83 | .Fa group 84 | by submitting the 85 | .Fa block 86 | to the specified 87 | .Fa queue 88 | once all blocks associated with the 89 | .Fa group 90 | have completed. 91 | The system holds a reference to the dispatch group while an asynchronous 92 | notification is pending, therefore it is valid to release the 93 | .Fa group 94 | after setting a notification block. 95 | The group will be empty at the time the notification block is submitted to the 96 | target queue. The group may either be released with 97 | .Fn dispatch_release 98 | or reused for additional operations. 99 | .Pp 100 | The 101 | .Fn dispatch_group_async 102 | convenience function behaves like so: 103 | .Bd -literal 104 | void 105 | dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block) 106 | { 107 | dispatch_retain(group); 108 | dispatch_group_enter(group); 109 | dispatch_async(queue, ^{ 110 | block(); 111 | dispatch_group_leave(group); 112 | dispatch_release(group); 113 | }); 114 | } 115 | .Ed 116 | .Sh RETURN VALUE 117 | The 118 | .Fn dispatch_group_create 119 | function returns NULL on failure and non-NULL on success. 120 | .Pp 121 | The 122 | .Fn dispatch_group_wait 123 | function returns zero upon success and non-zero after the timeout expires. 124 | If the timeout is 125 | .Va DISPATCH_TIME_FOREVER , 126 | then 127 | .Fn dispatch_group_wait 128 | waits forever and always returns zero. 129 | .Sh MEMORY MODEL 130 | Dispatch groups are retained and released via calls to 131 | .Fn dispatch_retain 132 | and 133 | .Fn dispatch_release . 134 | .Sh FUNDAMENTALS 135 | The 136 | .Fn dispatch_group_async 137 | and 138 | .Fn dispatch_group_notify 139 | functions are wrappers around 140 | .Fn dispatch_group_async_f 141 | and 142 | .Fn dispatch_group_notify_f 143 | respectively. 144 | .Sh SEE ALSO 145 | .Xr dispatch 3 , 146 | .Xr dispatch_async 3 , 147 | .Xr dispatch_object 3 , 148 | .Xr dispatch_queue_create 3 , 149 | .Xr dispatch_semaphore_create 3 , 150 | .Xr dispatch_time 3 151 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_object.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_object 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_object 7 | .Nd General manipulation of dispatch objects 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Ft void 11 | .Fo dispatch_retain 12 | .Fa "dispatch_object_t object" 13 | .Fc 14 | .Ft void 15 | .Fo dispatch_release 16 | .Fa "dispatch_object_t object" 17 | .Fc 18 | .Ft void 19 | .Fo dispatch_suspend 20 | .Fa "dispatch_object_t object" 21 | .Fc 22 | .Ft void 23 | .Fo dispatch_resume 24 | .Fa "dispatch_object_t object" 25 | .Fc 26 | .Ft "void *" 27 | .Fo dispatch_get_context 28 | .Fa "dispatch_object_t object" 29 | .Fc 30 | .Ft void 31 | .Fo dispatch_set_context 32 | .Fa "dispatch_object_t object" 33 | .Fa "void *context" 34 | .Fc 35 | .Ft void 36 | .Fo dispatch_set_finalizer_f 37 | .Fa "dispatch_object_t object" 38 | .Fa "dispatch_function_t finalizer" 39 | .Fc 40 | .Sh DESCRIPTION 41 | Dispatch objects share functions for coordinating memory management, suspension, 42 | cancellation and context pointers. While all dispatch objects are retainable, 43 | not all objects support suspension, context pointers or finalizers (currently 44 | only queues and sources support these additional interfaces). 45 | .Sh MEMORY MANGEMENT 46 | Objects returned by creation functions in the dispatch framework may be 47 | uniformly retained and released with the functions 48 | .Fn dispatch_retain 49 | and 50 | .Fn dispatch_release 51 | respectively. 52 | .Pp 53 | The dispatch framework does not guarantee that any given client has the last or 54 | only reference to a given object. Objects may be retained internally by the 55 | system. 56 | .Sh SUSPENSION 57 | The invocation of blocks on dispatch queues or dispatch sources may be suspended 58 | or resumed with the functions 59 | .Fn dispatch_suspend 60 | and 61 | .Fn dispatch_resume 62 | respectively. 63 | The dispatch framework always checks the suspension status before executing a 64 | block, but such changes never affect a block during execution (non-preemptive). 65 | Therefore the suspension of an object is asynchronous, unless it is performed 66 | from the context of the target queue for the given object. 67 | The result of suspending or resuming an object that is not a dispatch queue or 68 | a dispatch source is undefined. 69 | .Pp 70 | .Em Important : 71 | suspension applies to all aspects of the dispatch object life cycle, including 72 | the finalizer function and cancellation handler. Therefore it is important to 73 | balance calls to 74 | .Fn dispatch_suspend 75 | and 76 | .Fn dispatch_resume 77 | such that the dispatch object is fully resumed when the last reference is 78 | released. The result of releasing all references to a dispatch object while in 79 | a suspended state is undefined. 80 | .Sh CONTEXT POINTERS 81 | Dispatch queues and sources support supplemental context pointers. The value of 82 | the context pointer may be retrieved and updated with 83 | .Fn dispatch_get_context 84 | and 85 | .Fn dispatch_set_context 86 | respectively. 87 | The 88 | .Fn dispatch_set_finalizer_f 89 | specifies an optional per-object finalizer function that is invoked 90 | asynchronously if the context pointer is not NULL when the last 91 | reference to the object is released. 92 | This gives the 93 | application an opportunity to free the context data associated with the object. 94 | .Pp 95 | The result of getting or setting the context of an object that is not a 96 | dispatch queue or a dispatch source is undefined. 97 | .Sh SEE ALSO 98 | .Xr dispatch 3 , 99 | .Xr dispatch_group_create 3 , 100 | .Xr dispatch_queue_create 3 , 101 | .Xr dispatch_semaphore_create 3 , 102 | .Xr dispatch_source_create 3 103 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_once.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_once 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_once 7 | .Nd execute a block only once 8 | .Sh SYNOPSIS 9 | .Fd #include 10 | .Ft void 11 | .Fo dispatch_once 12 | .Fa "dispatch_once_t *predicate" "void (^block)(void)" 13 | .Fc 14 | .Ft void 15 | .Fo dispatch_once_f 16 | .Fa "dispatch_once_t *predicate" "void *context" "void (*function)(void *)" 17 | .Fc 18 | .Sh DESCRIPTION 19 | The 20 | .Fn dispatch_once 21 | function provides a simple and efficient mechanism to run an initializer 22 | exactly once, similar to 23 | .Xr pthread_once 3 . 24 | Well designed code hides the use of lazy initialization. 25 | For example: 26 | .Bd -literal 27 | FILE *getlogfile(void) 28 | { 29 | static dispatch_once_t pred; 30 | static FILE *logfile; 31 | 32 | dispatch_once(&pred, ^{ 33 | logfile = fopen(MY_LOG_FILE, "a"); 34 | }); 35 | 36 | return logfile; 37 | } 38 | .Ed 39 | .Pp 40 | .Sh FUNDAMENTALS 41 | The 42 | .Fn dispatch_once 43 | function is a wrapper around 44 | .Fn dispatch_once_f . 45 | .Sh SEE ALSO 46 | .Xr dispatch 3 47 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_semaphore_create.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_semaphore_create 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_semaphore_create , 7 | .Nm dispatch_semaphore_signal , 8 | .Nm dispatch_semaphore_wait 9 | .Nd synchronized counting semaphore 10 | .Sh SYNOPSIS 11 | .Fd #include 12 | .Ft dispatch_semaphore_t 13 | .Fo dispatch_semaphore_create 14 | .Fa "long count" 15 | .Fc 16 | .Ft long 17 | .Fo dispatch_semaphore_signal 18 | .Fa "dispatch_semaphore_t semaphore" 19 | .Fc 20 | .Ft long 21 | .Fo dispatch_semaphore_wait 22 | .Fa "dispatch_semaphore_t semaphore" "dispatch_time_t timeout" 23 | .Fc 24 | .Sh DESCRIPTION 25 | Dispatch semaphores are used to synchronize threads. 26 | The 27 | .Fa timeout 28 | parameter is creatable with the 29 | .Xr dispatch_time 3 30 | or 31 | .Xr dispatch_walltime 3 32 | functions. 33 | .Sh COMPLETION SYNCHRONIZATION 34 | If the 35 | .Fa count 36 | parameter is equal to zero, then the semaphore is useful for synchronizing completion of work. 37 | For example: 38 | .Bd -literal -offset indent 39 | sema = dispatch_semaphore_create(0); 40 | 41 | dispatch_async(queue, ^{ 42 | foo(); 43 | dispatch_semaphore_signal(sema); 44 | }); 45 | 46 | bar(); 47 | 48 | dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 49 | .Ed 50 | .Sh FINITE RESOURCE POOL 51 | If the 52 | .Fa count 53 | parameter is greater than zero, then the semaphore is useful for managing a finite pool of resources. 54 | For example, a library that wants to limit Unix descriptor usage: 55 | .Bd -literal -offset indent 56 | sema = dispatch_semaphore_create(getdtablesize() / 4); 57 | .Ed 58 | .Pp 59 | At each Unix FD allocation: 60 | .Bd -literal -offset indent 61 | dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 62 | fd = open("/etc/services", O_RDONLY); 63 | .Ed 64 | .Pp 65 | When each FD is closed: 66 | .Bd -literal -offset indent 67 | close(fd); 68 | dispatch_semaphore_signal(sema); 69 | .Ed 70 | .Sh RETURN VALUES 71 | The 72 | .Fn dispatch_semaphore_create 73 | function returns NULL if no memory is available or if the 74 | .Fa count 75 | parameter is less than zero. 76 | .Pp 77 | The 78 | .Fn dispatch_semaphore_signal 79 | function returns non-zero when a thread is woken. 80 | Otherwise, zero is returned. 81 | .Pp 82 | The 83 | .Fn dispatch_semaphore_wait 84 | function returns zero upon success and non-zero after the timeout expires. If the timeout is DISPATCH_TIME_FOREVER, then 85 | .Fn dispatch_semaphore_wait 86 | waits forever and always returns zero. 87 | .Sh MEMORY MODEL 88 | Dispatch semaphores are retained and released via calls to 89 | .Fn dispatch_retain 90 | and 91 | .Fn dispatch_release . 92 | .Sh CAVEATS 93 | Dispatch semaphores are strict counting semaphores. 94 | In other words, dispatch semaphores do not saturate at any particular value. 95 | Saturation can be achieved through atomic compare-and-swap logic. 96 | What follows is a saturating binary semaphore: 97 | .Bd -literal 98 | void 99 | saturating_semaphore_signal(dispatch_semaphore_t dsema, int *sent) 100 | { 101 | if (__sync_bool_compare_and_swap(sent, 0, 1)) { 102 | dispatch_semaphore_signal(dsema); 103 | } 104 | } 105 | 106 | void 107 | saturating_semaphore_wait(dispatch_semaphore_t dsema, int *sent) 108 | { 109 | *sent = 0; 110 | dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER); 111 | } 112 | .Ed 113 | .Sh SEE ALSO 114 | .Xr dispatch 3 , 115 | .Xr dispatch_object 3 116 | -------------------------------------------------------------------------------- /libdispatch-197/man/dispatch_time.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2009 Apple Inc. All rights reserved. 2 | .Dd May 1, 2009 3 | .Dt dispatch_time 3 4 | .Os Darwin 5 | .Sh NAME 6 | .Nm dispatch_time , 7 | .Nm dispatch_walltime 8 | .Nd Calculate temporal milestones 9 | .Sh SYNOPSIS 10 | .Fd #include 11 | .Vt static const dispatch_time_t DISPATCH_TIME_NOW = 0 ; 12 | .Vt static const dispatch_time_t DISPATCH_TIME_FOREVER = ~0ull ; 13 | .Ft dispatch_time_t 14 | .Fo dispatch_time 15 | .Fa "dispatch_time_t base" "int64_t offset" 16 | .Fc 17 | .Ft dispatch_time_t 18 | .Fo dispatch_walltime 19 | .Fa "struct timespec *base" "int64_t offset" 20 | .Fc 21 | .Sh DESCRIPTION 22 | The 23 | .Fn dispatch_time 24 | and 25 | .Fn dispatch_walltime 26 | functions provide a simple mechanism for expressing temporal milestones for use 27 | with dispatch functions that need timeouts or operate on a schedule. 28 | .Pp 29 | The 30 | .Fa dispatch_time_t 31 | type is a semi-opaque integer, with only the special values 32 | .Vt DISPATCH_TIME_NOW 33 | and 34 | .Vt DISPATCH_TIME_FOREVER 35 | being externally defined. All other values are represented using an internal 36 | format that is not safe for integer arithmetic or comparison. 37 | The internal format is subject to change. 38 | .Pp 39 | The 40 | .Fn dispatch_time 41 | function returns a milestone relative to an existing milestone after adding 42 | .Fa offset 43 | nanoseconds. 44 | If the 45 | .Fa base 46 | parameter maps internally to a wall clock, then the returned value is 47 | relative to the wall clock. 48 | Otherwise, if 49 | .Fa base 50 | is 51 | .Vt DISPATCH_TIME_NOW , 52 | then the the current time of the default host clock is used. 53 | .Pp 54 | The 55 | .Fn dispatch_walltime 56 | function is useful for creating a milestone relative to a fixed point in time 57 | using the wall clock, as specified by the optional 58 | .Fa base 59 | parameter. If 60 | .Fa base 61 | is NULL, then the current time of the wall clock is used. 62 | .Sh EDGE CONDITIONS 63 | The 64 | .Fn dispatch_time 65 | and 66 | .Fn dispatch_walltime 67 | functions detect overflow and underflow conditions when applying the 68 | .Fa offset 69 | parameter. 70 | .Pp 71 | Overflow causes 72 | .Vt DISPATCH_TIME_FOREVER 73 | to be returned. When 74 | .Fa base 75 | is 76 | .Vt DISPATCH_TIME_FOREVER , 77 | then the 78 | .Fa offset 79 | parameter is ignored. 80 | .Pp 81 | Underflow causes the smallest representable value to be 82 | returned for a given clock. 83 | .Sh EXAMPLES 84 | Create a milestone two seconds in the future: 85 | .Bd -literal -offset indent 86 | milestone = dispatch_time(DISPATCH_TIME_NOW, 2LL * NSEC_PER_SEC); 87 | .Ed 88 | .Pp 89 | Create a milestone for use as an infinite timeout: 90 | .Bd -literal -offset indent 91 | milestone = DISPATCH_TIME_FOREVER; 92 | .Ed 93 | .Pp 94 | Create a milestone on Tuesday, January 19, 2038: 95 | .Bd -literal -offset indent 96 | struct timespec ts; 97 | ts.tv_sec = 0x7FFFFFFF; 98 | ts.tv_nsec = 0; 99 | milestone = dispatch_walltime(&ts, 0); 100 | .Ed 101 | .Sh RETURN VALUE 102 | These functions return an abstract value for use with 103 | .Fn dispatch_after , 104 | .Fn dispatch_group_wait , 105 | or 106 | .Fn dispatch_semaphore_wait . 107 | .Sh SEE ALSO 108 | .Xr dispatch 3 , 109 | .Xr dispatch_after 3 , 110 | .Xr dispatch_group_create 3 , 111 | .Xr dispatch_semaphore_create 3 112 | -------------------------------------------------------------------------------- /libdispatch-197/src/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # 4 | 5 | lib_LTLIBRARIES=libdispatch.la 6 | noinst_LTLIBRARIES=libshims.la 7 | 8 | libdispatch_la_SOURCES= \ 9 | apply.c \ 10 | benchmark.c \ 11 | object.c \ 12 | once.c \ 13 | queue.c \ 14 | queue_kevent.c \ 15 | semaphore.c \ 16 | source.c \ 17 | source_kevent.c \ 18 | time.c 19 | 20 | libshims_la_SOURCES= \ 21 | shims/mach.c \ 22 | shims/time.c \ 23 | shims/tsd.c 24 | 25 | libdispatch_la_CFLAGS=-Wall 26 | INCLUDES=-I$(top_builddir) -I$(top_srcdir) \ 27 | @APPLE_LIBC_SOURCE_PATH@ @APPLE_XNU_SOURCE_PATH@ 28 | 29 | libdispatch_la_CFLAGS+=$(MARCH_FLAGS) $(CBLOCKS_FLAGS) $(KQUEUE_CFLAGS) 30 | 31 | if USE_LEGACY_API 32 | libdispatch_la_SOURCES+= \ 33 | legacy.c 34 | endif 35 | 36 | libdispatch_la_LIBADD=libshims.la $(KQUEUE_LIBS) 37 | libdispatch_la_DEPENDENCIES=libshims.la 38 | 39 | if USE_LIBPTHREAD_WORKQUEUE 40 | libdispatch_la_LIBADD+=-lpthread_workqueue 41 | endif 42 | 43 | if USE_MIG 44 | libdispatch_la_SOURCES+= \ 45 | protocolUser.c \ 46 | protocolServer.c 47 | BUILT_SOURCES= \ 48 | protocol.h \ 49 | protocolUser.c \ 50 | protocolServer.c \ 51 | protocolServer.h 52 | CLEANFILES=$BUILT_SOURCES 53 | 54 | protocol.h protocolUser.c protocolServer.h protocolServer.c: protocol.defs 55 | $(MIG) -user protocolUser.c -header protocol.h \ 56 | -server protocolServer.c -sheader protocolServer.h protocol.defs 57 | endif 58 | -------------------------------------------------------------------------------- /libdispatch-197/src/benchmark.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | 24 | struct __dispatch_benchmark_data_s { 25 | #if HAVE_MACH_ABSOLUTE_TIME 26 | mach_timebase_info_data_t tbi; 27 | #endif 28 | uint64_t loop_cost; 29 | void (*func)(void *); 30 | void *ctxt; 31 | size_t count; 32 | }; 33 | 34 | static void 35 | _dispatch_benchmark_init(void *context) 36 | { 37 | struct __dispatch_benchmark_data_s *bdata = (struct __dispatch_benchmark_data_s *)context; 38 | // try and simulate performance of real benchmark as much as possible 39 | // keep 'f', 'c' and 'cnt' in registers 40 | register void (*f)(void *) = bdata->func; 41 | register void *c = bdata->ctxt; 42 | register size_t cnt = bdata->count; 43 | size_t i = 0; 44 | uint64_t start, delta; 45 | #ifdef __LP64__ 46 | __uint128_t lcost; 47 | #else 48 | long double lcost; 49 | #endif 50 | #if HAVE_MACH_ABSOLUTE_TIME 51 | kern_return_t kr; 52 | 53 | kr = mach_timebase_info(&bdata->tbi); 54 | dispatch_assert_zero(kr); 55 | #endif 56 | 57 | start = _dispatch_absolute_time(); 58 | do { 59 | i++; 60 | f(c); 61 | } while (i < cnt); 62 | delta = _dispatch_absolute_time() - start; 63 | 64 | lcost = delta; 65 | #if HAVE_MACH_ABSOLUTE_TIME 66 | lcost *= bdata->tbi.numer; 67 | lcost /= bdata->tbi.denom; 68 | #endif 69 | lcost /= cnt; 70 | 71 | bdata->loop_cost = lcost; 72 | } 73 | 74 | #ifdef __BLOCKS__ 75 | uint64_t 76 | dispatch_benchmark(size_t count, void (^block)(void)) 77 | { 78 | struct Block_basic *bb = (struct Block_basic *)(void *)block; 79 | return dispatch_benchmark_f(count, block, (dispatch_function_t)bb->Block_invoke); 80 | } 81 | #endif 82 | 83 | uint64_t 84 | dispatch_benchmark_f(size_t count, register void *ctxt, register void (*func)(void *)) 85 | { 86 | static struct __dispatch_benchmark_data_s bdata = { 87 | .func = (dispatch_function_t)dummy_function, 88 | .count = 10000000ul, // ten million 89 | }; 90 | static dispatch_once_t pred; 91 | uint64_t ns, start, delta; 92 | #ifdef __LP64__ 93 | __uint128_t conversion, big_denom; 94 | #else 95 | long double conversion, big_denom; 96 | #endif 97 | size_t i = 0; 98 | 99 | dispatch_once_f(&pred, &bdata, _dispatch_benchmark_init); 100 | 101 | if (slowpath(count == 0)) { 102 | return 0; 103 | } 104 | 105 | start = _dispatch_absolute_time(); 106 | do { 107 | i++; 108 | func(ctxt); 109 | } while (i < count); 110 | delta = _dispatch_absolute_time() - start; 111 | 112 | conversion = delta; 113 | #if HAVE_MACH_ABSOLUTE_TIME 114 | conversion *= bdata.tbi.numer; 115 | big_denom = bdata.tbi.denom; 116 | #else 117 | big_denom = delta; 118 | #endif 119 | big_denom *= count; 120 | conversion /= big_denom; 121 | ns = conversion; 122 | 123 | return ns - bdata.loop_cost; 124 | } 125 | -------------------------------------------------------------------------------- /libdispatch-197/src/hw_shims.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_HW_SHIMS__ 28 | #define __DISPATCH_HW_SHIMS__ 29 | 30 | /* x86 has a 64 byte cacheline */ 31 | #define DISPATCH_CACHELINE_SIZE 64 32 | #define ROUND_UP_TO_CACHELINE_SIZE(x) (((x) + (DISPATCH_CACHELINE_SIZE - 1)) & ~(DISPATCH_CACHELINE_SIZE - 1)) 33 | #define ROUND_UP_TO_VECTOR_SIZE(x) (((x) + 15) & ~15) 34 | 35 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2) 36 | // GCC generates suboptimal register pressure 37 | // LLVM does better, but doesn't support tail calls 38 | // 6248590 __sync_*() intrinsics force a gratuitous "lea" instruction, with resulting register pressure 39 | #if 0 && defined(__i386__) || defined(__x86_64__) 40 | #define dispatch_atomic_xchg(p, n) ({ typeof(*(p)) _r; asm("xchg %0, %1" : "=r" (_r) : "m" (*(p)), "0" (n)); _r; }) 41 | #else 42 | #define dispatch_atomic_xchg(p, n) ((typeof(*(p)))__sync_lock_test_and_set((p), (n))) 43 | #endif 44 | #define dispatch_atomic_cmpxchg(p, o, n) __sync_bool_compare_and_swap((p), (o), (n)) 45 | #define dispatch_atomic_inc(p) __sync_add_and_fetch((p), 1) 46 | #define dispatch_atomic_dec(p) __sync_sub_and_fetch((p), 1) 47 | #define dispatch_atomic_add(p, v) __sync_add_and_fetch((p), (v)) 48 | #define dispatch_atomic_sub(p, v) __sync_sub_and_fetch((p), (v)) 49 | #define dispatch_atomic_or(p, v) __sync_fetch_and_or((p), (v)) 50 | #define dispatch_atomic_and(p, v) __sync_fetch_and_and((p), (v)) 51 | #if defined(__i386__) || defined(__x86_64__) 52 | /* GCC emits nothing for __sync_synchronize() on i386/x86_64. */ 53 | #define dispatch_atomic_barrier() __asm__ __volatile__("mfence") 54 | #else 55 | #define dispatch_atomic_barrier() __sync_synchronize() 56 | #endif 57 | #else 58 | #error "Please upgrade to GCC 4.2 or newer." 59 | #endif 60 | 61 | #if defined(__i386__) || defined(__x86_64__) 62 | #define _dispatch_hardware_pause() asm("pause") 63 | #define _dispatch_debugger() asm("int3") 64 | #else 65 | #define _dispatch_hardware_pause() asm("") 66 | #define _dispatch_debugger() asm("trap") 67 | #endif 68 | // really just a low level abort() 69 | #define _dispatch_hardware_crash() __builtin_trap() 70 | 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /libdispatch-197/src/kevent_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_KEVENT_INTERNAL__ 28 | #define __DISPATCH_KEVENT_INTERNAL__ 29 | 30 | #include 31 | #include 32 | 33 | struct dispatch_kevent_s { 34 | TAILQ_ENTRY(dispatch_kevent_s) dk_list; 35 | TAILQ_HEAD(, dispatch_source_s) dk_sources; 36 | struct kevent dk_kevent; 37 | }; 38 | 39 | extern const struct dispatch_source_vtable_s _dispatch_source_kevent_vtable; 40 | 41 | #if DISPATCH_DEBUG 42 | void dispatch_debug_kevents(struct kevent* kev, size_t count, const char* str); 43 | #else 44 | #define dispatch_debug_kevents(x, y, z) 45 | #endif 46 | 47 | void _dispatch_source_drain_kevent(struct kevent *); 48 | void _dispatch_update_kq(const struct kevent *); 49 | 50 | 51 | #endif /* __DISPATCH_KEVENT_INTERNAL__ */ 52 | -------------------------------------------------------------------------------- /libdispatch-197/src/object_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_OBJECT_INTERNAL__ 28 | #define __DISPATCH_OBJECT_INTERNAL__ 29 | 30 | enum { 31 | _DISPATCH_CONTINUATION_TYPE = 0x00000, // meta-type for continuations 32 | _DISPATCH_QUEUE_TYPE = 0x10000, // meta-type for queues 33 | _DISPATCH_SOURCE_TYPE = 0x20000, // meta-type for sources 34 | _DISPATCH_SEMAPHORE_TYPE = 0x30000, // meta-type for semaphores 35 | _DISPATCH_ATTR_TYPE = 0x10000000, // meta-type for attribute structures 36 | 37 | DISPATCH_CONTINUATION_TYPE = _DISPATCH_CONTINUATION_TYPE, 38 | 39 | DISPATCH_QUEUE_ATTR_TYPE = _DISPATCH_QUEUE_TYPE | _DISPATCH_ATTR_TYPE, 40 | 41 | DISPATCH_QUEUE_TYPE = 1 | _DISPATCH_QUEUE_TYPE, 42 | DISPATCH_QUEUE_GLOBAL_TYPE = 2 | _DISPATCH_QUEUE_TYPE, 43 | DISPATCH_QUEUE_MGR_TYPE = 3 | _DISPATCH_QUEUE_TYPE, 44 | 45 | DISPATCH_SEMAPHORE_TYPE = _DISPATCH_SEMAPHORE_TYPE, 46 | 47 | DISPATCH_SOURCE_ATTR_TYPE = _DISPATCH_SOURCE_TYPE | _DISPATCH_ATTR_TYPE, 48 | 49 | DISPATCH_SOURCE_KEVENT_TYPE = 1 | _DISPATCH_SOURCE_TYPE, 50 | }; 51 | 52 | #define DISPATCH_VTABLE_HEADER(x) \ 53 | unsigned long const do_type; \ 54 | const char *const do_kind; \ 55 | size_t (*const do_debug)(struct x *, char *, size_t); \ 56 | struct dispatch_queue_s *(*const do_invoke)(struct x *); \ 57 | bool (*const do_probe)(struct x *); \ 58 | void (*const do_dispose)(struct x *) 59 | 60 | #define dx_type(x) (x)->do_vtable->do_type 61 | #define dx_kind(x) (x)->do_vtable->do_kind 62 | #define dx_debug(x, y, z) (x)->do_vtable->do_debug((x), (y), (z)) 63 | #define dx_dispose(x) (x)->do_vtable->do_dispose(x) 64 | #define dx_invoke(x) (x)->do_vtable->do_invoke(x) 65 | #define dx_probe(x) (x)->do_vtable->do_probe(x) 66 | 67 | #define DISPATCH_STRUCT_HEADER(x, y) \ 68 | const struct y *do_vtable; \ 69 | struct x *volatile do_next; \ 70 | unsigned int do_ref_cnt; \ 71 | unsigned int do_xref_cnt; \ 72 | unsigned int do_suspend_cnt; \ 73 | struct dispatch_queue_s *do_targetq; \ 74 | void *do_ctxt; \ 75 | dispatch_function_t do_finalizer 76 | 77 | #define DISPATCH_OBJECT_GLOBAL_REFCNT (~0u) 78 | #define DISPATCH_OBJECT_SUSPEND_LOCK 1u // "word and bit" must be a power of two to be safely subtracted 79 | #define DISPATCH_OBJECT_SUSPEND_INTERVAL 2u 80 | #define DISPATCH_OBJECT_SUSPENDED(x) ((x)->do_suspend_cnt >= DISPATCH_OBJECT_SUSPEND_INTERVAL) 81 | #ifdef __LP64__ 82 | // the bottom nibble must not be zero, the rest of the bits should be random 83 | // we sign extend the 64-bit version so that a better instruction encoding is generated on Intel 84 | #define DISPATCH_OBJECT_LISTLESS ((void *)0xffffffff89abcdef) 85 | #else 86 | #define DISPATCH_OBJECT_LISTLESS ((void *)0x89abcdef) 87 | #endif 88 | 89 | #define _dispatch_trysuspend(x) __sync_bool_compare_and_swap(&(x)->do_suspend_cnt, 0, DISPATCH_OBJECT_SUSPEND_INTERVAL) 90 | // _dispatch_source_invoke() relies on this testing the whole suspend count 91 | // word, not just the lock bit. In other words, no point taking the lock 92 | // if the source is suspended or canceled. 93 | #define _dispatch_trylock(x) dispatch_atomic_cmpxchg(&(x)->do_suspend_cnt, 0, DISPATCH_OBJECT_SUSPEND_LOCK) 94 | 95 | struct dispatch_object_vtable_s { 96 | DISPATCH_VTABLE_HEADER(dispatch_object_s); 97 | }; 98 | 99 | struct dispatch_object_s { 100 | DISPATCH_STRUCT_HEADER(dispatch_object_s, dispatch_object_vtable_s); 101 | }; 102 | 103 | size_t dispatch_object_debug_attr(dispatch_object_t dou, char* buf, size_t bufsiz); 104 | 105 | void _dispatch_retain(dispatch_object_t dou); 106 | void _dispatch_release(dispatch_object_t dou); 107 | void _dispatch_dispose(dispatch_object_t dou); 108 | dispatch_queue_t _dispatch_wakeup(dispatch_object_t dou); 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /libdispatch-197/src/once.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | #undef dispatch_once 24 | #undef dispatch_once_f 25 | 26 | #ifdef __BLOCKS__ 27 | void 28 | dispatch_once(dispatch_once_t *val, void (^block)(void)) 29 | { 30 | struct Block_basic *bb = (struct Block_basic *)(void *)block; 31 | 32 | dispatch_once_f(val, block, (dispatch_function_t)bb->Block_invoke); 33 | } 34 | #endif 35 | 36 | DISPATCH_NOINLINE 37 | void 38 | dispatch_once_f(dispatch_once_t *val, void *ctxt, void (*func)(void *)) 39 | { 40 | volatile long *vval = val; 41 | 42 | if (dispatch_atomic_cmpxchg(val, 0l, 1l)) { 43 | func(ctxt); 44 | 45 | // The next barrier must be long and strong. 46 | // 47 | // The scenario: SMP systems with weakly ordered memory models 48 | // and aggressive out-of-order instruction execution. 49 | // 50 | // The problem: 51 | // 52 | // The dispatch_once*() wrapper macro causes the callee's 53 | // instruction stream to look like this (pseudo-RISC): 54 | // 55 | // load r5, pred-addr 56 | // cmpi r5, -1 57 | // beq 1f 58 | // call dispatch_once*() 59 | // 1f: 60 | // load r6, data-addr 61 | // 62 | // May be re-ordered like so: 63 | // 64 | // load r6, data-addr 65 | // load r5, pred-addr 66 | // cmpi r5, -1 67 | // beq 1f 68 | // call dispatch_once*() 69 | // 1f: 70 | // 71 | // Normally, a barrier on the read side is used to workaround 72 | // the weakly ordered memory model. But barriers are expensive 73 | // and we only need to synchronize once! After func(ctxt) 74 | // completes, the predicate will be marked as "done" and the 75 | // branch predictor will correctly skip the call to 76 | // dispatch_once*(). 77 | // 78 | // A far faster alternative solution: Defeat the speculative 79 | // read-ahead of peer CPUs. 80 | // 81 | // Modern architectures will throw away speculative results 82 | // once a branch mis-prediction occurs. Therefore, if we can 83 | // ensure that the predicate is not marked as being complete 84 | // until long after the last store by func(ctxt), then we have 85 | // defeated the read-ahead of peer CPUs. 86 | // 87 | // In other words, the last "store" by func(ctxt) must complete 88 | // and then N cycles must elapse before ~0l is stored to *val. 89 | // The value of N is whatever is sufficient to defeat the 90 | // read-ahead mechanism of peer CPUs. 91 | // 92 | // On some CPUs, the most fully synchronizing instruction might 93 | // need to be issued. 94 | 95 | dispatch_atomic_barrier(); 96 | *val = ~0l; 97 | } else { 98 | do { 99 | _dispatch_hardware_pause(); 100 | } while (*vval != ~0l); 101 | 102 | dispatch_atomic_barrier(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /libdispatch-197/src/os_shims.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_OS_SHIMS__ 28 | #define __DISPATCH_OS_SHIMS__ 29 | 30 | #include 31 | #if HAVE_PTHREAD_MACHDEP_H 32 | #include 33 | #endif 34 | #if HAVE_PTHREAD_WORKQUEUES 35 | #include 36 | #endif 37 | #if HAVE_PTHREAD_NP_H 38 | #include 39 | #endif 40 | 41 | #if USE_APPLE_CRASHREPORTER_INFO 42 | __private_extern__ const char *__crashreporter_info__; 43 | #endif 44 | 45 | #if !HAVE_DECL_FD_COPY 46 | #define FD_COPY(f, t) (void)(*(t) = *(f)) 47 | #endif 48 | 49 | #if TARGET_OS_WIN32 50 | #define bzero(ptr,len) memset((ptr), 0, (len)) 51 | #define snprintf _snprintf 52 | 53 | inline size_t strlcpy(char *dst, const char *src, size_t size) { 54 | int res = strlen(dst) + strlen(src) + 1; 55 | if (size > 0) { 56 | size_t n = size - 1; 57 | strncpy(dst, src, n); 58 | dst[n] = 0; 59 | } 60 | return res; 61 | } 62 | #endif 63 | 64 | #include "shims/getprogname.h" 65 | #include "shims/malloc_zone.h" 66 | #include "shims/tsd.h" 67 | #include "shims/perfmon.h" 68 | #include "shims/time.h" 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /libdispatch-197/src/private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_PRIVATE__ 28 | #define __DISPATCH_PRIVATE__ 29 | 30 | #ifdef __APPLE__ 31 | #include 32 | #endif 33 | 34 | #if HAVE_MACH 35 | #include 36 | #include 37 | #include 38 | #endif 39 | #if HAVE_UNISTD_H 40 | #include 41 | #endif 42 | #if HAVE_SYS_CDEFS_H 43 | #include 44 | #endif 45 | #include 46 | 47 | #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED 48 | /* iPhone OS does not make any legacy definitions visible */ 49 | #define DISPATCH_NO_LEGACY 50 | #endif 51 | 52 | #ifndef __DISPATCH_BUILDING_DISPATCH__ 53 | #include_next 54 | 55 | // Workaround 56 | #ifndef __DISPATCH_PUBLIC__ 57 | #include "/usr/include/dispatch/dispatch.h" 58 | #endif 59 | 60 | #ifndef __DISPATCH_INDIRECT__ 61 | #define __DISPATCH_INDIRECT__ 62 | #endif 63 | 64 | #include 65 | #include 66 | #include 67 | 68 | #ifndef DISPATCH_NO_LEGACY 69 | #include 70 | #endif 71 | 72 | #undef __DISPATCH_INDIRECT__ 73 | 74 | #endif /* !__DISPATCH_BUILDING_DISPATCH__ */ 75 | 76 | /* LEGACY: Use DISPATCH_API_VERSION */ 77 | #define LIBDISPATCH_VERSION DISPATCH_API_VERSION 78 | 79 | __DISPATCH_BEGIN_DECLS 80 | 81 | DISPATCH_EXPORT DISPATCH_NOTHROW 82 | void 83 | #if USE_LIBDISPATCH_INIT_CONSTRUCTOR 84 | libdispatch_init(void) __attribute__ ((constructor)); 85 | #else 86 | libdispatch_init(void); 87 | #endif 88 | 89 | #if HAVE_MACH 90 | #define DISPATCH_COCOA_COMPAT 1 91 | #if DISPATCH_COCOA_COMPAT 92 | 93 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 94 | DISPATCH_EXPORT DISPATCH_NOTHROW 95 | mach_port_t 96 | _dispatch_get_main_queue_port_4CF(void); 97 | 98 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 99 | DISPATCH_EXPORT DISPATCH_NOTHROW 100 | void 101 | _dispatch_main_queue_callback_4CF(mach_msg_header_t *msg); 102 | 103 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 104 | DISPATCH_EXPORT 105 | void (*dispatch_begin_thread_4GC)(void); 106 | 107 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 108 | DISPATCH_EXPORT 109 | void (*dispatch_end_thread_4GC)(void); 110 | 111 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 112 | DISPATCH_EXPORT 113 | void *(*_dispatch_begin_NSAutoReleasePool)(void); 114 | 115 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 116 | DISPATCH_EXPORT 117 | void (*_dispatch_end_NSAutoReleasePool)(void *); 118 | 119 | #endif 120 | #endif /* HAVE_MACH */ 121 | 122 | /* pthreads magic */ 123 | 124 | DISPATCH_NOTHROW void dispatch_atfork_prepare(void); 125 | DISPATCH_NOTHROW void dispatch_atfork_parent(void); 126 | DISPATCH_NOTHROW void dispatch_atfork_child(void); 127 | DISPATCH_NOTHROW void dispatch_init_pthread(pthread_t); 128 | 129 | #if HAVE_MACH 130 | /* 131 | * Extract the context pointer from a mach message trailer. 132 | */ 133 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 134 | void * 135 | dispatch_mach_msg_get_context(mach_msg_header_t *msg); 136 | #endif 137 | 138 | __DISPATCH_END_DECLS 139 | 140 | #endif 141 | -------------------------------------------------------------------------------- /libdispatch-197/src/protocol.defs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | /* 21 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | // '64' is used to align with Mach notifications and so that we don't fight with the notify symbols in Libsystem 28 | subsystem libdispatch_internal_protocol 64; 29 | 30 | serverprefix _dispatch_; 31 | userprefix _dispatch_send_; 32 | 33 | skip; /* was MACH_NOTIFY_FIRST: 64 */ 34 | 35 | /* MACH_NOTIFY_PORT_DELETED: 65 */ 36 | simpleroutine 37 | mach_notify_port_deleted( 38 | _notify : mach_port_move_send_once_t; 39 | _name : mach_port_name_t 40 | ); 41 | 42 | skip; /* was MACH_NOTIFY_MSG_ACCEPTED: 66 */ 43 | 44 | skip; /* was NOTIFY_OWNERSHIP_RIGHTS: 67 */ 45 | 46 | skip; /* was NOTIFY_RECEIVE_RIGHTS: 68 */ 47 | 48 | /* MACH_NOTIFY_PORT_DESTROYED: 69 */ 49 | simpleroutine 50 | mach_notify_port_destroyed( 51 | _notify : mach_port_move_send_once_t; 52 | _rights : mach_port_move_receive_t 53 | ); 54 | 55 | /* MACH_NOTIFY_NO_SENDERS: 70 */ 56 | simpleroutine 57 | mach_notify_no_senders( 58 | _notify : mach_port_move_send_once_t; 59 | _mscnt : mach_port_mscount_t 60 | ); 61 | 62 | /* MACH_NOTIFY_SEND_ONCE: 71 */ 63 | simpleroutine 64 | mach_notify_send_once( 65 | _notify : mach_port_move_send_once_t 66 | ); 67 | 68 | /* MACH_NOTIFY_DEAD_NAME: 72 */ 69 | simpleroutine 70 | mach_notify_dead_name( 71 | _notify : mach_port_move_send_once_t; 72 | _name : mach_port_name_t 73 | ); 74 | 75 | /* highly unlikely additional Mach notifications */ 76 | skip; 77 | skip; 78 | skip; 79 | skip; 80 | skip; 81 | 82 | simpleroutine 83 | wakeup_main_thread( 84 | _port : mach_port_t; 85 | WaitTime _waitTimeout : natural_t 86 | ); 87 | 88 | simpleroutine 89 | consume_send_once_right( 90 | _port : mach_port_move_send_once_t 91 | ); 92 | -------------------------------------------------------------------------------- /libdispatch-197/src/queue_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_QUEUE_PRIVATE__ 28 | #define __DISPATCH_QUEUE_PRIVATE__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #include // for HeaderDoc 33 | #endif 34 | 35 | __DISPATCH_BEGIN_DECLS 36 | 37 | 38 | /*! 39 | * @enum dispatch_queue_flags_t 40 | * 41 | * @constant DISPATCH_QUEUE_OVERCOMMIT 42 | * The queue will create a new thread for invoking blocks, regardless of how 43 | * busy the computer is. 44 | */ 45 | enum { 46 | DISPATCH_QUEUE_OVERCOMMIT = 0x2ull, 47 | }; 48 | 49 | #define DISPATCH_QUEUE_FLAGS_MASK (DISPATCH_QUEUE_OVERCOMMIT) 50 | 51 | #ifdef __BLOCKS__ 52 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 53 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 54 | void 55 | dispatch_barrier_sync(dispatch_queue_t queue, dispatch_block_t block); 56 | #endif 57 | 58 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 59 | DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW 60 | void 61 | dispatch_barrier_sync_f(dispatch_queue_t dq, void *context, dispatch_function_t work); 62 | 63 | #ifdef __BLOCKS__ 64 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 65 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 66 | void 67 | dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block); 68 | #endif 69 | 70 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 71 | DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW 72 | void 73 | dispatch_barrier_async_f(dispatch_queue_t dq, void *context, dispatch_function_t work); 74 | 75 | /*! 76 | * @function dispatch_queue_set_width 77 | * 78 | * @abstract 79 | * Set the width of concurrency for a given queue. The default width of a 80 | * privately allocated queue is one. 81 | * 82 | * @param queue 83 | * The queue to adjust. Passing the main queue, a default concurrent queue or 84 | * any other default queue will be ignored. 85 | * 86 | * @param width 87 | * The new maximum width of concurrency depending on available resources. 88 | * If zero is passed, then the value is promoted to one. 89 | * Negative values are magic values that map to automatic width values. 90 | * Unknown negative values default to DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS. 91 | */ 92 | #define DISPATCH_QUEUE_WIDTH_ACTIVE_CPUS -1 93 | #define DISPATCH_QUEUE_WIDTH_MAX_PHYSICAL_CPUS -2 94 | #define DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS -3 95 | 96 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 97 | DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 98 | void 99 | dispatch_queue_set_width(dispatch_queue_t dq, long width); 100 | 101 | 102 | 103 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 104 | extern const struct dispatch_queue_offsets_s { 105 | // always add new fields at the end 106 | const uint16_t dqo_version; 107 | const uint16_t dqo_label; 108 | const uint16_t dqo_label_size; 109 | const uint16_t dqo_flags; 110 | const uint16_t dqo_flags_size; 111 | const uint16_t dqo_serialnum; 112 | const uint16_t dqo_serialnum_size; 113 | const uint16_t dqo_width; 114 | const uint16_t dqo_width_size; 115 | const uint16_t dqo_running; 116 | const uint16_t dqo_running_size; 117 | } dispatch_queue_offsets; 118 | 119 | 120 | __DISPATCH_END_DECLS 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /libdispatch-197/src/semaphore_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_SEMAPHORE_INTERNAL__ 28 | #define __DISPATCH_SEMAPHORE_INTERNAL__ 29 | 30 | struct dispatch_sema_notify_s { 31 | struct dispatch_sema_notify_s *dsn_next; 32 | dispatch_queue_t dsn_queue; 33 | void *dsn_ctxt; 34 | void (*dsn_func)(void *); 35 | }; 36 | 37 | struct dispatch_semaphore_s { 38 | DISPATCH_STRUCT_HEADER(dispatch_semaphore_s, dispatch_semaphore_vtable_s); 39 | long dsema_value; 40 | long dsema_orig; 41 | size_t dsema_sent_ksignals; 42 | #if (USE_MACH_SEM + USE_POSIX_SEM + USE_WIN32_SEM) > 1 43 | #error "Too many supported semaphore types" 44 | #elif USE_MACH_SEM 45 | semaphore_t dsema_port; 46 | semaphore_t dsema_waiter_port; 47 | #elif USE_POSIX_SEM 48 | sem_t dsema_sem; 49 | #elif USE_WIN32_SEM 50 | HANDLE dsema_handle; 51 | HANDLE dsema_waiter_handle; 52 | #else 53 | #error "No supported semaphore type" 54 | #endif 55 | size_t dsema_group_waiters; 56 | struct dispatch_sema_notify_s *dsema_notify_head; 57 | struct dispatch_sema_notify_s *dsema_notify_tail; 58 | }; 59 | 60 | extern const struct dispatch_semaphore_vtable_s _dispatch_semaphore_vtable; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/getprogname.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2010 Mark Heily 3 | * All rights reserved. 4 | * 5 | * @APPLE_APACHE_LICENSE_HEADER_START@ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * @APPLE_APACHE_LICENSE_HEADER_END@ 20 | */ 21 | 22 | #ifndef __DISPATCH_SHIMS_GETPROGNAME__ 23 | #define __DISPATCH_SHIMS_GETPROGNAME__ 24 | 25 | #ifndef HAVE_GETPROGNAME 26 | 27 | static inline const char * 28 | getprogname(void) 29 | { 30 | # if defined(ANDROID) 31 | return "FIXME -- TODO"; 32 | # elif HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 33 | return program_invocation_short_name; 34 | # elif HAVE_GETEXECNAME 35 | return getexecname(); 36 | # else 37 | # error getprogname(3) is not available on this platform 38 | # endif 39 | } 40 | 41 | #endif /* HAVE_GETPROGNAME */ 42 | 43 | #endif /* __DISPATCH_SHIMS_GETPROGNAME__ */ 44 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/mach.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | #if HAVE_MACH 24 | void * 25 | dispatch_mach_msg_get_context(mach_msg_header_t *msg) 26 | { 27 | mach_msg_context_trailer_t *tp; 28 | void *context = NULL; 29 | 30 | tp = (mach_msg_context_trailer_t *)((uint8_t *)msg + round_msg(msg->msgh_size)); 31 | if (tp->msgh_trailer_size >= (mach_msg_size_t)sizeof(mach_msg_context_trailer_t)) { 32 | context = (void *)(uintptr_t)tp->msgh_context; 33 | } 34 | 35 | return context; 36 | } 37 | 38 | #ifndef DISPATCH_NO_LEGACY 39 | /* 40 | * Raw Mach message support 41 | */ 42 | boolean_t 43 | _dispatch_machport_callback(mach_msg_header_t *msg, mach_msg_header_t *reply, 44 | void (*callback)(mach_msg_header_t *)) 45 | { 46 | mig_reply_setup(msg, reply); 47 | ((mig_reply_error_t*)reply)->RetCode = MIG_NO_REPLY; 48 | 49 | callback(msg); 50 | 51 | return TRUE; 52 | } 53 | 54 | /* 55 | * CFMachPort compatibility 56 | */ 57 | boolean_t 58 | _dispatch_CFMachPortCallBack(mach_msg_header_t *msg, mach_msg_header_t *reply, 59 | void (*callback)(struct __CFMachPort *, void *msg, signed long size, void *)) 60 | { 61 | mig_reply_setup(msg, reply); 62 | ((mig_reply_error_t*)reply)->RetCode = MIG_NO_REPLY; 63 | 64 | callback(NULL, msg, msg->msgh_size, dispatch_mach_msg_get_context(msg)); 65 | 66 | return TRUE; 67 | } 68 | #endif 69 | #endif /* HAVE_MACH */ 70 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/malloc_zone.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #ifndef __DISPATCH_SHIMS_MALLOC_ZONE__ 22 | #define __DISPATCH_SHIMS_MALLOC_ZONE__ 23 | 24 | #include 25 | 26 | #include 27 | 28 | /* 29 | * Implement malloc zones as a simple wrapper around malloc(3) on systems 30 | * that don't support them. 31 | */ 32 | #if !HAVE_MALLOC_CREATE_ZONE 33 | typedef void * malloc_zone_t; 34 | 35 | static inline malloc_zone_t * 36 | malloc_create_zone(size_t start_size, unsigned flags) 37 | { 38 | return ((malloc_zone_t *)(-1)); 39 | } 40 | 41 | static inline void 42 | malloc_destroy_zone(malloc_zone_t *zone) 43 | { 44 | /* No-op. */ 45 | } 46 | 47 | static inline malloc_zone_t * 48 | malloc_default_zone(void) 49 | { 50 | return ((malloc_zone_t *)(-1)); 51 | } 52 | 53 | static inline malloc_zone_t * 54 | malloc_zone_from_ptr(const void *ptr) 55 | { 56 | return ((malloc_zone_t *)(-1)); 57 | } 58 | 59 | static inline void * 60 | malloc_zone_malloc(malloc_zone_t *zone, size_t size) 61 | { 62 | return (malloc(size)); 63 | } 64 | 65 | static inline void * 66 | malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size) 67 | { 68 | return (calloc(num_items, size)); 69 | } 70 | 71 | #if !TARGET_OS_WIN32 72 | static inline void * 73 | malloc_zone_realloc(malloc_zone_t *zone, void *ptr, size_t size) 74 | { 75 | return (realloc(ptr, size)); 76 | } 77 | #endif 78 | 79 | static inline void 80 | malloc_zone_free(malloc_zone_t *zone, void *ptr) 81 | { 82 | free(ptr); 83 | } 84 | 85 | static inline void 86 | malloc_set_zone_name(malloc_zone_t *zone, const char *name) 87 | { 88 | /* No-op. */ 89 | } 90 | #endif /* !HAVE_MALLOC_CREATE_ZONE */ 91 | 92 | #endif /* __DISPATCH_SHIMS_MALLOC_ZONE__ */ 93 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/perfmon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_SHIMS_PERFMON__ 28 | #define __DISPATCH_SHIMS_PERFMON__ 29 | 30 | #if DISPATCH_PERF_MON 31 | 32 | #if defined (USE_APPLE_TSD_OPTIMIZATIONS) && defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__)) 33 | #ifdef __LP64__ 34 | #define _dispatch_workitem_inc() asm("incq %%gs:%0" : "+m" \ 35 | (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc") 36 | #define _dispatch_workitem_dec() asm("decq %%gs:%0" : "+m" \ 37 | (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc") 38 | #else 39 | #define _dispatch_workitem_inc() asm("incl %%gs:%0" : "+m" \ 40 | (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc") 41 | #define _dispatch_workitem_dec() asm("decl %%gs:%0" : "+m" \ 42 | (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc") 43 | #endif 44 | #else /* !USE_APPLE_TSD_OPTIMIZATIONS */ 45 | static inline void 46 | _dispatch_workitem_inc(void) 47 | { 48 | unsigned long cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key); 49 | _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)++cnt); 50 | } 51 | static inline void 52 | _dispatch_workitem_dec(void) 53 | { 54 | unsigned long cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key); 55 | _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)--cnt); 56 | } 57 | #endif /* USE_APPLE_TSD_OPTIMIZATIONS */ 58 | 59 | // C99 doesn't define flsll() or ffsll() 60 | #ifdef __LP64__ 61 | #define flsll(x) flsl(x) 62 | #else 63 | static inline unsigned int 64 | flsll(uint64_t val) 65 | { 66 | union { 67 | struct { 68 | #ifdef __BIG_ENDIAN__ 69 | unsigned int hi, low; 70 | #else 71 | unsigned int low, hi; 72 | #endif 73 | } words; 74 | uint64_t word; 75 | } _bucket = { 76 | .word = val, 77 | }; 78 | if (_bucket.words.hi) { 79 | return fls(_bucket.words.hi) + 32; 80 | } 81 | return fls(_bucket.words.low); 82 | } 83 | #endif 84 | 85 | #else 86 | #define _dispatch_workitem_inc() 87 | #define _dispatch_workitem_dec() 88 | #endif // DISPATCH_PERF_MON 89 | 90 | #endif /* __DISPATCH_SHIMS_PERFMON__ */ 91 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | #if !(defined(__i386__) || defined(__x86_64__)) || !HAVE_MACH_ABSOLUTE_TIME 24 | _dispatch_host_time_data_s _dispatch_host_time_data; 25 | 26 | void 27 | _dispatch_get_host_time_init(void *context __attribute__((unused))) 28 | { 29 | #if HAVE_MACH_ABSOLUTE_TIME 30 | mach_timebase_info_data_t tbi; 31 | (void)dispatch_assume_zero(mach_timebase_info(&tbi)); 32 | _dispatch_host_time_data.frac = tbi.numer; 33 | _dispatch_host_time_data.frac /= tbi.denom; 34 | _dispatch_host_time_data.ratio_1_to_1 = (tbi.numer == tbi.denom); 35 | #elif TARGET_OS_WIN32 36 | LARGE_INTEGER freq; 37 | dispatch_assume(QueryPerformanceFrequency(&freq)); 38 | _dispatch_host_time_data.frac = (long double)NSEC_PER_SEC / (long double)freq.QuadPart; 39 | _dispatch_host_time_data.ratio_1_to_1 = (freq.QuadPart == 1); 40 | #else // TARGET_OS_WIN32 41 | _dispatch_host_time_data.frac = 1.0; 42 | _dispatch_host_time_data.ratio_1_to_1 = 1; 43 | #endif 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_SHIMS_TIME__ 28 | #define __DISPATCH_SHIMS_TIME__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #endif 33 | 34 | uint64_t _dispatch_get_nanoseconds(void); 35 | 36 | #if TARGET_OS_WIN32 37 | static inline unsigned int 38 | sleep(unsigned int seconds) 39 | { 40 | Sleep(seconds * 1000); // milliseconds 41 | return 0; 42 | } 43 | #endif 44 | 45 | #if (defined(__i386__) || defined(__x86_64__)) && HAVE_MACH_ABSOLUTE_TIME 46 | // x86 currently implements mach time in nanoseconds; this is NOT likely to change 47 | #define _dispatch_time_mach2nano(x) (x) 48 | #define _dispatch_time_nano2mach(x) (x) 49 | #else 50 | typedef struct _dispatch_host_time_data_s { 51 | long double frac; 52 | bool ratio_1_to_1; 53 | dispatch_once_t pred; 54 | } _dispatch_host_time_data_s; 55 | _dispatch_host_time_data_s _dispatch_host_time_data; 56 | void _dispatch_get_host_time_init(void *context); 57 | 58 | static inline uint64_t 59 | _dispatch_time_mach2nano(uint64_t machtime) 60 | { 61 | _dispatch_host_time_data_s *const data = &_dispatch_host_time_data; 62 | dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init); 63 | 64 | return (uint64_t)(machtime * data->frac); 65 | } 66 | 67 | static inline int64_t 68 | _dispatch_time_nano2mach(int64_t nsec) 69 | { 70 | _dispatch_host_time_data_s *const data = &_dispatch_host_time_data; 71 | dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init); 72 | 73 | if (slowpath(_dispatch_host_time_data.ratio_1_to_1)) { 74 | return nsec; 75 | } 76 | 77 | long double big_tmp = (long double)nsec; 78 | 79 | // Divide by tbi.numer/tbi.denom to convert nsec to Mach absolute time 80 | big_tmp /= data->frac; 81 | 82 | // Clamp to a 64bit signed int 83 | if (slowpath(big_tmp > INT64_MAX)) { 84 | return INT64_MAX; 85 | } 86 | if (slowpath(big_tmp < INT64_MIN)) { 87 | return INT64_MIN; 88 | } 89 | return (int64_t)big_tmp; 90 | } 91 | #endif 92 | 93 | static inline uint64_t 94 | _dispatch_absolute_time(void) 95 | { 96 | #if HAVE_MACH_ABSOLUTE_TIME 97 | return mach_absolute_time(); 98 | #elif TARGET_OS_WIN32 99 | LARGE_INTEGER now; 100 | if (!QueryPerformanceCounter(&now)) { 101 | return 0; 102 | } 103 | return now.QuadPart; 104 | #else 105 | struct timespec ts; 106 | int ret; 107 | 108 | #if HAVE_DECL_CLOCK_UPTIME 109 | ret = clock_gettime(CLOCK_UPTIME, &ts); 110 | #elif HAVE_DECL_CLOCK_MONOTONIC 111 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); 112 | #else 113 | #error "clock_gettime: no supported absolute time clock" 114 | #endif 115 | (void)dispatch_assume_zero(ret); 116 | 117 | /* XXXRW: Some kind of overflow detection needed? */ 118 | return (ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec); 119 | #endif 120 | } 121 | 122 | #endif /* __DISPATCH_SHIMS_TIME__ */ 123 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/tsd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | #if !HAVE_PTHREAD_KEY_INIT_NP 24 | pthread_key_t dispatch_queue_key; 25 | pthread_key_t dispatch_sema4_key; 26 | pthread_key_t dispatch_cache_key; 27 | pthread_key_t dispatch_bcounter_key; 28 | #endif 29 | -------------------------------------------------------------------------------- /libdispatch-197/src/shims/tsd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_SHIMS_TSD__ 28 | #define __DISPATCH_SHIMS_TSD__ 29 | 30 | #if HAVE_PTHREAD_KEY_INIT_NP 31 | static const unsigned long dispatch_queue_key = __PTK_LIBDISPATCH_KEY0; 32 | static const unsigned long dispatch_sema4_key = __PTK_LIBDISPATCH_KEY1; 33 | static const unsigned long dispatch_cache_key = __PTK_LIBDISPATCH_KEY2; 34 | static const unsigned long dispatch_bcounter_key = __PTK_LIBDISPATCH_KEY3; 35 | //__PTK_LIBDISPATCH_KEY4 36 | //__PTK_LIBDISPATCH_KEY5 37 | #else 38 | extern pthread_key_t dispatch_queue_key; 39 | extern pthread_key_t dispatch_sema4_key; 40 | extern pthread_key_t dispatch_cache_key; 41 | extern pthread_key_t dispatch_bcounter_key; 42 | #endif 43 | 44 | #if USE_APPLE_TSD_OPTIMIZATIONS 45 | #define SIMULATE_5491082 1 46 | #ifndef _PTHREAD_TSD_OFFSET 47 | #define _PTHREAD_TSD_OFFSET 0 48 | #endif 49 | 50 | static inline void 51 | _dispatch_thread_setspecific(unsigned long k, void *v) 52 | { 53 | #if defined(SIMULATE_5491082) && defined(__i386__) 54 | asm("movl %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "ri" (v) : "memory"); 55 | #elif defined(SIMULATE_5491082) && defined(__x86_64__) 56 | asm("movq %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "rn" (v) : "memory"); 57 | #else 58 | int res; 59 | if (_pthread_has_direct_tsd()) { 60 | res = _pthread_setspecific_direct(k, v); 61 | } else { 62 | res = pthread_setspecific(k, v); 63 | } 64 | dispatch_assert_zero(res); 65 | #endif 66 | } 67 | 68 | static inline void * 69 | _dispatch_thread_getspecific(unsigned long k) 70 | { 71 | #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__)) 72 | void *rval; 73 | asm("mov %%gs:%1, %0" : "=r" (rval) : "m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET))); 74 | return rval; 75 | #else 76 | if (_pthread_has_direct_tsd()) { 77 | return _pthread_getspecific_direct(k); 78 | } else { 79 | return pthread_getspecific(k); 80 | } 81 | #endif 82 | } 83 | 84 | #else /* !USE_APPLE_TSD_OPTIMIZATIONS */ 85 | 86 | static inline void 87 | _dispatch_thread_setspecific(pthread_key_t k, void *v) 88 | { 89 | int res; 90 | 91 | res = pthread_setspecific(k, v); 92 | dispatch_assert_zero(res); 93 | } 94 | 95 | static inline void * 96 | _dispatch_thread_getspecific(pthread_key_t k) 97 | { 98 | 99 | return pthread_getspecific(k); 100 | } 101 | #endif /* USE_APPLE_TSD_OPTIMIZATIONS */ 102 | 103 | #if HAVE_PTHREAD_KEY_INIT_NP 104 | static inline void 105 | _dispatch_thread_key_init_np(unsigned long k, void (*d)(void *)) 106 | { 107 | dispatch_assert_zero(pthread_key_init_np((int)k, d)); 108 | } 109 | #else 110 | static inline void 111 | _dispatch_thread_key_create(pthread_key_t *key, void (*destructor)(void *)) 112 | { 113 | 114 | dispatch_assert_zero(pthread_key_create(key, destructor)); 115 | } 116 | #endif 117 | 118 | #define _dispatch_thread_self (uintptr_t)pthread_self 119 | 120 | #endif /* __DISPATCH_SHIMS_TSD__ */ 121 | -------------------------------------------------------------------------------- /libdispatch-197/src/source_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | /* 22 | * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch 23 | * which are subject to change in future releases of Mac OS X. Any applications 24 | * relying on these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_SOURCE_PRIVATE__ 28 | #define __DISPATCH_SOURCE_PRIVATE__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #include // for HeaderDoc 33 | #endif 34 | 35 | /*! 36 | * @const DISPATCH_SOURCE_TYPE_VFS 37 | * @discussion Apple-internal dispatch source that monitors for vfs events 38 | * defined by dispatch_vfs_flags_t. 39 | * The handle is a process identifier (pid_t). 40 | */ 41 | #define DISPATCH_SOURCE_TYPE_VFS (&_dispatch_source_type_vfs) 42 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 43 | extern const struct dispatch_source_type_s _dispatch_source_type_vfs; 44 | 45 | /*! 46 | * @enum dispatch_source_vfs_flags_t 47 | * 48 | * @constant DISPATCH_VFS_NOTRESP 49 | * Server down. 50 | * 51 | * @constant DISPATCH_VFS_NEEDAUTH 52 | * Server bad auth. 53 | * 54 | * @constant DISPATCH_VFS_LOWDISK 55 | * We're low on space. 56 | * 57 | * @constant DISPATCH_VFS_MOUNT 58 | * New filesystem arrived. 59 | * 60 | * @constant DISPATCH_VFS_UNMOUNT 61 | * Filesystem has left. 62 | * 63 | * @constant DISPATCH_VFS_DEAD 64 | * Filesystem is dead, needs force unmount. 65 | * 66 | * @constant DISPATCH_VFS_ASSIST 67 | * Filesystem needs assistance from external program. 68 | * 69 | * @constant DISPATCH_VFS_NOTRESPLOCK 70 | * Server lockd down. 71 | * 72 | * @constant DISPATCH_VFS_UPDATE 73 | * Filesystem information has changed. 74 | * 75 | * @constant DISPATCH_VFS_VERYLOWDISK 76 | * File system has *very* little disk space left. 77 | */ 78 | enum { 79 | DISPATCH_VFS_NOTRESP = 0x0001, 80 | DISPATCH_VFS_NEEDAUTH = 0x0002, 81 | DISPATCH_VFS_LOWDISK = 0x0004, 82 | DISPATCH_VFS_MOUNT = 0x0008, 83 | DISPATCH_VFS_UNMOUNT = 0x0010, 84 | DISPATCH_VFS_DEAD = 0x0020, 85 | DISPATCH_VFS_ASSIST = 0x0040, 86 | DISPATCH_VFS_NOTRESPLOCK = 0x0080, 87 | DISPATCH_VFS_UPDATE = 0x0100, 88 | DISPATCH_VFS_VERYLOWDISK = 0x0200, 89 | }; 90 | 91 | /*! 92 | * @enum dispatch_source_mach_send_flags_t 93 | * 94 | * @constant DISPATCH_MACH_SEND_DELETED 95 | * The receive right corresponding to the given send right was destroyed. 96 | */ 97 | enum { 98 | DISPATCH_MACH_SEND_DELETED = 0x2, 99 | }; 100 | 101 | /*! 102 | * @enum dispatch_source_proc_flags_t 103 | * 104 | * @constant DISPATCH_PROC_REAP 105 | * The process has been reaped by the parent process via 106 | * wait*(). 107 | */ 108 | enum { 109 | DISPATCH_PROC_REAP = 0x10000000, 110 | }; 111 | 112 | __DISPATCH_BEGIN_DECLS 113 | 114 | #if HAVE_MACH 115 | /*! 116 | * @typedef dispatch_mig_callback_t 117 | * 118 | * @abstract 119 | * The signature of a function that handles Mach message delivery and response. 120 | */ 121 | typedef boolean_t (*dispatch_mig_callback_t)(mach_msg_header_t *message, mach_msg_header_t *reply); 122 | 123 | __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) 124 | DISPATCH_NONNULL_ALL DISPATCH_NOTHROW 125 | mach_msg_return_t 126 | dispatch_mig_server(dispatch_source_t ds, size_t maxmsgsz, dispatch_mig_callback_t callback); 127 | #endif 128 | 129 | __DISPATCH_END_DECLS 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /libdispatch-197/src/time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "internal.h" 22 | 23 | uint64_t 24 | _dispatch_get_nanoseconds(void) 25 | { 26 | #if !TARGET_OS_WIN32 27 | struct timeval now; 28 | int r = gettimeofday(&now, NULL); 29 | dispatch_assert_zero(r); 30 | dispatch_assert(sizeof(NSEC_PER_SEC) == 8); 31 | dispatch_assert(sizeof(NSEC_PER_USEC) == 8); 32 | return now.tv_sec * NSEC_PER_SEC + now.tv_usec * NSEC_PER_USEC; 33 | #else /* TARGET_OS_WIN32 */ 34 | // FILETIME is 100-nanosecond intervals since January 1, 1601 (UTC). 35 | FILETIME ft; 36 | ULARGE_INTEGER li; 37 | GetSystemTimeAsFileTime(&ft); 38 | li.LowPart = ft.dwLowDateTime; 39 | li.HighPart = ft.dwHighDateTime; 40 | return li.QuadPart * 100ull; 41 | #endif /* TARGET_OS_WIN32 */ 42 | } 43 | 44 | dispatch_time_t 45 | dispatch_time(dispatch_time_t inval, int64_t delta) 46 | { 47 | if (inval == DISPATCH_TIME_FOREVER) { 48 | return DISPATCH_TIME_FOREVER; 49 | } 50 | if ((int64_t)inval < 0) { 51 | // wall clock 52 | if (delta >= 0) { 53 | if ((int64_t)(inval -= delta) >= 0) { 54 | return DISPATCH_TIME_FOREVER; // overflow 55 | } 56 | return inval; 57 | } 58 | if ((int64_t)(inval -= delta) >= -1) { 59 | // -1 is special == DISPATCH_TIME_FOREVER == forever 60 | return -2; // underflow 61 | } 62 | return inval; 63 | } 64 | // mach clock 65 | delta = _dispatch_time_nano2mach(delta); 66 | if (inval == 0) { 67 | inval = _dispatch_absolute_time(); 68 | } 69 | if (delta >= 0) { 70 | if ((int64_t)(inval += delta) <= 0) { 71 | return DISPATCH_TIME_FOREVER; // overflow 72 | } 73 | return inval; 74 | } 75 | if ((int64_t)(inval += delta) < 1) { 76 | return 1; // underflow 77 | } 78 | return inval; 79 | } 80 | 81 | dispatch_time_t 82 | dispatch_walltime(const struct timespec *inval, int64_t delta) 83 | { 84 | int64_t nsec; 85 | 86 | if (inval) { 87 | nsec = inval->tv_sec * 1000000000ull + inval->tv_nsec; 88 | } else { 89 | nsec = _dispatch_get_nanoseconds(); 90 | } 91 | 92 | nsec += delta; 93 | if (nsec <= 1) { 94 | // -1 is special == DISPATCH_TIME_FOREVER == forever 95 | return delta >= 0 ? DISPATCH_TIME_FOREVER : (uint64_t)-2ll; 96 | } 97 | 98 | return -nsec; 99 | } 100 | 101 | uint64_t 102 | _dispatch_timeout(dispatch_time_t when) 103 | { 104 | uint64_t now; 105 | 106 | if (when == DISPATCH_TIME_FOREVER) { 107 | return DISPATCH_TIME_FOREVER; 108 | } 109 | if (when == 0) { 110 | return 0; 111 | } 112 | if ((int64_t)when < 0) { 113 | when = -(int64_t)when; 114 | now = _dispatch_get_nanoseconds(); 115 | return now >= when ? 0 : when - now; 116 | } 117 | now = _dispatch_absolute_time(); 118 | return now >= when ? 0 : _dispatch_time_mach2nano(when - now); 119 | } 120 | 121 | #if USE_POSIX_SEM 122 | /* 123 | * Unlike Mach semaphores, POSIX semaphores take an absolute, real time as an 124 | * argument to sem_timedwait(). This routine converts from dispatch_time_t 125 | * but assumes the caller has already handled the possibility of 126 | * DISPATCH_TIME_FOREVER. 127 | */ 128 | struct timespec 129 | _dispatch_timeout_ts(dispatch_time_t when) 130 | { 131 | struct timespec ts_realtime; 132 | uint64_t abstime, realtime; 133 | int ret; 134 | 135 | if (when == 0) { 136 | ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); 137 | (void)dispatch_assume_zero(ret); 138 | return (ts_realtime); 139 | } 140 | if ((int64_t)when < 0) { 141 | ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); 142 | (void)dispatch_assume_zero(ret); 143 | when = -(int64_t)when + ts_realtime.tv_sec * NSEC_PER_SEC + 144 | ts_realtime.tv_nsec; 145 | ts_realtime.tv_sec = when / NSEC_PER_SEC; 146 | ts_realtime.tv_nsec = when % NSEC_PER_SEC; 147 | return (ts_realtime); 148 | } 149 | 150 | /* 151 | * Rebase 'when': (when - abstime) + realtime. 152 | * 153 | * XXXRW: Should we cache this delta to avoid system calls? 154 | */ 155 | abstime = _dispatch_absolute_time(); 156 | ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); 157 | (void)dispatch_assume_zero(ret); 158 | realtime = ts_realtime.tv_sec * NSEC_PER_SEC + ts_realtime.tv_nsec + 159 | (when - abstime); 160 | ts_realtime.tv_sec = realtime / NSEC_PER_SEC; 161 | ts_realtime.tv_nsec = realtime % NSEC_PER_SEC; 162 | return (ts_realtime); 163 | } 164 | #endif 165 | -------------------------------------------------------------------------------- /libdispatch-197/testing/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # 4 | 5 | noinst_LTLIBRARIES=libtest.la 6 | 7 | libtest_la_SOURCES= \ 8 | dispatch_test.c \ 9 | shims/asprintf.c \ 10 | shims/arc4random.c \ 11 | shims/fgetln.c 12 | 13 | TESTS= \ 14 | dispatch_api \ 15 | dispatch_c99 \ 16 | dispatch_cascade \ 17 | dispatch_debug \ 18 | dispatch_priority \ 19 | dispatch_priority2 \ 20 | dispatch_starfish \ 21 | queue_finalizer 22 | 23 | dispatch_c99_CFLAGS=-std=c99 24 | dispatch_priority2_SOURCES=dispatch_priority.c 25 | dispatch_priority2_CFLAGS=-DUSE_SET_TARGET_QUEUE=1 26 | 27 | if HAVE_CBLOCKS 28 | CBLOCKS_TESTS= \ 29 | dispatch_after \ 30 | dispatch_apply \ 31 | dispatch_drift \ 32 | dispatch_group \ 33 | dispatch_pingpong \ 34 | dispatch_read \ 35 | dispatch_readsync \ 36 | dispatch_sema \ 37 | dispatch_timer_bit31 \ 38 | dispatch_timer_bit63 39 | endif 40 | TESTS+=$(CBLOCKS_TESTS) 41 | 42 | if HAVE_CXXBLOCKS 43 | CXXBLOCKS_TESTS= \ 44 | dispatch_plusplus 45 | dispatch_plusplus_SOURCES=dispatch_plusplus.cpp 46 | endif 47 | TESTS+=$(CXXBLOCKS_TESTS) 48 | 49 | if HAVE_CORESERVICES 50 | CORESERVICES_TESTS= \ 51 | dispatch_cffd 52 | dispatch_cffd_LDFLAGS=-framework CoreFoundation 53 | endif 54 | TESTS+=$(CORESERVICES_TESTS) 55 | 56 | if HAVE_POSIX_SPAWN_START_SUSPENDED 57 | POSIX_SPAWN_START_SUSPENDED_TESTS= \ 58 | dispatch_proc 59 | endif 60 | TESTS+=$(POSIX_SPAWN_START_SUSPENDED_TESTS) 61 | 62 | if USE_LEGACY_API 63 | LEGACY_TESTS= \ 64 | dispatch_timer_set_time 65 | endif 66 | TESTS+=$(LEGACY_TESTS) 67 | 68 | # 69 | # XXX: By broken, we mean non-portable. We should instead test for the OS 70 | # features they depend on, or port them as required. 71 | # 72 | BROKEN_TOOLS= \ 73 | harness \ 74 | nsoperation 75 | 76 | noinst_PROGRAMS=$(TESTS) 77 | 78 | TOOLS= \ 79 | summarize 80 | 81 | noinst_PROGRAMS+=$(TOOLS) 82 | 83 | INCLUDES=-I$(top_builddir) -I$(top_srcdir) 84 | LDADD=libtest.la ../src/libdispatch.la 85 | CFLAGS=-Wall $(MARCH_FLAGS) $(CBLOCKS_FLAGS) 86 | CXXFLAGS=-Wall $(MARCH_FLAGS) $(CXXBLOCKS_FLAGS) 87 | 88 | -------------------------------------------------------------------------------- /libdispatch-197/testing/Makefile.darwin: -------------------------------------------------------------------------------- 1 | # No workie: dispatch_sema 2 | 3 | MAKECMD= $(MAKE) -f Makefile.darwin 4 | 5 | TESTS= dispatch_apply \ 6 | dispatch_api \ 7 | dispatch_c99 \ 8 | dispatch_cffd \ 9 | dispatch_debug \ 10 | queue_finalizer \ 11 | dispatch_group \ 12 | dispatch_pingpong \ 13 | dispatch_plusplus \ 14 | dispatch_priority \ 15 | dispatch_priority2 \ 16 | dispatch_proc \ 17 | dispatch_read \ 18 | dispatch_after \ 19 | dispatch_sema \ 20 | dispatch_timer_bit31 \ 21 | dispatch_timer_bit63 \ 22 | dispatch_starfish \ 23 | dispatch_cascade \ 24 | dispatch_drift \ 25 | dispatch_readsync \ 26 | nsoperation 27 | 28 | all: harness summarize bench $(TESTS) 29 | @lipo -remove x86_64 -output dispatch_timer_bit31 dispatch_timer_bit31 2>/dev/null || true 30 | 31 | logs: $(addsuffix .testlog, $(TESTS)) 32 | debuglogs: $(addsuffix .debuglog, $(TESTS)) 33 | 34 | testbots: 35 | $(MAKECMD) test 36 | 37 | test: clean-logs 38 | $(MAKECMD) _test 39 | 40 | _test: all logs debuglogs 41 | @cat *.testlog *.debuglog 42 | @cat *.testlog *.debuglog | ./summarize 43 | 44 | # Override ARCHS and SDKROOT to cross-build test suite 45 | 46 | SRCS = dispatch_test.c 47 | OBJS = $(SRCS:%.c=%.o) 48 | ARCHS=i386 x86_64 ppc 49 | CFLAGS = -Werror -Wall -Wextra -Wshadow -mdynamic-no-pic -Os -g $(patsubst %, -arch %,$(ARCHS)) -DDISPATCH_NO_LEGACY -I.. 50 | CPPFLAGS = $(CFLAGS) 51 | LDFLAGS = $(patsubst %, -arch %,$(ARCHS)) 52 | LDLIBS = -lstdc++ 53 | 54 | ifneq ($(SDKROOT),) 55 | CFLAGS += -isysroot $(SDKROOT) 56 | LDFLAGS += -isysroot $(SDKROOT) 57 | CC = xcrun -sdk $(SDKROOT) gcc 58 | endif 59 | 60 | harness: harness.o $(OBJS) 61 | summarize: summarize.o 62 | bench: bench.o func.o 63 | $(CC) $(LDFLAGS) -framework Foundation $(LDLIBS) -o $@ $^ 64 | 65 | bench.o: bench.mm 66 | $(CC) -x objective-c++ $(CFLAGS) -c $^ -o $@ 67 | func.o: func.c 68 | $(CC) -x c++ $(CFLAGS) -c $^ -o $@ 69 | 70 | dispatch_apply: dispatch_apply.o $(OBJS) 71 | dispatch_api: dispatch_api.o $(OBJS) 72 | dispatch_c99: dispatch_c99.o $(OBJS) 73 | dispatch_cffd: dispatch_cffd.o $(OBJS) 74 | $(CC) $(LDFLAGS) -framework CoreFoundation -o $@ $^ 75 | dispatch_debug: dispatch_debug.o $(OBJS) 76 | dispatch_group: dispatch_group.o $(OBJS) 77 | dispatch_pingpong: dispatch_pingpong.o $(OBJS) 78 | dispatch_plusplus: dispatch_plusplus.o $(OBJS) 79 | dispatch_priority: dispatch_priority.o $(OBJS) 80 | dispatch_priority2: dispatch_priority2.o $(OBJS) 81 | dispatch_proc: dispatch_proc.o $(OBJS) 82 | queue_finalizer: queue_finalizer.o $(OBJS) 83 | dispatch_read: dispatch_read.o $(OBJS) 84 | dispatch_read2: dispatch_read2.o $(OBJS) 85 | dispatch_after: dispatch_after.o $(OBJS) 86 | dispatch_timer: dispatch_timer.o $(OBJS) 87 | dispatch_suspend_timer: dispatch_suspend_timer.o $(OBJS) 88 | dispatch_sema: dispatch_sema.o $(OBJS) 89 | dispatch_timer_bit31: dispatch_timer_bit31.o $(OBJS) 90 | dispatch_timer_bit63: dispatch_timer_bit63.o $(OBJS) 91 | dispatch_drift: dispatch_drift.o $(OBJS) 92 | dispatch_starfish: dispatch_starfish.o $(OBJS) 93 | dispatch_cascade: dispatch_cascade.o $(OBJS) 94 | dispatch_readsync: dispatch_readsync.o $(OBJS) 95 | ENVIRON_nsoperation = NOLEAKS=1 96 | nsoperation: nsoperation.o $(OBJS) 97 | $(CC) $(LDFLAGS) -framework Foundation -o $@ $^ 98 | 99 | dispatch_api.o: dispatch_api.c 100 | $(CC) -c $(CFLAGS) -include $(SDKROOT)/usr/include/dispatch/dispatch.h -pendantic -o $@ $^ 101 | 102 | dispatch_c99.o: dispatch_c99.c 103 | $(CC) -c $(CFLAGS) -std=c99 -pedantic -o $@ $^ 104 | 105 | dispatch_priority2.o: dispatch_priority.c 106 | $(CC) -c $(CFLAGS) -DUSE_SET_TARGET_QUEUE=1 -o $@ $^ 107 | 108 | $(addsuffix .testlog, $(TESTS)): harness $(TESTS) 109 | $(ENVIRON_$(basename $@)) ./harness ./$(basename $@) > $@ 110 | 111 | $(addsuffix .debuglog, $(TESTS)): harness $(TESTS) 112 | $(ENVIRON_$(basename $@)) DYLD_IMAGE_SUFFIX=_debug ./harness ./$(basename $@) > $@ 113 | 114 | clean-logs: 115 | rm -f *.testlog *.debuglog *.leakslog 116 | 117 | clean: clean-logs 118 | rm -f *.o *.dSYM bench harness summarize $(TESTS) 119 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_after.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "dispatch_test.h" 30 | #include 31 | 32 | void done(void *arg __attribute__((unused))) { 33 | sleep(1); 34 | test_stop(); 35 | } 36 | 37 | int 38 | main(void) 39 | { 40 | __block dispatch_time_t time_a_min, time_a, time_a_max; 41 | __block dispatch_time_t time_b_min, time_b, time_b_max; 42 | __block dispatch_time_t time_c_min, time_c, time_c_max; 43 | 44 | 45 | test_start("Dispatch After"); 46 | 47 | dispatch_async(dispatch_get_main_queue(), ^{ 48 | time_a_min = dispatch_time(0, 5.5*NSEC_PER_SEC); 49 | time_a = dispatch_time(0, 6*NSEC_PER_SEC); 50 | time_a_max = dispatch_time(0, 6.5*NSEC_PER_SEC); 51 | dispatch_after(time_a, dispatch_get_current_queue(), ^{ 52 | dispatch_time_t now_a = dispatch_time(0, 0); 53 | test_long_less_than("can't finish faster than 5.5s", 0, now_a - time_a_min); 54 | test_long_less_than("must finish faster than 6.5s", 0, time_a_max - now_a); 55 | 56 | time_b_min = dispatch_time(0, 1.5*NSEC_PER_SEC); 57 | time_b = dispatch_time(0, 2*NSEC_PER_SEC); 58 | time_b_max = dispatch_time(0, 2.5*NSEC_PER_SEC); 59 | dispatch_after(time_b, dispatch_get_current_queue(), ^{ 60 | dispatch_time_t now_b = dispatch_time(0, 0); 61 | test_long_less_than("can't finish faster than 1.5s", 0, now_b - time_b_min); 62 | test_long_less_than("must finish faster than 2.5s", 0, time_b_max - now_b); 63 | 64 | time_c_min = dispatch_time(0, 0*NSEC_PER_SEC); 65 | time_c = dispatch_time(0, 0*NSEC_PER_SEC); 66 | time_c_max = dispatch_time(0, .5*NSEC_PER_SEC); 67 | dispatch_after(time_c, dispatch_get_current_queue(), ^{ 68 | dispatch_time_t now_c = dispatch_time(0, 0); 69 | test_long_less_than("can't finish faster than 0s", 0, now_c - time_c_min); 70 | test_long_less_than("must finish faster than .5s", 0, time_c_max - now_c); 71 | 72 | dispatch_async_f(dispatch_get_current_queue(), NULL, done); 73 | }); 74 | }); 75 | }); 76 | }); 77 | 78 | dispatch_main(); 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_api.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | 25 | #include 26 | 27 | #include "dispatch_test.h" 28 | 29 | void 30 | work(void *context __attribute__((unused))) 31 | { 32 | test_stop(); 33 | exit(0); 34 | } 35 | 36 | int main(void) { 37 | test_start("Dispatch (Public) API"); 38 | dispatch_queue_t q = dispatch_get_main_queue(); 39 | test_ptr_notnull("dispatch_get_main_queue", q); 40 | 41 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 42 | dispatch_main(); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_apply.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include "src/hw_shims.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "dispatch_test.h" 32 | 33 | int 34 | main(void) 35 | { 36 | test_start("Dispatch Apply"); 37 | 38 | volatile __block int32_t count = 0; 39 | const int32_t final = 32; 40 | 41 | dispatch_queue_t queue = dispatch_get_global_queue(0, 0); 42 | test_ptr_notnull("dispatch_get_concurrent_queue", queue); 43 | 44 | dispatch_apply(final, queue, ^(size_t i __attribute__((unused))) { 45 | dispatch_atomic_inc(&count); 46 | }); 47 | 48 | test_long("count", count, final); 49 | test_stop(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_c99.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | 26 | #include "dispatch_test.h" 27 | 28 | void 29 | work(void *context __attribute__((unused))) 30 | { 31 | test_stop(); 32 | exit(0); 33 | } 34 | 35 | int main(void) { 36 | test_start("Dispatch C99"); 37 | dispatch_queue_t q = dispatch_get_main_queue(); 38 | test_ptr_notnull("dispatch_get_main_queue", q); 39 | 40 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 41 | dispatch_main(); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_cascade.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "dispatch_test.h" 29 | 30 | int done = 0; 31 | 32 | #define QUEUES 80 33 | dispatch_queue_t queues[QUEUES]; 34 | 35 | 36 | #define BLOCKS 10000 37 | union { 38 | size_t index; 39 | char padding[64]; 40 | } indices[BLOCKS]; 41 | 42 | size_t iterations = QUEUES * BLOCKS * 0.25; 43 | 44 | void 45 | histogram(void) { 46 | size_t counts[QUEUES] = {}; 47 | size_t maxcount = 0; 48 | 49 | size_t q; 50 | for (q = 0; q < QUEUES; ++q) { 51 | size_t i; 52 | for (i = 0; i < BLOCKS; ++i) { 53 | if (indices[i].index == q) { 54 | ++counts[q]; 55 | } 56 | } 57 | } 58 | 59 | for (q = 0; q < QUEUES; ++q) { 60 | if (counts[q] > maxcount) { 61 | maxcount = counts[q]; 62 | } 63 | } 64 | 65 | printf("maxcount = %ld\n", maxcount); 66 | 67 | size_t x,y; 68 | for (y = 20; y > 0; --y) { 69 | for (x = 0; x < QUEUES; ++x) { 70 | double fraction = (double)counts[x] / (double)maxcount; 71 | double value = fraction * (double)20; 72 | printf("%s", (value > y) ? "*" : " "); 73 | } 74 | printf("\n"); 75 | } 76 | } 77 | 78 | void 79 | cascade(void* context) { 80 | size_t idx, *idxptr = context; 81 | 82 | if (done) return; 83 | 84 | idx = *idxptr + 1; 85 | 86 | if (idx < QUEUES) { 87 | *idxptr = idx; 88 | dispatch_async_f(queues[idx], context, cascade); 89 | } 90 | 91 | if (__sync_sub_and_fetch(&iterations, 1) == 0) { 92 | done = 1; 93 | histogram(); 94 | test_stop(); 95 | exit(0); 96 | } 97 | } 98 | 99 | int 100 | main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) { 101 | int i; 102 | 103 | test_start("Dispatch Cascade"); 104 | 105 | for (i = 0; i < QUEUES; ++i) { 106 | queues[i] = dispatch_queue_create(NULL, NULL); 107 | } 108 | 109 | for (i = 0; i < BLOCKS; ++i) { 110 | cascade(&indices[i].index); 111 | } 112 | 113 | dispatch_main(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_cffd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | 40 | #include "dispatch_test.h" 41 | 42 | int debug = 0; 43 | 44 | #define DEBUG(...) do { \ 45 | if (debug) fprintf(stderr, __VA_ARGS__); \ 46 | } while(0); 47 | 48 | #define assert_errno(str, expr) do { \ 49 | if (!(expr)) { \ 50 | fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \ 51 | exit(1); \ 52 | } } while(0); 53 | 54 | int 55 | init_kqueue(void) 56 | { 57 | int kq; 58 | int res; 59 | struct kevent ke; 60 | static struct timespec t0; 61 | 62 | kq = kqueue(); 63 | assert_errno("kqueue", kq >= 0); 64 | 65 | EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0); 66 | 67 | res = kevent(kq, &ke, 1, NULL, 0, &t0); 68 | assert_errno("kevent", res == 0); 69 | 70 | return kq; 71 | } 72 | 73 | int 74 | read_kevent(int kq) 75 | { 76 | int res; 77 | struct kevent ke; 78 | //static struct timespec t0; 79 | 80 | res = kevent(kq, NULL, 0, &ke, 1, NULL); 81 | assert_errno("kevent", res >= 0); 82 | 83 | fprintf(stdout, "kevent.data = %ld\n", ke.data); 84 | 85 | return (res < 0); 86 | } 87 | 88 | 89 | static void 90 | cffd_callback(CFFileDescriptorRef cffd, 91 | CFOptionFlags callBackTypes __attribute__((unused)), 92 | void *info __attribute__((unused))) 93 | { 94 | int kq; 95 | 96 | kq = CFFileDescriptorGetNativeDescriptor(cffd); 97 | if (read_kevent(kq) == 0) { 98 | // ... 99 | } 100 | 101 | CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack); 102 | } 103 | 104 | void 105 | timer() 106 | { 107 | dispatch_source_t ds; 108 | ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 109 | assert(ds); 110 | dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0); 111 | dispatch_source_set_event_handler(ds, ^{ 112 | printf("ping\n"); 113 | }); 114 | dispatch_resume(ds); 115 | } 116 | 117 | void 118 | hangup() 119 | { 120 | dispatch_source_t ds; 121 | ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue()); 122 | assert(ds); 123 | dispatch_source_set_event_handler(ds, ^{ 124 | printf("hangup\n"); 125 | }); 126 | dispatch_resume(ds); 127 | } 128 | 129 | int 130 | main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) 131 | { 132 | int kq; 133 | CFFileDescriptorRef cffd; 134 | CFRunLoopSourceRef rls; 135 | CFFileDescriptorContext ctx; 136 | 137 | test_start("CFFileDescriptor"); 138 | 139 | signal(SIGHUP, SIG_IGN); 140 | 141 | kq = init_kqueue(); 142 | 143 | memset(&ctx, 0, sizeof(CFFileDescriptorContext)); 144 | cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx); 145 | assert(cffd); 146 | 147 | rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0); 148 | assert(rls); 149 | CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode); 150 | CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack); 151 | 152 | // timer(); 153 | // hangup(); 154 | 155 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false); 156 | 157 | test_stop(); 158 | 159 | return 0; 160 | } 161 | 162 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #include "dispatch_test.h" 30 | 31 | int 32 | main(void) 33 | { 34 | test_start("Dispatch Debug"); 35 | 36 | dispatch_queue_t main_q = dispatch_get_main_queue(); 37 | dispatch_debug(main_q, "dispatch_queue_t"); 38 | 39 | dispatch_queue_t default_q = dispatch_get_global_queue(0, 0); 40 | dispatch_debug(default_q, "dispatch_queue_t"); 41 | 42 | dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 43 | dispatch_debug(s, "dispatch_source_t"); 44 | 45 | dispatch_group_t g = dispatch_group_create(); 46 | dispatch_debug(g, "dispatch_group_t"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_drift.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | // 32 | // Verify that dispatch timers do not drift, that is to say, increasingly 33 | // stray from their intended interval. 34 | // 35 | // The jitter of the event handler is defined to be the amount of time 36 | // difference between the actual and expected timing of the event handler 37 | // invocation. The drift is defined to be the amount that the jitter changes 38 | // over time. 39 | // 40 | // Important: be sure to use the same clock when comparing actual and expected 41 | // values. Skew between different clocks is to be expected. 42 | // 43 | 44 | int 45 | main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) 46 | { 47 | __block uint32_t count = 0; 48 | __block double last_jitter = 0; 49 | 50 | // interval is 1/10th of a second 51 | uint64_t interval = NSEC_PER_SEC / 10; 52 | double interval_d = (double)interval / (double)NSEC_PER_SEC; 53 | 54 | // for 25 seconds 55 | unsigned int target = 25 / interval_d; 56 | 57 | __block double first = 0; 58 | __block double jittersum = 0; 59 | __block double driftsum = 0; 60 | 61 | test_start("Timer drift test"); 62 | 63 | dispatch_source_t timer; 64 | timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 65 | test_ptr_notnull("DISPATCH_SOURCE_TYPE_TIMER", timer); 66 | 67 | dispatch_source_set_event_handler(timer, ^{ 68 | struct timeval now_tv; 69 | gettimeofday(&now_tv, NULL); 70 | double now = now_tv.tv_sec + ((double)now_tv.tv_usec / (double)USEC_PER_SEC); 71 | 72 | if (first == 0) { 73 | first = now; 74 | } 75 | 76 | double goal = first + interval_d * count; 77 | double jitter = goal - now; 78 | double drift = jitter - last_jitter; 79 | 80 | count += dispatch_source_get_data(timer); 81 | jittersum += jitter; 82 | driftsum += drift; 83 | 84 | printf("%4d: jitter %f, drift %f\n", count, jitter, drift); 85 | 86 | if (count >= target) { 87 | test_double_less_than("average jitter", fabs(jittersum) / (double)count, 0.0001); 88 | test_double_less_than("average drift", fabs(driftsum) / (double)count, 0.0001); 89 | test_stop(); 90 | } 91 | last_jitter = jitter; 92 | }); 93 | 94 | struct timeval now_tv; 95 | struct timespec now_ts; 96 | 97 | gettimeofday(&now_tv, NULL); 98 | now_ts.tv_sec = now_tv.tv_sec; 99 | now_ts.tv_nsec = now_tv.tv_usec * NSEC_PER_USEC; 100 | 101 | dispatch_source_set_timer(timer, dispatch_walltime(&now_ts, interval), interval, 0); 102 | 103 | dispatch_resume(timer); 104 | 105 | dispatch_main(); 106 | return 0; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_group.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "dispatch_test.h" 31 | 32 | #ifndef NSEC_PER_SEC 33 | #define NSEC_PER_SEC 1000000000 34 | #endif 35 | 36 | dispatch_group_t 37 | create_group(size_t count, int delay) 38 | { 39 | size_t i; 40 | 41 | dispatch_group_t group = dispatch_group_create(); 42 | 43 | for (i = 0; i < count; ++i) { 44 | dispatch_queue_t queue = dispatch_queue_create(NULL, NULL); 45 | assert(queue); 46 | 47 | dispatch_group_async(group, queue, ^{ 48 | if (delay) { 49 | fprintf(stderr, "sleeping...\n"); 50 | sleep(delay); 51 | fprintf(stderr, "done.\n"); 52 | } 53 | }); 54 | 55 | dispatch_release(queue); 56 | } 57 | return group; 58 | } 59 | 60 | int 61 | main(void) 62 | { 63 | long res; 64 | 65 | test_start("Dispatch Group"); 66 | 67 | dispatch_group_t group; 68 | 69 | group = create_group(100, 0); 70 | test_ptr_notnull("dispatch_group_async", group); 71 | 72 | dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 73 | 74 | // should be OK to re-use a group 75 | dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{}); 76 | dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 77 | 78 | dispatch_release(group); 79 | group = NULL; 80 | 81 | group = create_group(3, 7); 82 | test_ptr_notnull("dispatch_group_async", group); 83 | 84 | res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC)); 85 | test_long("dispatch_group_wait", !res, 0); 86 | 87 | // retry after timeout (this time succeed) 88 | res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC)); 89 | test_long("dispatch_group_wait", res, 0); 90 | 91 | dispatch_release(group); 92 | group = NULL; 93 | 94 | group = create_group(100, 0); 95 | test_ptr_notnull("dispatch_group_async", group); 96 | 97 | dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 98 | dispatch_queue_t m = dispatch_get_main_queue(); 99 | dispatch_queue_t c = dispatch_get_current_queue(); 100 | test_ptr("Notification Received", m, c); 101 | test_stop(); 102 | }); 103 | 104 | dispatch_release(group); 105 | group = NULL; 106 | 107 | dispatch_main(); 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_pingpong.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | 26 | #include "dispatch_test.h" 27 | 28 | uint32_t count = 0; 29 | const uint32_t final = 1000000; // 10M 30 | 31 | void pingpongloop(dispatch_group_t group, dispatch_queue_t ping, dispatch_queue_t pong, size_t counter) { 32 | //printf("[%p] %s: %lu\n", (void*)(uintptr_t)pthread_self(), dispatch_queue_get_label(dispatch_get_current_queue()), counter); 33 | if (counter < final) { 34 | dispatch_group_async(group, pong, ^{ pingpongloop(group, pong, ping, counter+1); }); 35 | } else { 36 | count = counter; 37 | } 38 | } 39 | 40 | int main(void) { 41 | 42 | test_start("Dispatch Ping Pong"); 43 | 44 | dispatch_queue_t ping = dispatch_queue_create("ping", NULL); 45 | test_ptr_notnull("dispatch_queue_create(ping)", ping); 46 | dispatch_queue_t pong = dispatch_queue_create("pong", NULL); 47 | test_ptr_notnull("dispatch_queue_create(pong)", pong); 48 | 49 | dispatch_group_t group = dispatch_group_create(); 50 | test_ptr_notnull("dispatch_group_create", group); 51 | 52 | pingpongloop(group, ping, pong, 0); 53 | dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 54 | 55 | test_long("count", count, final); 56 | test_stop(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_plusplus.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include "dispatch_test.h" 25 | 26 | int main(void) { 27 | test_start("Dispatch C++"); 28 | dispatch_queue_t q = dispatch_get_main_queue(); 29 | test_ptr_notnull("dispatch_get_main_queue", q); 30 | 31 | dispatch_async(dispatch_get_main_queue(), ^{ 32 | test_stop(); 33 | exit(0); 34 | }); 35 | dispatch_main(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_priority.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #define __DISPATCH_INDIRECT__ 26 | #include "src/queue_private.h" 27 | #include 28 | #include 29 | #include 30 | #if HAVE_TARGETCONDITIONALS_H 31 | #include 32 | #endif 33 | 34 | #include "dispatch_test.h" 35 | 36 | int done = 0; 37 | 38 | #define BLOCKS 128 39 | #define PRIORITIES 3 40 | 41 | #if TARGET_OS_EMBEDDED 42 | #define LOOP_COUNT 2000000 43 | #else 44 | #define LOOP_COUNT 100000000 45 | #endif 46 | 47 | char *labels[PRIORITIES] = { "LOW", "DEFAULT", "HIGH" }; 48 | int priorities[PRIORITIES] = { DISPATCH_QUEUE_PRIORITY_LOW, DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_PRIORITY_HIGH }; 49 | 50 | union { 51 | long count; 52 | char padding[64]; 53 | } counts[PRIORITIES]; 54 | 55 | #define ITERATIONS (long)(PRIORITIES * BLOCKS * 0.50) 56 | long iterations = ITERATIONS; 57 | 58 | void 59 | histogram(void) { 60 | long maxcount = BLOCKS; 61 | long sc[PRIORITIES]; 62 | 63 | long total = 0; 64 | 65 | long x,y; 66 | for (y = 0; y < PRIORITIES; ++y) { 67 | sc[y] = counts[y].count; 68 | } 69 | 70 | for (y = 0; y < PRIORITIES; ++y) { 71 | printf("%s: %ld\n", labels[y], sc[y]); 72 | total += sc[y]; 73 | 74 | double fraction = (double)sc[y] / (double)maxcount; 75 | double value = fraction * (double)80; 76 | for (x = 0; x < 80; ++x) { 77 | printf("%s", (value > x) ? "*" : " "); 78 | } 79 | printf("\n"); 80 | } 81 | 82 | test_long("blocks completed", total, ITERATIONS); 83 | test_long_less_than("high priority precedence", (long)sc[0], (long)sc[2]); 84 | } 85 | 86 | void 87 | cpubusy(void* context) 88 | { 89 | long *count = context; 90 | long iterdone; 91 | 92 | long idx; 93 | for (idx = 0; idx < LOOP_COUNT; ++idx) { 94 | if (done) break; 95 | } 96 | 97 | if ((iterdone = __sync_sub_and_fetch(&iterations, 1)) == 0) { 98 | __sync_add_and_fetch(&done, 1); 99 | __sync_add_and_fetch(count, 1); 100 | histogram(); 101 | test_stop(); 102 | exit(0); 103 | } else if (iterdone > 0) { 104 | __sync_add_and_fetch(count, 1); 105 | } 106 | } 107 | 108 | void 109 | submit_work(dispatch_queue_t queue, void* context) 110 | { 111 | int i; 112 | 113 | for (i = 0; i < BLOCKS; ++i) { 114 | dispatch_async_f(queue, context, cpubusy); 115 | } 116 | 117 | #if USE_SET_TARGET_QUEUE 118 | dispatch_release(queue); 119 | #endif 120 | } 121 | 122 | int 123 | main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) 124 | { 125 | dispatch_queue_t q[PRIORITIES]; 126 | int i; 127 | 128 | #if USE_SET_TARGET_QUEUE 129 | test_start("Dispatch Priority (Set Target Queue)"); 130 | for(i = 0; i < PRIORITIES; i++) { 131 | q[i] = dispatch_queue_create(labels[i], NULL); 132 | test_ptr_notnull("q[i]", q[i]); 133 | assert(q[i]); 134 | dispatch_set_target_queue(q[i], dispatch_get_global_queue(priorities[i], 0)); 135 | dispatch_queue_set_width(q[i], DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS); 136 | } 137 | #else 138 | test_start("Dispatch Priority"); 139 | for(i = 0; i < PRIORITIES; i++) { 140 | q[i] = dispatch_get_global_queue(priorities[i], 0); 141 | } 142 | #endif 143 | 144 | for(i = 0; i < PRIORITIES; i++) { 145 | submit_work(q[i], &counts[i].count); 146 | } 147 | 148 | dispatch_main(); 149 | 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_proc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "dispatch_test.h" 33 | 34 | #define PID_CNT 5 35 | 36 | static long event_cnt; 37 | 38 | int 39 | main(void) 40 | { 41 | dispatch_source_t proc; 42 | int res; 43 | pid_t pid; 44 | 45 | test_start("Dispatch Proc"); 46 | 47 | // Creates a process and register multiple observers. Send a signal, 48 | // exit the process, etc., and verify all observers were notified. 49 | 50 | // 51 | // Simple child process that sleeps 2 seconds. 52 | // 53 | 54 | posix_spawnattr_t attr; 55 | res = posix_spawnattr_init(&attr); 56 | assert(res == 0); 57 | res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED); 58 | assert(res == 0); 59 | 60 | char* args[] = { 61 | "/bin/sleep", "2", NULL 62 | }; 63 | 64 | res = posix_spawnp(&pid, args[0], NULL, &attr, args, NULL); 65 | if (res < 0) { 66 | perror(args[0]); 67 | exit(127); 68 | } 69 | 70 | res = posix_spawnattr_destroy(&attr); 71 | assert(res == 0); 72 | 73 | dispatch_queue_t completion = dispatch_queue_create("completion", NULL); 74 | 75 | assert(pid > 0); 76 | 77 | // 78 | // Suspend the "completion" queue when each observer is created. 79 | // Each observer resumes the queue when the child process exits. 80 | // If the queue is resumed too few times (indicating that not all 81 | // observers received the exit event) then the test case will not exit 82 | // within the alloted time and result in failure. 83 | // 84 | 85 | int i; 86 | for (i = 0; i < PID_CNT; ++i) { 87 | dispatch_suspend(completion); 88 | proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_main_queue()); 89 | test_ptr_notnull("DISPATCH_SOURCE_TYPE_PROC", proc); 90 | 91 | dispatch_source_set_event_handler(proc, ^{ 92 | long flags = dispatch_source_get_data(proc); 93 | test_long("DISPATCH_PROC_EXIT", flags, DISPATCH_PROC_EXIT); 94 | event_cnt++; 95 | dispatch_release(proc); 96 | dispatch_resume(completion); 97 | }); 98 | 99 | dispatch_resume(proc); 100 | } 101 | 102 | 103 | // 104 | // The completion block will be pending on the completion queue until it 105 | // has been fully resumed, at which point the test will exit successfully. 106 | // 107 | 108 | dispatch_async(completion, ^{ 109 | int status; 110 | int res2 = waitpid(pid, &status, 0); 111 | assert(res2 != -1); 112 | test_long("Sub-process exited", WEXITSTATUS(status) | WTERMSIG(status), 0); 113 | test_long("Event count", event_cnt, PID_CNT); 114 | test_stop(); 115 | }); 116 | 117 | kill(pid, SIGCONT); 118 | 119 | dispatch_main(); 120 | 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_read.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "dispatch_test.h" 34 | 35 | static size_t bytes_total; 36 | static size_t bytes_read; 37 | 38 | int main(void) 39 | { 40 | const char *path = "/usr/share/dict/words"; 41 | struct stat sb; 42 | 43 | test_start("Dispatch Source Read"); 44 | 45 | exit(0); 46 | 47 | int infd = open(path, O_RDONLY); 48 | if (infd == -1) { 49 | perror(path); 50 | exit(EXIT_FAILURE); 51 | } 52 | if (fstat(infd, &sb) == -1) { 53 | perror(path); 54 | exit(EXIT_FAILURE); 55 | } 56 | bytes_total = sb.st_size; 57 | 58 | if (fcntl(infd, F_SETFL, O_NONBLOCK) != 0) { 59 | perror(path); 60 | exit(EXIT_FAILURE); 61 | } 62 | 63 | dispatch_queue_t main_q = dispatch_get_main_queue(); 64 | test_ptr_notnull("dispatch_get_main_queue", main_q); 65 | 66 | dispatch_source_t reader; 67 | 68 | reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, infd, 0, main_q); 69 | test_ptr_notnull("DISPATCH_SOURCE_TYPE_READ", reader); 70 | 71 | dispatch_source_set_event_handler(reader, ^{ 72 | size_t estimated = dispatch_source_get_data(reader); 73 | printf("bytes available: %zu\n", estimated); 74 | const ssize_t bufsiz = 1024*500; // 500 KB buffer 75 | static char buffer[1024*500]; // 500 KB buffer 76 | ssize_t actual = read(infd, buffer, sizeof(buffer)); 77 | bytes_read += actual; 78 | printf("bytes read: %zd\n", actual); 79 | if (actual < bufsiz) { 80 | actual = read(infd, buffer, sizeof(buffer)); 81 | bytes_read += actual; 82 | // confirm EOF condition 83 | test_long("EOF", actual, 0); 84 | dispatch_source_cancel(reader); 85 | dispatch_release(reader); 86 | } 87 | }); 88 | 89 | dispatch_source_set_cancel_handler(reader, ^{ 90 | test_long("Bytes read", bytes_read, bytes_total); 91 | int res = close(infd); 92 | test_errno("close", res == -1 ? errno : 0, 0); 93 | test_stop(); 94 | }); 95 | 96 | dispatch_resume(reader); 97 | 98 | dispatch_main(); 99 | } 100 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_readsync.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #define __DISPATCH_INDIRECT__ 25 | #include "src/queue_private.h" 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "dispatch_test.h" 33 | 34 | #define LAPS 10000 35 | #define INTERVAL 100 36 | 37 | static size_t r_count = LAPS; 38 | static size_t w_count = LAPS / INTERVAL; 39 | 40 | static void 41 | writer(void *ctxt __attribute__((unused))) 42 | { 43 | if (--w_count == 0) { 44 | if (r_count == 0) { 45 | test_stop(); 46 | } 47 | } 48 | } 49 | 50 | static void 51 | reader(void *ctxt __attribute__((unused))) 52 | { 53 | if (__sync_sub_and_fetch(&r_count, 1) == 0) { 54 | if (r_count == 0) { 55 | test_stop(); 56 | } 57 | } 58 | } 59 | 60 | int 61 | main(void) 62 | { 63 | dispatch_queue_t dq; 64 | 65 | test_start("Dispatch Reader/Writer Queues"); 66 | 67 | dq = dispatch_queue_create("com.apple.libdispatch.test_readsync", NULL); 68 | assert(dq); 69 | 70 | dispatch_queue_set_width(dq, LONG_MAX); 71 | 72 | dispatch_apply(LAPS, dispatch_get_global_queue(0, 0), ^(size_t idx) { 73 | dispatch_sync_f(dq, NULL, reader); 74 | 75 | if (idx % INTERVAL) { 76 | dispatch_barrier_async_f(dq, NULL, writer); 77 | } 78 | }); 79 | 80 | dispatch_release(dq); 81 | 82 | dispatch_main(); 83 | } 84 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_sema.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "dispatch_test.h" 29 | 30 | #define LAPS 10000 31 | 32 | int 33 | main(void) 34 | { 35 | static size_t total; 36 | dispatch_semaphore_t dsema; 37 | 38 | test_start("Dispatch Semaphore"); 39 | 40 | dsema = dispatch_semaphore_create(1); 41 | assert(dsema); 42 | 43 | dispatch_apply(LAPS, dispatch_get_global_queue(0, 0), ^(size_t idx __attribute__((unused))) { 44 | dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER); 45 | total++; 46 | dispatch_semaphore_signal(dsema); 47 | }); 48 | 49 | dispatch_release(dsema); 50 | 51 | test_long("count", total, LAPS); 52 | test_stop(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_starfish.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include "src/internal.h" 25 | #include 26 | #include 27 | #include 28 | #if HAVE_TARGETCONDITIONALS_H 29 | #include 30 | #endif 31 | 32 | #include "dispatch_test.h" 33 | 34 | #if TARGET_OS_EMBEDDED 35 | #define COUNT 300ul 36 | #define LAPS 10ul 37 | #else 38 | #define COUNT 1000ul 39 | #define LAPS 10ul 40 | #endif 41 | 42 | static dispatch_queue_t queues[COUNT]; 43 | static size_t lap_count_down = LAPS; 44 | static size_t count_down; 45 | static uint64_t start; 46 | 47 | static void do_test(void); 48 | 49 | static void 50 | collect(void *context __attribute__((unused))) 51 | { 52 | uint64_t delta; 53 | long double math; 54 | size_t i; 55 | 56 | if (--count_down) { 57 | return; 58 | } 59 | 60 | delta = _dispatch_absolute_time() - start; 61 | math = delta = _dispatch_time_mach2nano(delta); 62 | math /= COUNT * COUNT * 2ul + COUNT * 2ul; 63 | 64 | printf("lap: %ld\n", lap_count_down); 65 | printf("count: %lu\n", COUNT); 66 | printf("delta: %ju ns\n", (uintmax_t)delta); 67 | printf("math: %Lf ns / lap\n", math); 68 | 69 | for (i = 0; i < COUNT; i++) { 70 | dispatch_release(queues[i]); 71 | } 72 | 73 | // our malloc could be a lot better, 74 | // this result is really a malloc torture test 75 | test_long_less_than("Latency" , (unsigned long)math, 1000); 76 | 77 | if (--lap_count_down) { 78 | return do_test(); 79 | } 80 | 81 | // give the threads some time to settle before test_stop() runs "leaks" 82 | // ...also note, this is a total cheat. dispatch_after lets this 83 | // thread go idle, so dispatch cleans up the continuations cache. 84 | // Doign the "old style" sleep left that stuff around and leaks 85 | // took a LONG TIME to complete. Long enough that the test harness 86 | // decided to kill us. 87 | dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, test_stop_after_delay); 88 | } 89 | 90 | static void 91 | pong(void *context) 92 | { 93 | dispatch_queue_t this_q = context; 94 | size_t replies = (size_t)dispatch_get_context(this_q); 95 | 96 | dispatch_set_context(this_q, (void *)--replies); 97 | if (!replies) { 98 | //printf("collect from: %s\n", dispatch_queue_get_label(dispatch_get_current_queue())); 99 | dispatch_async_f(dispatch_get_main_queue(), NULL, collect); 100 | } 101 | } 102 | 103 | static void 104 | ping(void *context) 105 | { 106 | dispatch_queue_t reply_q = context; 107 | 108 | dispatch_async_f(reply_q, reply_q, pong); 109 | } 110 | 111 | static void 112 | start_node(void *context) 113 | { 114 | dispatch_queue_t this_q = context; 115 | size_t i; 116 | 117 | dispatch_set_context(this_q, (void *)COUNT); 118 | 119 | for (i = 0; i < COUNT; i++) { 120 | dispatch_async_f(queues[i], this_q, ping); 121 | } 122 | } 123 | 124 | void 125 | do_test(void) 126 | { 127 | size_t i; 128 | 129 | count_down = COUNT; 130 | 131 | start = _dispatch_absolute_time(); 132 | 133 | for (i = 0; i < COUNT; i++) { 134 | char buf[1000]; 135 | snprintf(buf, sizeof(buf), "com.example.starfish-node#%ld", i); 136 | queues[i] = dispatch_queue_create(buf, NULL); 137 | dispatch_suspend(queues[i]); 138 | } 139 | 140 | for (i = 0; i < COUNT; i++) { 141 | dispatch_async_f(queues[i], queues[i], start_node); 142 | } 143 | 144 | for (i = 0; i < COUNT; i++) { 145 | dispatch_resume(queues[i]); 146 | } 147 | } 148 | 149 | int 150 | main(void) 151 | { 152 | test_start("Dispatch Starfish"); 153 | 154 | do_test(); 155 | 156 | dispatch_main(); 157 | } 158 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_test.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "os_shims.h" 3 | #include 4 | 5 | __DISPATCH_BEGIN_DECLS 6 | 7 | void test_start(const char* desc); 8 | void test_stop(void); 9 | void test_stop_after_delay(void *delay); 10 | 11 | void _test_ptr_null(const char* file, long line, const char* desc, const void* ptr); 12 | #define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b) 13 | 14 | void _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr); 15 | #define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b) 16 | 17 | void _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected); 18 | #define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c) 19 | 20 | void _test_long(const char* file, long line, const char* desc, long actual, long expected); 21 | #define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c) 22 | 23 | void _test_long_less_than(const char* file, long line, const char* desc, long actual, long max_expected); 24 | #define test_long_less_than(a,b,c) _test_long_less_than(__FILE__, __LINE__, a, b, c) 25 | 26 | void _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected); 27 | #define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m) 28 | 29 | void _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected); 30 | #define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m) 31 | 32 | void _test_errno(const char* file, long line, const char* desc, long actual, long expected); 33 | #define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c) 34 | 35 | __DISPATCH_END_DECLS 36 | 37 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_timer_bit31.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "dispatch_test.h" 31 | 32 | // 33 | // There were several bugs related to sign extension / integer overflow that 34 | // were encountered during development. Create a timer whose interval has the 35 | // 31st bit of the word set to verify that it behaves correctly. 36 | // 37 | // 2 s < 0x80000000ull ns < 4 s 38 | // 39 | 40 | int 41 | main(void) 42 | { 43 | test_start("Dispatch Source Timer, bit 31"); 44 | 45 | dispatch_queue_t main_q = dispatch_get_main_queue(); 46 | test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 47 | 48 | dispatch_source_t timer; 49 | struct timeval start_time; 50 | 51 | gettimeofday(&start_time, NULL); 52 | 53 | timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 54 | test_ptr_notnull("DISPATCH_SOURCE_TYPE_TIMER", timer); 55 | 56 | dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0x80000000ull), 0, 0); 57 | 58 | dispatch_source_set_event_handler(timer, ^{ 59 | dispatch_source_cancel(timer); 60 | }); 61 | 62 | dispatch_source_set_cancel_handler(timer, ^{ 63 | struct timeval end_time; 64 | gettimeofday(&end_time, NULL); 65 | 66 | test_long_less_than("elapsed time < 4s", end_time.tv_sec - start_time.tv_sec, 4); 67 | test_long_less_than("elapsed time > 2s", 1, end_time.tv_sec - start_time.tv_sec); 68 | test_stop(); 69 | }); 70 | 71 | dispatch_resume(timer); 72 | 73 | dispatch_main(); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_timer_bit63.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "dispatch_test.h" 31 | 32 | // 33 | // There were several bugs related to sign extension / integer overflow that 34 | // were encountered during development. Create a timer whose interval has the 35 | // 63rd bit of the word set to verify that it behaves correctly. 36 | // 37 | // The expected side-effect of integer overflow would be for the timer to 38 | // fire immediately since its interval has "elapsed." If the timer does not 39 | // fire within 1s we can assume the arithmetic was performed correctly. 40 | // 41 | 42 | int 43 | main(void) 44 | { 45 | test_start("Dispatch Source Timer, bit 63"); 46 | 47 | //uint64_t interval = 0xffffffffffffffffull; 48 | uint64_t interval = 0x8000000000000001ull; 49 | 50 | dispatch_queue_t mainq = dispatch_get_main_queue(); 51 | 52 | __block int i = 0; 53 | struct timeval start_time; 54 | 55 | gettimeofday(&start_time, NULL); 56 | 57 | dispatch_source_t ds; 58 | ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainq); 59 | assert(ds); 60 | dispatch_source_set_event_handler(ds, ^{ 61 | assert(i < 1); 62 | printf("%d\n", i++); 63 | }); 64 | dispatch_source_set_timer(ds, DISPATCH_TIME_NOW, interval, 0); 65 | dispatch_resume(ds); 66 | 67 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), 68 | dispatch_get_main_queue(), ^{ 69 | test_stop(); 70 | }); 71 | 72 | dispatch_main(); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /libdispatch-197/testing/dispatch_timer_set_time.c: -------------------------------------------------------------------------------- 1 | #include "config/config.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define __DISPATCH_INDIRECT__ 9 | #include "src/internal.h" 10 | 11 | #include "dispatch_test.h" 12 | 13 | int main(void) 14 | { 15 | test_start("Dispatch Update Timer"); 16 | 17 | dispatch_queue_t main_q = dispatch_get_main_queue(); 18 | test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 19 | 20 | __block int i = 0; 21 | struct timeval start_time; 22 | 23 | gettimeofday(&start_time, NULL); 24 | dispatch_source_attr_t attr = dispatch_source_attr_create(); 25 | dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) { 26 | struct timeval end_time; 27 | gettimeofday(&end_time, NULL); 28 | // Make sure we actually managed to adjust the interval 29 | // duration. Seven one second ticks would blow past 30 | // this. 31 | test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3); 32 | test_ptr_notnull("finalizer ran", ds); 33 | test_stop(); 34 | }); 35 | 36 | dispatch_source_t s = dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL, 37 | 1000000000ull, 38 | 0, 39 | attr, 40 | main_q, 41 | ^(dispatch_event_t ev) { 42 | long err; 43 | if (dispatch_event_get_error(ev, &err)) { 44 | test_errno("dispatch_event_get_error", err, ECANCELED); 45 | dispatch_release(dispatch_event_get_source(ev)); 46 | } else { 47 | fprintf(stderr, "%d\n", ++i); 48 | if (i >= 7) { 49 | dispatch_cancel(dispatch_event_get_source(ev)); 50 | } else if (i == 1) { 51 | dispatch_source_timer_set_time(dispatch_event_get_source(ev), 100, 0); 52 | } 53 | } 54 | }); 55 | test_ptr_notnull("dispatch_source_timer_create", s); 56 | 57 | dispatch_release(attr); 58 | 59 | dispatch_main(); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /libdispatch-197/testing/func.c: -------------------------------------------------------------------------------- 1 | extern "C" { 2 | void 3 | func(void) 4 | { 5 | } 6 | #ifdef __BLOCKS__ 7 | void (^block)(void) = ^{ }; 8 | #endif 9 | }; 10 | -------------------------------------------------------------------------------- /libdispatch-197/testing/harness.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "dispatch_test.h" 33 | 34 | extern char **environ; 35 | 36 | int 37 | main(int argc, char *argv[]) 38 | { 39 | dispatch_source_t proc; 40 | dispatch_source_t sig; 41 | int res; 42 | pid_t pid; 43 | 44 | if (argc < 2) { 45 | fprintf(stderr, "usage: harness [...]\n"); 46 | exit(1); 47 | } 48 | 49 | // 50 | // Start the test in a suspended state. Will send SIGCONT once ready. 51 | // 52 | 53 | posix_spawnattr_t attr; 54 | res = posix_spawnattr_init(&attr); 55 | assert(res == 0); 56 | res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED); 57 | assert(res == 0); 58 | 59 | int i; 60 | char** newargv = calloc(argc, sizeof(void*)); 61 | for (i = 1; i < argc; ++i) { 62 | newargv[i-1] = argv[i]; 63 | } 64 | newargv[i-1] = NULL; 65 | 66 | res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ); 67 | if (res) { 68 | errno = res; 69 | perror(newargv[0]); 70 | exit(EXIT_FAILURE); 71 | } 72 | //fprintf(stderr, "pid = %d\n", pid); 73 | assert(pid > 0); 74 | 75 | // 76 | // Monitor the test process exit status. 77 | // 78 | 79 | dispatch_queue_t main_q = dispatch_get_main_queue(); 80 | 81 | proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q); 82 | assert(proc); 83 | 84 | dispatch_source_set_event_handler(proc, ^{ 85 | int status; 86 | int res2 = waitpid(pid, &status, 0); 87 | assert(res2 != -1); 88 | test_long("Process exited", WEXITSTATUS(status) | WTERMSIG(status), 0); 89 | exit(0); 90 | }); 91 | 92 | dispatch_resume(proc); 93 | 94 | // 95 | // Forcibly stop currently running test after 30 second timeout. 96 | // 97 | 98 | uint64_t timeout = 30LL * NSEC_PER_SEC; 99 | 100 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), main_q, ^{ 101 | kill(pid, SIGKILL); 102 | fprintf(stderr, "Terminating unresponsive process (%0.1lfs)\n", (double)timeout/NSEC_PER_SEC); 103 | }); 104 | 105 | // 106 | // Control-c forcibly stops currently running test. 107 | // 108 | 109 | // Ignore the default signal handler so that dispatch can handle it. 110 | signal(SIGINT, SIG_IGN); 111 | 112 | sig = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, main_q); 113 | assert(sig); 114 | 115 | dispatch_source_set_event_handler(sig, ^{ 116 | fprintf(stderr, "Terminating process due to signal\n"); 117 | kill(pid, SIGKILL); 118 | }); 119 | dispatch_resume(sig); 120 | 121 | // 122 | // Start the test. 123 | // 124 | 125 | kill(pid, SIGCONT); 126 | 127 | dispatch_main(); 128 | 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /libdispatch-197/testing/leaks-wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | /usr/bin/leaks "$@" 2>&1 | tee "$@.leakslog" | grep -q " 0 leaks for 0 total leaked bytes" 4 | 5 | if [ $? -eq 0 ]; then 6 | rm "$@.leakslog" 7 | exit 0 8 | else 9 | exit $? 10 | fi 11 | -------------------------------------------------------------------------------- /libdispatch-197/testing/nsoperation.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | #include "dispatch_test.h" 30 | 31 | @interface MYOperation : NSOperation 32 | { 33 | } 34 | @end 35 | 36 | @implementation MYOperation 37 | 38 | - (id) init 39 | { 40 | self = [super init]; 41 | return self; 42 | } 43 | 44 | - (void)main 45 | { 46 | test_stop(); 47 | } 48 | 49 | @end 50 | 51 | int 52 | main(void) 53 | { 54 | test_start("NSOperation"); 55 | 56 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 57 | 58 | NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 59 | test_ptr_notnull("NSOperationQueue", queue); 60 | 61 | MYOperation *operation = [[MYOperation alloc] init]; 62 | test_ptr_notnull("NSOperation", operation); 63 | 64 | [queue addOperation:operation]; 65 | [operation release]; 66 | 67 | [[NSRunLoop mainRunLoop] run]; 68 | 69 | [pool release]; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /libdispatch-197/testing/os_shims.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @APPLE_APACHE_LICENSE_HEADER_START@ 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @APPLE_APACHE_LICENSE_HEADER_END@ 17 | */ 18 | 19 | 20 | #ifndef __DISPATCH_TESTS_OS_SHIMS__ 21 | #define __DISPATCH_TESTS_OS_SHIMS__ 22 | 23 | #include "config/config.h" 24 | 25 | #include "shims/asprintf.h" 26 | #include "shims/arc4random.h" 27 | #include "shims/fgetln.h" 28 | 29 | #endif /* __DISPATCH_TESTS_OS_SHIMS__ */ 30 | 31 | -------------------------------------------------------------------------------- /libdispatch-197/testing/queue_finalizer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "config/config.h" 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "dispatch_test.h" 32 | 33 | void *ctxt_magic; 34 | 35 | static void 36 | finalizer(void *ctxt) 37 | { 38 | test_ptr("finalizer ran", ctxt, ctxt_magic); 39 | test_stop(); 40 | } 41 | 42 | int 43 | main(void) { 44 | test_start("Dispatch Queue Finalizer"); 45 | 46 | #ifdef __LP64__ 47 | ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random()); 48 | #else 49 | ctxt_magic = (void*)arc4random(); 50 | #endif 51 | 52 | dispatch_queue_t q = dispatch_queue_create(NULL, NULL); 53 | test_ptr_notnull("dispatch_queue_new", q); 54 | 55 | dispatch_set_context(q, ctxt_magic); 56 | 57 | dispatch_set_finalizer_f(q, finalizer); 58 | 59 | dispatch_release(q); 60 | 61 | dispatch_main(); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /libdispatch-197/testing/shims/arc4random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @APPLE_APACHE_LICENSE_HEADER_START@ 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @APPLE_APACHE_LICENSE_HEADER_END@ 17 | */ 18 | 19 | 20 | #ifndef __DISPATCH_TESTS_SHIMS_ARC4RANDOM__ 21 | #define __DISPATCH_TESTS_SHIMS_ARC4RANDOM__ 22 | 23 | #ifndef HAVE_ARC4RANDOM 24 | unsigned int arc4random(); 25 | #endif 26 | 27 | #endif /* __DISPATCH_TESTS_SHIMS_ARC4RANDOM__ */ 28 | 29 | 30 | -------------------------------------------------------------------------------- /libdispatch-197/testing/shims/asprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libdispatch asprintf portability shim, based on 3 | * http://www.opensource.apple.com/source/OpenSSH/OpenSSH-142/openssh/openbsd-compat/bsd-asprintf.c 4 | * 5 | * 6 | * Copyright (c) 2004 Darren Tucker. 7 | * 8 | * Based originally on asprintf.c from OpenBSD: 9 | * Copyright (c) 1997 Todd C. Miller 10 | * 11 | * Permission to use, copy, modify, and distribute this software for any 12 | * purpose with or without fee is hereby granted, provided that the above 13 | * copyright notice and this permission notice appear in all copies. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 16 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 17 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 18 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 21 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config/config.h" 25 | 26 | #ifndef HAVE_VASPRINTF 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #define INIT_SZ 128 35 | 36 | int 37 | vasprintf(char **str, const char *fmt, va_list ap) 38 | { 39 | int ret = -1; 40 | va_list ap2; 41 | char *string, *newstr; 42 | size_t len; 43 | 44 | va_copy(ap2, ap); 45 | if ((string = malloc(INIT_SZ)) == NULL) 46 | goto fail; 47 | 48 | ret = vsnprintf(string, INIT_SZ, fmt, ap2); 49 | if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ 50 | *str = string; 51 | } else if (ret < 0) { /* Bad length */ 52 | free(string); 53 | goto fail; 54 | } else { /* bigger than initial, realloc allowing for nul */ 55 | len = (size_t)ret + 1; 56 | if ((newstr = realloc(string, len)) == NULL) { 57 | free(string); 58 | goto fail; 59 | } else { 60 | va_end(ap2); 61 | va_copy(ap2, ap); 62 | ret = vsnprintf(newstr, len, fmt, ap2); 63 | if (ret >= 0 && (size_t)ret < len) { 64 | *str = newstr; 65 | } else { /* failed with realloc'ed string, give up */ 66 | free(newstr); 67 | goto fail; 68 | } 69 | } 70 | } 71 | va_end(ap2); 72 | return (ret); 73 | 74 | fail: 75 | *str = NULL; 76 | errno = ENOMEM; 77 | va_end(ap2); 78 | return (-1); 79 | } 80 | #endif 81 | 82 | #ifndef HAVE_ASPRINTF 83 | int asprintf(char **str, const char *fmt, ...) 84 | { 85 | va_list ap; 86 | int ret; 87 | 88 | *str = NULL; 89 | va_start(ap, fmt); 90 | ret = vasprintf(str, fmt, ap); 91 | va_end(ap); 92 | 93 | return ret; 94 | } 95 | #endif 96 | -------------------------------------------------------------------------------- /libdispatch-197/testing/shims/asprintf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @APPLE_APACHE_LICENSE_HEADER_START@ 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @APPLE_APACHE_LICENSE_HEADER_END@ 17 | */ 18 | 19 | 20 | #ifndef __DISPATCH_TESTS_SHIMS_ASPRINTF__ 21 | #define __DISPATCH_TESTS_SHIMS_ASPRINTF__ 22 | 23 | #ifndef HAVE_ASPRINTF 24 | int asprintf(char **str, const char *fmt, ...); 25 | #endif 26 | 27 | #endif /* __DISPATCH_TESTS_SHIMS_ASPRINTF__ */ 28 | -------------------------------------------------------------------------------- /libdispatch-197/testing/shims/fgetln.c: -------------------------------------------------------------------------------- 1 | /* $NetBSD: fgetln.c,v 1.3 2006/09/25 07:18:17 lukem Exp $ */ 2 | 3 | /*- 4 | * libdispatch fgetln shim 5 | * Based on http://www.opensource.apple.com/source/lukemftpd/lukemftpd-32/tnftpd/libnetbsd/fgetln.c 6 | * 7 | * Copyright (c) 1998 The NetBSD Foundation, Inc. 8 | * All rights reserved. 9 | * 10 | * This code is derived from software contributed to The NetBSD Foundation 11 | * by Christos Zoulas. 12 | * 13 | * Redistribution and use in source and binary forms, with or without 14 | * modification, are permitted provided that the following conditions 15 | * are met: 16 | * 1. Redistributions of source code must retain the above copyright 17 | * notice, this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright 19 | * notice, this list of conditions and the following disclaimer in the 20 | * documentation and/or other materials provided with the distribution. 21 | * 3. Neither the name of The NetBSD Foundation nor the names of its 22 | * contributors may be used to endorse or promote products derived 23 | * from this software without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 | * POSSIBILITY OF SUCH DAMAGE. 36 | */ 37 | 38 | #include "config/config.h" 39 | 40 | #ifndef HAVE_FGETLN 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | char * 49 | fgetln(fp, len) 50 | FILE *fp; 51 | size_t *len; 52 | { 53 | static char *buf = NULL; 54 | static size_t bufsiz = 0; 55 | char *ptr; 56 | 57 | 58 | if (buf == NULL) { 59 | bufsiz = BUFSIZ; 60 | if ((buf = malloc(bufsiz)) == NULL) 61 | return NULL; 62 | } 63 | 64 | if (fgets(buf, bufsiz, fp) == NULL) 65 | return NULL; 66 | 67 | *len = 0; 68 | while ((ptr = strchr(&buf[*len], '\n')) == NULL) { 69 | size_t nbufsiz = bufsiz + BUFSIZ; 70 | char *nbuf = realloc(buf, nbufsiz); 71 | 72 | if (nbuf == NULL) { 73 | int oerrno = errno; 74 | free(buf); 75 | errno = oerrno; 76 | buf = NULL; 77 | return NULL; 78 | } else 79 | buf = nbuf; 80 | 81 | *len = bufsiz; 82 | if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL) 83 | return buf; 84 | 85 | bufsiz = nbufsiz; 86 | } 87 | 88 | *len = (ptr - buf) + 1; 89 | return buf; 90 | } 91 | 92 | #endif /* #ifndef HAVE_FGETLN */ 93 | 94 | 95 | -------------------------------------------------------------------------------- /libdispatch-197/testing/shims/fgetln.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @APPLE_APACHE_LICENSE_HEADER_START@ 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @APPLE_APACHE_LICENSE_HEADER_END@ 17 | */ 18 | 19 | 20 | #ifndef __DISPATCH_TESTS_SHIMS_FGETLN__ 21 | #define __DISPATCH_TESTS_SHIMS_FGETLN__ 22 | 23 | #ifndef HAVE_FGETLN 24 | #include 25 | 26 | char *fgetln(FILE *stream, size_t *len); 27 | #endif 28 | 29 | #endif /* __DISPATCH_TESTS_SHIMS_FGETLN__ */ 30 | -------------------------------------------------------------------------------- /libdispatch-197/testing/summarize.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 3 | * 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ 19 | */ 20 | 21 | #include "os_shims.h" 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | int 28 | has_prefix(const char* str, const char* prefix) { 29 | return (strncmp(str, prefix, strlen(prefix)) == 0); 30 | } 31 | 32 | int 33 | print_summary(FILE* f, long total, long pass, long fail) { 34 | fprintf(f, "Total: %ld\n", total); 35 | fprintf(f, "Passed: %ld (%0.0lf%%)\n", pass, ((double)pass / (double)total) * (double)100.0); 36 | fprintf(f, "Failed: %ld (%0.0lf%%)\n", fail, ((double)fail / (double)total) * (double)100.0); 37 | fprintf(f, "\n"); 38 | return 0; 39 | } 40 | 41 | int main(int argc, char* argv[]) { 42 | if (argc > 1) { 43 | fprintf(stderr, "%s: usage: summarize\n", argv[0]); 44 | exit(1); 45 | } 46 | 47 | /* 48 | FILE* f = fopen(argv[1], "w"); 49 | if (f == NULL) { 50 | perror(argv[1]); 51 | exit(1); 52 | } 53 | */ 54 | FILE* f = stdout; 55 | 56 | fprintf(f, "\n==================================================\n"); 57 | fprintf(f, "[SUMMARY] Test Summary\n"); 58 | fprintf(f, "==================================================\n\n"); 59 | 60 | size_t len; 61 | char* ln; 62 | long total = 0; 63 | long pass = 0; 64 | long fail = 0; 65 | long total_total = 0; 66 | long total_pass = 0; 67 | long total_fail = 0; 68 | for(;;) { 69 | ln = fgetln(stdin, &len); 70 | //if (ln) fprintf(stdout, "%.*s", (int)len, ln); 71 | if (ln == NULL || has_prefix(ln, "[TEST]")) { 72 | if (total) { 73 | print_summary(f, total, pass, fail); 74 | } 75 | total_total += total; 76 | total_pass += pass; 77 | total_fail += fail; 78 | total = 0; 79 | pass = 0; 80 | fail = 0; 81 | if (ln) { 82 | fprintf(f, "%.*s", (int)len, ln); 83 | } else { 84 | fprintf(f, "[TOTAL]\n"); 85 | print_summary(f, total_total, total_pass, total_fail); 86 | break; 87 | } 88 | } else if (has_prefix(ln, "[PASS]")) { 89 | ++total; 90 | ++pass; 91 | } else if (has_prefix(ln, "[FAIL]")) { 92 | ++total; 93 | ++fail; 94 | } 95 | } 96 | 97 | return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS); 98 | } 99 | -------------------------------------------------------------------------------- /libdispatch.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mheily/opengcd/0c586e58762782af4958bf1254d4b241b9d26f89/libdispatch.tgz -------------------------------------------------------------------------------- /libdispatch_0~svn197.orig.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mheily/opengcd/0c586e58762782af4958bf1254d4b241b9d26f89/libdispatch_0~svn197.orig.tar.gz -------------------------------------------------------------------------------- /patch/disable_dispatch_read.patch: -------------------------------------------------------------------------------- 1 | Description: disable the dispatch_read unit test which needs libkqueue 2.0a 2 | Author: Mark Heily 3 | 4 | 5 | Index: testing/dispatch_read.c 6 | =================================================================== 7 | --- testing/dispatch_read.c 2011-07-01 22:44:18.000000000 -0400 8 | +++ testing/dispatch_read.c 2011-07-01 22:44:33.000000000 -0400 9 | @@ -42,6 +42,8 @@ 10 | 11 | test_start("Dispatch Source Read"); 12 | 13 | + exit(0); 14 | + 15 | int infd = open(path, O_RDONLY); 16 | if (infd == -1) { 17 | perror(path); 18 | -------------------------------------------------------------------------------- /patch/dispatch-atomic.diff: -------------------------------------------------------------------------------- 1 | --- configure.ac.OLD 2012-09-05 21:40:24.000000000 -0400 2 | +++ configure.ac 2012-09-05 21:41:12.000000000 -0400 3 | @@ -125,14 +125,14 @@ 4 | 5 | AM_INIT_AUTOMAKE([foreign]) 6 | 7 | -DISPATCH_C_ATOMIC_BUILTINS 8 | - 9 | -case $dispatch_cv_atomic in 10 | - yes) ;; 11 | - -march*) MARCH_FLAGS="$dispatch_cv_atomic" 12 | - AC_SUBST([MARCH_FLAGS]) ;; 13 | - *) AC_MSG_ERROR([No gcc builtin atomic operations available]) ;; 14 | -esac 15 | +#DISPATCH_C_ATOMIC_BUILTINS 16 | +# 17 | +#case $dispatch_cv_atomic in 18 | +# yes) ;; 19 | +# -march*) MARCH_FLAGS="$dispatch_cv_atomic" 20 | +# AC_SUBST([MARCH_FLAGS]) ;; 21 | +# *) AC_MSG_ERROR([No gcc builtin atomic operations available]) ;; 22 | +#esac 23 | 24 | # 25 | # Find libraries we will need 26 | -------------------------------------------------------------------------------- /patch/dispatch-blocks.diff: -------------------------------------------------------------------------------- 1 | --- m4/blocks.m4.OLD 2012-09-05 22:10:01.000000000 -0400 2 | +++ m4/blocks.m4 2012-09-05 22:10:46.000000000 -0400 3 | @@ -50,7 +50,8 @@ 4 | ], [ 5 | AC_MSG_RESULT([-lBlocksRuntime]) 6 | ], [ 7 | - AC_MSG_ERROR([can't find Blocks runtime]) 8 | + AC_MSG_RESULT([-lBlocksRuntime]) 9 | +#AC_MSG_ERROR([can't find Blocks runtime]) 10 | ]) 11 | ]) 12 | CFLAGS="$saveCFLAGS" 13 | @@ -97,7 +98,8 @@ 14 | ], [ 15 | AC_MSG_RESULT([-lBlocksRuntime]) 16 | ], [ 17 | - AC_MSG_ERROR([can't find Blocks runtime]) 18 | + AC_MSG_RESULT([-lBlocksRuntime]) 19 | +# AC_MSG_ERROR([can't find Blocks runtime]) 20 | ]) 21 | ]) 22 | CXXFLAGS="$saveCXXFLAGS" 23 | -------------------------------------------------------------------------------- /patch/dispatch-internal.diff: -------------------------------------------------------------------------------- 1 | --- src/internal.h.OLD 2012-09-05 22:20:25.000000000 -0400 2 | +++ src/internal.h 2012-09-05 22:20:56.000000000 -0400 3 | @@ -120,7 +120,7 @@ 4 | #include 5 | #include 6 | #if HAVE_SYS_SYSCTL_H 7 | -#include 8 | +//#include 9 | #endif 10 | #include 11 | #include 12 | -------------------------------------------------------------------------------- /patch/dispatch-private_extern.diff: -------------------------------------------------------------------------------- 1 | --- src/shims/time.h 2012-09-30 21:05:42.000000000 -0400 2 | +++ src/shims/time.h 2012-09-30 21:06:02.000000000 -0400 3 | @@ -52,8 +52,8 @@ 4 | bool ratio_1_to_1; 5 | dispatch_once_t pred; 6 | } _dispatch_host_time_data_s; 7 | -__private_extern__ _dispatch_host_time_data_s _dispatch_host_time_data; 8 | -__private_extern__ void _dispatch_get_host_time_init(void *context); 9 | +_dispatch_host_time_data_s _dispatch_host_time_data; 10 | +void _dispatch_get_host_time_init(void *context); 11 | 12 | static inline uint64_t 13 | _dispatch_time_mach2nano(uint64_t machtime) 14 | -------------------------------------------------------------------------------- /patch/dispatch-semaphore.diff: -------------------------------------------------------------------------------- 1 | --- configure.ac.OLD 2012-09-05 21:47:00.000000000 -0400 2 | +++ configure.ac 2012-09-05 21:47:32.000000000 -0400 3 | @@ -219,6 +219,7 @@ 4 | # We support both Mach semaphores and POSIX semaphores; if the former are 5 | # available, prefer them. 6 | # 7 | +have_sem_init="true" # Android has these but does not expose them publicly 8 | AC_MSG_CHECKING([what semaphore type to use]); 9 | AS_IF([test "x$have_mach" = "xtrue"], 10 | [AC_DEFINE(USE_MACH_SEM, 1,[Define to use Mach semaphores]) 11 | -------------------------------------------------------------------------------- /patch/dispatch-spawn.diff: -------------------------------------------------------------------------------- 1 | Index: testing/dispatch_test.c 2 | =================================================================== 3 | --- testing/dispatch_test.c (revision 22) 4 | +++ testing/dispatch_test.c (working copy) 5 | @@ -114,7 +114,7 @@ 6 | free(expected_str); 7 | } 8 | 9 | -#include 10 | +//#include 11 | 12 | extern char **environ; 13 | 14 | -------------------------------------------------------------------------------- /patch/dispatch-workaround.diff: -------------------------------------------------------------------------------- 1 | Index: dispatch/source.h 2 | =================================================================== 3 | --- dispatch/source.h (revision 22) 4 | +++ dispatch/source.h (working copy) 5 | @@ -30,7 +30,7 @@ 6 | #include 7 | #include 8 | #endif 9 | -#include 10 | +#include 11 | 12 | /*! 13 | * @header 14 | Index: src/internal.h 15 | =================================================================== 16 | --- src/internal.h (revision 22) 17 | +++ src/internal.h (working copy) 18 | @@ -138,7 +138,7 @@ 19 | #include 20 | #include 21 | #include 22 | -#include 23 | +//#include 24 | #if USE_POSIX_SEM 25 | #include 26 | #endif 27 | Index: src/shims/getprogname.h 28 | =================================================================== 29 | --- src/shims/getprogname.h (revision 22) 30 | +++ src/shims/getprogname.h (working copy) 31 | @@ -27,9 +27,11 @@ 32 | static inline const char * 33 | getprogname(void) 34 | { 35 | -# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 36 | +# if defined(ANDROID) 37 | + return "FIXME -- TODO"; 38 | +# elif HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 39 | return program_invocation_short_name; 40 | -#elif HAVE_GETEXECNAME 41 | +# elif HAVE_GETEXECNAME 42 | return getexecname(); 43 | # else 44 | # error getprogname(3) is not available on this platform 45 | -------------------------------------------------------------------------------- /patch/getloadavg.diff: -------------------------------------------------------------------------------- 1 | Index: src/posix/manager.c 2 | =================================================================== 3 | --- src/posix/manager.c (revision 258) 4 | +++ src/posix/manager.c (working copy) 5 | @@ -109,6 +109,37 @@ 6 | return 2; 7 | } 8 | 9 | +/* FIXME: should test for symbol instead of for Android */ 10 | +#if defined(__ANDROID_API__) 11 | + 12 | +#include 13 | + 14 | +int getloadavg(double loadavg[], int nelem) 15 | +{ 16 | + int fd; 17 | + ssize_t len; 18 | + char buf[80]; 19 | + 20 | + /* FIXME: this restriction allows the code to be simpler */ 21 | + if (nelem != 1) 22 | + return (-1); 23 | + 24 | + fd = open("/proc/loadavg", O_RDONLY); 25 | + if (fd < 0) 26 | + return (-1); 27 | + 28 | + len = read(fd, &buf, sizeof(buf)); 29 | + (void) close(fd); 30 | + if (len < 0) 31 | + return (-1); 32 | + 33 | + if (sscanf(buf, "%lf ", &loadavg[0]) < 1) 34 | + return (-1); 35 | + 36 | + return (0); 37 | +} 38 | +#endif /* defined(__ANDROID_API__) */ 39 | + 40 | void 41 | manager_reinit(void) 42 | { 43 | @@ -148,10 +179,13 @@ 44 | worker_min = 2; // we can start with a small amount, worker_idle_threshold will be used as new dynamic low watermark 45 | worker_idle_threshold = worker_idle_threshold_per_cpu(); 46 | 47 | + /* FIXME: hope this will be supported in Android soon */ 48 | +#if !defined(__ANDROID_API__) 49 | if (pthread_atfork(NULL, NULL, manager_reinit) < 0) { 50 | dbg_perror("pthread_atfork()"); 51 | return (-1); 52 | } 53 | +#endif /* defined(__ANDROID_API__) */ 54 | 55 | return (0); 56 | } 57 | @@ -279,6 +313,8 @@ 58 | } 59 | 60 | pthread_exit(NULL); 61 | + /* NOTREACHED */ 62 | + return (NULL); 63 | } 64 | 65 | static void * 66 | -------------------------------------------------------------------------------- /patch/kqueue-private.diff: -------------------------------------------------------------------------------- 1 | Index: src/common/private.h 2 | =================================================================== 3 | --- src/common/private.h (revision 553) 4 | +++ src/common/private.h (working copy) 5 | @@ -35,6 +35,18 @@ 6 | 7 | #include "tree.h" 8 | 9 | +/* 10 | + * Android does not provide pthread_cancel() or related functions. 11 | + */ 12 | +#ifdef __BIONIC__ 13 | +#define pthread_setcancelstate(a,b) 0 14 | +#endif 15 | + 16 | +/* Workaround for Android */ 17 | +#ifndef EPOLLONESHOT 18 | +#define EPOLLONESHOT (1 << 30) 19 | +#endif 20 | + 21 | /* GCC atomic builtins. 22 | * See: http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html 23 | */ 24 | @@ -51,10 +63,11 @@ 25 | #define MAX_KEVENT 512 26 | 27 | 28 | -#ifndef NDEBUG 29 | 30 | extern int KQUEUE_DEBUG; 31 | 32 | +#ifndef NDEBUG 33 | + 34 | #define dbg_puts(str) do { \ 35 | if (KQUEUE_DEBUG) \ 36 | fprintf(stderr, "KQ: %s(): %s\n", __func__,str); \ 37 | -------------------------------------------------------------------------------- /patch/libdispatch-r197_v2.patch: -------------------------------------------------------------------------------- 1 | Index: configure.ac 2 | =================================================================== 3 | --- configure.ac 2011-06-28 22:06:58.000000000 -0400 4 | +++ configure.ac 2011-06-28 22:07:19.000000000 -0400 5 | @@ -8,6 +8,7 @@ 6 | AC_CONFIG_AUX_DIR(config) 7 | AC_CONFIG_HEADER([config/config.h]) 8 | AC_CONFIG_MACRO_DIR([m4]) 9 | +AC_PROG_CC([clang gcc cc]) 10 | AC_USE_SYSTEM_EXTENSIONS 11 | AM_MAINTAINER_MODE 12 | 13 | @@ -112,7 +113,6 @@ 14 | ) 15 | 16 | AC_USE_SYSTEM_EXTENSIONS 17 | -AC_PROG_CC 18 | AC_PROG_CXX 19 | AC_PROG_INSTALL 20 | AC_PROG_LIBTOOL 21 | Index: m4/private-extern.m4 22 | =================================================================== 23 | --- m4/private-extern.m4 2011-06-28 22:06:52.000000000 -0400 24 | +++ m4/private-extern.m4 2011-06-28 22:07:19.000000000 -0400 25 | @@ -19,7 +19,15 @@ 26 | [dispatch_cv_private_extern=yes], 27 | [dispatch_cv_private_extern=no])]) 28 | 29 | -if test $dispatch_cv_private_extern = yes; then 30 | +if test $host_os = "linux-gnu" ; then 31 | + # 32 | + # WORKAROUND: Building with clang and the GNU linker causes this error: 33 | + # 34 | + # /usr/bin/ld: .libs/libdispatch_la-apply.o: relocation R_X86_64_PC32 against symbol `_dispatch_hw_config' can not be used when making a shared object; recompile with -fPIC 35 | + # 36 | + AC_DEFINE([__private_extern__], [extern], 37 | + [Define to a replacement for __private_extern]) 38 | +elif test $dispatch_cv_private_extern = yes; then 39 | AC_DEFINE(HAVE_PRIVATE_EXTERN, 1, Define if __private_extern__ present) 40 | elif test $dispatch_cv_hidden_visibility_attribute = yes; then 41 | AC_DEFINE(HAVE_PRIVATE_EXTERN, 1, Define if __private_extern__ present) 42 | Index: src/queue.c 43 | =================================================================== 44 | --- src/queue.c 2011-06-28 22:07:04.000000000 -0400 45 | +++ src/queue.c 2011-06-28 22:07:19.000000000 -0400 46 | @@ -23,6 +23,8 @@ 47 | #include "protocol.h" 48 | #endif 49 | 50 | +static void _dispatch_queue_cleanup(void *ctxt); 51 | + 52 | void 53 | dummy_function(void) 54 | { 55 | @@ -377,7 +379,7 @@ 56 | // The first xchg on the tail will tell the enqueueing thread that it 57 | // is safe to blindly write out to the head pointer. A cmpxchg honors 58 | // the algorithm. 59 | - dispatch_atomic_cmpxchg(&dq->dq_items_head, mediator, NULL); 60 | + (void) dispatch_atomic_cmpxchg(&dq->dq_items_head, mediator, NULL); 61 | _dispatch_debug("no work on global work queue"); 62 | return NULL; 63 | } 64 | @@ -474,12 +476,13 @@ 65 | _dispatch_hw_config.cc_max_physical = 66 | _dispatch_hw_config.cc_max_active; 67 | #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN) 68 | - _dispatch_hw_config.cc_max_active = (int)sysconf(_SC_NPROCESSORS_ONLN); 69 | - if (_dispatch_hw_config.cc_max_active < 0) 70 | - _dispatch_hw_config.cc_max_active = 1; 71 | + int ret; 72 | + 73 | + ret = (int)sysconf(_SC_NPROCESSORS_ONLN); 74 | + 75 | _dispatch_hw_config.cc_max_logical = 76 | _dispatch_hw_config.cc_max_physical = 77 | - _dispatch_hw_config.cc_max_active; 78 | + _dispatch_hw_config.cc_max_active = (ret < 0) ? 1 : ret; 79 | #else 80 | #warning "_dispatch_queue_set_width_init: no supported way to query CPU count" 81 | _dispatch_hw_config.cc_max_logical = 82 | @@ -941,8 +944,33 @@ 83 | if (pthread_main_np()) { 84 | #endif 85 | _dispatch_program_is_probably_callback_driven = true; 86 | - pthread_exit(NULL); 87 | - DISPATCH_CRASH("pthread_exit() returned"); 88 | +#if defined(__linux__) 89 | + /* 90 | + * Workaround for a GNU/Linux bug that causes the process to 91 | + * become a zombie when the main thread calls pthread_exit(). 92 | + */ 93 | + sigset_t mask; 94 | + void *p; 95 | + 96 | + p = pthread_getspecific(dispatch_queue_key); 97 | + if (p != NULL) 98 | + _dispatch_queue_cleanup(p); 99 | + p = pthread_getspecific(dispatch_sema4_key); 100 | + if (p != NULL) 101 | + _dispatch_release(p); 102 | + p = pthread_getspecific(dispatch_cache_key); 103 | + if (p != NULL) 104 | + _dispatch_cache_cleanup2(p); 105 | + 106 | + sigfillset(&mask); 107 | + pthread_sigmask(SIG_SETMASK, &mask, NULL); 108 | + for (;;) { 109 | + pause(); 110 | + } 111 | +#else /* !defined(__linux__) */ 112 | + pthread_exit(NULL); 113 | + DISPATCH_CRASH("pthread_exit() returned"); 114 | +#endif 115 | #if HAVE_PTHREAD_MAIN_NP 116 | } 117 | DISPATCH_CLIENT_CRASH("dispatch_main() must be called on the main thread"); 118 | --------------------------------------------------------------------------------