static void *threadproc( void *param ) { athread *p = (athread *)param; /* Register this thread with the Objective-C garbage collector */ #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 objc_registerThreadWithCollector(); #endif p->result = p->function(p->context); p->finished = 1; return 0; }
/* * Unwrap the arguments and re-run main() */ static void *apple_main (void *arg) { objc_registerThreadWithCollector(); if (main_fptr == NULL) { main_fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); if (main_fptr == NULL) { JLI_ReportErrorMessageSys("error locating main entrypoint\n"); exit(1); } } struct NSAppArgs *args = (struct NSAppArgs *) arg; exit(main_fptr(args->argc, args->argv)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Start() //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void* CFMachPortThread::Start(CFMachPortThread* thread) { // Register this thread the the garbage collector if needed #if (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5) objc_registerThreadWithCollector(); #endif thread->mRunLoop = CFRunLoopGetCurrent(); try { // Create the CACFMachPort used by this thread CACFMachPort machPort(thread->mPortCallBack, thread->mUserData); // Extract the wrapped Mach port so it can be reported to clients thread->mPort = machPort.GetMachPort(); // Add the the port's run loop source to this run loop CFRunLoopAddSource(thread->mRunLoop.GetCFObject(), machPort.GetRunLoopSource(), kCFRunLoopCommonModes); // Indicate that the run loop will need to be stopped during the next call to Reset(). thread->mState = kRunning; CFRunLoopRun(); // In the unlikey event that CFRunLoopRun() returned prior to CFRunLoopStop() being called in Reset(), wait until the stopping condition has been explicitly signaled prior to // letting the thread run to completion. This will make sure that the run loop is still valid when CFRunLoopStop() is called. { PTA::Mutex::Locker locker(thread->mStoppingMutex); (void) pthread_cond_wait(thread->mStoppingCondition, thread->mStoppingMutex); thread->mState = kInvalid; } } catch (...) { thread->mState = kInvalid; return reinterpret_cast<void*>(-1); } return 0; }
static void iokit_poll_notifications(void *opaque) { int phase = 0; Semaphore *handshake = opaque; IONotificationPortRef notification_port; io_object_t notifier; io_connect_t root_port; CFRunLoopSourceRef notification_run_loop_source; log_debug("Started notification poll thread"); // need to register this PThread with the Objective-C garbage collector, // because CoreFoundation uses Objective-C #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 objc_registerThreadWithCollector(); #endif // register for system sleep/wake notifications root_port = IORegisterForSystemPower(&root_port, ¬ification_port, iokit_handle_notifications, ¬ifier); if (root_port == MACH_PORT_NULL) { log_error("Could not register for root power domain"); goto cleanup; } phase = 1; // get notification run loop source notification_run_loop_source = IONotificationPortGetRunLoopSource(notification_port); if (notification_run_loop_source == NULL) { log_error("Could not get notification run loop source"); goto cleanup; } CFRunLoopAddSource(CFRunLoopGetCurrent(), notification_run_loop_source, kCFRunLoopDefaultMode); phase = 2; // start loop _run_loop = (CFRunLoopRef)CFRetain(CFRunLoopGetCurrent()); _running = true; semaphore_release(handshake); CFRunLoopRun(); log_debug("Stopped notification poll thread"); cleanup: if (!_running) { // need to release the handshake in all cases, otherwise iokit_init // will block forever in semaphore_acquire semaphore_release(handshake); } switch (phase) { // no breaks, all cases fall through intentionally case 2: CFRunLoopRemoveSource(CFRunLoopGetCurrent(), notification_run_loop_source, kCFRunLoopDefaultMode); case 1: IODeregisterForSystemPower(¬ifier); IOServiceClose(root_port); IONotificationPortDestroy(notification_port); default: break; } _running = false; }
void RegisterThread() { objc_registerThreadWithCollector(); }