source/AccessorySDK/Support/DispatchLite.c
OSStatus	LibDispatch_EnsureInitialized( void )
{
	OSStatus		err;
	
	pthread_mutex_lock( &gDispatchInitializeMutex );
	require_action_quiet( !gDispatchInitialized, exit, err = kNoErr );
	
	// Set up thread pool.

#if( !TARGET_OS_WINDOWS )
	gDispatchThreadPool_Quit = false;
	
	err = pthread_mutex_init( &gDispatchThreadPool_Mutex, NULL );
	require_noerr( err, exit );
	gDispatchThreadPool_MutexPtr = &gDispatchThreadPool_Mutex;
	
	err = pthread_cond_init( &gDispatchThreadPool_Condition, NULL );
	require_noerr( err, exit );
	gDispatchThreadPool_ConditionPtr = &gDispatchThreadPool_Condition;
	
	err = pthread_attr_init( &gDispatchThreadPool_Attr );
	require_noerr( err, exit );
	gDispatchThreadPool_AttrPtr = &gDispatchThreadPool_Attr;
	
	err = pthread_attr_setdetachstate( gDispatchThreadPool_AttrPtr, PTHREAD_CREATE_DETACHED );
	require_noerr( err, exit );
	
	gDispatchThreadPool_ItemsHead = NULL;
	gDispatchThreadPool_ItemsNext = &gDispatchThreadPool_ItemsHead;
#endif
	
	// Set up global queues.
	
	err = pthread_key_create( &gDispatchKey_CurrentQueue, NULL ); 
	require_noerr( err, exit );
	gDispatchKey_CurrentQueuePtr = &gDispatchKey_CurrentQueue;
	
	gDispatchConcurrentQueues[ 0 ] = __dispatch_queue_create_internal( "com.apple.low-priority-root-queue" );
	require_action( gDispatchConcurrentQueues[ 0 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 0 ]->concurrent = true;
	
	gDispatchConcurrentQueues[ 1 ] = __dispatch_queue_create_internal( "com.apple.root-queue" );
	require_action( gDispatchConcurrentQueues[ 1 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 1 ]->concurrent = true;
	
	gDispatchConcurrentQueues[ 2 ] = __dispatch_queue_create_internal( "com.apple.high-priority-root-queue" );
	require_action( gDispatchConcurrentQueues[ 2 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 2 ]->concurrent = true;
	
	// Set up the main queue.
	
#if( !TARGET_OS_WINDOWS )
	gDispatchMainQueue_WorkPending = false;
#endif
	
	gDispatchMainQueue = __dispatch_queue_create_internal( "com.apple.main-queue" );
	require_action( gDispatchMainQueue, exit, err = ENOMEM );
	
#if( !TARGET_OS_WINDOWS )
	err = pthread_cond_init( &gDispatchMainQueue_Condition, NULL );
	require_noerr( err, exit );
	gDispatchMainQueue_ConditionPtr = &gDispatchMainQueue_Condition;
#endif
	
	pthread_setspecific( gDispatchKey_CurrentQueue, gDispatchMainQueue );
	
	// Set up kqueue support.
	
#if( DISPATCH_LITE_USE_KQUEUE )
	err = pthread_mutex_init( &gDispatchKQueue_Mutex, NULL );
	require_noerr( err, exit );
	gDispatchKQueue_MutexPtr = &gDispatchKQueue_Mutex;
	
	gDispatchKQueue_DrainQueue = __dispatch_queue_create_internal( "com.apple.kqueue-drain" );
	require_action( gDispatchKQueue_DrainQueue, exit, err = ENOMEM );
#endif
	
	// Set up CF support.
	
#if( DISPATCH_LITE_CF_ENABLED )
	err = __LibDispatchCF_EnsureInitialized();
	require_noerr( err, exit );
#endif
	
	// Set up select() support.
	
#if( DISPATCH_LITE_USE_SELECT )
	err = __LibDispatch_SelectEnsureInitialized();
	require_noerr( err, exit );
#endif
	
	// Set up Windows support.
	
#if( TARGET_OS_WINDOWS )
{
	HINSTANCE		instance;
	WNDCLASSEX		wcex;
	HWND			wind;
	WSADATA			wsaData;
	
	gDispatchConcurrentCount = 0;
	gDispatchSerialCount     = 0;
	
	instance = GetModuleHandle( NULL );
	err = map_global_value_errno( instance, instance );
	require_noerr( err, exit );
	
	wcex.cbSize			= sizeof( wcex );
	wcex.style			= 0;
	wcex.lpfnWndProc	= __LibDispatch_MainQueueWinProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= instance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hbrBackground	= NULL;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= TEXT( "libdispatch_main" );
	wcex.hIconSm		= NULL;
	RegisterClassEx( &wcex );
	
	wind = CreateWindow( wcex.lpszClassName, wcex.lpszClassName, 0, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, 
		instance, NULL );
	err = map_global_value_errno( wind, wind );
	require_noerr( err, exit );
	gDispatchMainQueueWindow = wind;
	
	// Set up handling of IO.
	
	err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
	check_noerr( err );
	gDispatchDidWSAStartup = true;
	
	err = pthread_mutex_init( &gDispatchSocketHashTableMutex, NULL );
	require_noerr( err, exit );
	gDispatchSocketHashTableMutexPtr = &gDispatchSocketHashTableMutex;
	
	err = __LibDispatch_IOThreadStart();
	require_noerr( err, exit );
}
#endif // TARGET_OS_WINDOWS
	
	gDispatchInitialized = true;
	
exit:
	pthread_mutex_unlock( &gDispatchInitializeMutex );
	if( err ) LibDispatch_Finalize();
	return( err );
}
//===========================================================================================================================
//	LibDispatch_EnsureInitialized
//
//	Note: Must be called from the main thread.
//===========================================================================================================================
LibDispatch_EnsureInitialized
Note: Must be called from the main thread.
LibDispatch_EnsureInitialized Note : Must be called from the main thread .
OSStatus	LibDispatch_EnsureInitialized( void )
{
	OSStatus		err;
	pthread_mutex_lock( &gDispatchInitializeMutex );
	require_action_quiet( !gDispatchInitialized, exit, err = kNoErr );
#if( !TARGET_OS_WINDOWS )
	gDispatchThreadPool_Quit = false;
	err = pthread_mutex_init( &gDispatchThreadPool_Mutex, NULL );
	require_noerr( err, exit );
	gDispatchThreadPool_MutexPtr = &gDispatchThreadPool_Mutex;
	err = pthread_cond_init( &gDispatchThreadPool_Condition, NULL );
	require_noerr( err, exit );
	gDispatchThreadPool_ConditionPtr = &gDispatchThreadPool_Condition;
	err = pthread_attr_init( &gDispatchThreadPool_Attr );
	require_noerr( err, exit );
	gDispatchThreadPool_AttrPtr = &gDispatchThreadPool_Attr;
	err = pthread_attr_setdetachstate( gDispatchThreadPool_AttrPtr, PTHREAD_CREATE_DETACHED );
	require_noerr( err, exit );
	gDispatchThreadPool_ItemsHead = NULL;
	gDispatchThreadPool_ItemsNext = &gDispatchThreadPool_ItemsHead;
#endif
	err = pthread_key_create( &gDispatchKey_CurrentQueue, NULL ); 
	require_noerr( err, exit );
	gDispatchKey_CurrentQueuePtr = &gDispatchKey_CurrentQueue;
	gDispatchConcurrentQueues[ 0 ] = __dispatch_queue_create_internal( "com.apple.low-priority-root-queue" );
	require_action( gDispatchConcurrentQueues[ 0 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 0 ]->concurrent = true;
	gDispatchConcurrentQueues[ 1 ] = __dispatch_queue_create_internal( "com.apple.root-queue" );
	require_action( gDispatchConcurrentQueues[ 1 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 1 ]->concurrent = true;
	gDispatchConcurrentQueues[ 2 ] = __dispatch_queue_create_internal( "com.apple.high-priority-root-queue" );
	require_action( gDispatchConcurrentQueues[ 2 ], exit, err = ENOMEM );
	gDispatchConcurrentQueues[ 2 ]->concurrent = true;
#if( !TARGET_OS_WINDOWS )
	gDispatchMainQueue_WorkPending = false;
#endif
	gDispatchMainQueue = __dispatch_queue_create_internal( "com.apple.main-queue" );
	require_action( gDispatchMainQueue, exit, err = ENOMEM );
#if( !TARGET_OS_WINDOWS )
	err = pthread_cond_init( &gDispatchMainQueue_Condition, NULL );
	require_noerr( err, exit );
	gDispatchMainQueue_ConditionPtr = &gDispatchMainQueue_Condition;
#endif
	pthread_setspecific( gDispatchKey_CurrentQueue, gDispatchMainQueue );
#if( DISPATCH_LITE_USE_KQUEUE )
	err = pthread_mutex_init( &gDispatchKQueue_Mutex, NULL );
	require_noerr( err, exit );
	gDispatchKQueue_MutexPtr = &gDispatchKQueue_Mutex;
	gDispatchKQueue_DrainQueue = __dispatch_queue_create_internal( "com.apple.kqueue-drain" );
	require_action( gDispatchKQueue_DrainQueue, exit, err = ENOMEM );
#endif
#if( DISPATCH_LITE_CF_ENABLED )
	err = __LibDispatchCF_EnsureInitialized();
	require_noerr( err, exit );
#endif
#if( DISPATCH_LITE_USE_SELECT )
	err = __LibDispatch_SelectEnsureInitialized();
	require_noerr( err, exit );
#endif
#if( TARGET_OS_WINDOWS )
{
	HINSTANCE		instance;
	WNDCLASSEX		wcex;
	HWND			wind;
	WSADATA			wsaData;
	gDispatchConcurrentCount = 0;
	gDispatchSerialCount     = 0;
	instance = GetModuleHandle( NULL );
	err = map_global_value_errno( instance, instance );
	require_noerr( err, exit );
	wcex.cbSize			= sizeof( wcex );
	wcex.style			= 0;
	wcex.lpfnWndProc	= __LibDispatch_MainQueueWinProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= instance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hbrBackground	= NULL;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= TEXT( "libdispatch_main" );
	wcex.hIconSm		= NULL;
	RegisterClassEx( &wcex );
	wind = CreateWindow( wcex.lpszClassName, wcex.lpszClassName, 0, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, 
		instance, NULL );
	err = map_global_value_errno( wind, wind );
	require_noerr( err, exit );
	gDispatchMainQueueWindow = wind;
	err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
	check_noerr( err );
	gDispatchDidWSAStartup = true;
	err = pthread_mutex_init( &gDispatchSocketHashTableMutex, NULL );
	require_noerr( err, exit );
	gDispatchSocketHashTableMutexPtr = &gDispatchSocketHashTableMutex;
	err = __LibDispatch_IOThreadStart();
	require_noerr( err, exit );
}
#endif 
	gDispatchInitialized = true;
exit:
	pthread_mutex_unlock( &gDispatchInitializeMutex );
	if( err ) LibDispatch_Finalize();
	return( err );
}
OSStatus LibDispatch_EnsureInitialized ( void ) { OSStatus err ; pthread_mutex_lock ( & gDispatchInitializeMutex ) ; require_action_quiet ( ! gDispatchInitialized , exit , err = kNoErr ) ; #if ( ! TARGET_OS_WINDOWS ) gDispatchThreadPool_Quit = false ; err = pthread_mutex_init ( & gDispatchThreadPool_Mutex , NULL ) ; require_noerr ( err , exit ) ; gDispatchThreadPool_MutexPtr = & gDispatchThreadPool_Mutex ; err = pthread_cond_init ( & gDispatchThreadPool_Condition , NULL ) ; require_noerr ( err , exit ) ; gDispatchThreadPool_ConditionPtr = & gDispatchThreadPool_Condition ; err = pthread_attr_init ( & gDispatchThreadPool_Attr ) ; require_noerr ( err , exit ) ; gDispatchThreadPool_AttrPtr = & gDispatchThreadPool_Attr ; err = pthread_attr_setdetachstate ( gDispatchThreadPool_AttrPtr , PTHREAD_CREATE_DETACHED ) ; require_noerr ( err , exit ) ; gDispatchThreadPool_ItemsHead = NULL ; gDispatchThreadPool_ItemsNext = & gDispatchThreadPool_ItemsHead ; #endif err = pthread_key_create ( & gDispatchKey_CurrentQueue , NULL ) ; require_noerr ( err , exit ) ; gDispatchKey_CurrentQueuePtr = & gDispatchKey_CurrentQueue ; gDispatchConcurrentQueues [ 0 ] = __dispatch_queue_create_internal ( " " ) ; require_action ( gDispatchConcurrentQueues [ 0 ] , exit , err = ENOMEM ) ; gDispatchConcurrentQueues [ 0 ] -> concurrent = true ; gDispatchConcurrentQueues [ 1 ] = __dispatch_queue_create_internal ( " " ) ; require_action ( gDispatchConcurrentQueues [ 1 ] , exit , err = ENOMEM ) ; gDispatchConcurrentQueues [ 1 ] -> concurrent = true ; gDispatchConcurrentQueues [ 2 ] = __dispatch_queue_create_internal ( " " ) ; require_action ( gDispatchConcurrentQueues [ 2 ] , exit , err = ENOMEM ) ; gDispatchConcurrentQueues [ 2 ] -> concurrent = true ; #if ( ! TARGET_OS_WINDOWS ) gDispatchMainQueue_WorkPending = false ; #endif gDispatchMainQueue = __dispatch_queue_create_internal ( " " ) ; require_action ( gDispatchMainQueue , exit , err = ENOMEM ) ; #if ( ! TARGET_OS_WINDOWS ) err = pthread_cond_init ( & gDispatchMainQueue_Condition , NULL ) ; require_noerr ( err , exit ) ; gDispatchMainQueue_ConditionPtr = & gDispatchMainQueue_Condition ; #endif pthread_setspecific ( gDispatchKey_CurrentQueue , gDispatchMainQueue ) ; #if ( DISPATCH_LITE_USE_KQUEUE ) err = pthread_mutex_init ( & gDispatchKQueue_Mutex , NULL ) ; require_noerr ( err , exit ) ; gDispatchKQueue_MutexPtr = & gDispatchKQueue_Mutex ; gDispatchKQueue_DrainQueue = __dispatch_queue_create_internal ( " " ) ; require_action ( gDispatchKQueue_DrainQueue , exit , err = ENOMEM ) ; #endif #if ( DISPATCH_LITE_CF_ENABLED ) err = __LibDispatchCF_EnsureInitialized ( ) ; require_noerr ( err , exit ) ; #endif #if ( DISPATCH_LITE_USE_SELECT ) err = __LibDispatch_SelectEnsureInitialized ( ) ; require_noerr ( err , exit ) ; #endif #if ( TARGET_OS_WINDOWS ) { HINSTANCE instance ; WNDCLASSEX wcex ; HWND wind ; WSADATA wsaData ; gDispatchConcurrentCount = 0 ; gDispatchSerialCount = 0 ; instance = GetModuleHandle ( NULL ) ; err = map_global_value_errno ( instance , instance ) ; require_noerr ( err , exit ) ; wcex . cbSize = sizeof ( wcex ) ; wcex . style = 0 ; wcex . lpfnWndProc = __LibDispatch_MainQueueWinProc ; wcex . cbClsExtra = 0 ; wcex . cbWndExtra = 0 ; wcex . hInstance = instance ; wcex . hIcon = NULL ; wcex . hCursor = NULL ; wcex . hbrBackground = NULL ; wcex . lpszMenuName = NULL ; wcex . lpszClassName = TEXT ( " " ) ; wcex . hIconSm = NULL ; RegisterClassEx ( & wcex ) ; wind = CreateWindow ( wcex . lpszClassName , wcex . lpszClassName , 0 , CW_USEDEFAULT , 0 , CW_USEDEFAULT , 0 , NULL , NULL , instance , NULL ) ; err = map_global_value_errno ( wind , wind ) ; require_noerr ( err , exit ) ; gDispatchMainQueueWindow = wind ; err = WSAStartup ( MAKEWORD ( 2 , 2 ) , & wsaData ) ; check_noerr ( err ) ; gDispatchDidWSAStartup = true ; err = pthread_mutex_init ( & gDispatchSocketHashTableMutex , NULL ) ; require_noerr ( err , exit ) ; gDispatchSocketHashTableMutexPtr = & gDispatchSocketHashTableMutex ; err = __LibDispatch_IOThreadStart ( ) ; require_noerr ( err , exit ) ; } #endif gDispatchInitialized = true ; exit : pthread_mutex_unlock ( & gDispatchInitializeMutex ) ; if ( err ) LibDispatch_Finalize ( ) ; return ( err ) ; }
LibDispatch_EnsureInitialized
Note: Must be called from the main thread.
LibDispatch_EnsureInitialized Note : Must be called from the main thread .
// Set up thread pool. // Set up global queues. // Set up the main queue. // Set up kqueue support. // Set up CF support. // Set up select() support. // Set up Windows support. // Set up handling of IO. // TARGET_OS_WINDOWS