├── .gitignore ├── .gitmodules ├── .mailmap ├── CMakeLists.txt ├── INSTALL.md ├── LICENSE ├── PATCHES ├── README.md ├── TESTING.md ├── cmake ├── config.h.in └── modules │ ├── CMakeLists.txt │ ├── DTrace.cmake │ ├── DispatchAppleOptions.cmake │ ├── DispatchCompilerWarnings.cmake │ ├── DispatchSanitization.cmake │ ├── FindBlocksRuntime.cmake │ ├── FindLibRT.cmake │ ├── SwiftSupport.cmake │ └── dispatchConfig.cmake.in ├── config └── config.h ├── dispatch ├── CMakeLists.txt ├── base.h ├── block.h ├── darwin │ └── module.modulemap ├── data.h ├── dispatch-vfs.yaml.in ├── dispatch.h ├── generic │ └── module.modulemap ├── group.h ├── introspection.h ├── io.h ├── object.h ├── once.h ├── queue.h ├── semaphore.h ├── source.h └── time.h ├── libdispatch.xcodeproj └── project.pbxproj ├── man ├── CMakeLists.txt ├── dispatch.3 ├── dispatch_after.3 ├── dispatch_api.3 ├── dispatch_apply.3 ├── dispatch_async.3 ├── dispatch_benchmark.3 ├── dispatch_data_create.3 ├── dispatch_group_create.3 ├── dispatch_io_create.3 ├── dispatch_io_read.3 ├── dispatch_object.3 ├── dispatch_once.3 ├── dispatch_queue_create.3 ├── dispatch_read.3 ├── dispatch_semaphore_create.3 ├── dispatch_source_create.3 └── dispatch_time.3 ├── os ├── CMakeLists.txt ├── firehose_buffer_private.h ├── firehose_server_private.h ├── generic_base.h ├── generic_unix_base.h ├── generic_win_base.h ├── object.h ├── object_private.h ├── voucher_activity_private.h └── voucher_private.h ├── private ├── CMakeLists.txt ├── benchmark.h ├── darwin │ └── module.modulemap ├── data_private.h ├── generic │ └── module.modulemap ├── introspection_private.h ├── io_private.h ├── layout_private.h ├── mach_private.h ├── private.h ├── queue_private.h ├── source_private.h ├── time_private.h └── workloop_private.h ├── resolver ├── resolved.h ├── resolver.c └── resolver.h ├── src ├── .gitignore ├── BlocksRuntime │ ├── Block.h │ ├── Block_private.h │ ├── BlocksRuntime.def │ ├── CMakeLists.txt │ ├── data.c │ └── runtime.c ├── CMakeLists.txt ├── allocator.c ├── allocator_internal.h ├── apply.c ├── benchmark.c ├── block.cpp ├── data.c ├── data.m ├── data_internal.h ├── event │ ├── event.c │ ├── event_config.h │ ├── event_epoll.c │ ├── event_internal.h │ ├── event_kevent.c │ ├── event_windows.c │ ├── workqueue.c │ └── workqueue_internal.h ├── firehose │ ├── firehose.defs │ ├── firehose_buffer.c │ ├── firehose_buffer_internal.h │ ├── firehose_inline_internal.h │ ├── firehose_internal.h │ ├── firehose_reply.defs │ ├── firehose_server.c │ ├── firehose_server_internal.h │ ├── firehose_server_object.m │ └── firehose_types.defs ├── init.c ├── inline_internal.h ├── internal.h ├── introspection.c ├── introspection_internal.h ├── io.c ├── io_internal.h ├── libdispatch.codes ├── mach.c ├── mach_internal.h ├── object.c ├── object.m ├── object_internal.h ├── once.c ├── protocol.defs ├── provider.d ├── queue.c ├── queue_internal.h ├── semaphore.c ├── semaphore_internal.h ├── shims.c ├── shims.h ├── shims │ ├── android_stubs.h │ ├── atomic.h │ ├── atomic_sfb.h │ ├── generic_sys_queue.h │ ├── generic_win_stubs.c │ ├── generic_win_stubs.h │ ├── getprogname.c │ ├── getprogname.h │ ├── hw_config.h │ ├── lock.c │ ├── lock.h │ ├── mach.h │ ├── perfmon.h │ ├── priority.h │ ├── target.h │ ├── time.h │ ├── tsd.h │ ├── yield.c │ └── yield.h ├── source.c ├── source_internal.h ├── swift │ ├── Block.swift │ ├── CMakeLists.txt │ ├── Data.swift │ ├── Dispatch.apinotes │ ├── Dispatch.swift │ ├── DispatchStubs.m │ ├── IO.swift │ ├── Private.swift │ ├── Queue.swift │ ├── Source.swift │ ├── Time.swift │ ├── Wrapper.swift │ └── shims │ │ ├── DispatchOverlayShims.h │ │ └── module.modulemap ├── time.c ├── trace.h ├── transform.c ├── voucher.c └── voucher_internal.h ├── tests ├── .gitignore ├── CMakeLists.txt ├── Foundation │ ├── bench.mm │ ├── dispatch_apply_gc.m │ ├── dispatch_sync_gc.m │ └── nsoperation.m ├── bsdtestharness.c ├── bsdtests.c ├── bsdtests.h ├── bsdtestsummarize.c ├── cffd.c ├── dispatch_after.c ├── dispatch_api.c ├── dispatch_apply.c ├── dispatch_c99.c ├── dispatch_cascade.c ├── dispatch_cf_main.c ├── dispatch_concur.c ├── dispatch_context_for_key.c ├── dispatch_data.c ├── dispatch_deadname.c ├── dispatch_debug.c ├── dispatch_drift.c ├── dispatch_group.c ├── dispatch_io.c ├── dispatch_io_muxed.c ├── dispatch_io_net.c ├── dispatch_io_pipe.c ├── dispatch_io_pipe_close.c ├── dispatch_overcommit.c ├── dispatch_pingpong.c ├── dispatch_plusplus.cpp ├── dispatch_priority.c ├── dispatch_proc.c ├── dispatch_queue_finalizer.c ├── dispatch_read.c ├── dispatch_read2.c ├── dispatch_readsync.c ├── dispatch_select.c ├── dispatch_sema.c ├── dispatch_starfish.c ├── dispatch_suspend_timer.c ├── dispatch_sync_on_main.c ├── dispatch_test.c ├── dispatch_test.h ├── dispatch_timer.c ├── dispatch_timer_bit31.c ├── dispatch_timer_bit63.c ├── dispatch_timer_set_time.c ├── dispatch_timer_short.c ├── dispatch_timer_timeout.c ├── dispatch_transform.c ├── dispatch_vm.c ├── dispatch_vnode.c ├── dispatch_workqueue.c ├── func.c ├── generic_unix_port.h ├── generic_win_port.c ├── generic_win_port.h └── leaks-wrapper.sh ├── tools ├── dispatch_timers.d ├── dispatch_trace.d └── voucher_trace.d ├── xcodeconfig ├── libdispatch-dyld-stub.xcconfig ├── libdispatch-introspection.xcconfig ├── libdispatch-mp-static.xcconfig ├── libdispatch-resolved.xcconfig ├── libdispatch-resolver.xcconfig ├── libdispatch.aliases ├── libdispatch.clean ├── libdispatch.dirty ├── libdispatch.interposable ├── libdispatch.order ├── libdispatch.xcconfig ├── libfirehose.xcconfig └── libfirehose_kernel.xcconfig └── xcodescripts ├── check-order.sh ├── install-dtrace.sh ├── install-headers.sh ├── install-manpages.sh ├── mig-headers.sh ├── postprocess-headers.sh └── run-on-install.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac OS X filesystem metadata 2 | .DS_Store 3 | 4 | # Xcode user artifacts 5 | xcuserdata 6 | project.xcworkspace 7 | 8 | # python generated files 9 | *.pyc 10 | 11 | # build files generated by the configure script 12 | *.ninja 13 | .ninja_deps 14 | .ninja_log 15 | 16 | Build 17 | .build 18 | 19 | # build files generated by autotools 20 | Makefile 21 | Makefile.in 22 | config.log 23 | configure 24 | aclocal.m4 25 | autom4te.cache 26 | config.log 27 | config.status 28 | config 29 | configure 30 | libtool 31 | .dirstamp 32 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swiftlang/swift-corelibs-libdispatch/402997366b7b9bb9d5b3c851d668b6db627f5a37/.gitmodules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Grand Central Dispatch 2 | 3 | Grand Central Dispatch (GCD or libdispatch) provides comprehensive support for concurrent code execution on multicore hardware. 4 | 5 | libdispatch is currently available on all Darwin platforms. This project aims to make a modern version of libdispatch available on all other Swift platforms. To do this, we will implement as much of the portable subset of the API as possible, using the existing open source C implementation. 6 | 7 | libdispatch on Darwin is a combination of logic in the `xnu` kernel alongside the user-space Library. The kernel has the most information available to balance workload across the entire system. As a first step, however, we believe it is useful to bring up the basic functionality of the library using user-space pthread primitives on Linux. Eventually, a Linux kernel module could be developed to support more informed thread scheduling. 8 | 9 | ## Project Status 10 | 11 | A port of libdispatch to Linux has been completed. On Linux, since Swift 3, swift-corelibs-libdispatch has been included in all Swift releases and is used by other swift-corelibs projects. 12 | 13 | Opportunities to contribute and on-going work include: 14 | 15 | 1. Develop a test suite for the Swift APIs of libdispatch. 16 | 2. Enhance libdispatch as needed to support Swift language evolution and the needs of the other Core Libraries projects. 17 | 18 | ## Build and Install 19 | 20 | For detailed instructions on building and installing libdispatch, see [INSTALL.md](INSTALL.md) 21 | 22 | ## Testing 23 | 24 | For detailed instructions on testing libdispatch, see [TESTING.md](TESTING.md) 25 | -------------------------------------------------------------------------------- /TESTING.md: -------------------------------------------------------------------------------- 1 | ## Testing libdispatch 2 | 3 | ### Running tests 4 | 5 | A C-based test suite can be found in the tests subdirectory. 6 | It uses the automake testing harness to execute the tests. 7 | 8 | A default set of tests that are always expected to pass can 9 | be executed by doing 10 | 11 | ``` 12 | make check 13 | ``` 14 | 15 | An extended test suite that includes some tests that may fail 16 | occasionally can be enabled at configure time: 17 | 18 | ``` 19 | ./configure --enable-extended-test-suite 20 | make check 21 | ``` 22 | 23 | ### Additional prerequisites 24 | 25 | A few test cases require additional packages to be installed. 26 | In particular, several IO tests assume /usr/bin/vi is available 27 | as an input file and will fail if it is not present. 28 | -------------------------------------------------------------------------------- /cmake/modules/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set(DISPATCH_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/dispatchExports.cmake) 3 | configure_file(dispatchConfig.cmake.in 4 | ${CMAKE_CURRENT_BINARY_DIR}/dispatchConfig.cmake) 5 | 6 | get_property(DISPATCH_EXPORTS GLOBAL PROPERTY DISPATCH_EXPORTS) 7 | export(TARGETS ${DISPATCH_EXPORTS} FILE ${DISPATCH_EXPORTS_FILE}) 8 | -------------------------------------------------------------------------------- /cmake/modules/DTrace.cmake: -------------------------------------------------------------------------------- 1 | 2 | function(dtrace_usdt_probe script) 3 | set(options) 4 | set(single_parameter_options TARGET_NAME OUTPUT_SOURCES) 5 | set(multiple_parameter_options) 6 | 7 | cmake_parse_arguments("" "${options}" "${single_parameter_options}" "${multiple_parameter_options}" ${ARGN}) 8 | 9 | get_filename_component(script_we ${script} NAME_WE) 10 | 11 | add_custom_command(OUTPUT 12 | ${CMAKE_CURRENT_BINARY_DIR}/${script_we}.h 13 | COMMAND 14 | ${dtrace_EXECUTABLE} -h -s ${script} -o ${CMAKE_CURRENT_BINARY_DIR}/${script_we}.h 15 | DEPENDS 16 | ${script}) 17 | add_custom_target(dtrace-usdt-header-${script_we} 18 | DEPENDS 19 | ${CMAKE_CURRENT_BINARY_DIR}/${script_we}.h) 20 | if(_TARGET_NAME) 21 | set(${_TARGET_NAME} dtrace-usdt-header-${script_we} PARENT_SCOPE) 22 | endif() 23 | if(_OUTPUT_SOURCES) 24 | set(${_OUTPUT_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/${script_we}.h PARENT_SCOPE) 25 | endif() 26 | endfunction() 27 | -------------------------------------------------------------------------------- /cmake/modules/DispatchAppleOptions.cmake: -------------------------------------------------------------------------------- 1 | 2 | set(WITH_APPLE_PTHREAD_SOURCE "" CACHE PATH "Path to Apple's libpthread") 3 | set(WITH_APPLE_LIBPLATFORM_SOURCE "" CACHE PATH "Path to Apple's libplatform") 4 | set(WITH_APPLE_LIBCLOSURE_SOURCE "" CACHE PATH "Path to Apple's libclosure") 5 | set(WITH_APPLE_XNU_SOURCE "" CACHE PATH "Path to Apple's XNU") 6 | set(WITH_APPLE_OBJC4_SOURCE "" CACHE PATH "Path to Apple's ObjC4") 7 | 8 | if(WITH_APPLE_PTHREAD_SOURCE) 9 | include_directories(SYSTEM "${WITH_APPLE_PTHREAD_SOURCE}") 10 | endif() 11 | if(WITH_APPLE_LIBPLATFORM_SOURCE) 12 | include_directories(SYSTEM "${WITH_APPLE_LIBPLATFORM_SOURCE}/include") 13 | endif() 14 | if(WITH_APPLE_LIBCLOSURE_SOURCE) 15 | include_directories(SYSTEM "${WITH_APPLE_LIBCLOSURE_SOURCE}") 16 | endif() 17 | if(WITH_APPLE_XNU_SOURCE) 18 | # FIXME(compnerd) this should use -idirafter 19 | include_directories("${WITH_APPLE_XNU_SOURCE}/libkern") 20 | include_directories(SYSTEM 21 | "${WITH_APPLE_XNU_SOURCE}/bsd" 22 | "${WITH_APPLE_XNU_SOURCE}/libsyscall" 23 | "${WITH_APPLE_XNU_SOURCE}/libsyscall/wrappers/libproc") 24 | 25 | # hack for xnu/bsd/sys/event.h EVFILT_SOCK declaration 26 | add_definitions(-DPRIVATE=1) 27 | endif() 28 | 29 | if(IS_DIRECTORY "/System/Library/Frameworks/System.framework/PrivateHeaders") 30 | include_directories(SYSTEM 31 | "/System/Library/Frameworks/System.framework/PrivateHeaders") 32 | endif() 33 | 34 | option(ENABLE_APPLE_TSD_OPTIMIZATIONS "use non-portable pthread TSD optimizations" OFF) 35 | if(ENABLE_APPLE_TSD_OPTIMIZATIONS) 36 | set(USE_APPLE_TSD_OPTIMIZATIONS 1) 37 | else() 38 | set(USE_APPLE_TSD_OPTIMIZATIONS 0) 39 | endif() 40 | 41 | # TODO(compnerd) link in libpthread headers 42 | 43 | 44 | -------------------------------------------------------------------------------- /cmake/modules/DispatchSanitization.cmake: -------------------------------------------------------------------------------- 1 | 2 | set(DISPATCH_USE_SANITIZER "" CACHE STRING 3 | "Define the sanitizer used to build binaries and tests.") 4 | 5 | if(APPLE AND DISPATCH_USE_SANITIZER) 6 | message(FATAL_ERROR "building libdispatch with sanitization is not supported on Darwin") 7 | endif() 8 | 9 | if(DISPATCH_USE_SANITIZER) 10 | # TODO(compnerd) ensure that the compiler supports these options before adding 11 | # them. At the moment, assume that this will just be used with a GNU 12 | # compatible driver and that the options are spelt correctly in light of that. 13 | add_compile_options("-fno-omit-frame-pointer") 14 | if(CMAKE_BUILD_TYPE MATCHES "Debug") 15 | add_compile_options("-O1") 16 | elseif(NOT CMAKE_BUILD_TYPE MATCHES "Debug" AND 17 | NOT CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo") 18 | add_compile_options("-gline-tables-only") 19 | endif() 20 | 21 | if(LLVM_USE_SANITIZER STREQUAL "Address") 22 | add_compile_options("-fsanitize=address") 23 | elseif(DISPATCH_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 24 | add_compile_options("-fsanitize=memory") 25 | if(DISPATCH_USE_SANITIZER STREQUAL "MemoryWithOrigins") 26 | add_compile_options("-fsanitize-memory-track-origins") 27 | endif() 28 | elseif(DISPATCH_USE_SANITIZER STREQUAL "Undefined") 29 | add_compile_options("-fsanitize=undefined") 30 | add_compile_options("-fno-sanitize=vptr,function") 31 | add_compile_options("-fno-sanitize-recover=all") 32 | elseif(DISPATCH_USE_SANITIZER STREQUAL "Thread") 33 | add_compile_options("-fsanitize=thread") 34 | elseif(DISPATCH_USE_SANITIZER STREQUAL "Address;Undefined" OR 35 | DISPATCH_USE_SANITIZER STREQUAL "Undefined;Address") 36 | add_compile_options("-fsanitize=address,undefined") 37 | add_compile_options("-fno-sanitize=vptr,function") 38 | add_compile_options("-fno-sanitize-recover=all") 39 | elseif(DISPATCH_USE_SANITIZER STREQUAL "Leaks") 40 | add_compile_options("-fsanitize=leak") 41 | else() 42 | message(FATAL_ERROR "unsupported value of DISPATCH_USE_SANITIZER: ${DISPATCH_USE_SANITIZER}") 43 | endif() 44 | endif() 45 | -------------------------------------------------------------------------------- /cmake/modules/FindBlocksRuntime.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # FindBlocksRuntime 3 | # ----------------- 4 | # 5 | # Find libBlocksRuntime library and headers. 6 | # 7 | # The module defines the following variables: 8 | # 9 | # ## 10 | # 11 | # BlocksRuntime_FOUND - true if libBlocksRuntime was found 12 | # BlocksRuntime_INCLUDE_DIR - include search path 13 | # BlocksRuntime_LIBRARIES - libraries to link 14 | 15 | if(BlocksRuntime_INCLUDE_DIR AND BlocksRuntime_LIBRARIES) 16 | set(BlocksRuntime_FOUND TRUE) 17 | else() 18 | find_path(BlocksRuntime_INCLUDE_DIR 19 | NAMES 20 | Blocks.h 21 | HINTS 22 | ${CMAKE_INSTALL_FULL_INCLUDEDIR}) 23 | find_library(BlocksRuntime_LIBRARIES 24 | NAMES 25 | BlocksRuntime libBlocksRuntime 26 | HINTS 27 | ${CMAKE_INSTALL_FULL_LIBDIR}) 28 | 29 | include(FindPackageHandleStandardArgs) 30 | find_package_handle_standard_args(BlocksRuntime 31 | REQUIRED_VARS 32 | BlocksRuntime_LIBRARIES 33 | BlocksRuntime_INCLUDE_DIR) 34 | 35 | mark_as_advanced(BlocksRuntime_LIBRARIES BlocksRuntime_INCLUDE_DIR) 36 | endif() 37 | 38 | if(BlocksRuntime_FOUND) 39 | if(NOT TARGET BlocksRuntime::BlocksRuntime) 40 | add_library(BlocksRuntime::BlocksRuntime UNKNOWN IMPORTED) 41 | set_target_properties(BlocksRuntime::BlocksRuntime 42 | PROPERTIES 43 | IMPORTED_LOCATION 44 | ${BlocksRuntime_LIBRARIES} 45 | INTERFACE_INCLUDE_DIRECTORIES 46 | ${BlocksRuntime_INCLUDE_DIR}) 47 | endif() 48 | endif() 49 | -------------------------------------------------------------------------------- /cmake/modules/FindLibRT.cmake: -------------------------------------------------------------------------------- 1 | #.rst: 2 | # FindLibRT 3 | # --------- 4 | # 5 | # Find librt library and headers. 6 | # 7 | # The mdoule defines the following variables: 8 | # 9 | # :: 10 | # 11 | # LibRT_FOUND - true if librt was found 12 | # LibRT_INCLUDE_DIR - include search path 13 | # LibRT_LIBRARIES - libraries to link 14 | 15 | if(UNIX) 16 | find_path(LibRT_INCLUDE_DIR 17 | NAMES 18 | time.h) 19 | find_library(LibRT_LIBRARIES rt) 20 | 21 | include(FindPackageHandleStandardArgs) 22 | find_package_handle_standard_args(LibRT 23 | REQUIRED_VARS 24 | LibRT_LIBRARIES 25 | LibRT_INCLUDE_DIR) 26 | 27 | if(LibRT_FOUND) 28 | if(NOT TARGET RT::rt) 29 | add_library(RT::rt UNKNOWN IMPORTED) 30 | set_target_properties(RT::rt 31 | PROPERTIES 32 | IMPORTED_LOCATION ${LibRT_LIBRARIES} 33 | INTERFACE_INCLUDE_DIRECTORIES ${LibRT_INCLUDE_DIR}) 34 | endif() 35 | endif() 36 | 37 | mark_as_advanced(LibRT_LIBRARIES LibRT_INCLUDE_DIR) 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /cmake/modules/SwiftSupport.cmake: -------------------------------------------------------------------------------- 1 | include_guard() 2 | 3 | if(NOT dispatch_MODULE_TRIPLE) 4 | set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) 5 | if(CMAKE_Swift_COMPILER_TARGET) 6 | list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET}) 7 | endif() 8 | execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) 9 | 10 | string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") 11 | set(dispatch_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files") 12 | mark_as_advanced(dispatch_MODULE_TRIPLE) 13 | 14 | message(CONFIGURE_LOG "Swift module triple: ${module_triple}") 15 | endif() 16 | 17 | function(install_swift_module target) 18 | get_target_property(module ${target} Swift_MODULE_NAME) 19 | if(NOT module) 20 | set(module ${target}) 21 | endif() 22 | install( 23 | FILES $/${module}.swiftdoc 24 | DESTINATION ${INSTALL_TARGET_DIR}/${module}.swiftmodule 25 | RENAME ${dispatch_MODULE_TRIPLE}.swiftdoc) 26 | install( 27 | FILES $/${module}.swiftmodule 28 | DESTINATION ${INSTALL_TARGET_DIR}/${module}.swiftmodule 29 | RENAME ${dispatch_MODULE_TRIPLE}.swiftmodule) 30 | endfunction() 31 | -------------------------------------------------------------------------------- /cmake/modules/dispatchConfig.cmake.in: -------------------------------------------------------------------------------- 1 | 2 | set(DISPATCH_HAS_SWIFT_SDK_OVERLAY @ENABLE_SWIFT@) 3 | 4 | if(NOT TARGET dispatch) 5 | include(@DISPATCH_EXPORTS_FILE@) 6 | endif() 7 | 8 | -------------------------------------------------------------------------------- /dispatch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | if(APPLE) 3 | set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap) 4 | else() 5 | set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap) 6 | endif() 7 | configure_file(dispatch-vfs.yaml.in 8 | ${CMAKE_BINARY_DIR}/dispatch-vfs-overlay.yaml 9 | @ONLY) 10 | 11 | install(FILES 12 | base.h 13 | block.h 14 | data.h 15 | dispatch.h 16 | group.h 17 | introspection.h 18 | io.h 19 | object.h 20 | once.h 21 | queue.h 22 | semaphore.h 23 | source.h 24 | time.h 25 | DESTINATION 26 | "${INSTALL_DISPATCH_HEADERS_DIR}") 27 | if(ENABLE_SWIFT) 28 | install(FILES 29 | ${DISPATCH_MODULE_MAP} 30 | DESTINATION 31 | "${INSTALL_DISPATCH_HEADERS_DIR}") 32 | endif() 33 | 34 | -------------------------------------------------------------------------------- /dispatch/darwin/module.modulemap: -------------------------------------------------------------------------------- 1 | module Dispatch [system] [extern_c] { 2 | umbrella header "dispatch.h" 3 | export * 4 | } 5 | 6 | module DispatchIntrospection [system] [extern_c] { 7 | header "introspection.h" 8 | export * 9 | } 10 | -------------------------------------------------------------------------------- /dispatch/dispatch-vfs.yaml.in: -------------------------------------------------------------------------------- 1 | --- 2 | version: 0 3 | case-sensitive: false 4 | use-external-names: false 5 | roots: 6 | - name: "@CMAKE_CURRENT_SOURCE_DIR@" 7 | type: directory 8 | contents: 9 | - name: module.modulemap 10 | type: file 11 | external-contents: "@DISPATCH_MODULE_MAP@" 12 | -------------------------------------------------------------------------------- /dispatch/dispatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 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 | #include 28 | #include 29 | #elif defined(_WIN32) 30 | #include 31 | #elif defined(__unix__) 32 | #include 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 42 | #include 43 | #endif 44 | #include 45 | #if defined(_WIN32) 46 | #include 47 | #endif 48 | 49 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__has_feature) 50 | #if __has_feature(modules) 51 | #if !defined(__arm__) 52 | #include // for off_t (to match Glibc.modulemap) 53 | #endif 54 | #endif 55 | #endif 56 | 57 | #define DISPATCH_API_VERSION 20180109 58 | 59 | #ifndef __DISPATCH_BUILDING_DISPATCH__ 60 | #ifndef __DISPATCH_INDIRECT__ 61 | #define __DISPATCH_INDIRECT__ 62 | #endif 63 | 64 | #include 65 | #include 66 | #include 67 | #include 68 | #include 69 | #include 70 | #include 71 | #include 72 | #include 73 | #include 74 | #include 75 | #include 76 | 77 | #undef __DISPATCH_INDIRECT__ 78 | #endif /* !__DISPATCH_BUILDING_DISPATCH__ */ 79 | 80 | #endif /* __DISPATCH_PUBLIC__ */ 81 | -------------------------------------------------------------------------------- /dispatch/generic/module.modulemap: -------------------------------------------------------------------------------- 1 | module Dispatch { 2 | requires blocks 3 | export * 4 | link "dispatch" 5 | link "BlocksRuntime" 6 | } 7 | 8 | module DispatchIntrospection [system] [extern_c] { 9 | header "introspection.h" 10 | export * 11 | } 12 | 13 | module CDispatch [system] [extern_c] { 14 | umbrella header "dispatch.h" 15 | export * 16 | requires blocks 17 | link "dispatch" 18 | } 19 | -------------------------------------------------------------------------------- /man/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # TODO(compnerd) add symlinks 3 | if(NOT ENABLE_SWIFT) 4 | install(FILES 5 | dispatch.3 6 | dispatch_after.3 7 | dispatch_api.3 8 | dispatch_apply.3 9 | dispatch_async.3 10 | dispatch_data_create.3 11 | dispatch_group_create.3 12 | dispatch_io_create.3 13 | dispatch_io_read.3 14 | dispatch_object.3 15 | dispatch_once.3 16 | dispatch_queue_create.3 17 | dispatch_read.3 18 | dispatch_semaphore_create.3 19 | dispatch_source_create.3 20 | dispatch_time.3 21 | DESTINATION 22 | "${CMAKE_INSTALL_FULL_MANDIR}/man3") 23 | endif() 24 | -------------------------------------------------------------------------------- /man/dispatch.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2012 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_data_create 3 , 35 | .Xr dispatch_group_create 3 , 36 | .Xr dispatch_io_create 3 , 37 | .Xr dispatch_io_read 3 , 38 | .Xr dispatch_object 3 , 39 | .Xr dispatch_once 3 , 40 | .Xr dispatch_queue_create 3 , 41 | .Xr dispatch_semaphore_create 3 , 42 | .Xr dispatch_source_create 3 , 43 | .Xr dispatch_time 3 44 | -------------------------------------------------------------------------------- /man/dispatch_after.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2010 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 | Submission of the block may be delayed by the system in order to improve power consumption and system performance. 35 | The system applies a leeway (see 36 | .Xr dispatch_source_set_timer 3 ) 37 | that is equal to one tenth of the interval between 38 | .Fa when 39 | and the time at which the function is called, with the leeway capped to at least one millisecond and at most one minute. 40 | .Pp 41 | For a more detailed description about submitting blocks to queues, see 42 | .Xr dispatch_async 3 . 43 | .Sh CAVEATS 44 | .Fn dispatch_after 45 | retains the passed queue. 46 | .Pp 47 | Specifying 48 | .Vt DISPATCH_TIME_NOW 49 | as the 50 | .Fa when 51 | parameter 52 | is supported, but is not as efficient as calling 53 | .Fn dispatch_async . 54 | .Pp 55 | The result of passing 56 | .Vt DISPATCH_TIME_FOREVER 57 | as the 58 | .Fa when 59 | parameter is undefined. 60 | .Pp 61 | .Sh FUNDAMENTALS 62 | The 63 | .Fn dispatch_after 64 | function is a wrapper around 65 | .Fn dispatch_after_f . 66 | .Sh SEE ALSO 67 | .Xr dispatch 3 , 68 | .Xr dispatch_async 3 , 69 | .Xr dispatch_time 3 70 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /man/dispatch_semaphore_create.3: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 2008-2012 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 | .Pp 27 | The 28 | .Fn dispatch_semaphore_wait 29 | function decrements the semaphore. If the resulting value is less than zero, 30 | it waits for a signal from a thread that increments the semaphore by calling 31 | .Fn dispatch_semaphore_signal 32 | before returning. 33 | The 34 | .Fa timeout 35 | parameter is creatable with the 36 | .Xr dispatch_time 3 37 | or 38 | .Xr dispatch_walltime 3 39 | functions. If the timeout is reached without a signal being received, the semaphore 40 | is re-incremented before the function returns. 41 | .Pp 42 | The 43 | .Fn dispatch_semaphore_signal 44 | function increments the counting semaphore. If the previous value was less than zero, 45 | it wakes one of the threads that are waiting in 46 | .Fn dispatch_semaphore_wait 47 | before returning. 48 | .Sh COMPLETION SYNCHRONIZATION 49 | If the 50 | .Fa count 51 | parameter is equal to zero, then the semaphore is useful for synchronizing 52 | completion of work. 53 | For example: 54 | .Bd -literal -offset indent 55 | sema = dispatch_semaphore_create(0); 56 | 57 | dispatch_async(queue, ^{ 58 | foo(); 59 | dispatch_semaphore_signal(sema); 60 | }); 61 | 62 | bar(); 63 | 64 | dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 65 | .Ed 66 | .Sh FINITE RESOURCE POOL 67 | If the 68 | .Fa count 69 | parameter is greater than zero, then the semaphore is useful for managing a 70 | finite pool of resources. 71 | For example, a library that wants to limit Unix descriptor usage: 72 | .Bd -literal -offset indent 73 | sema = dispatch_semaphore_create(getdtablesize() / 4); 74 | .Ed 75 | .Pp 76 | At each Unix FD allocation: 77 | .Bd -literal -offset indent 78 | dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 79 | fd = open("/etc/services", O_RDONLY); 80 | .Ed 81 | .Pp 82 | When each FD is closed: 83 | .Bd -literal -offset indent 84 | close(fd); 85 | dispatch_semaphore_signal(sema); 86 | .Ed 87 | .Sh RETURN VALUES 88 | The 89 | .Fn dispatch_semaphore_create 90 | function returns NULL if no memory is available or if the 91 | .Fa count 92 | parameter is less than zero. 93 | .Pp 94 | The 95 | .Fn dispatch_semaphore_signal 96 | function returns non-zero when a thread is woken. 97 | Otherwise, zero is returned. 98 | .Pp 99 | The 100 | .Fn dispatch_semaphore_wait 101 | function returns zero upon success and non-zero after the timeout expires. If 102 | the timeout is DISPATCH_TIME_FOREVER, then 103 | .Fn dispatch_semaphore_wait 104 | waits forever and always returns zero. 105 | .Sh MEMORY MODEL 106 | Dispatch semaphores are retained and released via calls to 107 | .Fn dispatch_retain 108 | and 109 | .Fn dispatch_release . 110 | .Sh CAVEATS 111 | Unbalanced dispatch semaphores cannot be released. 112 | For a given semaphore, calls to 113 | .Fn dispatch_semaphore_signal 114 | and 115 | .Fn dispatch_semaphore_wait 116 | must be balanced before 117 | .Fn dispatch_release 118 | is called on it. 119 | .Sh SEE ALSO 120 | .Xr dispatch 3 , 121 | .Xr dispatch_object 3 122 | -------------------------------------------------------------------------------- /os/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # TODO(compnerd) ensure that object_private.h voucher_activity_private.h 3 | # voucher_private.h are included in the source tarball 4 | 5 | install(FILES 6 | generic_base.h 7 | generic_unix_base.h 8 | generic_win_base.h 9 | object.h 10 | DESTINATION 11 | "${INSTALL_OS_HEADERS_DIR}") 12 | 13 | -------------------------------------------------------------------------------- /os/generic_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2014 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 __OS_GENERIC_BASE__ 22 | #define __OS_GENERIC_BASE__ 23 | 24 | #if !defined(__BEGIN_DECLS) && !defined(__END_DECLS) 25 | #if defined(__cplusplus) 26 | #define __BEGIN_DECLS extern "C" { 27 | #define __END_DECLS } 28 | #else 29 | #define __BEGIN_DECLS 30 | #define __END_DECLS 31 | #endif 32 | #endif 33 | 34 | #endif /* __OS_GENERIC_BASE__ */ 35 | -------------------------------------------------------------------------------- /os/generic_unix_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This source file is part of the Swift.org open source project 3 | * 4 | * Copyright (c) 2015 Apple Inc. and the Swift project authors 5 | * 6 | * Licensed under Apache License v2.0 with Runtime Library Exception 7 | * 8 | * See https://swift.org/LICENSE.txt for license information 9 | * See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | * 11 | */ 12 | 13 | #ifndef __OS_GENERIC_UNIX_BASE__ 14 | #define __OS_GENERIC_UNIX_BASE__ 15 | 16 | #include 17 | 18 | #if __has_include() 19 | #include 20 | #endif 21 | 22 | #if defined(__FreeBSD__) 23 | #include 24 | #include 25 | #endif 26 | #include 27 | 28 | #if __has_include() 29 | #include 30 | #endif 31 | 32 | #ifndef API_AVAILABLE 33 | #define API_AVAILABLE(...) 34 | #endif 35 | #ifndef API_DEPRECATED 36 | #define API_DEPRECATED(...) 37 | #endif 38 | #ifndef API_UNAVAILABLE 39 | #define API_UNAVAILABLE(...) 40 | #endif 41 | #ifndef API_DEPRECATED_WITH_REPLACEMENT 42 | #define API_DEPRECATED_WITH_REPLACEMENT(...) 43 | #endif 44 | 45 | #if __GNUC__ 46 | #define OS_EXPECT(x, v) __builtin_expect((x), (v)) 47 | #define OS_UNUSED __attribute__((__unused__)) 48 | #else 49 | #define OS_EXPECT(x, v) (x) 50 | #define OS_UNUSED 51 | #endif 52 | 53 | #ifndef os_likely 54 | #define os_likely(x) OS_EXPECT(!!(x), 1) 55 | #endif 56 | #ifndef os_unlikely 57 | #define os_unlikely(x) OS_EXPECT(!!(x), 0) 58 | #endif 59 | 60 | #if __has_feature(assume_nonnull) 61 | #define OS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") 62 | #define OS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") 63 | #else 64 | #define OS_ASSUME_NONNULL_BEGIN 65 | #define OS_ASSUME_NONNULL_END 66 | #endif 67 | 68 | #if __has_builtin(__builtin_assume) 69 | #define OS_COMPILER_CAN_ASSUME(expr) __builtin_assume(expr) 70 | #else 71 | #define OS_COMPILER_CAN_ASSUME(expr) ((void)(expr)) 72 | #endif 73 | 74 | #if __has_feature(attribute_availability_swift) 75 | // equivalent to __SWIFT_UNAVAILABLE from Availability.h 76 | #define OS_SWIFT_UNAVAILABLE(_msg) \ 77 | __attribute__((__availability__(swift, unavailable, message=_msg))) 78 | #else 79 | #define OS_SWIFT_UNAVAILABLE(_msg) 80 | #endif 81 | 82 | #if __has_attribute(swift_private) 83 | # define OS_REFINED_FOR_SWIFT __attribute__((__swift_private__)) 84 | #else 85 | # define OS_REFINED_FOR_SWIFT 86 | #endif 87 | 88 | #if __has_attribute(swift_name) 89 | # define OS_SWIFT_NAME(_name) __attribute__((__swift_name__(#_name))) 90 | #else 91 | # define OS_SWIFT_NAME(_name) 92 | #endif 93 | 94 | #define __OS_STRINGIFY(s) #s 95 | #define OS_STRINGIFY(s) __OS_STRINGIFY(s) 96 | #define __OS_CONCAT(x, y) x ## y 97 | #define OS_CONCAT(x, y) __OS_CONCAT(x, y) 98 | 99 | #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums) 100 | #define OS_ENUM(_name, _type, ...) \ 101 | typedef enum : _type { __VA_ARGS__ } _name##_t 102 | #else 103 | #define OS_ENUM(_name, _type, ...) \ 104 | enum { __VA_ARGS__ }; typedef _type _name##_t 105 | #endif 106 | 107 | /* 108 | * Stub out misc linking and compilation attributes 109 | */ 110 | 111 | #ifdef OS_EXPORT 112 | #undef OS_EXPORT 113 | #endif 114 | #define OS_EXPORT 115 | 116 | #ifdef OS_WARN_RESULT_NEEDS_RELEASE 117 | #undef OS_WARN_RESULT_NEEDS_RELEASE 118 | #endif 119 | 120 | #ifdef OS_WARN_RESULT 121 | #undef OS_WARN_RESULT 122 | #endif 123 | #define OS_WARN_RESULT 124 | 125 | #ifdef OS_NOTHROW 126 | #undef OS_NOTHROW 127 | #endif 128 | #define OS_NOTHROW 129 | 130 | #endif /* __OS_GENERIC_UNIX_BASE__ */ 131 | -------------------------------------------------------------------------------- /private/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # TODO(compnerd) ensure that benchmark.h data_private.h introduction_private.h 3 | # io_private.h layout_private.h mach_private.h private.h queue_private.h 4 | # source_private.h are included in the source tarball 5 | 6 | if (INSTALL_PRIVATE_HEADERS) 7 | install(FILES 8 | benchmark.h 9 | data_private.h 10 | introspection_private.h 11 | io_private.h 12 | layout_private.h 13 | mach_private.h 14 | private.h 15 | queue_private.h 16 | source_private.h 17 | time_private.h 18 | workloop_private.h 19 | DESTINATION 20 | "${INSTALL_DISPATCH_HEADERS_DIR}") 21 | endif() 22 | -------------------------------------------------------------------------------- /private/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_ASSUME_NONNULL_BEGIN 36 | 37 | __BEGIN_DECLS 38 | 39 | /*! 40 | * @function dispatch_benchmark 41 | * 42 | * @abstract 43 | * Count the average number of cycles a given block takes to execute. 44 | * 45 | * @param count 46 | * The number of times to serially execute the given block. 47 | * 48 | * @param block 49 | * The block to execute. 50 | * 51 | * @result 52 | * The approximate number of cycles the block takes to execute. 53 | * 54 | * @discussion 55 | * This function is for debugging and performance analysis work. For the best 56 | * results, pass a high count value to dispatch_benchmark(). When benchmarking 57 | * concurrent code, please compare the serial version of the code against the 58 | * concurrent version, and compare the concurrent version on different classes 59 | * of hardware. Please look for inflection points with various data sets and 60 | * keep the following facts in mind: 61 | * 62 | * 1) Code bound by computational bandwidth may be inferred by proportional 63 | * changes in performance as concurrency is increased. 64 | * 2) Code bound by memory bandwidth may be inferred by negligible changes in 65 | * performance as concurrency is increased. 66 | * 3) Code bound by critical sections may be inferred by retrograde changes in 67 | * performance as concurrency is increased. 68 | * 3a) Intentional: locks, mutexes, and condition variables. 69 | * 3b) Accidental: unrelated and frequently modified data on the same 70 | * cache-line. 71 | */ 72 | #ifdef __BLOCKS__ 73 | API_AVAILABLE(macos(10.6), ios(4.0)) 74 | DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW 75 | uint64_t 76 | dispatch_benchmark(size_t count, dispatch_block_t block); 77 | #endif 78 | 79 | API_AVAILABLE(macos(10.6), ios(4.0)) 80 | DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NOTHROW 81 | uint64_t 82 | dispatch_benchmark_f(size_t count, void *_Nullable ctxt, 83 | dispatch_function_t func); 84 | 85 | __END_DECLS 86 | 87 | DISPATCH_ASSUME_NONNULL_END 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /private/darwin/module.modulemap: -------------------------------------------------------------------------------- 1 | module DispatchPrivate [system] [extern_c] { 2 | umbrella header "private.h" 3 | exclude header "mach_private.h" 4 | export * 5 | } 6 | 7 | module DispatchIntrospectionPrivate [system] [extern_c] { 8 | header "introspection_private.h" 9 | export * 10 | } 11 | -------------------------------------------------------------------------------- /private/generic/module.modulemap: -------------------------------------------------------------------------------- 1 | module DispatchPrivate [system] [extern_c] { 2 | umbrella header "private.h" 3 | exclude header "mach_private.h" 4 | export * 5 | } 6 | 7 | module DispatchIntrospectionPrivate [system] [extern_c] { 8 | header "introspection_private.h" 9 | export * 10 | } 11 | -------------------------------------------------------------------------------- /private/layout_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 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_LAYOUT_PRIVATE__ 22 | #define __DISPATCH_LAYOUT_PRIVATE__ 23 | 24 | #ifndef __DISPATCH_INDIRECT__ 25 | #error "Please #include instead of this file directly." 26 | #include // for HeaderDoc 27 | #endif 28 | 29 | __BEGIN_DECLS 30 | 31 | API_AVAILABLE(macos(10.6), ios(4.0)) 32 | DISPATCH_EXPORT const struct dispatch_queue_offsets_s { 33 | // always add new fields at the end 34 | const uint16_t dqo_version; 35 | const uint16_t dqo_label; 36 | const uint16_t dqo_label_size; 37 | const uint16_t dqo_flags; 38 | const uint16_t dqo_flags_size; 39 | const uint16_t dqo_serialnum; 40 | const uint16_t dqo_serialnum_size; 41 | const uint16_t dqo_width; 42 | const uint16_t dqo_width_size; 43 | const uint16_t dqo_running; 44 | const uint16_t dqo_running_size; 45 | // fields added in dqo_version 5: 46 | const uint16_t dqo_suspend_cnt; 47 | const uint16_t dqo_suspend_cnt_size; 48 | const uint16_t dqo_target_queue; 49 | const uint16_t dqo_target_queue_size; 50 | const uint16_t dqo_priority; 51 | const uint16_t dqo_priority_size; 52 | } dispatch_queue_offsets; 53 | 54 | #if DISPATCH_LAYOUT_SPI 55 | /*! 56 | * @group Data Structure Layout SPI 57 | * SPI intended for CoreSymbolication only 58 | */ 59 | 60 | API_AVAILABLE(macos(10.10), ios(8.0)) 61 | DISPATCH_EXPORT const struct dispatch_tsd_indexes_s { 62 | // always add new fields at the end 63 | const uint16_t dti_version; 64 | const uint16_t dti_queue_index; 65 | const uint16_t dti_voucher_index; 66 | const uint16_t dti_qos_class_index; 67 | /* version 3 */ 68 | const uint16_t dti_continuation_cache_index; 69 | } dispatch_tsd_indexes; 70 | 71 | #if TARGET_OS_MAC 72 | 73 | #include 74 | 75 | API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0)) 76 | DISPATCH_EXPORT const struct dispatch_allocator_layout_s { 77 | const uint16_t dal_version; 78 | /* version 1 */ 79 | /* Pointer to the allocator metadata address, points to NULL if unused */ 80 | void **const dal_allocator_zone; 81 | /* Magical "isa" for allocations that are on freelists */ 82 | void *const *const dal_deferred_free_isa; 83 | /* Size of allocations made in the magazine */ 84 | const uint16_t dal_allocation_size; 85 | /* fields used by the enumerator */ 86 | const uint16_t dal_magazine_size; 87 | const uint16_t dal_first_allocation_offset; 88 | const uint16_t dal_allocation_isa_offset; 89 | /* Enumerates allocated continuations */ 90 | kern_return_t (*dal_enumerator)(task_t remote_task, 91 | const struct dispatch_allocator_layout_s *remote_allocator_layout, 92 | vm_address_t zone_address, memory_reader_t reader, 93 | void (^recorder)(vm_address_t dc_address, void *dc_mem, 94 | size_t size, bool *stop)); 95 | } dispatch_allocator_layout; 96 | #endif // TARGET_OS_MAC 97 | #endif // DISPATCH_LAYOUT_SPI 98 | 99 | __END_DECLS 100 | 101 | #endif // __DISPATCH_LAYOUT_PRIVATE__ 102 | -------------------------------------------------------------------------------- /private/time_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 20017 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. Any applications relying on 24 | * these interfaces WILL break. 25 | */ 26 | 27 | #ifndef __DISPATCH_TIME_PRIVATE__ 28 | #define __DISPATCH_TIME_PRIVATE__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #include // for HeaderDoc 33 | #endif 34 | 35 | /* 36 | * @constant DISPATCH_MONOTONICTIME_NOW 37 | * A dispatch_time_t value that corresponds to the current value of the 38 | * platform's monotonic clock. On Apple platforms, this clock is based on 39 | * mach_continuous_time(). Use this value with the dispatch_time() function to 40 | * derive a time value for a timer in monotonic time (i.e. a timer that 41 | * continues to tick while the system is asleep). For example: 42 | * 43 | * dispatch_time_t t = dispatch_time(DISPATCH_MONOTONICTIME_NOW,5*NSEC_PER_SEC); 44 | * dispatch_source_t ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 45 | * 0, 0, q); 46 | * dispatch_source_set_event_handler(ds, ^{ ... }); 47 | * dispatch_source_set_timer(ds, t, 10 * NSEC_PER_SEC, 0); 48 | * dispatch_activate(ds); 49 | */ 50 | enum { 51 | DISPATCH_MONOTONICTIME_NOW DISPATCH_ENUM_API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0)) = (1ull << 63) 52 | }; 53 | 54 | #ifdef __APPLE__ 55 | 56 | // Helper macros for up time, montonic time and wall time. 57 | #define _dispatch_uptime_after_nsec(t) \ 58 | dispatch_time(DISPATCH_TIME_NOW, (t)) 59 | #define _dispatch_uptime_after_usec(t) \ 60 | dispatch_time(DISPATCH_TIME_NOW, (t) * NSEC_PER_USEC) 61 | #define _dispatch_uptime_after_msec(t) \ 62 | dispatch_time(DISPATCH_TIME_NOW, (t) * NSEC_PER_MSEC) 63 | #define _dispatch_uptime_after_sec(t) \ 64 | dispatch_time(DISPATCH_TIME_NOW, (t) * NSEC_PER_SEC) 65 | 66 | #define _dispatch_monotonictime_after_nsec(t) \ 67 | dispatch_time(DISPATCH_MONOTONICTIME_NOW, (t)) 68 | #define _dispatch_monotonictime_after_usec(t) \ 69 | dispatch_time(DISPATCH_MONOTONICTIME_NOW, (t) * NSEC_PER_USEC) 70 | #define _dispatch_monotonictime_after_msec(t) \ 71 | dispatch_time(DISPATCH_MONOTONICTIME_NOW, (t) * NSEC_PER_MSEC) 72 | #define _dispatch_monotonictime_after_sec(t) \ 73 | dispatch_time(DISPATCH_MONOTONICTIME_NOW, (t) * NSEC_PER_SEC) 74 | 75 | #define _dispatch_walltime_after_nsec(t) \ 76 | dispatch_time(DISPATCH_WALLTIME_NOW, (t)) 77 | #define _dispatch_walltime_after_usec(t) \ 78 | dispatch_time(DISPATCH_WALLTIME_NOW, (t) * NSEC_PER_USEC) 79 | #define _dispatch_walltime_after_msec(t) \ 80 | dispatch_time(DISPATCH_WALLTIME_NOW, (t) * NSEC_PER_MSEC) 81 | #define _dispatch_walltime_after_sec(t) \ 82 | dispatch_time(DISPATCH_WALLTIME_NOW, (t) * NSEC_PER_SEC) 83 | 84 | #endif // __APPLE__ 85 | 86 | #endif 87 | 88 | -------------------------------------------------------------------------------- /resolver/resolved.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2013 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 | -------------------------------------------------------------------------------- /resolver/resolver.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2013 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 | -------------------------------------------------------------------------------- /resolver/resolver.h: -------------------------------------------------------------------------------- 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 | /* 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_RESOLVERS__ 28 | #define __DISPATCH_RESOLVERS__ 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | provider.h 2 | module.map 3 | module.build.map 4 | .libs 5 | *.lo 6 | *.la 7 | *.o 8 | *.swiftmodule 9 | *.swiftdoc 10 | -------------------------------------------------------------------------------- /src/BlocksRuntime/Block.h: -------------------------------------------------------------------------------- 1 | // This source file is part of the Swift.org open source project 2 | // 3 | // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors 4 | // Licensed under Apache License v2.0 with Runtime Library Exception 5 | // 6 | // See https://swift.org/LICENSE.txt for license information 7 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 8 | // 9 | 10 | 11 | #ifndef _Block_H_ 12 | #define _Block_H_ 13 | 14 | #if defined(_WIN32) 15 | # if defined(BlocksRuntime_STATIC) 16 | # define BLOCK_ABI 17 | # else 18 | # if defined(BlocksRuntime_EXPORTS) 19 | # define BLOCK_ABI __declspec(dllexport) 20 | # else 21 | # define BLOCK_ABI __declspec(dllimport) 22 | # endif 23 | # endif 24 | #else 25 | # define BLOCK_ABI __attribute__((__visibility__("default"))) 26 | #endif 27 | 28 | #if !defined(BLOCK_EXPORT) 29 | # if defined(__cplusplus) 30 | # define BLOCK_EXPORT extern "C" BLOCK_ABI 31 | # else 32 | # define BLOCK_EXPORT extern BLOCK_ABI 33 | # endif 34 | #endif 35 | 36 | #if __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | // Create a heap based copy of a Block or simply add a reference to an existing one. 41 | // This must be paired with Block_release to recover memory, even when running 42 | // under Objective-C Garbage Collection. 43 | BLOCK_EXPORT void *_Block_copy(const void *aBlock); 44 | 45 | // Lose the reference, and if heap based and last reference, recover the memory 46 | BLOCK_EXPORT void _Block_release(const void *aBlock); 47 | 48 | // Used by the compiler. Do not call this function yourself. 49 | BLOCK_EXPORT void _Block_object_assign(void *, const void *, const int); 50 | 51 | // Used by the compiler. Do not call this function yourself. 52 | BLOCK_EXPORT void _Block_object_dispose(const void *, const int); 53 | 54 | // Used by the compiler. Do not use these variables yourself. 55 | #if defined(_WIN32) 56 | extern void * _NSConcreteGlobalBlock[32]; 57 | extern void * _NSConcreteStackBlock[32]; 58 | #else 59 | BLOCK_EXPORT void * _NSConcreteGlobalBlock[32]; 60 | BLOCK_EXPORT void * _NSConcreteStackBlock[32]; 61 | #endif 62 | 63 | #if __cplusplus 64 | } 65 | #endif 66 | 67 | // Type correct macros 68 | 69 | #define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__))) 70 | #define Block_release(...) _Block_release((const void *)(__VA_ARGS__)) 71 | 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/BlocksRuntime/BlocksRuntime.def: -------------------------------------------------------------------------------- 1 | LIBRARY BlocksRuntime 2 | EXPORTS 3 | _NSConcreteGlobalBlock CONSTANT 4 | _NSConcreteStackBlock CONSTANT 5 | -------------------------------------------------------------------------------- /src/BlocksRuntime/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | add_library(BlocksRuntime 3 | data.c 4 | runtime.c) 5 | if(WIN32) 6 | target_sources(BlocksRuntime PRIVATE 7 | BlocksRuntime.def) 8 | 9 | if(NOT BUILD_SHARED_LIBS) 10 | target_compile_definitions(BlocksRuntime PRIVATE 11 | BlocksRuntime_STATIC) 12 | endif() 13 | endif() 14 | 15 | target_include_directories(BlocksRuntime PUBLIC 16 | ${CMAKE_CURRENT_SOURCE_DIR}) 17 | if(HAVE_OBJC AND CMAKE_DL_LIBS) 18 | target_link_libraries(BlocksRuntime PUBLIC 19 | ${CMAKE_DL_LIBS}) 20 | endif() 21 | 22 | if(LINKER_SUPPORTS_BUILD_ID) 23 | target_link_options(BlocksRuntime PRIVATE "LINKER:--build-id=sha1") 24 | endif() 25 | 26 | add_library(BlocksRuntime::BlocksRuntime ALIAS BlocksRuntime) 27 | 28 | install(FILES Block.h 29 | DESTINATION ${INSTALL_BLOCK_HEADERS_DIR}) 30 | if(INSTALL_PRIVATE_HEADERS) 31 | install(FILES Block_private.h 32 | DESTINATION ${INSTALL_BLOCK_HEADERS_DIR}) 33 | endif() 34 | set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS BlocksRuntime) 35 | install(TARGETS BlocksRuntime 36 | EXPORT dispatchExports 37 | ARCHIVE DESTINATION ${INSTALL_TARGET_DIR} 38 | LIBRARY DESTINATION ${INSTALL_TARGET_DIR} 39 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 40 | -------------------------------------------------------------------------------- /src/BlocksRuntime/data.c: -------------------------------------------------------------------------------- 1 | // This source file is part of the Swift.org open source project 2 | // 3 | // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors 4 | // Licensed under Apache License v2.0 with Runtime Library Exception 5 | // 6 | // See https://swift.org/LICENSE.txt for license information 7 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 8 | // 9 | 10 | /******************** 11 | NSBlock support 12 | 13 | We allocate space and export a symbol to be used as the Class for the on-stack and malloc'ed copies until ObjC arrives on the scene. These data areas are set up by Foundation to link in as real classes post facto. 14 | 15 | We keep these in a separate file so that we can include the runtime code in test subprojects but not include the data so that compiled code that sees the data in libSystem doesn't get confused by a second copy. Somehow these don't get unified in a common block. 16 | **********************/ 17 | #include "Block.h" 18 | 19 | #if defined(_WIN32) 20 | void * _NSConcreteStackBlock[32] = { 0 }; 21 | void * _NSConcreteGlobalBlock[32] = { 0 }; 22 | #else 23 | BLOCK_ABI void * _NSConcreteStackBlock[32] = { 0 }; 24 | BLOCK_ABI void * _NSConcreteGlobalBlock[32] = { 0 }; 25 | #endif 26 | 27 | BLOCK_ABI void * _NSConcreteMallocBlock[32] = { 0 }; 28 | BLOCK_ABI void * _NSConcreteAutoBlock[32] = { 0 }; 29 | BLOCK_ABI void * _NSConcreteFinalizingBlock[32] = { 0 }; 30 | BLOCK_ABI void * _NSConcreteWeakBlockVariable[32] = { 0 }; 31 | -------------------------------------------------------------------------------- /src/benchmark.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 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 | struct __dispatch_benchmark_data_s { 24 | #if HAVE_MACH_ABSOLUTE_TIME 25 | mach_timebase_info_data_t tbi; 26 | #endif 27 | uint64_t loop_cost; 28 | void (*func)(void *); 29 | void *ctxt; 30 | size_t count; 31 | }; 32 | 33 | static void 34 | _dispatch_benchmark_init(void *context) 35 | { 36 | struct __dispatch_benchmark_data_s *bdata = context; 37 | // try and simulate performance of real benchmark as much as possible 38 | // keep 'f', 'c' and 'cnt' in registers 39 | register void (*f)(void *) = bdata->func; 40 | register void *c = bdata->ctxt; 41 | register size_t cnt = bdata->count; 42 | size_t i = 0; 43 | uint64_t start, delta; 44 | #if DISPATCH_SIZEOF_PTR == 8 && !defined(_WIN32) 45 | __uint128_t lcost; 46 | #else 47 | long double lcost; 48 | #endif 49 | #if HAVE_MACH_ABSOLUTE_TIME 50 | kern_return_t kr; 51 | 52 | kr = mach_timebase_info(&bdata->tbi); 53 | dispatch_assert_zero(kr); 54 | #endif 55 | 56 | start = _dispatch_uptime(); 57 | do { 58 | i++; 59 | f(c); 60 | } while (i < cnt); 61 | delta = _dispatch_uptime() - start; 62 | 63 | lcost = delta; 64 | #if HAVE_MACH_ABSOLUTE_TIME 65 | lcost *= bdata->tbi.numer; 66 | lcost /= bdata->tbi.denom; 67 | #endif 68 | lcost /= cnt; 69 | 70 | bdata->loop_cost = lcost > UINT64_MAX ? UINT64_MAX : (uint64_t)lcost; 71 | } 72 | 73 | #ifdef __BLOCKS__ 74 | uint64_t 75 | dispatch_benchmark(size_t count, void (^block)(void)) 76 | { 77 | return dispatch_benchmark_f(count, block, _dispatch_Block_invoke(block)); 78 | } 79 | #endif 80 | 81 | static void 82 | _dispatch_benchmark_dummy_function(void *ctxt DISPATCH_UNUSED) 83 | { 84 | } 85 | 86 | uint64_t 87 | dispatch_benchmark_f(size_t count, register void *ctxt, 88 | register void (*func)(void *)) 89 | { 90 | static struct __dispatch_benchmark_data_s bdata = { 91 | .func = _dispatch_benchmark_dummy_function, 92 | .count = 10000000ul, // ten million 93 | }; 94 | static dispatch_once_t pred; 95 | uint64_t ns, start, delta; 96 | #if DISPATCH_SIZEOF_PTR == 8 && !defined(_WIN32) 97 | __uint128_t conversion, big_denom; 98 | #else 99 | long double conversion, big_denom; 100 | #endif 101 | size_t i = 0; 102 | 103 | dispatch_once_f(&pred, &bdata, _dispatch_benchmark_init); 104 | 105 | if (unlikely(count == 0)) { 106 | return 0; 107 | } 108 | 109 | start = _dispatch_uptime(); 110 | do { 111 | i++; 112 | func(ctxt); 113 | } while (i < count); 114 | delta = _dispatch_uptime() - start; 115 | 116 | conversion = delta; 117 | #if HAVE_MACH_ABSOLUTE_TIME 118 | conversion *= bdata.tbi.numer; 119 | big_denom = bdata.tbi.denom; 120 | #else 121 | big_denom = delta; 122 | #endif 123 | big_denom *= count; 124 | conversion /= big_denom; 125 | ns = conversion > UINT64_MAX ? UINT64_MAX : (uint64_t)conversion; 126 | 127 | return ns - bdata.loop_cost; 128 | } 129 | -------------------------------------------------------------------------------- /src/event/workqueue_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017-2017 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_WORKQUEUE_INTERNAL__ 28 | #define __DISPATCH_WORKQUEUE_INTERNAL__ 29 | 30 | void _dispatch_workq_worker_register(dispatch_queue_global_t root_q); 31 | void _dispatch_workq_worker_unregister(dispatch_queue_global_t root_q); 32 | 33 | #if defined(__linux__) || defined(_WIN32) || defined(__OpenBSD__) || defined(__FreeBSD__) 34 | #define HAVE_DISPATCH_WORKQ_MONITORING 1 35 | #else 36 | #define HAVE_DISPATCH_WORKQ_MONITORING 0 37 | #endif 38 | 39 | #endif /* __DISPATCH_WORKQUEUE_INTERNAL__ */ 40 | 41 | -------------------------------------------------------------------------------- /src/firehose/firehose.defs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 "firehose_types.defs" 25 | 26 | subsystem firehose 11600; 27 | serverprefix firehose_server_; 28 | userprefix firehose_send_; 29 | 30 | UseSpecialReplyPort 1; 31 | 32 | simpleroutine register( 33 | server_port : mach_port_t; 34 | mem_port : mach_port_move_send_t; 35 | mem_size : mach_vm_size_t; 36 | comm_mem_recvp : mach_port_move_receive_t; 37 | comm_io_recvp : mach_port_move_receive_t; 38 | comm_sendp : mach_port_make_send_t; 39 | extra_info_port : mach_port_move_send_t; 40 | extra_info_size : mach_vm_size_t; 41 | ServerAuditToken atoken : audit_token_t 42 | ); 43 | 44 | routine push_and_wait( 45 | RequestPort comm_port : mach_port_t; 46 | SReplyPort reply_port : mach_port_make_send_once_t; 47 | out push_reply : firehose_push_reply_t; 48 | out quarantinedOut : boolean_t 49 | ); 50 | 51 | simpleroutine push_async( 52 | RequestPort comm_port : mach_port_t; 53 | in qos_class : qos_class_t; 54 | WaitTime timeout : natural_t 55 | ); 56 | 57 | routine get_logging_prefs( 58 | RequestPort server_port : mach_port_t; 59 | out mem_port : mach_port_t; 60 | out mem_size : mach_vm_size_t 61 | ); 62 | 63 | routine should_send_strings( 64 | RequestPort server_port : mach_port_t; 65 | out strings_needed : boolean_t 66 | ); 67 | -------------------------------------------------------------------------------- /src/firehose/firehose_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __FIREHOSE_INTERNAL__ 22 | #define __FIREHOSE_INTERNAL__ 23 | 24 | #if OS_FIREHOSE_SPI 25 | 26 | // make sure this is defined so that we get MIG_SERVER_DIED when a send once 27 | // notification is sent back because of a crashed server 28 | #ifndef __MigTypeCheck 29 | #define __MigTypeCheck 1 30 | #endif 31 | 32 | #define fcp_quarntined fcp_quarantined 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #include "os/firehose_server_private.h" 45 | #include "firehose_buffer_internal.h" 46 | #ifdef FIREHOSE_SERVER 47 | #include "firehose_server_internal.h" 48 | #endif 49 | #include "firehose_inline_internal.h" 50 | 51 | #endif // OS_FIREHOSE_SPI 52 | 53 | #endif // __FIREHOSE_INTERNAL__ 54 | -------------------------------------------------------------------------------- /src/firehose/firehose_reply.defs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 "firehose_types.defs" 25 | 26 | subsystem firehoseReply 11700; 27 | 28 | serverprefix firehose_client_; 29 | userprefix firehose_send_; 30 | 31 | skip; // firehose_register 32 | 33 | simpleroutine push_reply( 34 | RequestPort req_port : mach_port_move_send_once_t; 35 | in ReturnCode : kern_return_t; 36 | in push_reply : firehose_push_reply_t; 37 | in quarantined : boolean_t 38 | ); 39 | 40 | simpleroutine push_notify_async( 41 | RequestPort comm_port : mach_port_t; 42 | in push_reply : firehose_push_reply_t; 43 | in quarantined : boolean_t; 44 | WaitTime timeout : natural_t 45 | ); 46 | 47 | skip; // get_logging_prefs_reply 48 | 49 | skip; // should_send_strings 50 | -------------------------------------------------------------------------------- /src/firehose/firehose_server_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __FIREHOSE_SERVER_INTERNAL__ 22 | #define __FIREHOSE_SERVER_INTERNAL__ 23 | 24 | OS_OBJECT_CLASS_DECL(firehose_client); 25 | #define FIREHOSE_CLIENT_CLASS OS_OBJECT_VTABLE(firehose_client) 26 | 27 | typedef struct firehose_snapshot_s *firehose_snapshot_t; 28 | struct firehose_snapshot_s { 29 | firehose_snapshot_handler_t handler; 30 | dispatch_group_t fs_group; 31 | }; 32 | 33 | struct firehose_client_s { 34 | union { 35 | _OS_OBJECT_HEADER(void *os_obj_isa, os_obj_ref_cnt, os_obj_xref_cnt); 36 | struct _os_object_s fc_as_os_object; 37 | }; 38 | TAILQ_ENTRY(firehose_client_s) fc_entry; 39 | struct firehose_client_s *volatile fc_next[2]; 40 | 41 | firehose_buffer_t fc_buffer; 42 | uint64_t volatile fc_mem_sent_flushed_pos; 43 | uint64_t volatile fc_mem_flushed_pos; 44 | uint64_t volatile fc_io_sent_flushed_pos; 45 | uint64_t volatile fc_io_flushed_pos; 46 | 47 | #define FC_STATE_ENQUEUED(for_io) (uint16_t)(0x0001u << (for_io)) 48 | #define FC_STATE_MEM_ENQUEUED 0x0001 49 | #define FC_STATE_IO_ENQUEUED 0x0002 50 | 51 | #define FC_STATE_CANCELING(for_io) (uint16_t)(0x0010u << (for_io)) 52 | #define FC_STATE_MEM_CANCELING 0x0010 53 | #define FC_STATE_IO_CANCELING 0x0020 54 | 55 | #define FC_STATE_CANCELED(for_io) (uint16_t)(0x0100u << (for_io)) 56 | #define FC_STATE_MEM_CANCELED 0x0100 57 | #define FC_STATE_IO_CANCELED 0x0200 58 | #define FC_STATE_CANCELED_MASK 0x0300 59 | 60 | void *volatile fc_ctxt; 61 | 62 | union { 63 | dispatch_mach_t fc_mach_channel[FIREHOSE_BUFFER_NPUSHPORTS]; 64 | dispatch_source_t fc_kernel_source; 65 | }; 66 | mach_port_t fc_recvp[FIREHOSE_BUFFER_NPUSHPORTS]; 67 | mach_port_t fc_sendp; 68 | os_unfair_lock fc_lock; 69 | pid_t fc_pid; 70 | int fc_pidversion; 71 | uid_t fc_euid; 72 | os_atomic(uint16_t) fc_state; 73 | os_atomic(uint8_t) fc_mach_channel_refcnt; 74 | // These bits are mutated from different locking domains, and so cannot be 75 | // safely consolidated into a bit-field. 76 | bool volatile fc_strings_cached; 77 | bool volatile fc_memory_corrupted; 78 | bool volatile fc_needs_io_snapshot; 79 | bool volatile fc_needs_mem_snapshot; 80 | bool volatile fc_quarantined; 81 | } DISPATCH_ATOMIC64_ALIGN; 82 | 83 | void 84 | _firehose_client_xref_dispose(struct firehose_client_s *fc); 85 | void 86 | _firehose_client_dispose(struct firehose_client_s *fc); 87 | 88 | extern unsigned char __libfirehose_serverVersionString[]; 89 | extern double __libfirehose_serverVersionNumber; 90 | 91 | #endif // __FIREHOSE_SERVER_INTERNAL__ 92 | -------------------------------------------------------------------------------- /src/firehose/firehose_server_object.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 !USE_OBJC || _OS_OBJECT_OBJC_ARC 24 | #error the firehose server requires the objc-runtime, no ARC 25 | #endif 26 | 27 | @implementation OS_OBJECT_CLASS(firehose_client) 28 | DISPATCH_UNAVAILABLE_INIT() 29 | + (void)load { } 30 | 31 | - (void)_xref_dispose 32 | { 33 | _firehose_client_xref_dispose((struct firehose_client_s *)self); 34 | [super _xref_dispose]; 35 | } 36 | 37 | - (void)_dispose 38 | { 39 | _firehose_client_dispose((struct firehose_client_s *)self); 40 | [super _dispose]; 41 | } 42 | 43 | - (NSString *)debugDescription 44 | { 45 | return nil; 46 | } 47 | @end 48 | -------------------------------------------------------------------------------- /src/firehose/firehose_types.defs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | import ; 25 | import ; 26 | 27 | type firehose_push_reply_t = struct [2] of uint64_t; 28 | type qos_class_t = unsigned; 29 | -------------------------------------------------------------------------------- /src/libdispatch.codes: -------------------------------------------------------------------------------- 1 | 0x2bdc0008 DISPATCH_ARIADNE_dispatch_main 2 | 3 | 0x2e010004 DISPATCH_VOUCHER_dc_push 4 | 0x2e010008 DISPATCH_VOUCHER_dc_pop 5 | 0x2e01000c DISPATCH_VOUCHER_dmsg_push 6 | 0x2e010010 DISPATCH_VOUCHER_dmsg_pop 7 | 0x2e010018 DISPATCH_VOUCHER_activity_adopt 8 | 9 | 0x2e020004 DISPATCH_PERF_non_leaf_retarget 10 | 0x2e020008 DISPATCH_PERF_post_activate_retarget 11 | 0x2e02000c DISPATCH_PERF_post_activate_mutation 12 | 0x2e020010 DISPATCH_PERF_delayed_registration 13 | 0x2e020014 DISPATCH_PERF_mutable_target 14 | 0x2e020018 DISPATCH_PERF_strict_bg_timer 15 | 0x2e02001c DISPATCH_PERF_suspended_timer_fire 16 | 0x2e020020 DISPATCH_PERF_handlerless_source_fire 17 | 0x2e020024 DISPATCH_PERF_source_registration_without_qos 18 | 19 | 0x2e030004 DISPATCH_MACH_MSG_hdr_move 20 | 21 | 0x2e040004 DISPATCH_PERF_MON_worker_thread 22 | 0x2e040008 DISPATCH_PERF_MON_worker_useless 23 | 24 | 0x2e050004 DISPATCH_QOS_TRACE_queue_creation 25 | 0x2e050008 DISPATCH_QOS_TRACE_queue_dispose 26 | 0x2e05000c DISPATCH_QOS_TRACE_block_creation 27 | 0x2e050010 DISPATCH_QOS_TRACE_block_dispose 28 | 0x2e050014 DISPATCH_QOS_TRACE_cont_push_eb 29 | 0x2e050018 DISPATCH_QOS_TRACE_cont_push_ab 30 | 0x2e05001c DISPATCH_QOS_TRACE_cont_push_f 31 | 0x2e050020 DISPATCH_QOS_TRACE_source_push 32 | 0x2e050024 DISPATCH_QOS_TRACE_cont_pop 33 | 0x2e050028 DISPATCH_QOS_TRACE_source_pop 34 | 0x2e05002c DISPATCH_QOS_TRACE_queue_item_done 35 | 0x2e050030 DISPATCH_QOS_TRACE_source_callout 36 | 0x2e050034 DISPATCH_QOS_TRACE_source_dispose 37 | 38 | 0x2e060004 DISPATCH_FIREHOSE_TRACE_reserver_gave_up 39 | 0x2e060008 DISPATCH_FIREHOSE_TRACE_reserver_wait 40 | 0x2e06000c DISPATCH_FIREHOSE_TRACE_allocator 41 | 0x2e060010 DISPATCH_FIREHOSE_TRACE_wait_for_logd 42 | 0x2e060014 DISPATCH_FIREHOSE_TRACE_chunk_install 43 | -------------------------------------------------------------------------------- /src/once.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 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 | 27 | #ifdef __BLOCKS__ 28 | void 29 | dispatch_once(dispatch_once_t *val, dispatch_block_t block) 30 | { 31 | dispatch_once_f(val, block, _dispatch_Block_invoke(block)); 32 | } 33 | #endif 34 | 35 | #if DISPATCH_ONCE_INLINE_FASTPATH 36 | #define DISPATCH_ONCE_SLOW_INLINE inline DISPATCH_ALWAYS_INLINE 37 | #else 38 | #define DISPATCH_ONCE_SLOW_INLINE DISPATCH_NOINLINE 39 | #endif // DISPATCH_ONCE_INLINE_FASTPATH 40 | 41 | DISPATCH_NOINLINE 42 | static void 43 | _dispatch_once_callout(dispatch_once_gate_t l, void *ctxt, 44 | dispatch_function_t func) 45 | { 46 | _dispatch_client_callout(ctxt, func); 47 | _dispatch_once_gate_broadcast(l); 48 | } 49 | 50 | DISPATCH_NOINLINE 51 | void 52 | dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func) 53 | { 54 | dispatch_once_gate_t l = (dispatch_once_gate_t)val; 55 | 56 | #if !DISPATCH_ONCE_INLINE_FASTPATH || DISPATCH_ONCE_USE_QUIESCENT_COUNTER 57 | uintptr_t v = os_atomic_load(&l->dgo_once, acquire); 58 | if (likely(v == DLOCK_ONCE_DONE)) { 59 | return; 60 | } 61 | #if DISPATCH_ONCE_USE_QUIESCENT_COUNTER 62 | if (likely(DISPATCH_ONCE_IS_GEN(v))) { 63 | return _dispatch_once_mark_done_if_quiesced(l, v); 64 | } 65 | #endif 66 | #endif 67 | if (_dispatch_once_gate_tryenter(l)) { 68 | return _dispatch_once_callout(l, ctxt, func); 69 | } 70 | return _dispatch_once_wait(l); 71 | } 72 | -------------------------------------------------------------------------------- /src/protocol.defs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 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 | // '64' is used to align with Mach notifications and so that we don't fight 25 | // with the notify symbols in Libsystem 26 | subsystem libdispatch_internal_protocol 64; 27 | 28 | serverprefix _dispatch_; 29 | userprefix _dispatch_send_; 30 | 31 | skip; /* was MACH_NOTIFY_FIRST: 64 */ 32 | 33 | /* MACH_NOTIFY_PORT_DELETED: 65 */ 34 | simpleroutine 35 | mach_notify_port_deleted( 36 | _notify : mach_port_move_send_once_t; 37 | _name : mach_port_name_t 38 | ); 39 | 40 | /* MACH_NOTIFY_SEND_POSSIBLE: 66 */ 41 | simpleroutine 42 | mach_notify_send_possible( 43 | _notify : mach_port_move_send_once_t; 44 | _name : mach_port_name_t 45 | ); 46 | 47 | skip; /* was NOTIFY_OWNERSHIP_RIGHTS: 67 */ 48 | 49 | skip; /* was NOTIFY_RECEIVE_RIGHTS: 68 */ 50 | 51 | /* MACH_NOTIFY_PORT_DESTROYED: 69 */ 52 | simpleroutine 53 | mach_notify_port_destroyed( 54 | _notify : mach_port_move_send_once_t; 55 | _rights : mach_port_move_receive_t 56 | ); 57 | 58 | /* MACH_NOTIFY_NO_SENDERS: 70 */ 59 | simpleroutine 60 | mach_notify_no_senders( 61 | _notify : mach_port_move_send_once_t; 62 | _mscnt : mach_port_mscount_t 63 | ); 64 | 65 | /* MACH_NOTIFY_SEND_ONCE: 71 */ 66 | simpleroutine 67 | mach_notify_send_once( 68 | _notify : mach_port_move_send_once_t 69 | ); 70 | 71 | /* MACH_NOTIFY_DEAD_NAME: 72 */ 72 | simpleroutine 73 | mach_notify_dead_name( 74 | _notify : mach_port_move_send_once_t; 75 | _name : mach_port_name_t 76 | ); 77 | 78 | /* highly unlikely additional Mach notifications */ 79 | skip; 80 | skip; 81 | skip; 82 | skip; 83 | skip; 84 | 85 | simpleroutine 86 | wakeup_runloop_thread( 87 | _port : mach_port_t; 88 | WaitTime _waitTimeout : natural_t 89 | ); 90 | 91 | simpleroutine 92 | consume_send_once_right( 93 | _port : mach_port_move_send_once_t 94 | ); 95 | -------------------------------------------------------------------------------- /src/shims.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 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 | #include "shims.h" 23 | 24 | #if !HAVE_STRLCPY 25 | size_t strlcpy(char *dst, const char *src, size_t size) 26 | { 27 | size_t res = strlen(dst) + strlen(src) + 1; 28 | if (size > 0) { 29 | size_t n = size - 1; 30 | strncpy(dst, src, n); 31 | dst[n] = 0; 32 | } 33 | return res; 34 | } 35 | #endif 36 | -------------------------------------------------------------------------------- /src/shims/android_stubs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This source file is part of the Swift.org open source project 3 | * 4 | * Copyright (c) 2015 Apple Inc. and the Swift project authors 5 | * 6 | * Licensed under Apache License v2.0 with Runtime Library Exception 7 | * 8 | * See https://swift.org/LICENSE.txt for license information 9 | * See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | * 11 | */ 12 | 13 | // forward declarations for functions we are stubbing out 14 | // in the intial android port. 15 | 16 | #ifndef __DISPATCH__ANDROID__STUBS__INTERNAL 17 | #define __DISPATCH__ANDROID__STUBS__INTERNAL 18 | 19 | #if !__has_feature(c_static_assert) 20 | #define _Static_assert(...) 21 | #endif 22 | 23 | #endif /* __DISPATCH__ANDROID__STUBS__INTERNAL */ 24 | -------------------------------------------------------------------------------- /src/shims/atomic_sfb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2013 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_ATOMIC_SFB__ 28 | #define __DISPATCH_SHIMS_ATOMIC_SFB__ 29 | 30 | #if defined(__x86_64__) || defined(__i386__) 31 | 32 | // Returns UINT_MAX if all the bits in p were already set. 33 | DISPATCH_ALWAYS_INLINE 34 | static inline unsigned int 35 | os_atomic_set_first_bit(volatile unsigned long *p, unsigned int max) 36 | { 37 | unsigned long val, bit; 38 | if (max > (sizeof(val) * 8)) { 39 | __asm__ ( 40 | "1: \n\t" 41 | "mov %[_p], %[_val] \n\t" 42 | "not %[_val] \n\t" 43 | "bsf %[_val], %[_bit] \n\t" /* val is 0 => set zf */ 44 | "jz 2f \n\t" 45 | "lock \n\t" 46 | "bts %[_bit], %[_p] \n\t" /* cf = prev bit val */ 47 | "jc 1b \n\t" /* lost race, retry */ 48 | "jmp 3f \n\t" 49 | "2: \n\t" 50 | "mov %[_all_ones], %[_bit]" "\n\t" 51 | "3: \n\t" 52 | : [_p] "=m" (*p), [_val] "=&r" (val), [_bit] "=&r" (bit) 53 | : [_all_ones] "i" ((__typeof__(bit))UINT_MAX) : "memory", "cc"); 54 | } else { 55 | __asm__ ( 56 | "1: \n\t" 57 | "mov %[_p], %[_val] \n\t" 58 | "not %[_val] \n\t" 59 | "bsf %[_val], %[_bit] \n\t" /* val is 0 => set zf */ 60 | "jz 2f \n\t" 61 | "cmp %[_max], %[_bit] \n\t" 62 | "jg 2f \n\t" 63 | "lock \n\t" 64 | "bts %[_bit], %[_p] \n\t" /* cf = prev bit val */ 65 | "jc 1b \n\t" /* lost race, retry */ 66 | "jmp 3f \n\t" 67 | "2: \n\t" 68 | "mov %[_all_ones], %[_bit]" "\n\t" 69 | "3: \n\t" 70 | : [_p] "=m" (*p), [_val] "=&r" (val), [_bit] "=&r" (bit) 71 | : [_all_ones] "i" ((__typeof__(bit))UINT_MAX), 72 | [_max] "g" ((__typeof__(bit))max) : "memory", "cc"); 73 | } 74 | return (unsigned int)bit; 75 | } 76 | 77 | #else 78 | 79 | #if __clang__ && __clang_major__ < 5 // 80 | #define __builtin_ffs(x) __builtin_ffs((unsigned int)(x)) 81 | #endif 82 | 83 | DISPATCH_ALWAYS_INLINE 84 | static inline unsigned int 85 | os_atomic_set_first_bit(volatile unsigned long *p, unsigned int max_index) 86 | { 87 | unsigned int index; 88 | unsigned long b, b_masked; 89 | 90 | os_atomic_rmw_loop(p, b, b_masked, relaxed, { 91 | // ffs returns 1 + index, or 0 if none set 92 | index = (unsigned int)__builtin_ffsl((long)~b); 93 | if (unlikely(index == 0)) { 94 | os_atomic_rmw_loop_give_up(return UINT_MAX); 95 | } 96 | index--; 97 | if (unlikely(index > max_index)) { 98 | os_atomic_rmw_loop_give_up(return UINT_MAX); 99 | } 100 | b_masked = b | (1UL << index); 101 | }); 102 | 103 | return index; 104 | } 105 | 106 | #endif 107 | 108 | #endif // __DISPATCH_SHIMS_ATOMIC_SFB__ 109 | -------------------------------------------------------------------------------- /src/shims/generic_win_stubs.c: -------------------------------------------------------------------------------- 1 | #include "internal.h" 2 | 3 | typedef void (WINAPI *_precise_time_fn_t)(PULONGLONG); 4 | 5 | DISPATCH_STATIC_GLOBAL(dispatch_once_t _dispatch_precise_time_pred); 6 | DISPATCH_STATIC_GLOBAL(_precise_time_fn_t _dispatch_QueryInterruptTimePrecise_ptr); 7 | DISPATCH_STATIC_GLOBAL(_precise_time_fn_t _dispatch_QueryUnbiasedInterruptTimePrecise_ptr); 8 | 9 | typedef NTSTATUS (NTAPI *_NtQueryInformationFile_fn_t)(HANDLE FileHandle, 10 | PIO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation, ULONG Length, 11 | FILE_INFORMATION_CLASS FileInformationClass); 12 | 13 | DISPATCH_STATIC_GLOBAL(dispatch_once_t _dispatch_ntdll_pred); 14 | DISPATCH_STATIC_GLOBAL(_NtQueryInformationFile_fn_t _dispatch_NtQueryInformationFile_ptr); 15 | 16 | bool 17 | _dispatch_handle_is_socket(HANDLE hFile) 18 | { 19 | // GetFileType() returns FILE_TYPE_PIPE for both pipes and sockets. We can 20 | // disambiguate by checking if PeekNamedPipe() fails with 21 | // ERROR_INVALID_FUNCTION. 22 | if (GetFileType(hFile) == FILE_TYPE_PIPE && 23 | !PeekNamedPipe(hFile, NULL, 0, NULL, NULL, NULL)) { 24 | return GetLastError() == ERROR_INVALID_FUNCTION; 25 | } 26 | return false; 27 | } 28 | 29 | static void 30 | _dispatch_init_precise_time(void *context DISPATCH_UNUSED) 31 | { 32 | HMODULE kernelbase = LoadLibraryW(L"KernelBase.dll"); 33 | if (!kernelbase) { 34 | DISPATCH_INTERNAL_CRASH(0, "failed to load KernelBase.dll"); 35 | } 36 | _dispatch_QueryInterruptTimePrecise_ptr = (_precise_time_fn_t) 37 | GetProcAddress(kernelbase, "QueryInterruptTimePrecise"); 38 | _dispatch_QueryUnbiasedInterruptTimePrecise_ptr = (_precise_time_fn_t) 39 | GetProcAddress(kernelbase, "QueryUnbiasedInterruptTimePrecise"); 40 | if (!_dispatch_QueryInterruptTimePrecise_ptr) { 41 | DISPATCH_INTERNAL_CRASH(0, "could not locate QueryInterruptTimePrecise"); 42 | } 43 | if (!_dispatch_QueryUnbiasedInterruptTimePrecise_ptr) { 44 | DISPATCH_INTERNAL_CRASH(0, "could not locate QueryUnbiasedInterruptTimePrecise"); 45 | } 46 | } 47 | 48 | void 49 | _dispatch_QueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise) 50 | { 51 | dispatch_once_f(&_dispatch_precise_time_pred, NULL, _dispatch_init_precise_time); 52 | return _dispatch_QueryInterruptTimePrecise_ptr(lpInterruptTimePrecise); 53 | } 54 | 55 | void 56 | _dispatch_QueryUnbiasedInterruptTimePrecise(PULONGLONG lpUnbiasedInterruptTimePrecise) 57 | { 58 | dispatch_once_f(&_dispatch_precise_time_pred, NULL, _dispatch_init_precise_time); 59 | return _dispatch_QueryUnbiasedInterruptTimePrecise_ptr(lpUnbiasedInterruptTimePrecise); 60 | } 61 | 62 | static void 63 | _dispatch_init_ntdll(void *context DISPATCH_UNUSED) 64 | { 65 | HMODULE ntdll = LoadLibraryW(L"ntdll.dll"); 66 | if (!ntdll) { 67 | // ntdll is not required. 68 | return; 69 | } 70 | _dispatch_NtQueryInformationFile_ptr = (_NtQueryInformationFile_fn_t) 71 | GetProcAddress(ntdll, "NtQueryInformationFile"); 72 | } 73 | 74 | NTSTATUS _dispatch_NtQueryInformationFile(HANDLE FileHandle, 75 | PIO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation, ULONG Length, 76 | FILE_INFORMATION_CLASS FileInformationClass) 77 | { 78 | dispatch_once_f(&_dispatch_ntdll_pred, NULL, _dispatch_init_ntdll); 79 | if (!_dispatch_NtQueryInformationFile_ptr) { 80 | return STATUS_NOT_SUPPORTED; 81 | } 82 | return _dispatch_NtQueryInformationFile_ptr(FileHandle, IoStatusBlock, 83 | FileInformation, Length, FileInformationClass); 84 | } 85 | -------------------------------------------------------------------------------- /src/shims/generic_win_stubs.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __DISPATCH__STUBS__INTERNAL 3 | #define __DISPATCH__STUBS__INTERNAL 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | /* 18 | * Stub out defines for missing types 19 | */ 20 | 21 | typedef __typeof__(_Generic((__SIZE_TYPE__)0, \ 22 | unsigned long long int : (long long int)0, \ 23 | unsigned long int : (long int)0, \ 24 | unsigned int : (int)0, \ 25 | unsigned short : (short)0, \ 26 | unsigned char : (signed char)0)) ssize_t; 27 | 28 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 29 | #define S_ISFIFO(mode) ((mode) & _S_IFIFO) 30 | #define S_ISREG(mode) ((mode) & _S_IFREG) 31 | #define S_ISSOCK(mode) 0 32 | 33 | #define O_NONBLOCK 04000 34 | 35 | #define bzero(ptr,len) memset((ptr), 0, (len)) 36 | 37 | // Report when an unported code path executes. 38 | #define WIN_PORT_ERROR() \ 39 | _RPTF1(_CRT_ASSERT, "WIN_PORT_ERROR in %s", __FUNCTION__) 40 | 41 | #define strcasecmp _stricmp 42 | 43 | bool _dispatch_handle_is_socket(HANDLE hFile); 44 | 45 | /* 46 | * Wrappers for dynamically loaded Windows APIs 47 | */ 48 | 49 | void _dispatch_QueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise); 50 | void _dispatch_QueryUnbiasedInterruptTimePrecise(PULONGLONG lpUnbiasedInterruptTimePrecise); 51 | 52 | #ifndef HAVE_FILE_PIPE_LOCAL_INFORMATION 53 | enum { 54 | FilePipeLocalInformation = 24, 55 | }; 56 | 57 | typedef struct _FILE_PIPE_LOCAL_INFORMATION { 58 | ULONG NamedPipeType; 59 | ULONG NamedPipeConfiguration; 60 | ULONG MaximumInstances; 61 | ULONG CurrentInstances; 62 | ULONG InboundQuota; 63 | ULONG ReadDataAvailable; 64 | ULONG OutboundQuota; 65 | ULONG WriteQuotaAvailable; 66 | ULONG NamedPipeState; 67 | ULONG NamedPipeEnd; 68 | } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION; 69 | #endif 70 | 71 | NTSTATUS _dispatch_NtQueryInformationFile(HANDLE FileHandle, 72 | PIO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation, ULONG Length, 73 | FILE_INFORMATION_CLASS FileInformationClass); 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/shims/getprogname.c: -------------------------------------------------------------------------------- 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 | #include "getprogname.h" 23 | 24 | #if !HAVE_GETPROGNAME 25 | 26 | #if defined(_WIN32) 27 | #define WIN32_LEAN_AND_MEAN 28 | #ifndef _WIN32_WINNT 29 | #define _WIN32_WINNT 0x0600 30 | #endif /* _WIN32_WINNT */ 31 | #include 32 | #include 33 | 34 | static INIT_ONCE getprogname_init_once = INIT_ONCE_STATIC_INIT; 35 | static TCHAR progname[_MAX_FNAME]; 36 | 37 | static BOOL CALLBACK 38 | getprogname_init_once_handler(PINIT_ONCE InitOnce, PVOID Parameter, 39 | PVOID *lpContext) 40 | { 41 | TCHAR path[MAX_PATH]; 42 | DWORD length = GetModuleFileName(NULL, path, sizeof(path)); 43 | 44 | if (length < 0) { 45 | progname[0] = '\0'; 46 | return TRUE; 47 | } else { 48 | const char *filename; 49 | 50 | path[MAX_PATH - 1] = '\0'; 51 | filename = strrchr(path, '\\'); 52 | if (filename != NULL) { 53 | filename++; 54 | } else { 55 | filename = path; 56 | } 57 | strcpy_s(progname, sizeof(progname), filename); 58 | return TRUE; 59 | } 60 | } 61 | 62 | const char * 63 | getprogname(void) 64 | { 65 | (void)InitOnceExecuteOnce(&getprogname_init_once, 66 | getprogname_init_once_handler, 67 | NULL, 68 | NULL); 69 | return progname; 70 | } 71 | #endif /* _WIN32 */ 72 | #endif /* HAVE_GETPROGNAME */ 73 | -------------------------------------------------------------------------------- /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 | #include 23 | 24 | #ifndef __DISPATCH_SHIMS_GETPROGNAME__ 25 | #define __DISPATCH_SHIMS_GETPROGNAME__ 26 | 27 | #if !HAVE_GETPROGNAME 28 | 29 | #ifdef __ANDROID__ 30 | extern const char *__progname; 31 | #endif /* __ANDROID */ 32 | 33 | #if defined(_WIN32) 34 | const char *getprogname(void); 35 | #else 36 | 37 | static inline char * 38 | getprogname(void) 39 | { 40 | # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 41 | return program_invocation_short_name; 42 | # elif defined(__ANDROID__) 43 | return __progname; 44 | # else 45 | # error getprogname(3) is not available on this platform 46 | # endif 47 | } 48 | #endif /* _WIN32 */ 49 | #endif /* HAVE_GETPROGNAME */ 50 | 51 | #endif /* __DISPATCH_SHIMS_GETPROGNAME__ */ 52 | -------------------------------------------------------------------------------- /src/shims/mach.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 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_MACH__ 22 | #define __DISPATCH_SHIMS_MACH__ 23 | 24 | /* 25 | * Stub out defines for some mach types and related macros 26 | */ 27 | 28 | typedef uint32_t mach_port_t; 29 | 30 | #define MACH_PORT_NULL (0) 31 | #define MACH_PORT_DEAD (-1) 32 | 33 | typedef uint32_t mach_error_t; 34 | 35 | typedef uint32_t mach_msg_return_t; 36 | 37 | typedef uint32_t mach_msg_bits_t; 38 | 39 | typedef void *dispatch_mach_msg_t; 40 | 41 | typedef uint64_t firehose_activity_id_t; 42 | 43 | typedef void *mach_msg_header_t; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/shims/target.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 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 | // These are the portable dispatch version requirements macros, isolated from 28 | // the rest of the C internal headers to be suitable for inclusion in MIG defs, 29 | // asm, etc. 30 | 31 | #ifndef __DISPATCH_SHIMS_TARGET__ 32 | #define __DISPATCH_SHIMS_TARGET__ 33 | 34 | #ifdef __APPLE__ 35 | #include 36 | #include 37 | 38 | #if TARGET_OS_OSX 39 | # define DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(x) \ 40 | (__MAC_OS_X_VERSION_MIN_REQUIRED >= (x)) 41 | # if !DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(101200) 42 | # error "OS X hosts older than OS X 10.12 aren't supported anymore" 43 | # endif // !DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(101200) 44 | #elif TARGET_OS_SIMULATOR 45 | # define DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(x) \ 46 | (IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED >= (x)) 47 | # if !DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(101200) 48 | # error "Simulator hosts older than OS X 10.12 aren't supported anymore" 49 | # endif // !DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(101200) 50 | #else 51 | # define DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(x) 1 52 | # if __IPHONE_OS_VERSION_MIN_REQUIRED < 90000 53 | # error "iOS hosts older than iOS 9.0 aren't supported anymore" 54 | # endif 55 | #endif 56 | 57 | #else // !__APPLE__ 58 | #define DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(x) 0 59 | #endif // !__APPLE__ 60 | 61 | #endif // __DISPATCH_SHIMS_TARGET__ 62 | -------------------------------------------------------------------------------- /src/shims/yield.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 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 | DISPATCH_NOINLINE 24 | static void * 25 | __DISPATCH_WAIT_FOR_ENQUEUER__(void **ptr) 26 | { 27 | int spins = 0; 28 | // Different platforms may expand `_dispatch_preemption_yield` to a 29 | // no-op, but `(void)++spins` is not considered a use like 30 | // `(void)spins` is. Add a use to avoid unused var warnings. 31 | (void)spins; 32 | 33 | void *value; 34 | while ((value = os_atomic_load(ptr, relaxed)) == NULL) { 35 | _dispatch_preemption_yield(++spins); 36 | } 37 | return value; 38 | } 39 | 40 | void * 41 | _dispatch_wait_for_enqueuer(void **ptr) 42 | { 43 | #if !DISPATCH_HW_CONFIG_UP 44 | #if (defined(__arm__) && defined(__APPLE__)) || defined(__arm64__) 45 | int spins = DISPATCH_WAIT_SPINS_WFE; 46 | void *value; 47 | while (unlikely(spins-- > 0)) { 48 | if (likely(value = __builtin_arm_ldrex(ptr))) { 49 | __builtin_arm_clrex(); 50 | return value; 51 | } 52 | __builtin_arm_wfe(); 53 | } 54 | #else 55 | int spins = DISPATCH_WAIT_SPINS; 56 | void *value; 57 | while (unlikely(spins-- > 0)) { 58 | if (likely(value = os_atomic_load(ptr, relaxed))) { 59 | return value; 60 | } 61 | dispatch_hardware_pause(); 62 | } 63 | #endif 64 | #endif // DISPATCH_HW_CONFIG_UP 65 | return __DISPATCH_WAIT_FOR_ENQUEUER__(ptr); 66 | } 67 | -------------------------------------------------------------------------------- /src/source_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013 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_INTERNAL__ 28 | #define __DISPATCH_SOURCE_INTERNAL__ 29 | 30 | #ifndef __DISPATCH_INDIRECT__ 31 | #error "Please #include instead of this file directly." 32 | #include // for HeaderDoc 33 | #endif 34 | 35 | _OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(dispatch_source, dispatch_object) 36 | DISPATCH_CLASS_DECL_BARE(source, QUEUE); 37 | 38 | #define DISPATCH_SOURCE_CLASS_HEADER(x) \ 39 | DISPATCH_LANE_CLASS_HEADER(x); \ 40 | uint16_t \ 41 | /* set under the drain lock */ \ 42 | ds_is_installed:1, \ 43 | dm_connect_handler_called:1, \ 44 | dm_cancel_handler_called:1, \ 45 | dm_is_xpc:1, \ 46 | __ds_flags_pad : 12; \ 47 | uint16_t __dq_flags_separation[0]; \ 48 | uint16_t \ 49 | /* set under the send queue lock */ \ 50 | dm_needs_mgr:1, \ 51 | dm_disconnected:1, \ 52 | __dm_flags_pad : 14 53 | 54 | struct dispatch_source_s { 55 | DISPATCH_SOURCE_CLASS_HEADER(source); 56 | } DISPATCH_ATOMIC64_ALIGN; 57 | dispatch_assert_valid_lane_type(dispatch_source_s); 58 | dispatch_static_assert(sizeof(struct dispatch_source_s) <= 128); 59 | 60 | void _dispatch_source_xref_dispose(dispatch_source_t ds); 61 | void _dispatch_source_dispose(dispatch_source_t ds, bool *allow_free); 62 | void _dispatch_source_activate(dispatch_source_t ds, bool *allow_resume); 63 | void _dispatch_source_invoke(dispatch_source_t ds, 64 | dispatch_invoke_context_t dic, dispatch_invoke_flags_t flags); 65 | void _dispatch_source_wakeup(dispatch_source_t ds, dispatch_qos_t qos, 66 | dispatch_wakeup_flags_t flags); 67 | void _dispatch_source_merge_evt(dispatch_unote_t du, uint32_t flags, 68 | uintptr_t data, pthread_priority_t pp); 69 | DISPATCH_COLD 70 | size_t _dispatch_source_debug(dispatch_source_t ds, char* buf, size_t bufsiz); 71 | 72 | #endif /* __DISPATCH_SOURCE_INTERNAL__ */ 73 | -------------------------------------------------------------------------------- /src/swift/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(SwiftSupport) 2 | 3 | if(HAVE_OBJC) 4 | add_library(DispatchStubs STATIC 5 | DispatchStubs.m) 6 | target_include_directories(DispatchStubs PRIVATE 7 | ${PROJECT_SOURCE_DIR}) 8 | endif() 9 | 10 | add_library(swiftDispatch 11 | Block.swift 12 | Data.swift 13 | Dispatch.swift 14 | IO.swift 15 | Private.swift 16 | Queue.swift 17 | Source.swift 18 | Time.swift 19 | Wrapper.swift) 20 | target_compile_options(swiftDispatch PRIVATE 21 | "SHELL:-Xcc -fblocks" 22 | "SHELL:-Xcc -fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap" 23 | "SHELL:-Xcc -I${PROJECT_SOURCE_DIR}" 24 | "SHELL:-Xcc -I${PROJECT_SOURCE_DIR}/src/swift/shims") 25 | target_compile_options(swiftDispatch PUBLIC 26 | "SHELL:-vfsoverlay ${CMAKE_BINARY_DIR}/dispatch-vfs-overlay.yaml") 27 | set_target_properties(swiftDispatch PROPERTIES 28 | Swift_MODULE_NAME Dispatch 29 | Swift_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/swift 30 | INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}/swift) 31 | target_link_libraries(swiftDispatch PRIVATE 32 | $<$:DispatchStubs> 33 | BlocksRuntime::BlocksRuntime) 34 | target_link_libraries(swiftDispatch PUBLIC 35 | dispatch) 36 | if(NOT APPLE AND NOT WIN32) 37 | target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath") 38 | set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN") 39 | endif() 40 | 41 | install_swift_module(swiftDispatch) 42 | set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS swiftDispatch) 43 | install(TARGETS swiftDispatch 44 | EXPORT dispatchExports 45 | ARCHIVE DESTINATION ${INSTALL_TARGET_DIR} 46 | LIBRARY DESTINATION ${INSTALL_TARGET_DIR} 47 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 48 | if(HAVE_OBJC AND NOT BUILD_SHARED_LIBS) 49 | set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS DispatchStubs) 50 | install(TARGETS DispatchStubs 51 | EXPORT dispatchExports 52 | DESTINATION ${INSTALL_TARGET_DIR}) 53 | endif() 54 | -------------------------------------------------------------------------------- /src/swift/DispatchStubs.m: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | #include 14 | 15 | #if USE_OBJC 16 | @protocol OS_dispatch_source; 17 | @protocol OS_dispatch_source_mach_send; 18 | @protocol OS_dispatch_source_mach_recv; 19 | @protocol OS_dispatch_source_memorypressure; 20 | @protocol OS_dispatch_source_proc; 21 | @protocol OS_dispatch_source_read; 22 | @protocol OS_dispatch_source_signal; 23 | @protocol OS_dispatch_source_timer; 24 | @protocol OS_dispatch_source_data_add; 25 | @protocol OS_dispatch_source_data_or; 26 | @protocol OS_dispatch_source_data_replace; 27 | @protocol OS_dispatch_source_vnode; 28 | @protocol OS_dispatch_source_write; 29 | 30 | __attribute__((__constructor__)) 31 | static void _dispatch_overlay_constructor() { 32 | if (Class source = objc_lookUpClass("OS_dispatch_source")) { 33 | class_addProtocol(source, @protocol(OS_dispatch_source)); 34 | class_addProtocol(source, @protocol(OS_dispatch_source_mach_send)); 35 | class_addProtocol(source, @protocol(OS_dispatch_source_mach_recv)); 36 | class_addProtocol(source, @protocol(OS_dispatch_source_memorypressure)); 37 | class_addProtocol(source, @protocol(OS_dispatch_source_proc)); 38 | class_addProtocol(source, @protocol(OS_dispatch_source_read)); 39 | class_addProtocol(source, @protocol(OS_dispatch_source_signal)); 40 | class_addProtocol(source, @protocol(OS_dispatch_source_timer)); 41 | class_addProtocol(source, @protocol(OS_dispatch_source_data_add)); 42 | class_addProtocol(source, @protocol(OS_dispatch_source_data_or)); 43 | class_addProtocol(source, @protocol(OS_dispatch_source_data_replace)); 44 | class_addProtocol(source, @protocol(OS_dispatch_source_vnode)); 45 | class_addProtocol(source, @protocol(OS_dispatch_source_write)); 46 | } 47 | } 48 | #endif /* USE_OBJC */ 49 | -------------------------------------------------------------------------------- /src/swift/shims/module.modulemap: -------------------------------------------------------------------------------- 1 | module _DispatchOverlayShims { 2 | header "DispatchOverlayShims.h" 3 | } 4 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.lo 3 | *.la 4 | *.log 5 | *.trs 6 | .libs 7 | bsdtestharness 8 | bsdtestsummarize 9 | dispatch 10 | dispatch_after 11 | dispatch_api 12 | dispatch_apply 13 | dispatch_c99 14 | dispatch_cascade 15 | dispatch_concur 16 | dispatch_context_for_key 17 | dispatch_data 18 | dispatch_debug 19 | dispatch_drift 20 | dispatch_group 21 | dispatch_io 22 | dispatch_io_net 23 | dispatch_overcommit 24 | dispatch_pingpong 25 | dispatch_plusplus 26 | dispatch_priority 27 | dispatch_priority2 28 | dispatch_queue_finalizer 29 | dispatch_read 30 | dispatch_read2 31 | dispatch_readsync 32 | dispatch_select 33 | dispatch_sema 34 | dispatch_starfish 35 | dispatch_suspend_timer 36 | dispatch_timer 37 | dispatch_timer_bit31 38 | dispatch_timer_bit63 39 | dispatch_timer_set_time 40 | dispatch_timer_short 41 | dispatch_timer_timeout 42 | dispatch_vnode 43 | leaks-wrapper 44 | 45 | -------------------------------------------------------------------------------- /tests/Foundation/dispatch_apply_gc.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011 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 | #import 23 | #include 24 | 25 | #include 26 | #include "dispatch_test.h" 27 | 28 | #if __OBJC_GC__ 29 | const size_t final = 50000, desclen = 538892; 30 | #else 31 | const size_t final = 1000, desclen = 8892; 32 | #endif 33 | NSAutoreleasePool *pool = nil; 34 | 35 | static void 36 | work(void* ctxt __attribute__((unused))) 37 | { 38 | pool = [[NSAutoreleasePool alloc] init]; 39 | NSMutableArray *a = [NSMutableArray array]; 40 | OSSpinLock sl = OS_SPINLOCK_INIT, *l = &sl; 41 | 42 | dispatch_apply(final, dispatch_get_global_queue(0, 0), ^(size_t i){ 43 | NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithDecimal: 44 | [[NSNumber numberWithInteger:i] decimalValue]]; 45 | OSSpinLockLock(l); 46 | [a addObject:n]; 47 | OSSpinLockUnlock(l); 48 | }); 49 | test_long("count", [a count], final); 50 | test_long("description length", [[a description] length], desclen); 51 | a = nil; 52 | [pool drain]; 53 | test_stop_after_delay((void*)(intptr_t)1); 54 | } 55 | 56 | int 57 | main(void) 58 | { 59 | dispatch_test_start("Dispatch Apply GC"); // 60 | dispatch_async_f(dispatch_get_main_queue(), (void*)(intptr_t)1, work); 61 | CFRunLoopRun(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /tests/Foundation/dispatch_sync_gc.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011 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 | #import 23 | 24 | #include 25 | #include "dispatch_test.h" 26 | 27 | #if __OBJC_GC__ 28 | const size_t final = 50000, desclen = 538892; 29 | #else 30 | const size_t final = 1000, desclen = 8892; 31 | #endif 32 | NSAutoreleasePool *pool = nil; 33 | 34 | static void 35 | work(void* ctxt __attribute__((unused))) 36 | { 37 | pool = [[NSAutoreleasePool alloc] init]; 38 | NSMutableArray *a = [NSMutableArray array]; 39 | dispatch_group_t g = dispatch_group_create(); 40 | 41 | dispatch_group_async(g, dispatch_get_global_queue(0, 0), ^{ 42 | NSUInteger i; 43 | for (i = 0; i < final; i++) { 44 | NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithDecimal: 45 | [[NSNumber numberWithInteger:i] decimalValue]]; 46 | dispatch_sync(dispatch_get_main_queue(), ^{ 47 | [a addObject:n]; 48 | }); 49 | } 50 | }); 51 | dispatch_group_notify(g, dispatch_get_main_queue(), ^{ 52 | test_long("count", [a count], final); 53 | test_long("description length", [[a description] length], desclen); 54 | [pool drain]; 55 | test_stop_after_delay((void*)(intptr_t)1); 56 | }); 57 | dispatch_release(g); 58 | } 59 | 60 | int 61 | main(void) 62 | { 63 | dispatch_test_start("Dispatch Sync GC"); // 64 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 65 | CFRunLoopRun(); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /tests/Foundation/nsoperation.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 30 | #include "dispatch_test.h" 31 | 32 | @interface MYOperation : NSOperation 33 | { 34 | } 35 | @end 36 | 37 | @implementation MYOperation 38 | 39 | - (id) init 40 | { 41 | self = [super init]; 42 | return self; 43 | } 44 | 45 | - (void)main 46 | { 47 | test_stop(); 48 | } 49 | 50 | @end 51 | 52 | int 53 | main(void) 54 | { 55 | dispatch_test_start("NSOperation"); 56 | 57 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 58 | 59 | NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 60 | test_ptr_notnull("NSOperationQueue", queue); 61 | 62 | MYOperation *operation = [[MYOperation alloc] init]; 63 | test_ptr_notnull("NSOperation", operation); 64 | 65 | [queue addOperation:operation]; 66 | [operation release]; 67 | 68 | [[NSRunLoop mainRunLoop] run]; 69 | 70 | [pool release]; 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /tests/bsdtestsummarize.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #include 26 | 27 | int 28 | has_prefix(const char* str, const char* prefix) 29 | { 30 | return (strncmp(str, prefix, strlen(prefix)) == 0); 31 | } 32 | 33 | int 34 | print_summary(FILE* f, long total, long pass, long fail, long skip) 35 | { 36 | double dpass = 100.0 ,dfail = 0.0, dskip = 0.0; 37 | fprintf(f, "Total: %ld\n", total); 38 | if (total) { 39 | dpass = ((double)pass / (double)(total - skip)) * 10000.0; 40 | dpass = floor(dpass) / 100.0; 41 | 42 | dfail = ((double)fail / (double)(total - skip)) * 10000.0; 43 | dfail = ceil(dfail) / 100.0; 44 | 45 | dskip = ((double)skip / (double)total) * 10000.0; 46 | dskip = ceil(dskip) / 100.0; 47 | } 48 | fprintf(f, "Passed: %ld (%0.2lf%%)\nFailed: %ld (%0.2lf%%)\nSkipped: %ld (%0.2lf%%)\n\n", pass, dpass, fail, dfail, skip, dskip); 49 | return 0; 50 | } 51 | 52 | int 53 | main(int argc, char* argv[]) 54 | { 55 | if (argc > 1) { 56 | fprintf(stderr, "%s: usage: summarize\n", argv[0]); 57 | exit(1); 58 | } 59 | 60 | /* 61 | FILE* f = fopen(argv[1], "w"); 62 | if (f == NULL) { 63 | perror(argv[1]); 64 | exit(1); 65 | } 66 | */ 67 | FILE* f = stdout; 68 | 69 | fprintf(f, "\n==================================================\n"); 70 | fprintf(f, "[SUMMARY] Test Summary\n"); 71 | fprintf(f, "==================================================\n\n"); 72 | 73 | size_t len; 74 | char* ln, lastln[1024]; 75 | int first_test = 1; 76 | long total = 0; 77 | long pass = 0; 78 | long fail = 0; 79 | long skip = 0; 80 | long total_total = 0; 81 | long total_pass = 0; 82 | long total_fail = 0; 83 | long total_skip = 0; 84 | for(;;) { 85 | ln = fgetln(stdin, &len); 86 | //if (ln) fprintf(stdout, "%.*s", (int)len, ln); 87 | if (ln == NULL || (has_prefix(ln, "[TEST]") && 88 | strncmp(ln, lastln, MIN(len,1024)))) { 89 | if (total || !first_test) { 90 | print_summary(f, total, pass, fail, skip); 91 | first_test = 0; 92 | } 93 | total_total += total; 94 | total_pass += pass; 95 | total_fail += fail; 96 | total_skip += skip; 97 | total = 0; 98 | pass = 0; 99 | fail = 0; 100 | skip = 0; 101 | if (ln) { 102 | fprintf(f, "%.*s", (int)len, ln); 103 | strncpy(lastln, ln, MIN(len,1024)); 104 | } else { 105 | fprintf(f, "[TOTAL]\n"); 106 | print_summary(f, total_total, total_pass, total_fail, total_skip); 107 | break; 108 | } 109 | } else if (has_prefix(ln, "[PASS]")) { 110 | ++total; 111 | ++pass; 112 | } else if (has_prefix(ln, "[FAIL]")) { 113 | ++total; 114 | ++fail; 115 | } else if (has_prefix(ln, "[SKIP]")) { 116 | ++total; 117 | ++skip; 118 | } 119 | } 120 | 121 | return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS); 122 | } 123 | -------------------------------------------------------------------------------- /tests/dispatch_after.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 24 | #include 25 | #endif 26 | #include 27 | #include 28 | #ifdef __APPLE__ 29 | #include 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #include "dispatch_test.h" 36 | 37 | static void 38 | done(void *arg /*__unused */) 39 | { 40 | (void)arg; 41 | sleep(1); 42 | test_stop(); 43 | } 44 | 45 | static void 46 | test_after(void) 47 | { 48 | __block dispatch_time_t time_a_min, time_a, time_a_max; 49 | __block dispatch_time_t time_b_min, time_b, time_b_max; 50 | __block dispatch_time_t time_c_min, time_c, time_c_max; 51 | 52 | dispatch_test_start("Dispatch After"); 53 | 54 | dispatch_async(dispatch_get_main_queue(), ^{ 55 | time_a_min = dispatch_time(0, (int64_t)(5.5*NSEC_PER_SEC)); 56 | time_a = dispatch_time(0, (int64_t)(6*NSEC_PER_SEC)); 57 | time_a_max = dispatch_time(0, (int64_t)(6.5*NSEC_PER_SEC)); 58 | dispatch_after(time_a, dispatch_get_main_queue(), ^{ 59 | dispatch_time_t now_a = dispatch_time(0, 0); 60 | test_long_less_than("can't finish faster than 5.5s", 0, (long)(now_a - time_a_min)); 61 | test_long_less_than("must finish faster than 6.5s", 0, (long)(time_a_max - now_a)); 62 | 63 | time_b_min = dispatch_time(0, (int64_t)(1.5*NSEC_PER_SEC)); 64 | time_b = dispatch_time(0, (int64_t)(2*NSEC_PER_SEC)); 65 | time_b_max = dispatch_time(0, (int64_t)(2.5*NSEC_PER_SEC)); 66 | dispatch_after(time_b, dispatch_get_main_queue(), ^{ 67 | dispatch_time_t now_b = dispatch_time(0, 0); 68 | test_long_less_than("can't finish faster than 1.5s", 0, (long)(now_b - time_b_min)); 69 | test_long_less_than("must finish faster than 2.5s", 0, (long)(time_b_max - now_b)); 70 | 71 | time_c_min = dispatch_time(0, 0*NSEC_PER_SEC); 72 | time_c = dispatch_time(0, 0*NSEC_PER_SEC); 73 | time_c_max = dispatch_time(0, (int64_t)(.5*NSEC_PER_SEC)); 74 | dispatch_after(time_c, dispatch_get_main_queue(), ^{ 75 | dispatch_time_t now_c = dispatch_time(0, 0); 76 | test_long_less_than("can't finish faster than 0s", 0, (long)(now_c - time_c_min)); 77 | test_long_less_than("must finish faster than .5s", 0, (long)(time_c_max - now_c)); 78 | 79 | dispatch_async_f(dispatch_get_main_queue(), NULL, done); 80 | }); 81 | }); 82 | }); 83 | }); 84 | } 85 | 86 | int 87 | main(void) 88 | { 89 | test_after(); 90 | dispatch_main(); 91 | return 1; 92 | } 93 | -------------------------------------------------------------------------------- /tests/dispatch_api.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | 23 | #include 24 | #include "dispatch_test.h" 25 | 26 | static void 27 | work(void *context __attribute__((unused))) 28 | { 29 | test_stop(); 30 | exit(0); 31 | } 32 | 33 | int 34 | main(void) 35 | { 36 | dispatch_queue_t q; 37 | dispatch_test_start("Dispatch (Public) API"); 38 | 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 | -------------------------------------------------------------------------------- /tests/dispatch_c99.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 25 | #include "dispatch_test.h" 26 | 27 | static void 28 | work(void *context __attribute__((unused))) 29 | { 30 | test_stop(); 31 | exit(0); 32 | } 33 | 34 | int 35 | main(void) 36 | { 37 | dispatch_test_start("Dispatch C99"); 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 | -------------------------------------------------------------------------------- /tests/dispatch_cascade.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 24 | #include 25 | #endif 26 | #include 27 | 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | int done = 0; 32 | 33 | #define QUEUES 80 34 | dispatch_queue_t queues[QUEUES]; 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) / 4; 43 | 44 | static void 45 | noop(void *ctxt __attribute__((unused))) 46 | { 47 | return; 48 | } 49 | 50 | static void 51 | cleanup(void *ctxt __attribute__((unused))) 52 | { 53 | size_t q; 54 | for (q = 0; q < QUEUES; ++q) { 55 | dispatch_sync_f(queues[q], NULL, noop); 56 | dispatch_release(queues[q]); 57 | } 58 | test_stop(); 59 | exit(0); 60 | } 61 | 62 | static void 63 | histogram(void) 64 | { 65 | size_t counts[QUEUES] = {}; 66 | size_t maxcount = 0; 67 | 68 | size_t q; 69 | for (q = 0; q < QUEUES; ++q) { 70 | size_t i; 71 | for (i = 0; i < BLOCKS; ++i) { 72 | if (indices[i].index == q) { 73 | ++counts[q]; 74 | } 75 | } 76 | } 77 | 78 | for (q = 0; q < QUEUES; ++q) { 79 | if (counts[q] > maxcount) { 80 | maxcount = counts[q]; 81 | } 82 | } 83 | 84 | printf("maxcount = %zd\n", maxcount); 85 | 86 | size_t x,y; 87 | for (y = 20; y > 0; --y) { 88 | for (x = 0; x < QUEUES; ++x) { 89 | double fraction = (double)counts[x] / (double)maxcount; 90 | double value = fraction * (double)20; 91 | printf("%s", (value > y) ? "*" : " "); 92 | } 93 | printf("\n"); 94 | } 95 | } 96 | 97 | static void 98 | cascade(void* context) 99 | { 100 | size_t idx, *idxptr = context; 101 | 102 | if (done) return; 103 | 104 | idx = *idxptr + 1; 105 | 106 | if (idx < QUEUES) { 107 | *idxptr = idx; 108 | dispatch_async_f(queues[idx], context, cascade); 109 | } 110 | 111 | if (__sync_sub_and_fetch(&iterations, 1) == 0) { 112 | done = 1; 113 | histogram(); 114 | dispatch_async_f(dispatch_get_main_queue(), NULL, cleanup); 115 | } 116 | } 117 | 118 | int 119 | main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) 120 | { 121 | int i; 122 | 123 | dispatch_test_start("Dispatch Cascade"); 124 | 125 | for (i = 0; i < QUEUES; ++i) { 126 | queues[i] = dispatch_queue_create(NULL, NULL); 127 | } 128 | 129 | for (i = 0; i < BLOCKS; ++i) { 130 | cascade(&indices[i].index); 131 | } 132 | 133 | dispatch_main(); 134 | 135 | return 0; 136 | } 137 | -------------------------------------------------------------------------------- /tests/dispatch_cf_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 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 | #ifdef __APPLE__ 25 | #include 26 | #endif 27 | 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | const int32_t final = 10; 32 | static volatile int32_t count; 33 | 34 | static void 35 | work(void* ctxt __attribute__((unused))) 36 | { 37 | int32_t c = OSAtomicIncrement32(&count); 38 | if (c < final-1) { 39 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 40 | CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{ 41 | fprintf(stderr, "CFRunLoopPerformBlock %d\n", c); 42 | test_long_less_than("CFRunLoopPerformBlock", count, final); 43 | }); 44 | } 45 | } 46 | 47 | int 48 | main(void) 49 | { 50 | dispatch_test_start("Dispatch CF main queue"); // 51 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 52 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 53 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), 54 | dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 55 | test_long("done", count, final); 56 | test_stop(); 57 | }); 58 | CFRunLoopRun(); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/dispatch_data.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011 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 25 | #ifdef __APPLE__ 26 | #include 27 | #endif 28 | 29 | #include 30 | #include "dispatch_test.h" 31 | 32 | #ifndef DISPATCHTEST_DATA 33 | #if DISPATCH_API_VERSION >= 20100226 && DISPATCH_API_VERSION != 20101110 34 | #define DISPATCHTEST_DATA 1 35 | #endif 36 | #endif 37 | 38 | dispatch_group_t g; 39 | 40 | #if DISPATCHTEST_DATA 41 | static void 42 | test_concat(void) 43 | { 44 | dispatch_group_enter(g); 45 | dispatch_async(dispatch_get_main_queue(), ^{ 46 | const char* buffer1 = "This is buffer1 "; 47 | size_t size1 = 17; 48 | const char* buffer2 = "This is buffer2 "; 49 | size_t size2 = 17; 50 | __block bool buffer2_destroyed = false; 51 | 52 | dispatch_data_t data1 = dispatch_data_create(buffer1, size1, NULL, NULL); 53 | dispatch_data_t data2 = dispatch_data_create(buffer2, size2, 54 | dispatch_get_main_queue(), ^{ 55 | buffer2_destroyed = true; 56 | }); 57 | dispatch_data_t concat = dispatch_data_create_concat(data1, data2); 58 | 59 | dispatch_release(data1); 60 | dispatch_release(data2); 61 | 62 | test_long("Data size of concatenated dispatch data", 63 | (long)dispatch_data_get_size(concat), 34); 64 | 65 | const void* contig; 66 | size_t contig_size; 67 | dispatch_data_t contig_data = 68 | dispatch_data_create_map(concat, &contig, &contig_size); 69 | 70 | dispatch_release(concat); 71 | dispatch_release(contig_data); 72 | test_long("Contiguous memory size", (long)contig_size, 34); 73 | dispatch_async(dispatch_get_main_queue(), ^{ 74 | test_long("buffer2 destroyed", buffer2_destroyed, true); 75 | dispatch_group_leave(g); 76 | }); 77 | }); 78 | } 79 | 80 | static void 81 | test_cleanup(void) // 82 | { 83 | static char buffer4[16]; 84 | dispatch_group_enter(g); 85 | dispatch_async(dispatch_get_main_queue(), ^{ 86 | void *buffer3 = malloc(1024); 87 | dispatch_data_t data3 = dispatch_data_create(buffer3, 0, 88 | dispatch_get_main_queue(), DISPATCH_DATA_DESTRUCTOR_FREE); 89 | __block bool buffer4_destroyed = false; 90 | dispatch_data_t data4 = dispatch_data_create(buffer4, 16, 91 | dispatch_get_main_queue(), ^{ 92 | buffer4_destroyed = true; 93 | }); 94 | dispatch_release(data3); 95 | dispatch_release(data4); 96 | dispatch_async(dispatch_get_main_queue(), ^{ 97 | test_long("buffer4 destroyed", buffer4_destroyed, true); 98 | dispatch_group_leave(g); 99 | }); 100 | }); 101 | } 102 | #endif 103 | 104 | int 105 | main(void) 106 | { 107 | dispatch_test_start("Dispatch Data"); 108 | g = dispatch_group_create(); 109 | 110 | #if DISPATCHTEST_DATA 111 | test_concat(); 112 | test_cleanup(); 113 | #endif 114 | 115 | dispatch_group_notify(g, dispatch_get_main_queue(), ^{ 116 | dispatch_release(g); 117 | test_stop(); 118 | }); 119 | dispatch_main(); 120 | } 121 | -------------------------------------------------------------------------------- /tests/dispatch_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | int 32 | main(void) 33 | { 34 | #if defined(_WIN32) 35 | _putenv_s("LIBDISPATCH_LOG", "stderr"); 36 | #else 37 | setenv("LIBDISPATCH_LOG", "stderr", 1); // rdar://problem/8493990 38 | #endif 39 | dispatch_test_start("Dispatch Debug"); 40 | 41 | dispatch_queue_t main_q = dispatch_get_main_queue(); 42 | dispatch_debug(main_q, "dispatch_queue_t"); 43 | 44 | dispatch_queue_t default_q = dispatch_get_global_queue(0, 0); 45 | dispatch_debug(default_q, "dispatch_queue_t"); 46 | 47 | dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 48 | dispatch_source_set_timer(s, DISPATCH_TIME_FOREVER, 1000000000ull, 100); 49 | dispatch_debug(s, "dispatch_source_t"); 50 | 51 | dispatch_group_t g = dispatch_group_create(); 52 | dispatch_debug(g, "dispatch_group_t"); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /tests/dispatch_drift.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #ifdef __APPLE__ 22 | #include 23 | #endif 24 | #include 25 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 26 | #include 27 | #include 28 | #endif 29 | #include 30 | #include 31 | #ifdef __APPLE__ 32 | #include 33 | #endif 34 | #include 35 | #include "dispatch_test.h" 36 | 37 | #if LENIENT_DEADLINES 38 | #define ACCEPTABLE_DRIFT 0.1 39 | #else 40 | #define ACCEPTABLE_DRIFT 0.001 41 | #endif 42 | 43 | int 44 | main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) 45 | { 46 | __block uint32_t count = 0; 47 | __block double last_jitter = 0; 48 | __block double drift_sum = 0; 49 | #if defined(_WIN32) 50 | // 25 times a second (Windows timer resolution is poor) 51 | uint64_t interval = 1000000000 / 25; 52 | #else 53 | // 100 times a second 54 | uint64_t interval = 1000000000 / 100; 55 | #endif 56 | double interval_d = interval / 1000000000.0; 57 | // for 25 seconds 58 | unsigned int target = (unsigned int)(25.0 / interval_d); 59 | 60 | dispatch_test_start("Dispatch Timer Drift"); 61 | 62 | dispatch_source_t t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 63 | test_ptr_notnull("dispatch_source_create", t); 64 | 65 | dispatch_source_set_timer(t, dispatch_time(DISPATCH_TIME_NOW, (int64_t)interval), interval, 0); 66 | 67 | dispatch_source_set_event_handler(t, ^{ 68 | struct timeval now_tv; 69 | static double first = 0; 70 | gettimeofday(&now_tv, NULL); 71 | double now = now_tv.tv_sec + now_tv.tv_usec / 1000000.0; 72 | 73 | if (count == 0) { 74 | // Because this is taken at 1st timer fire, 75 | // later jitter values may be negitave. 76 | // This doesn't effect the drift calculation. 77 | first = now; 78 | } 79 | double goal = first + interval_d * count; 80 | double jitter = goal - now; 81 | double drift = jitter - last_jitter; 82 | drift_sum += drift; 83 | 84 | printf("%4d: jitter %f, drift %f\n", count, jitter, drift); 85 | 86 | if (target <= ++count) { 87 | drift_sum /= count - 1; 88 | if (drift_sum < 0) { 89 | drift_sum = -drift_sum; 90 | } 91 | double acceptable_drift = ACCEPTABLE_DRIFT; 92 | test_double_less_than("drift", drift_sum, acceptable_drift); 93 | test_stop(); 94 | } 95 | last_jitter = jitter; 96 | }); 97 | 98 | dispatch_resume(t); 99 | 100 | dispatch_main(); 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /tests/dispatch_io_pipe_close.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 26 | #include 27 | #elif defined(_WIN32) 28 | #include 29 | #endif 30 | 31 | #include 32 | #include "dispatch_test.h" 33 | #include 34 | 35 | int 36 | main() { 37 | dispatch_test_start(NULL); 38 | 39 | #if defined(_WIN32) 40 | dispatch_fd_t readFD, writeFD; 41 | if (!CreatePipe((PHANDLE)&readFD, (PHANDLE)&writeFD, NULL, 0)) { 42 | test_long("CreatePipe", GetLastError(), ERROR_SUCCESS); 43 | test_stop(); 44 | _Exit(EXIT_FAILURE); 45 | } 46 | #else 47 | int pipe_fds[2] = { -1, -1 }; 48 | int pipe_err = pipe(pipe_fds); 49 | int readFD = pipe_fds[0]; 50 | int writeFD = pipe_fds[1]; 51 | if (pipe_err) { 52 | test_errno("pipe", errno, 0); 53 | test_stop(); 54 | _Exit(EXIT_FAILURE); 55 | } 56 | #endif 57 | 58 | printf("readFD=%lld, writeFD=%lld\n", (long long)readFD, (long long)writeFD); 59 | dispatch_queue_t q = dispatch_queue_create("q", NULL); 60 | dispatch_io_t io = dispatch_io_create(DISPATCH_IO_STREAM, readFD, q, ^(int err) { 61 | printf("cleanup, err=%d\n", err); 62 | dispatch_test_fd_close(readFD); 63 | printf("all done\n"); 64 | test_stop(); 65 | _Exit(EXIT_SUCCESS); 66 | }); 67 | dispatch_io_set_low_water(io, 0); 68 | dispatch_io_read(io, 0, UINT_MAX, q, ^(bool done, dispatch_data_t data, int err) { 69 | printf("read: \%d, %zu, %d\n", done, data == NULL ? 0 : dispatch_data_get_size(data), err); 70 | if (data != NULL && dispatch_data_get_size(data) > 0) { 71 | // will only happen once 72 | printf("closing writeFD\n"); 73 | dispatch_test_fd_close(writeFD); 74 | dispatch_after(DISPATCH_TIME_NOW + 1, q, ^{ 75 | dispatch_io_close(io, 0); 76 | }); 77 | } 78 | }); 79 | dispatch_resume(io); 80 | printf("writing\n"); 81 | dispatch_test_fd_write(writeFD, "x", 1); 82 | printf("wrtten\n"); 83 | dispatch_main(); 84 | } 85 | -------------------------------------------------------------------------------- /tests/dispatch_overcommit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #ifdef __linux__ 22 | // for asprintf 23 | #define _GNU_SOURCE 1 24 | #endif 25 | #include 26 | #include 27 | #include 28 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 29 | #include 30 | #endif 31 | #include 32 | #include 33 | #ifdef __APPLE__ 34 | #include 35 | #endif 36 | 37 | #include 38 | #include "dispatch_test.h" 39 | 40 | int32_t count = 0; 41 | const int32_t final = 32; 42 | 43 | int 44 | main(void) 45 | { 46 | dispatch_test_start("Dispatch Overcommit"); 47 | 48 | int i; 49 | for (i = 0; i < final; ++i) { 50 | char* name; 51 | (void)asprintf(&name, "test.overcommit.%d", i); 52 | 53 | dispatch_queue_t queue = dispatch_queue_create(name, NULL); 54 | test_ptr_notnull("dispatch_queue_create", queue); 55 | free(name); 56 | dispatch_set_target_queue(queue, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT)); 57 | 58 | dispatch_async(queue, ^{ 59 | OSAtomicIncrement32(&count); 60 | if (count == final) { 61 | test_long("count", count, final); 62 | test_stop(); 63 | } else { 64 | while (1); // spin 65 | } 66 | }); 67 | } 68 | 69 | dispatch_main(); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /tests/dispatch_pingpong.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 25 | #include "dispatch_test.h" 26 | 27 | uint32_t count = 0; 28 | const uint32_t final = 1000000; // 10M 29 | 30 | static void 31 | pingpongloop(dispatch_group_t group, dispatch_queue_t ping, dispatch_queue_t pong, size_t counter) 32 | { 33 | //printf("[%p] %s: %lu\n", (void*)(uintptr_t)pthread_self(), dispatch_queue_get_label(dispatch_get_current_queue()), counter); 34 | if (counter < final) { 35 | dispatch_group_async(group, pong, ^{ pingpongloop(group, pong, ping, counter+1); }); 36 | } else { 37 | count = (uint32_t)counter; 38 | } 39 | } 40 | 41 | int 42 | main(void) 43 | { 44 | dispatch_test_start("Dispatch Ping Pong"); 45 | 46 | dispatch_queue_t ping = dispatch_queue_create("ping", NULL); 47 | test_ptr_notnull("dispatch_queue_create(ping)", ping); 48 | dispatch_queue_t pong = dispatch_queue_create("pong", NULL); 49 | test_ptr_notnull("dispatch_queue_create(pong)", pong); 50 | 51 | dispatch_group_t group = dispatch_group_create(); 52 | test_ptr_notnull("dispatch_group_create", group); 53 | 54 | pingpongloop(group, ping, pong, 0); 55 | dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 56 | 57 | test_long("count", count, final); 58 | 59 | dispatch_release(ping); 60 | dispatch_release(pong); 61 | dispatch_release(group); 62 | 63 | test_stop(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /tests/dispatch_plusplus.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | 25 | #include 26 | #include "dispatch_test.h" 27 | 28 | int 29 | main(void) 30 | { 31 | dispatch_test_start("Dispatch C++"); 32 | dispatch_queue_t q = dispatch_get_main_queue(); 33 | test_ptr_notnull("dispatch_get_main_queue", q); 34 | 35 | dispatch_async(dispatch_get_main_queue(), ^{ 36 | test_stop(); 37 | exit(0); 38 | }); 39 | dispatch_main(); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /tests/dispatch_queue_finalizer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 23 | #include 24 | #endif 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | void *ctxt_magic = NULL; 34 | 35 | static void 36 | finalize(void *ctxt) 37 | { 38 | test_ptr_null("finalizer ran", NULL); 39 | test_ptr("correct context", ctxt, ctxt_magic); 40 | test_stop(); 41 | } 42 | 43 | static void 44 | never_call(void *ctxt) 45 | { 46 | test_ptr_notnull("never_call should not run", NULL); 47 | test_ptr("correct context", ctxt, NULL); 48 | } 49 | 50 | int 51 | main(void) 52 | { 53 | dispatch_test_start("Dispatch Queue Finalizer"); 54 | 55 | #if HAS_ARC4RANDOM 56 | #if defined(__LP64__) || defined(_WIN64) 57 | ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random()); 58 | #else 59 | ctxt_magic = (void*)arc4random(); 60 | #endif 61 | #else // that is, if !HAS_ARC4RANDOM 62 | ctxt_magic = (void *)random(); 63 | #endif 64 | 65 | // we need a non-NULL value for the tests to work properly 66 | if (ctxt_magic == NULL) { 67 | ctxt_magic = &ctxt_magic; 68 | } 69 | 70 | dispatch_queue_t q = dispatch_queue_create("com.apple.testing.finalizer", NULL); 71 | test_ptr_notnull("dispatch_queue_new", q); 72 | 73 | dispatch_set_finalizer_f(q, finalize); 74 | 75 | dispatch_queue_t q_null_context = dispatch_queue_create("com.apple.testing.finalizer.context_null", NULL); 76 | 77 | dispatch_set_context(q_null_context, NULL); 78 | dispatch_set_finalizer_f(q_null_context, never_call); 79 | dispatch_release(q_null_context); 80 | 81 | // Don't test k 82 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 83 | // Usign async to set the context helps test that blocks are 84 | // run before the release as opposed to just thrown away. 85 | dispatch_async(q, ^{ 86 | dispatch_set_context(q, ctxt_magic); 87 | }); 88 | 89 | dispatch_release(q); 90 | }); 91 | 92 | dispatch_main(); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /tests/dispatch_read.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #include 26 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 27 | #include 28 | #endif 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | #include "dispatch_test.h" 35 | 36 | static size_t bytes_total; 37 | static size_t bytes_read; 38 | 39 | static void 40 | test_fin(void *cxt) 41 | { 42 | test_ptr("test_fin run", cxt, cxt); 43 | test_stop(); 44 | } 45 | 46 | int 47 | main(void) 48 | { 49 | char *path = dispatch_test_get_large_file(); 50 | 51 | dispatch_test_start("Dispatch Source Read"); 52 | 53 | dispatch_fd_t infd = dispatch_test_fd_open(path, O_RDONLY); 54 | if (infd == -1) { 55 | perror(path); 56 | exit(EXIT_FAILURE); 57 | } 58 | dispatch_test_release_large_file(path); 59 | free(path); 60 | 61 | bytes_total = (size_t)dispatch_test_fd_lseek(infd, 0, SEEK_END); 62 | dispatch_test_fd_lseek(infd, 0, SEEK_SET); 63 | 64 | #if !defined(_WIN32) 65 | if (fcntl(infd, F_SETFL, O_NONBLOCK) != 0) { 66 | perror(path); 67 | exit(EXIT_FAILURE); 68 | } 69 | #endif 70 | 71 | if (!dispatch_test_check_evfilt_read_for_fd(infd)) { 72 | test_skip("EVFILT_READ kevent not firing for test file"); 73 | test_fin(NULL); 74 | } 75 | 76 | dispatch_queue_t main_q = dispatch_get_main_queue(); 77 | test_ptr_notnull("dispatch_get_main_queue", main_q); 78 | 79 | dispatch_source_t reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)infd, 0, main_q); 80 | test_ptr_notnull("dispatch_source_create", reader); 81 | assert(reader); 82 | 83 | dispatch_source_set_event_handler(reader, ^{ 84 | size_t estimated = dispatch_source_get_data(reader); 85 | fprintf(stderr, "bytes available: %zu\n", estimated); 86 | test_double_less_than_or_equal("estimated", estimated, bytes_total - bytes_read); 87 | const ssize_t bufsiz = 1024*500; // 500 KB buffer 88 | static char buffer[1024*500]; // 500 KB buffer 89 | ssize_t actual = dispatch_test_fd_read(infd, buffer, sizeof(buffer)); 90 | bytes_read += (size_t)actual; 91 | printf("bytes read: %zd\n", actual); 92 | if (actual < bufsiz) { 93 | actual = dispatch_test_fd_read(infd, buffer, sizeof(buffer)); 94 | bytes_read += (size_t)actual; 95 | // confirm EOF condition 96 | test_long("EOF", actual, 0); 97 | dispatch_source_cancel(reader); 98 | } 99 | }); 100 | 101 | dispatch_source_set_cancel_handler(reader, ^{ 102 | test_sizet("Bytes read", bytes_read, bytes_total); 103 | int res = dispatch_test_fd_close(infd); 104 | test_errno("close", res == -1 ? errno : 0, 0); 105 | dispatch_release(reader); 106 | }); 107 | 108 | dispatch_set_context(reader, reader); 109 | dispatch_set_finalizer_f(reader, test_fin); 110 | 111 | dispatch_resume(reader); 112 | 113 | dispatch_main(); 114 | } 115 | -------------------------------------------------------------------------------- /tests/dispatch_sema.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if !USE_WIN32_SEM 23 | #include 24 | #endif 25 | #include 26 | #include 27 | 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | #define LAPS 10000 32 | 33 | int 34 | main(void) 35 | { 36 | static long total; 37 | dispatch_semaphore_t dsema; 38 | 39 | dispatch_test_start("Dispatch Semaphore"); 40 | 41 | dsema = dispatch_semaphore_create(1); 42 | assert(dsema); 43 | 44 | dispatch_apply(LAPS, dispatch_get_global_queue(0, 0), ^(size_t idx __attribute__((unused))) { 45 | dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER); 46 | total++; 47 | dispatch_semaphore_signal(dsema); 48 | }); 49 | 50 | dispatch_release(dsema); 51 | 52 | test_long("count", total, LAPS); 53 | test_stop(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /tests/dispatch_suspend_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | 25 | #include 26 | 27 | #include 28 | #include "dispatch_test.h" 29 | 30 | dispatch_source_t tweedledee; 31 | dispatch_source_t tweedledum; 32 | 33 | static void 34 | fini(void *cxt) 35 | { 36 | test_ptr_notnull("finalizer ran", cxt); 37 | if (cxt == tweedledum) { 38 | test_stop(); 39 | } 40 | } 41 | 42 | static void 43 | test_timer(void) 44 | { 45 | dispatch_test_start("Dispatch Suspend Timer"); 46 | 47 | dispatch_queue_t main_q = dispatch_get_main_queue(); 48 | //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 49 | 50 | __block int i = 0, i_prime = 0; 51 | __block int j = 0; 52 | 53 | tweedledee = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 54 | test_ptr_notnull("dispatch_source_timer_create", tweedledee); 55 | 56 | dispatch_source_set_timer(tweedledee, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0); 57 | 58 | dispatch_source_set_cancel_handler(tweedledee, ^{ 59 | dispatch_release(tweedledee); 60 | }); 61 | 62 | dispatch_source_set_event_handler(tweedledee, ^{ 63 | i_prime += dispatch_source_get_data(tweedledee); 64 | fprintf(stderr, "tweedledee %d (%d)\n", ++i, i_prime); 65 | if (i == 10) { 66 | dispatch_source_cancel(tweedledee); 67 | } 68 | }); 69 | 70 | dispatch_set_context(tweedledee, tweedledee); 71 | dispatch_set_finalizer_f(tweedledee, fini); 72 | dispatch_resume(tweedledee); 73 | 74 | tweedledum = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 75 | test_ptr_notnull("dispatch_source_timer_create", tweedledum); 76 | 77 | dispatch_source_set_timer(tweedledum, dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC + NSEC_PER_SEC / 2), 3 * NSEC_PER_SEC, 0); 78 | 79 | dispatch_source_set_cancel_handler(tweedledum, ^{ 80 | dispatch_release(tweedledum); 81 | }); 82 | 83 | dispatch_source_set_event_handler(tweedledum, ^{ 84 | switch(++j) { 85 | case 1: 86 | fprintf(stderr, "suspending timer for 3 seconds\n"); 87 | dispatch_suspend(tweedledee); 88 | break; 89 | case 2: 90 | fprintf(stderr, "resuming timer\n"); 91 | test_long("tweedledee tick count", i, 3); 92 | test_long("tweedledee virtual tick count", i_prime, 3); 93 | dispatch_resume(tweedledee); 94 | break; 95 | default: 96 | test_long("tweedledee tick count", i, 7); 97 | test_long("tweedledee virtual tick count", i_prime, 9); 98 | dispatch_source_cancel(tweedledum); 99 | break; 100 | } 101 | }); 102 | 103 | dispatch_set_context(tweedledum, tweedledum); 104 | dispatch_set_finalizer_f(tweedledum, fini); 105 | dispatch_resume(tweedledum); 106 | } 107 | 108 | int 109 | main(void) 110 | { 111 | test_timer(); 112 | dispatch_main(); 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /tests/dispatch_sync_on_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 26 | #include 27 | #endif 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | const int32_t final = 10; 34 | int global_count = 0; 35 | 36 | static void 37 | work(void* ctxt __attribute__((unused))) 38 | { 39 | if (global_count == INT_MAX) { 40 | test_stop(); 41 | } 42 | printf("Firing timer on main %d\n", ++global_count); 43 | dispatch_after_f(dispatch_time(0, 100000*NSEC_PER_USEC), 44 | dispatch_get_main_queue(), NULL, work); 45 | } 46 | 47 | int 48 | main(void) 49 | { 50 | dispatch_test_start("Dispatch Sync on main"); // 51 | 52 | dispatch_queue_t dq = dispatch_queue_create("foo.bar", NULL); 53 | dispatch_async(dq, ^{ 54 | dispatch_async_f(dispatch_get_main_queue(), NULL, work); 55 | int i; 56 | for (i=0; i 22 | #include 23 | 24 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) 25 | #include 26 | #elif defined(_WIN32) 27 | #include 28 | #endif 29 | 30 | #if defined(__FreeBSD__) 31 | #include 32 | #endif 33 | 34 | #define test_group_wait(g) do { \ 35 | if (dispatch_group_wait(g, dispatch_time(DISPATCH_TIME_NOW, \ 36 | 25ull * NSEC_PER_SEC))) { \ 37 | test_long("group wait timed out", 1, 0); \ 38 | test_stop(); \ 39 | } } while (0) 40 | 41 | #if defined(__cplusplus) 42 | extern "C" { 43 | #endif 44 | 45 | void dispatch_test_start(const char* desc); 46 | 47 | bool dispatch_test_check_evfilt_read_for_fd(dispatch_fd_t fd); 48 | 49 | char *dispatch_test_get_large_file(void); 50 | void dispatch_test_release_large_file(const char *path); 51 | 52 | void _dispatch_test_current(const char* file, long line, const char* desc, dispatch_queue_t expected); 53 | #define dispatch_test_current(a,b) _dispatch_test_current(__SOURCE_FILE__, __LINE__, a, b) 54 | 55 | #if __APPLE__ 56 | int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, 57 | size_t *newpl); 58 | #endif 59 | 60 | dispatch_fd_t dispatch_test_fd_open(const char *path, int flags); 61 | int dispatch_test_fd_close(dispatch_fd_t fd); 62 | off_t dispatch_test_fd_lseek(dispatch_fd_t fd, off_t offset, int whence); 63 | ssize_t dispatch_test_fd_pread(dispatch_fd_t fd, void *buf, size_t count, off_t offset); 64 | ssize_t dispatch_test_fd_read(dispatch_fd_t fd, void *buf, size_t count); 65 | ssize_t dispatch_test_fd_write(dispatch_fd_t fd, const void *buf, size_t count); 66 | 67 | #if defined(__cplusplus) 68 | } /* extern "C" */ 69 | #endif 70 | -------------------------------------------------------------------------------- /tests/dispatch_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | 28 | #include 29 | #include "dispatch_test.h" 30 | 31 | static bool finalized = false; 32 | 33 | static void 34 | test_fin(void *cxt) 35 | { 36 | test_ptr("finalizer ran", cxt, cxt); 37 | finalized = true; 38 | test_stop(); 39 | } 40 | 41 | static void 42 | test_timer(void) 43 | { 44 | dispatch_test_start("Dispatch Source Timer"); 45 | 46 | const int stop_at = 3; 47 | 48 | dispatch_queue_t main_q = dispatch_get_main_queue(); 49 | //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 50 | 51 | int64_t j; 52 | 53 | // create timers in two classes: 54 | // * ones that should trigger before the test ends 55 | // * ones that shouldn't trigger before the test ends 56 | for (j = 1; j <= 5; ++j) 57 | { 58 | dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0)); 59 | test_ptr_notnull("dispatch_source_create", s); 60 | 61 | int64_t delta = (int64_t)((uint64_t)j * NSEC_PER_SEC + NSEC_PER_SEC / 10); 62 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, delta), DISPATCH_TIME_FOREVER, 0); 63 | 64 | dispatch_source_set_event_handler(s, ^{ 65 | if (!finalized) { 66 | test_long_less_than("timer number", (long)j, stop_at); 67 | fprintf(stderr, "timer[%lu]\n", (unsigned long)j); 68 | } 69 | dispatch_release(s); 70 | }); 71 | dispatch_resume(s); 72 | } 73 | 74 | __block int i = 0; 75 | 76 | dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 77 | test_ptr_notnull("dispatch_source_create", s); 78 | 79 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC, 0); 80 | 81 | dispatch_source_set_cancel_handler(s, ^{ 82 | test_ptr_notnull("cancel handler run", s); 83 | dispatch_release(s); 84 | }); 85 | 86 | dispatch_source_set_event_handler(s, ^{ 87 | fprintf(stderr, "%d\n", ++i); 88 | if (i >= stop_at) { 89 | test_long("i", i, stop_at); 90 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), 91 | DISPATCH_TIME_FOREVER, 0); 92 | dispatch_source_cancel(s); 93 | } 94 | }); 95 | 96 | dispatch_set_context(s, s); 97 | dispatch_set_finalizer_f(s, test_fin); 98 | 99 | dispatch_resume(s); 100 | } 101 | 102 | int 103 | main(void) 104 | { 105 | test_timer(); 106 | dispatch_main(); 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /tests/dispatch_timer_bit31.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | static void 34 | test_timer(void) 35 | { 36 | dispatch_test_start("Dispatch Source Timer, bit 31"); 37 | 38 | dispatch_queue_t main_q = dispatch_get_main_queue(); 39 | //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 40 | 41 | struct timeval start_time; 42 | 43 | static dispatch_source_t s; 44 | s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 45 | test_ptr_notnull("dispatch_source_create", s); 46 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0x80000000ull), 0x80000000ull, 0); 47 | gettimeofday(&start_time, NULL); 48 | 49 | dispatch_source_set_event_handler(s, ^{ 50 | dispatch_source_cancel(s); 51 | }); 52 | 53 | dispatch_source_set_cancel_handler(s, ^{ 54 | struct timeval end_time; 55 | gettimeofday(&end_time, NULL); 56 | // check, s/b 2.0799... seconds, which is <4 seconds 57 | // when it could end on a bad boundry. 58 | test_long_less_than("needs to finish faster than 4 seconds", end_time.tv_sec - start_time.tv_sec, 4); 59 | // And it has to take at least two seconds... 60 | test_long_less_than("can't finish faster than 2 seconds", 1, end_time.tv_sec - start_time.tv_sec); 61 | test_stop(); 62 | }); 63 | 64 | dispatch_resume(s); 65 | } 66 | 67 | int 68 | main(void) 69 | { 70 | test_timer(); 71 | dispatch_main(); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /tests/dispatch_timer_bit63.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | static void 34 | test_timer(void) 35 | { 36 | dispatch_test_start("Dispatch Source Timer, bit 63"); 37 | 38 | //uint64_t interval = 0xffffffffffffffffull; 39 | uint64_t interval = 0x8000000000000001ull; 40 | 41 | dispatch_queue_t mainq = dispatch_get_main_queue(); 42 | 43 | __block int i = 0; 44 | struct timeval start_time; 45 | 46 | gettimeofday(&start_time, NULL); 47 | 48 | static dispatch_source_t ds; 49 | ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainq); 50 | assert(ds); 51 | dispatch_source_set_event_handler(ds, ^{ 52 | assert(i < 1); 53 | printf("%d\n", i++); 54 | }); 55 | dispatch_source_set_timer(ds, DISPATCH_TIME_NOW, interval, 0); 56 | dispatch_resume(ds); 57 | 58 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), 59 | dispatch_get_main_queue(), ^{ 60 | test_stop(); 61 | }); 62 | } 63 | 64 | int 65 | main(void) 66 | { 67 | test_timer(); 68 | dispatch_main(); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /tests/dispatch_timer_set_time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | static void 34 | test_timer(void) 35 | { 36 | dispatch_test_start("Dispatch Update Timer"); 37 | 38 | dispatch_queue_t main_q = dispatch_get_main_queue(); 39 | //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); 40 | 41 | __block int i = 0; 42 | struct timeval start_time; 43 | 44 | gettimeofday(&start_time, NULL); 45 | 46 | dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); 47 | test_ptr_notnull("dispatch_source_create", s); 48 | 49 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0); 50 | 51 | dispatch_source_set_cancel_handler(s, ^{ 52 | struct timeval end_time; 53 | gettimeofday(&end_time, NULL); 54 | // Make sure we actually managed to adjust the interval 55 | // duration. Fifteen one second ticks would blow past 56 | // this. 57 | test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 10); 58 | test_stop(); 59 | 60 | dispatch_release(s); 61 | }); 62 | 63 | dispatch_source_set_event_handler(s, ^{ 64 | fprintf(stderr, "%d\n", ++i); 65 | if (i >= 15) { 66 | dispatch_source_cancel(s); 67 | } else if (i == 1) { 68 | dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC / 10, 0); 69 | } 70 | }); 71 | test_ptr_notnull("dispatch_source_timer_create", s); 72 | 73 | dispatch_resume(s); 74 | } 75 | 76 | int 77 | main(void) { 78 | test_timer(); 79 | dispatch_main(); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /tests/dispatch_timer_timeout.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | int 34 | main(void) 35 | { 36 | dispatch_test_start("Dispatch Source Timeout"); // 37 | 38 | uint64_t mini_interval = 100ull; // 100 ns 39 | uint64_t long_interval = 2000000000ull; // 2 secs 40 | 41 | dispatch_queue_t timer_queue = dispatch_queue_create("timer_queue", NULL); 42 | 43 | __block int value_to_be_changed = 5; 44 | __block int fired = 0; 45 | 46 | dispatch_source_t mini_timer, long_timer; 47 | mini_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timer_queue); 48 | dispatch_source_set_event_handler(mini_timer, ^{ 49 | printf("Firing mini-timer %d\n", ++fired); 50 | printf("Suspending mini-timer queue\n"); 51 | dispatch_suspend(timer_queue); 52 | }); 53 | dispatch_source_set_timer(mini_timer, DISPATCH_TIME_NOW, mini_interval, 0); 54 | 55 | long_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0)); 56 | dispatch_source_set_event_handler(long_timer, ^{ 57 | printf("Firing long timer\n"); 58 | value_to_be_changed = 10; 59 | dispatch_source_cancel(long_timer); 60 | }); 61 | dispatch_source_set_timer(long_timer, 62 | dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC) , 63 | long_interval, 0); 64 | 65 | dispatch_resume(mini_timer); 66 | dispatch_resume(long_timer); 67 | sleep(6); 68 | test_long("Checking final value", value_to_be_changed, 10); 69 | test_long("Mini-timer fired", fired, 1); 70 | dispatch_source_cancel(mini_timer); 71 | dispatch_release(mini_timer); 72 | dispatch_resume(timer_queue); 73 | dispatch_release(timer_queue); 74 | dispatch_release(long_timer); 75 | test_stop(); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /tests/dispatch_vnode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 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 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include 31 | #include "dispatch_test.h" 32 | 33 | #define ITERATIONS 1000 34 | long iterations, notifications; 35 | 36 | int 37 | main(void) 38 | { 39 | dispatch_test_start("Dispatch VNODE RENAME"); 40 | 41 | char path1[] = "/tmp/dispatchtest_vnode.XXXXXX"; 42 | char path2[] = "/tmp/dispatchtest_vnode.XXXXXX"; 43 | int fd1 = mkstemp(path1); 44 | if (fd1 == -1) { 45 | test_errno("mkstemp", errno, 0); 46 | test_stop(); 47 | } 48 | close(fd1); 49 | if (!mktemp(path2)) { 50 | test_errno("mktemp", errno, 0); 51 | test_stop(); 52 | } 53 | char *currentName = path1; 54 | char *renameDest = path2; 55 | dispatch_semaphore_t renamedSemaphore = dispatch_semaphore_create(0); 56 | dispatch_group_t g = dispatch_group_create(); 57 | 58 | while (iterations++ < ITERATIONS) { 59 | int fd = open(currentName, O_EVTONLY); 60 | if (fd == -1) { 61 | test_errno("open", errno, 0); 62 | test_stop(); 63 | } 64 | 65 | dispatch_source_t monitorSource = dispatch_source_create( 66 | DISPATCH_SOURCE_TYPE_VNODE, fd, DISPATCH_VNODE_RENAME, 67 | dispatch_get_global_queue(0, 0)); 68 | dispatch_source_set_event_handler(monitorSource, ^{ 69 | dispatch_semaphore_signal(renamedSemaphore); 70 | }); 71 | dispatch_source_set_cancel_handler(monitorSource, ^{ 72 | close(fd); 73 | }); 74 | #if DISPATCH_API_VERSION >= 20100818 // 75 | dispatch_group_enter(g); 76 | dispatch_source_set_registration_handler(monitorSource, ^{ 77 | dispatch_group_leave(g); 78 | }); 79 | #endif 80 | dispatch_resume(monitorSource); 81 | dispatch_group_wait(g, DISPATCH_TIME_FOREVER); 82 | 83 | if(rename(currentName, renameDest)) { 84 | test_errno("rename", errno, 0); 85 | test_stop(); 86 | } 87 | char *tmp = currentName; 88 | currentName = renameDest; 89 | renameDest = tmp; 90 | 91 | if(!dispatch_semaphore_wait(renamedSemaphore, 92 | dispatch_time(DISPATCH_TIME_NOW, 10 * 1000 * NSEC_PER_USEC))) { 93 | fprintf(stderr, "."); 94 | notifications++; 95 | dispatch_source_cancel(monitorSource); 96 | } else { 97 | fprintf(stderr, "!"); 98 | dispatch_source_cancel(monitorSource); 99 | dispatch_semaphore_signal(renamedSemaphore); 100 | } 101 | if (!(iterations % 80)) { 102 | fprintf(stderr, "\n"); 103 | } 104 | dispatch_release(monitorSource); 105 | } 106 | fprintf(stderr, "\n"); 107 | unlink(currentName); 108 | dispatch_release(g); 109 | dispatch_release(renamedSemaphore); 110 | test_long("VNODE RENAME notifications", notifications, ITERATIONS); 111 | test_stop(); 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /tests/dispatch_workqueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dispatch_test.h" 3 | 4 | struct test_context { 5 | uint32_t ncpu; 6 | int flag; 7 | }; 8 | 9 | static void 10 | timeout(void *context) 11 | { 12 | struct test_context *ctx = (struct test_context *)context; 13 | sleep(2); // Give the monitor the best chance of firing. 14 | test_int32_format(ctx->flag, 1, "flag"); 15 | test_stop(); 16 | } 17 | 18 | static void 19 | raise_flag(void *context) 20 | { 21 | struct test_context *ctx = (struct test_context *)context; 22 | ctx->flag++; 23 | } 24 | 25 | static void 26 | spin(void *context) 27 | { 28 | struct test_context *ctx = (struct test_context *)context; 29 | sleep(ctx->ncpu * 2); 30 | } 31 | 32 | static uint32_t 33 | activecpu(void) 34 | { 35 | uint32_t activecpu; 36 | #if defined(__linux__) || defined(__OpenBSD__) 37 | activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN); 38 | #elif defined(_WIN32) 39 | SYSTEM_INFO si; 40 | GetSystemInfo(&si); 41 | activecpu = si.dwNumberOfProcessors; 42 | #else 43 | size_t s = sizeof(activecpu); 44 | sysctlbyname("hw.activecpu", &activecpu, &s, NULL, 0); 45 | #endif 46 | return activecpu; 47 | } 48 | 49 | struct test_context ctx; 50 | 51 | int 52 | main(void) 53 | { 54 | uint32_t ncpu = activecpu(); 55 | 56 | dispatch_test_start("Dispatch workqueue"); 57 | 58 | dispatch_queue_t global = dispatch_get_global_queue(0, 0); 59 | test_ptr_notnull("dispatch_get_global_queue", global); 60 | 61 | ctx.ncpu = ncpu; 62 | dispatch_async_f(global, &ctx, timeout); 63 | 64 | for(int i = 0; i < (int)ncpu - 1; i++) { 65 | dispatch_async_f(global, &ctx, spin); 66 | } 67 | 68 | // All cpus are tied up at this point. Workqueue 69 | // should execute this function by overcommit. 70 | dispatch_async_f(global, &ctx, raise_flag); 71 | 72 | dispatch_main(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /tests/func.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 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 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | void 25 | func(void) 26 | { 27 | } 28 | #ifdef __BLOCKS__ 29 | void (^block)(void) = ^{ }; 30 | #endif 31 | #ifdef __cplusplus 32 | }; 33 | #endif 34 | -------------------------------------------------------------------------------- /tests/generic_unix_port.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static inline int32_t 5 | OSAtomicIncrement32(volatile int32_t *var) 6 | { 7 | return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_RELAXED)+1; 8 | } 9 | 10 | static inline int32_t 11 | OSAtomicIncrement32Barrier(volatile int32_t *var) 12 | { 13 | return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_SEQ_CST)+1; 14 | } 15 | 16 | static inline int32_t 17 | OSAtomicAdd32(int32_t val, volatile int32_t *var) 18 | { 19 | return __c11_atomic_fetch_add((_Atomic(int)*)var, val, __ATOMIC_RELAXED)+val; 20 | } 21 | 22 | // Simulation of mach_absolute_time related infrastructure 23 | // For now, use gettimeofday. 24 | // Consider using clockgettime(CLOCK_MONOTONIC) instead. 25 | 26 | #include 27 | 28 | struct mach_timebase_info { 29 | uint32_t numer; 30 | uint32_t denom; 31 | }; 32 | 33 | typedef struct mach_timebase_info *mach_timebase_info_t; 34 | typedef struct mach_timebase_info mach_timebase_info_data_t; 35 | 36 | typedef int kern_return_t; 37 | 38 | static inline 39 | uint64_t 40 | mach_absolute_time() 41 | { 42 | struct timeval tv; 43 | gettimeofday(&tv,NULL); 44 | return (1000ull)*((unsigned long long)tv.tv_sec*(1000000ull) + (unsigned long long)tv.tv_usec); 45 | } 46 | 47 | static inline 48 | int 49 | mach_timebase_info(mach_timebase_info_t tbi) 50 | { 51 | tbi->numer = 1; 52 | tbi->denom = 1; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /tests/generic_win_port.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef int kern_return_t; 10 | #ifndef HAVE_PID_T 11 | typedef int pid_t; 12 | #endif 13 | 14 | #if defined(_WIN64) 15 | typedef long long ssize_t; 16 | #else 17 | typedef long ssize_t; 18 | #endif 19 | 20 | struct mach_timebase_info { 21 | uint32_t numer; 22 | uint32_t denom; 23 | }; 24 | 25 | typedef struct mach_timebase_info *mach_timebase_info_t; 26 | typedef struct mach_timebase_info mach_timebase_info_data_t; 27 | 28 | static inline int32_t 29 | OSAtomicIncrement32(volatile int32_t *var) 30 | { 31 | return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_RELAXED)+1; 32 | } 33 | 34 | static inline int32_t 35 | OSAtomicIncrement32Barrier(volatile int32_t *var) 36 | { 37 | return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_SEQ_CST)+1; 38 | } 39 | 40 | static inline int32_t 41 | OSAtomicAdd32(int32_t val, volatile int32_t *var) 42 | { 43 | return __c11_atomic_fetch_add((_Atomic(int)*)var, val, __ATOMIC_RELAXED)+val; 44 | } 45 | 46 | WCHAR * 47 | argv_to_command_line(char **argv); 48 | 49 | int 50 | asprintf(char **strp, const char *format, ...); 51 | 52 | void 53 | filetime_to_timeval(struct timeval *tp, const FILETIME *ft); 54 | 55 | pid_t 56 | getpid(void); 57 | 58 | int 59 | gettimeofday(struct timeval *tp, void *tzp); 60 | 61 | uint64_t 62 | mach_absolute_time(void); 63 | 64 | static inline 65 | int 66 | mach_timebase_info(mach_timebase_info_t tbi) 67 | { 68 | tbi->numer = 1; 69 | tbi->denom = 1; 70 | return 0; 71 | } 72 | 73 | #ifndef HAVE_MKSTEMP 74 | dispatch_fd_t 75 | mkstemp(char *tmpl); 76 | #endif 77 | 78 | void 79 | print_winapi_error(const char *function_name, DWORD error); 80 | 81 | intptr_t 82 | random(void); 83 | 84 | unsigned int 85 | sleep(unsigned int seconds); 86 | 87 | int 88 | usleep(unsigned int usec); 89 | -------------------------------------------------------------------------------- /tests/leaks-wrapper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | /usr/bin/leaks "$@" 2>&1 | tee "${TMPDIR}$*.leakslog" | grep -q " 0 leaks for 0 total leaked bytes" 4 | 5 | if [ $? -eq 0 ]; then 6 | rm "${TMPDIR}$*.leakslog" 7 | exit 0 8 | else 9 | exit $? 10 | fi 11 | -------------------------------------------------------------------------------- /tools/dispatch_timers.d: -------------------------------------------------------------------------------- 1 | #!/usr/sbin/dtrace -s 2 | 3 | /* 4 | * Copyright (c) 2012-2013 Apple Inc. All rights reserved. 5 | * 6 | * @APPLE_APACHE_LICENSE_HEADER_START@ 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * @APPLE_APACHE_LICENSE_HEADER_END@ 21 | */ 22 | 23 | /* 24 | * Usage: dispatch_timers.d -p [pid] 25 | * traced process must have been executed with 26 | * DYLD_LIBRARY_PATH=/usr/lib/system/introspection or with 27 | * DYLD_IMAGE_SUFFIX=_profile or DYLD_IMAGE_SUFFIX=_debug 28 | */ 29 | 30 | #pragma D option quiet 31 | #pragma D option zdefs 32 | 33 | typedef struct dispatch_trace_timer_params_s { 34 | int64_t deadline, interval, leeway; 35 | } *dispatch_trace_timer_params_t; 36 | 37 | dispatch$target:libdispatch*.dylib::timer-configure, 38 | dispatch$target:libdispatch*.dylib::timer-program, 39 | dispatch$target:libdispatch*.dylib::timer-wake, 40 | dispatch$target:libdispatch*.dylib::timer-fire /!start/ { 41 | start = walltimestamp; 42 | } 43 | 44 | /* 45 | * Trace dispatch timer configuration and programming: 46 | * Timer configuration indicates that dispatch_source_set_timer() was called. 47 | * Timer programming indicates that the dispatch manager is about to sleep 48 | * for 'deadline' ns (but may wake up earlier if non-timer events occur). 49 | * Time parameters are in nanoseconds, a value of -1 means "forever". 50 | * 51 | * probe timer-configure/-program(dispatch_source_t source, 52 | * dispatch_function_t function, dispatch_trace_timer_params_t params) 53 | */ 54 | dispatch$target:libdispatch*.dylib::timer-configure, 55 | dispatch$target:libdispatch*.dylib::timer-program { 56 | this->p = (dispatch_trace_timer_params_t)copyin(arg2, 57 | sizeof(struct dispatch_trace_timer_params_s)); 58 | printf("%8dus %-15s: 0x%0?p deadline: %11dns interval: %11dns leeway: %11dns", 59 | (walltimestamp-start)/1000, probename, arg0, 60 | this->p ? this->p->deadline : 0, this->p ? this->p->interval : 0, 61 | this->p ? this->p->leeway : 0); 62 | usym(arg1); 63 | printf("\n"); 64 | } 65 | dispatch$target:libdispatch*.dylib::timer-configure { 66 | printf(" / --- Begin ustack"); 67 | ustack(); 68 | printf(" \ --- End ustack\n"); 69 | } 70 | 71 | /* 72 | * Trace dispatch timer wakes and fires: 73 | * Timer wakes indicate that the dispatch manager woke up due to expiry of the 74 | * deadline for the specified timer. 75 | * Timer fires indicate that that the dispatch manager scheduled the event 76 | * handler of the specified timer for asynchronous execution (may occur without 77 | * a corresponding timer wake if the manager was awake processing other events 78 | * when the timer deadline expired). 79 | * 80 | * probe timer-wake/-fire(dispatch_source_t source, 81 | * dispatch_function_t function) 82 | */ 83 | dispatch$target:libdispatch*.dylib::timer-wake, 84 | dispatch$target:libdispatch*.dylib::timer-fire { 85 | printf("%8dus %-15s: 0x%0?p%-70s", (walltimestamp-start)/1000, probename, 86 | arg0, ""); 87 | usym(arg1); 88 | printf("\n"); 89 | } 90 | -------------------------------------------------------------------------------- /tools/dispatch_trace.d: -------------------------------------------------------------------------------- 1 | #!/usr/sbin/dtrace -s 2 | 3 | /* 4 | * Copyright (c) 2010-2013 Apple Inc. All rights reserved. 5 | * 6 | * @APPLE_APACHE_LICENSE_HEADER_START@ 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * @APPLE_APACHE_LICENSE_HEADER_END@ 21 | */ 22 | 23 | /* 24 | * Usage: dispatch_trace.d -p [pid] 25 | * traced process must have been executed with 26 | * DYLD_LIBRARY_PATH=/usr/lib/system/introspection or with 27 | * DYLD_IMAGE_SUFFIX=_profile or DYLD_IMAGE_SUFFIX=_debug 28 | */ 29 | 30 | #pragma D option quiet 31 | #pragma D option zdefs 32 | #pragma D option bufsize=16m 33 | 34 | BEGIN { 35 | printf("%-8s %-3s %-8s %-35s%-15s%-?s %-43s%-?s %-14s%-?s %s\n", 36 | "Time us", "CPU", "Thread", "Function", "Probe", "Queue", "Label", 37 | "Item", "Kind", "Context", "Symbol"); 38 | } 39 | 40 | dispatch$target:libdispatch*.dylib::queue-push, 41 | dispatch$target:libdispatch*.dylib::queue-pop, 42 | dispatch$target:libdispatch*.dylib::callout-entry, 43 | dispatch$target:libdispatch*.dylib::callout-return /!start/ { 44 | start = walltimestamp; 45 | } 46 | 47 | /* 48 | * Trace queue push and pop operations: 49 | * 50 | * probe queue-push/-pop(dispatch_queue_t queue, const char *label, 51 | * dispatch_object_t item, const char *kind, 52 | * dispatch_function_t function, void *context) 53 | */ 54 | dispatch$target:libdispatch*.dylib::queue-push, 55 | dispatch$target:libdispatch*.dylib::queue-pop { 56 | printf("%-8d %-3d 0x%08p %-35s%-15s0x%0?p %-43s0x%0?p %-14s0x%0?p", 57 | (walltimestamp-start)/1000, cpu, tid, probefunc, probename, arg0, 58 | copyinstr(arg1, 42), arg2, copyinstr(arg3, 13), arg5); 59 | usym(arg4); 60 | printf("\n"); 61 | } 62 | 63 | /* 64 | * Trace callouts to client functions: 65 | * 66 | * probe callout-entry/-return(dispatch_queue_t queue, const char *label, 67 | * dispatch_function_t function, void *context) 68 | */ 69 | dispatch$target:libdispatch*.dylib::callout-entry, 70 | dispatch$target:libdispatch*.dylib::callout-return { 71 | printf("%-8d %-3d 0x%08p %-35s%-15s0x%0?p %-43s%-?s %-14s0x%0?p", 72 | (walltimestamp-start)/1000, cpu, tid, probefunc, probename, arg0, 73 | copyinstr(arg1, 42), "", "", arg3); 74 | usym(arg2); 75 | printf("\n"); 76 | } 77 | -------------------------------------------------------------------------------- /tools/voucher_trace.d: -------------------------------------------------------------------------------- 1 | #!/usr/sbin/dtrace -s 2 | 3 | /* 4 | * Copyright (c) 2017 Apple Inc. All rights reserved. 5 | * 6 | * @APPLE_APACHE_LICENSE_HEADER_START@ 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * @APPLE_APACHE_LICENSE_HEADER_END@ 21 | */ 22 | 23 | /* 24 | * Usage: voucher_trace.d -p [pid] 25 | * traced process must have been executed with 26 | * DYLD_LIBRARY_PATH=/usr/lib/system/introspection or with 27 | * DYLD_IMAGE_SUFFIX=_profile or DYLD_IMAGE_SUFFIX=_debug 28 | */ 29 | 30 | #pragma D option quiet 31 | #pragma D option zdefs 32 | #pragma D option bufsize=16m 33 | 34 | BEGIN { 35 | printf("Starting to trace voucher operations...\n"); 36 | } 37 | 38 | voucher$target:libdispatch*.dylib::create 39 | { 40 | printf("ALLOC voucher 0x%p, thread %#llx, ref 1, port %#x, aid %#llx", arg0, tid, arg1, arg2); 41 | ustack(10); 42 | printf("\n") 43 | } 44 | 45 | voucher$target:libdispatch*.dylib::dispose 46 | { 47 | printf("FREE voucher 0x%p, thread %#llx, ref 0", arg0, tid); 48 | ustack(10); 49 | printf("\n") 50 | } 51 | 52 | voucher$target:libdispatch*.dylib::retain 53 | { 54 | printf("RETAIN voucher 0x%p, thread %#llx, ref %d", arg0, tid, arg1); 55 | ustack(10); 56 | printf("\n") 57 | } 58 | 59 | voucher$target:libdispatch*.dylib::release 60 | { 61 | printf("RELEASE voucher 0x%p, thread %#llx, ref %d", arg0, tid, arg1); 62 | ustack(10); 63 | printf("\n") 64 | } 65 | 66 | voucher$target:libdispatch*.dylib::adopt 67 | { 68 | printf("ADOPT voucher 0x%p, thread %#llx", arg0, tid); 69 | ustack(10); 70 | printf("\n") 71 | } 72 | 73 | voucher$target:libdispatch*.dylib::orphan 74 | { 75 | printf("ORPHAN voucher 0x%p, thread %#llx", arg0, tid); 76 | ustack(10); 77 | printf("\n") 78 | } 79 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch-dyld-stub.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2016 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 | PRODUCT_NAME = libdispatch_dyld_stub 22 | INSTALL_PATH = /usr/local/lib/dyld_stub 23 | BUILD_VARIANTS = normal 24 | GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) DISPATCH_VARIANT_DYLD_STUB=1 $(STATICLIB_PREPROCESSOR_DEFINITIONS) 25 | OTHER_LDFLAGS = 26 | VERSIONING_SYSTEM = 27 | EXCLUDED_SOURCE_FILE_NAMES = * 28 | INCLUDED_SOURCE_FILE_NAMES = voucher.c // minimal with DISPATCH_VARIANT_DYLD_STUB 29 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch-introspection.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2012-2013 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 | BUILD_VARIANTS = normal 22 | INSTALL_PATH = /usr/lib/system/introspection 23 | 24 | GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) DISPATCH_INTROSPECTION=1 25 | CONFIGURATION_BUILD_DIR = $(BUILD_DIR)/introspection 26 | OTHER_LDFLAGS = $(OTHER_LDFLAGS) -Wl,-interposable_list,$(SRCROOT)/xcodeconfig/libdispatch.interposable 27 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch-mp-static.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2012-2013 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 | // skip simulator 22 | SUPPORTED_PLATFORMS = macosx iphoneos appletvos watchos 23 | PRODUCT_NAME = libdispatch 24 | INSTALL_PATH = /usr/local/lib/system 25 | BUILD_VARIANTS = normal debug 26 | GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) $(STATICLIB_PREPROCESSOR_DEFINITIONS) 27 | OTHER_LDFLAGS = 28 | SKIP_INSTALL[sdk=*simulator*] = YES 29 | EXCLUDED_SOURCE_FILE_NAMES[sdk=*simulator*] = * 30 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch-resolved.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2010-2011 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 | SUPPORTED_PLATFORMS = iphoneos 22 | PRODUCT_NAME = libdispatch_$(DISPATCH_RESOLVED_VARIANT) 23 | OTHER_LDFLAGS = 24 | SKIP_INSTALL = YES 25 | VERSIONING_SYSTEM = 26 | EXCLUDED_SOURCE_FILE_NAMES = * 27 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch-resolver.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2010-2011 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 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch.aliases: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2013-2014 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 | __dispatch_data_destructor_vm_deallocate __dispatch_data_destructor_munmap 22 | __dispatch_queue_attrs __dispatch_queue_attr_concurrent 23 | __dispatch_source_type_memorypressure __dispatch_source_type_memorystatus 24 | _dispatch_assert_queue$V2 _dispatch_assert_queue 25 | _dispatch_assert_queue_not$V2 _dispatch_assert_queue_not 26 | _dispatch_queue_create_with_target$V2 _dispatch_queue_create_with_target 27 | _dispatch_source_set_timer __dispatch_source_set_runloop_timer_4CF 28 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch.clean: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 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 | __dispatch_bug.last_seen 22 | __dispatch_bug_deprecated.last_seen 23 | __dispatch_bug_kevent_client.last_seen 24 | __dispatch_bug_kevent_client.last_seen.37 25 | __dispatch_bug_kevent_client.last_seen.39 26 | __dispatch_bug_kevent_vanished.last_seen 27 | __dispatch_bug_mach_client.last_seen 28 | 29 | __dispatch_build_pred 30 | __dispatch_build 31 | 32 | __dispatch_child_of_unsafe_fork 33 | __dispatch_continuation_cache_limit 34 | __dispatch_data_empty 35 | __dispatch_host_time_data.0 36 | __dispatch_host_time_data.1 37 | __dispatch_host_time_mach2nano 38 | __dispatch_host_time_nano2mach 39 | __dispatch_source_timer_use_telemetry 40 | __dispatch_timers_force_max_leeway 41 | __os_object_debug_missing_pools 42 | _dispatch_benchmark_f.bdata 43 | _dispatch_benchmark_f.pred 44 | _dispatch_io_defaults 45 | _dispatch_log_disabled 46 | _dispatch_logfile 47 | 48 | __dyld_private 49 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch.interposable: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2013 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 | # Interposable API hooks in the introspection library 22 | 23 | _dispatch_introspection_hook_queue_create 24 | _dispatch_introspection_hook_queue_destroy 25 | _dispatch_introspection_hook_queue_item_enqueue 26 | _dispatch_introspection_hook_queue_item_dequeue 27 | _dispatch_introspection_hook_queue_item_complete 28 | _dispatch_introspection_hook_queue_callout_begin 29 | _dispatch_introspection_hook_queue_callout_end 30 | -------------------------------------------------------------------------------- /xcodeconfig/libdispatch.order: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2013 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 | # Must be kept in sync with ObjC TFB checks in object_internal.h 22 | 23 | # dispatch_object_t classes 24 | _OBJC_CLASS_$_OS_dispatch_object 25 | _OBJC_CLASS_$_OS_dispatch_semaphore 26 | __OS_dispatch_semaphore_vtable 27 | _OBJC_CLASS_$_OS_dispatch_group 28 | __OS_dispatch_group_vtable 29 | _OBJC_CLASS_$_OS_dispatch_queue 30 | __OS_dispatch_queue_vtable 31 | _OBJC_CLASS_$_OS_dispatch_workloop 32 | __OS_dispatch_workloop_vtable 33 | _OBJC_CLASS_$_OS_dispatch_queue_serial 34 | __OS_dispatch_queue_serial_vtable 35 | _OBJC_CLASS_$_OS_dispatch_queue_concurrent 36 | __OS_dispatch_queue_concurrent_vtable 37 | _OBJC_CLASS_$_OS_dispatch_queue_global 38 | __OS_dispatch_queue_global_vtable 39 | _OBJC_CLASS_$_OS_dispatch_queue_pthread_root 40 | __OS_dispatch_queue_pthread_root_vtable 41 | _OBJC_CLASS_$_OS_dispatch_queue_main 42 | __OS_dispatch_queue_main_vtable 43 | _OBJC_CLASS_$_OS_dispatch_queue_runloop 44 | __OS_dispatch_queue_runloop_vtable 45 | _OBJC_CLASS_$_OS_dispatch_queue_mgr 46 | __OS_dispatch_queue_mgr_vtable 47 | _OBJC_CLASS_$_OS_dispatch_queue_attr 48 | __OS_dispatch_queue_attr_vtable 49 | _OBJC_CLASS_$_OS_dispatch_source 50 | __OS_dispatch_source_vtable 51 | _OBJC_CLASS_$_OS_dispatch_mach 52 | __OS_dispatch_mach_vtable 53 | _OBJC_CLASS_$_OS_dispatch_mach_msg 54 | __OS_dispatch_mach_msg_vtable 55 | _OBJC_CLASS_$_OS_dispatch_io 56 | __OS_dispatch_io_vtable 57 | _OBJC_CLASS_$_OS_dispatch_operation 58 | __OS_dispatch_operation_vtable 59 | _OBJC_CLASS_$_OS_dispatch_disk 60 | __OS_dispatch_disk_vtable 61 | # os_object_t classes 62 | _OBJC_CLASS_$_OS_object 63 | _OBJC_CLASS_$_OS_voucher 64 | #_OBJC_CLASS_$_OS_voucher_recipe 65 | # non-os_object_t classes 66 | _OBJC_CLASS_$_OS_dispatch_data 67 | _OBJC_CLASS_$_OS_dispatch_data_empty 68 | # metaclasses 69 | _OBJC_METACLASS_$_OS_dispatch_object 70 | _OBJC_METACLASS_$_OS_dispatch_semaphore 71 | _OBJC_METACLASS_$_OS_dispatch_group 72 | _OBJC_METACLASS_$_OS_dispatch_queue 73 | _OBJC_METACLASS_$_OS_dispatch_workloop 74 | _OBJC_METACLASS_$_OS_dispatch_queue_serial 75 | _OBJC_METACLASS_$_OS_dispatch_queue_concurrent 76 | _OBJC_METACLASS_$_OS_dispatch_queue_global 77 | _OBJC_METACLASS_$_OS_dispatch_queue_pthread_root 78 | _OBJC_METACLASS_$_OS_dispatch_queue_main 79 | _OBJC_METACLASS_$_OS_dispatch_queue_runloop 80 | _OBJC_METACLASS_$_OS_dispatch_queue_mgr 81 | _OBJC_METACLASS_$_OS_dispatch_queue_attr 82 | _OBJC_METACLASS_$_OS_dispatch_source 83 | _OBJC_METACLASS_$_OS_dispatch_mach 84 | _OBJC_METACLASS_$_OS_dispatch_mach_msg 85 | _OBJC_METACLASS_$_OS_dispatch_io 86 | _OBJC_METACLASS_$_OS_dispatch_operation 87 | _OBJC_METACLASS_$_OS_dispatch_disk 88 | _OBJC_METACLASS_$_OS_object 89 | _OBJC_METACLASS_$_OS_voucher 90 | #_OBJC_METACLASS_$_OS_voucher_recipe 91 | _OBJC_METACLASS_$_OS_dispatch_data 92 | _OBJC_METACLASS_$_OS_dispatch_data_empty 93 | -------------------------------------------------------------------------------- /xcodeconfig/libfirehose.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2015 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 | SUPPORTED_PLATFORMS = macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator 22 | PRODUCT_NAME = $(TARGET_NAME) 23 | INSTALL_PATH = /usr/local/lib/ 24 | GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) FIREHOSE_SERVER=1 DISPATCH_USE_DTRACE=0 25 | OTHER_MIGFLAGS = -novouchers 26 | OTHER_LDFLAGS = 27 | PUBLIC_HEADERS_FOLDER_PATH = /usr/include/os 28 | PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/os 29 | STRIP_INSTALLED_PRODUCT = NO 30 | COPY_PHASE_STRIP = NO 31 | SEPARATE_STRIP = NO 32 | VALID_ARCHS[sdk=macosx*] = $(NATIVE_ARCH_ACTUAL) 33 | 34 | COPY_HEADERS_RUN_UNIFDEF = YES 35 | COPY_HEADERS_UNIFDEF_FLAGS = -UKERNEL 36 | -------------------------------------------------------------------------------- /xcodeconfig/libfirehose_kernel.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2015 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 | SUPPORTED_PLATFORMS = macosx iphoneos appletvos watchos 22 | PRODUCT_NAME = $(TARGET_NAME) 23 | INSTALL_PATH = /usr/local/lib/kernel/ 24 | GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) KERNEL=1 DISPATCH_USE_DTRACE=0 25 | OTHER_MIGFLAGS = -novouchers 26 | OTHER_LDFLAGS = 27 | OTHER_CFLAGS = -mkernel -nostdinc -Wno-packed 28 | PUBLIC_HEADERS_FOLDER_PATH = /usr/include/os 29 | PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/kernel/os 30 | HEADER_SEARCH_PATHS = $(PROJECT_DIR) $(SDKROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders $(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers $(SDKROOT)/usr/local/include/os $(SDKROOT)/usr/local/include/firehose 31 | STRIP_INSTALLED_PRODUCT = NO 32 | COPY_PHASE_STRIP = NO 33 | SEPARATE_STRIP = NO 34 | VALID_ARCHS[sdk=macosx*] = $(NATIVE_ARCH_ACTUAL) 35 | 36 | COPY_HEADERS_RUN_UNIFDEF = YES 37 | COPY_HEADERS_UNIFDEF_FLAGS = -DKERNEL=1 -DOS_FIREHOSE_SPI=1 -DOS_VOUCHER_ACTIVITY_SPI_TYPES=1 -UOS_VOUCHER_ACTIVITY_SPI 38 | -------------------------------------------------------------------------------- /xcodescripts/check-order.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2018 Apple Inc. 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 | test "$ACTION" = install || exit 0 23 | 24 | list_objc_syms () 25 | { 26 | nm -arch $1 -nU ${DSTROOT}/usr/lib/system/libdispatch.dylib | grep _OBJC | cut -d' ' -f3 27 | } 28 | 29 | list_mutable_data_syms () 30 | { 31 | nm -arch $1 -m ${DSTROOT}/usr/lib/system/libdispatch.dylib |grep __DATA|egrep -v '(__const|__crash_info)'|sed 's/^.* //' 32 | } 33 | 34 | list_objc_order () 35 | { 36 | grep '^_OBJC' "${SCRIPT_INPUT_FILE_0}" 37 | } 38 | 39 | list_dirty_order () 40 | { 41 | grep '^[^#]' "${SCRIPT_INPUT_FILE_1}" 42 | } 43 | 44 | list_clean_order () 45 | { 46 | grep '^[^#]' "${SCRIPT_INPUT_FILE_2}" 47 | } 48 | 49 | fail= 50 | 51 | case "$PLATFORM_NAME" in 52 | *simulator) exit 0;; 53 | *) ;; 54 | esac 55 | 56 | if comm -12 <(list_dirty_order | sort) <(list_clean_order | sort) | grep .; then 57 | echo 1>&2 "error: *** SYMBOLS CAN'T BE BOTH CLEAN AND DIRTY ***" 58 | comm 1>&2 -12 <(list_dirty_order | sort) <(list_clean_order | sort) 59 | fail=t 60 | fi 61 | 62 | for arch in $ARCHS; do 63 | if test "$PLATFORM_NAME" = macosx -a "$arch" = i386; then 64 | continue 65 | fi 66 | 67 | if list_mutable_data_syms $arch | sort | uniq -c | grep -qvw 1; then 68 | echo 1>&2 "error: *** DUPLICATED SYMBOL NAMES FOR SLICE $arch ***" 69 | list_mutable_data_syms $arch | sort | uniq -c | grep -qw 1 1>&2 70 | fail=t 71 | fi 72 | 73 | if comm -23 <(list_mutable_data_syms $arch | sort) <((list_dirty_order; list_clean_order) | sort) | grep -q .; then 74 | echo 1>&2 "error: *** SYMBOLS NOT MARKED CLEAN OR DIRTY FOR SLICE $arch ***" 75 | comm 1>&2 -23 <(list_mutable_data_syms $arch | sort) <((list_dirty_order; list_clean_order) | sort) 76 | fail=t 77 | fi 78 | 79 | if comm -13 <(list_mutable_data_syms $arch | sort) <((list_dirty_order; list_clean_order) | sort) | grep -q .; then 80 | echo 1>&2 "warning: *** Found unknown symbols in dirty/clean files for slice $arch ***" 81 | comm 1>&2 -13 <(list_mutable_data_syms $arch | sort) <((list_dirty_order; list_clean_order) | sort) 82 | fi 83 | 84 | if ! cmp -s <(list_objc_syms $arch) <(list_objc_order); then 85 | echo 1>&2 "error: *** SYMBOL ORDER IS NOT WHAT IS EXPECTED FOR SLICE $arch ***" 86 | diff 1>&2 -U100 <(list_objc_syms $arch) <(list_objc_order) || fail=t 87 | fi 88 | done 89 | 90 | test -z "$fail" 91 | -------------------------------------------------------------------------------- /xcodescripts/install-dtrace.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2013 Apple Inc. 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 | # This check equates to "is macosx or a simulator platform" 23 | if [ "${PLATFORM_NAME}" == "${DEVICE_PLATFORM_NAME}" ]; then exit 0; fi 24 | 25 | if [ "${DEPLOYMENT_LOCATION}" != YES ]; then 26 | DSTROOT="${CONFIGURATION_BUILD_DIR}" 27 | fi 28 | 29 | mkdir -p "${DSTROOT}${PUBLIC_HEADERS_FOLDER_PATH}" || true 30 | cp -X "${SCRIPT_INPUT_FILE_1}" \ 31 | "${DSTROOT}${PUBLIC_HEADERS_FOLDER_PATH}/${SCRIPT_OUTPUT_FILE_0##/*/}" 32 | -------------------------------------------------------------------------------- /xcodescripts/install-headers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2012 Apple Inc. 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 | if [ "${DEPLOYMENT_LOCATION}" != YES ]; then 23 | DSTROOT="${CONFIGURATION_BUILD_DIR}" 24 | fi 25 | 26 | mkdir -p "${DSTROOT}${OS_PUBLIC_HEADERS_FOLDER_PATH}" || true 27 | mkdir -p "${DSTROOT}${OS_PRIVATE_HEADERS_FOLDER_PATH}" || true 28 | cp -X "${SCRIPT_INPUT_FILE_1}" "${DSTROOT}${OS_PUBLIC_HEADERS_FOLDER_PATH}" 29 | cp -X "${SCRIPT_INPUT_FILE_2}" "${DSTROOT}${OS_PRIVATE_HEADERS_FOLDER_PATH}" 30 | cp -X "${SCRIPT_INPUT_FILE_3}" "${DSTROOT}${OS_PRIVATE_HEADERS_FOLDER_PATH}" 31 | cp -X "${SCRIPT_INPUT_FILE_4}" "${DSTROOT}${OS_PRIVATE_HEADERS_FOLDER_PATH}" 32 | cp -X "${SCRIPT_INPUT_FILE_5}" "${DSTROOT}${OS_PRIVATE_HEADERS_FOLDER_PATH}" 33 | -------------------------------------------------------------------------------- /xcodescripts/mig-headers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2010-2011 Apple Inc. 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 | export MIGCC="$(xcrun -find cc)" 23 | export MIGCOM="$(xcrun -find migcom)" 24 | export PATH="${PLATFORM_DEVELOPER_BIN_DIR}:${DEVELOPER_BIN_DIR}:${PATH}" 25 | 26 | for p in ${HEADER_SEARCH_PATHS}; do 27 | OTHER_MIGFLAGS="${OTHER_MIGFLAGS} -I${p}" 28 | done 29 | 30 | for a in ${ARCHS}; do 31 | xcrun mig ${OTHER_MIGFLAGS} -arch $a -header "${SCRIPT_OUTPUT_FILE_0}" \ 32 | -sheader "${SCRIPT_OUTPUT_FILE_1}" -user /dev/null \ 33 | -server /dev/null "${SCRIPT_INPUT_FILE_0}" 34 | xcrun mig ${OTHER_MIGFLAGS} -arch $a -header "${SCRIPT_OUTPUT_FILE_2}" \ 35 | -sheader "${SCRIPT_OUTPUT_FILE_3}" -user /dev/null \ 36 | -server /dev/null "${SCRIPT_INPUT_FILE_1}" 37 | xcrun mig ${OTHER_MIGFLAGS} -arch $a -header "${SCRIPT_OUTPUT_FILE_4}" \ 38 | -sheader "${SCRIPT_OUTPUT_FILE_5}" -user /dev/null \ 39 | -server /dev/null "${SCRIPT_INPUT_FILE_2}" 40 | done 41 | -------------------------------------------------------------------------------- /xcodescripts/postprocess-headers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2010-2011 Apple Inc. 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 | -------------------------------------------------------------------------------- /xcodescripts/run-on-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Copyright (c) 2016 Apple Inc. 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 | if [[ "x${ACTION}" == "xinstall" && "x${SKIP_INSTALL}" == "xNO" ]]; then 23 | $@ 24 | else 25 | exit 0 26 | fi 27 | --------------------------------------------------------------------------------