OSErr createWorkerThread ( WorkerActionRoutine actionRoutine, WorkerCancelRoutine cancelRoutine, WorkerResponseMainThreadCallback responseCallback, void *refcon, WorkerThreadRef *outWorker ) { WorkerThreadRef worker; if ( !actionRoutine || !responseCallback || !outWorker ) return paramErr; worker = calloc( 1, sizeof( WorkerThread ) ); if ( ! worker ) return memFullErr; worker->referenceCount = 1; worker->actionRoutine = actionRoutine; worker->cancelRoutine = cancelRoutine; worker->responseCallback = responseCallback; worker->refcon = refcon; // get the OS version Gestalt(gestaltSystemVersion, &worker->osVersion); // create a one-shot event loop timer worker->responseEventLoopTimerUPP = NewEventLoopTimerUPP( runWorkerResponseEventLoopTimer ); InstallEventLoopTimer( GetMainEventLoop(), kEventDurationForever, kEventDurationForever, worker->responseEventLoopTimerUPP, worker, &worker->responseEventLoopTimer ); // sem_init() not yet implemented as of MacOS X 10.3 - so we're going to use the MP APIs instead // the POSIX calls we could use when sem_init() is supported are clearly marked with the POSIX prefix // Named semaphors are available but not really ideal for our needs in this sample // Reference: http://developer.apple.com/documentation/Carbon/Reference/Multiprocessing_Services/multiproc_ref/function_group_3.html //POSIX sem_init( &worker->requestSemaphore, 0 /* not shared */, 0 /* initial value */ ); //POSIX sem_init( &worker->shutdownSemaphore, 0 /* not shared */, 0 /* initial value */ ); MPCreateSemaphore(1, 0, &worker->requestSemaphore); MPCreateSemaphore(1, 0, &worker->shutdownSemaphore); pthread_create( &worker->workerThread, NULL, runWorkerThread, worker ); *outWorker = worker; return noErr; }
delivery_man::delivery_man(): m_pPackage(NULL), m_pSemaphore(kInvalidID), m_bPackageWaiting(false) { assert(at_st()); OSStatus lStatus = MPCreateSemaphore(1UL, 0UL, &m_pSemaphore); // TODO - throw on error here assert(lStatus == noErr); }
wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount) { m_isOk = false ; m_semaphore = kInvalidID ; if ( maxcount == 0 ) { // make it practically infinite maxcount = INT_MAX; } verify_noerr( MPCreateSemaphore( maxcount, initialcount, & m_semaphore) ); m_isOk = ( m_semaphore != kInvalidID ) ; if ( !IsOk() ) wxFAIL_MSG(wxT("Error when creating semaphore") ) ; }
cFUSemaphore::cFUSemaphore(uint32 initialValue, uint32 maximumValue) #if defined(WIN32) : semaphoreHandle(nullptr) #endif // windows-only { assert(initialValue <= maximumValue); #if defined(WIN32) semaphoreHandle = CreateSemaphore(nullptr, initialValue, maximumValue, nullptr); assert(semaphoreHandle != nullptr); #elif defined(FP_APPLE) MPCreateSemaphore(maximumValue, initialValue, &semaphoreHandle); #elif defined(LINUX) || defined(ANDROID) || defined(IOS) sem_init(&semaphoreHandle, 0, initialValue); #endif }
hsGlobalSemaphore::hsGlobalSemaphore(int initialValue) { OSStatus status = MPCreateSemaphore(kPosInfinity32, initialValue, &fSemaId); hsThrowIfOSErr(status); }