/**
 * Placed at the end of all monitor functions, this leaves a monitor.
 */
void monitor_leave(monitor_t *monitor) {
  if (monitor->next_count > 0) {
    semaphore_signal(monitor->sem_id, NEXT_SEM);
  } else {
    semaphore_signal(monitor->sem_id, MUTEX_SEM);
  }
}
Пример #2
0
static int vsync_isr(void *data)
{

  stm_display_output_handle_interrupts(pOutput);

  topfields++;

  if((topfields % framerate) == 0)
  {
    semaphore_signal(framerate_sem);
  }

  semaphore_signal(frameupdate_sem);

  if(pHDMI)
  {
    stm_display_status_t oldDisplayStatus = currentDisplayStatus;

    stm_display_output_get_status(pHDMI, &currentDisplayStatus);

    if(hotplug_poll_pio)
    {
      unsigned char hotplugstate = *hotplug_poll_pio & hotplug_pio_pin;
      if(currentDisplayStatus == STM_DISPLAY_DISCONNECTED)
      {
        /*
         * If the device has just been plugged in, flag that we now need to
         * start the output.
         */
        if(hotplugstate != 0)
        {
          currentDisplayStatus = STM_DISPLAY_NEEDS_RESTART;
          stm_display_output_set_status(pHDMI, currentDisplayStatus);
        }
      }
      else
      {
        /*
         * We may either be waiting for the output to be started, or already
         * started, so only change the state if the device has now been
         * disconnected.
         */
        if(hotplugstate == 0)
        {
          currentDisplayStatus = STM_DISPLAY_DISCONNECTED;
          stm_display_output_set_status(pHDMI, currentDisplayStatus);
        }
      }
    }

    if(oldDisplayStatus != currentDisplayStatus)
    {
      semaphore_signal(hotplug_sem);
    }
  }

  return OS21_SUCCESS;
}
Пример #3
0
void queue_push(Queue *queue, int item) {
  semaphore_wait(queue->spaces);
  semaphore_wait(queue->mutex);

  queue->array[queue->next_in] = item;
  queue->next_in = queue_incr(queue, queue->next_in);

  semaphore_signal(queue->mutex);
  semaphore_signal(queue->items);
}
Пример #4
0
void *
start_routine(void *id)
{
    semaphore_signal(g_sem[1]);
    fprintf(stderr, "thread: %lx about to decrement semaphore count\n", id);
    semaphore_wait(g_sem[0]);
    fprintf(stderr, "thread: %lx succeeded in decrementing semaphore count\n", id);
    semaphore_signal(g_sem[1]);
    return NULL;
}
/**
 * Waits on the specified condition variable in the monitor.
 * cond is the zero based index of the condition variable which
 * must be less than the number of condition variables the monitor
 * was created with.
 */
void monitor_cond_wait(monitor_t *monitor, int cond) {
  monitor->x_count[cond]++;

  if (monitor->next_count > 0) {
    semaphore_signal(monitor->sem_id, NEXT_SEM);
  } else {
    semaphore_signal(monitor->sem_id, MUTEX_SEM);
  }

  semaphore_wait(monitor->sem_id, MONITOR_SEM_COUNT + cond);
  monitor->x_count[cond]--;
}
Пример #6
0
int queue_pop(Queue *queue) {
  semaphore_wait(queue->items);
  semaphore_wait(queue->mutex);
  
  int item = queue->array[queue->next_out];
  queue->next_out = queue_incr(queue, queue->next_out);

  semaphore_signal(queue->mutex);
  semaphore_signal(queue->spaces);

  return item;
}
Пример #7
0
/*
 * Add an integer to the buffer.  Yield control to another
 * thread if the buffer is full.
 */
void bounded_buffer_add(BoundedBuffer *bufp, const BufferElem *elem) {
    /* Not adding until the queue is known to not be full */
    semaphore_wait(bufp->full_sema);
    /* Accessing the mutex semaphore, to ensure only one thread is running */
    semaphore_wait(bufp->single_sema);
    /* Now the buffer has space */
    bufp->buffer[(bufp->first + bufp->count) % bufp->length] = *elem;
    bufp->count++;
    /* Signaling that this thread is done with the buffer */
    semaphore_signal(bufp->single_sema);
    /* Incrementing the empty sema */
    semaphore_signal(bufp->empty_sema);
}
IOReturn com_ximeta_driver_NDASDiskArrayController::receiveMsg ( void * newRequest, void *command, void *, void * )
{
	DbgIOLog(DEBUG_MASK_NDAS_TRACE, ("receiveMsg: Entered\n"));
	
	switch((int)newRequest) {
		case kNDASCommandQueueAppend:
		{
			DbgIOLog(DEBUG_MASK_NDAS_INFO, ("Append Command!!!\n"));

			if(!command) {
				DbgIOLog(DEBUG_MASK_NDAS_INFO, ("No Command!!!\n"));
				
				return kIOReturnBadArgument;
			}
			
			com_ximeta_driver_NDASCommand	*tempCommand = (com_ximeta_driver_NDASCommand *)command;	

			fCommandArray->setObject(tempCommand);
			
			if( 1 == fCommandArray->getCount() ) {
				// Notify to the worker thread.
				semaphore_signal(fCommandSema);
			}			
		}
			break;
		case kNDASCommandQueueCompleteCommand:
		{
			DbgIOLog(DEBUG_MASK_NDAS_INFO, ("Complete Command!!!\n"));
			
//			com_ximeta_driver_NDASCommand	*tempCommand = (com_ximeta_driver_NDASCommand *)fCommandArray->getObject(0);
						
			fCommandArray->removeObject(0);

			if( 0 < fCommandArray->getCount() ) {
			
				// Notify to the worker thread.
				semaphore_signal(fCommandSema);
			}			
		}
			break;
		default:
			DbgIOLog(DEBUG_MASK_NDAS_ERROR, ("Invalid Command!!! %d\n", (int)newRequest));
			
			return kIOReturnInvalid;
	}
			
	return kIOReturnSuccess;
}
Пример #9
0
/*
 * Get an integer from the buffer.  Yield control to another
 * thread if the buffer is empty.
 */
void bounded_buffer_take(BoundedBuffer *bufp, BufferElem *elem) {
    /* Not taking until the queue is known to not be empty */
    semaphore_wait(bufp->empty_sema);
    /* Accessing the mutex semaphore, to ensure only one thread is running */
    semaphore_wait(bufp->single_sema);
    /* Copy the element from the buffer, and clear the record */
    *elem = bufp->buffer[bufp->first];
    bufp->buffer[bufp->first] = empty;
    bufp->count--;
    bufp->first = (bufp->first + 1) % bufp->length;

    /* Signaling that this thread is done with the buffer */
    semaphore_signal(bufp->single_sema);
    /* Incrementing the full sema */
    semaphore_signal(bufp->full_sema);
}
Пример #10
0
void Threading::Semaphore::Post(int multiple)
{
	for (int i = 0; i < multiple; ++i) {
		MACH_CHECK(semaphore_signal(m_sema));
	}
	__atomic_add_fetch(&m_counter, multiple, __ATOMIC_SEQ_CST);
}
Пример #11
0
void
os_sem_post(os_sem_t *sem, char *what)
{
    if (KERN_SUCCESS!=semaphore_signal(*sem))
        lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno));
    FSHOW((stderr, "%s: os_sem_post(%p) ok\n", what, sem));
}
Пример #12
0
static void ReceiveEvtVSync (STEVT_CallReason_t Reason,
                                const ST_DeviceName_t RegistrantName,
                                STEVT_EventConstant_t Event,
                                const void *EventData,
                                const void *SubscriberData_p )
{
    STVTG_VSYNC_t EventType;

    EventType = *((STVTG_VSYNC_t*)EventData);
    switch (EventType)
    {
        case STVTG_TOP:
            stlayer_osd_context.stosd_ActiveTopNotBottom = TRUE;
            break;

        case STVTG_BOTTOM:
            stlayer_osd_context.stosd_ActiveTopNotBottom = FALSE;
            break;

        default :
            break;
    }
    if ((stlayer_osd_context.UpdateTop)
            || (stlayer_osd_context.UpdateBottom))
    {
        semaphore_signal(stlayer_osd_context.stosd_VSync_p);
    }
}
Пример #13
0
void RBSemaphore::Signal() throw(RBException)
{
	mValue.Decrement();
	
	int errorCode = semaphore_signal(mMachSemaphore);
	RBAssertNoErr(errorCode, CFSTR("semaphore_signal failed with error code %d"), errorCode);
}
Пример #14
0
void HighPrecisionClock::runLoop() {
    if (!(MachThreadSelf("MWorks High Precision Clock").setRealtime(period, computation, period))) {
        merror(M_SCHEDULER_MESSAGE_DOMAIN, "HighPrecisionClock failed to achieve real time scheduling");
    }
    
    uint64_t startTime = mach_absolute_time();
    
    while (true) {
        {
            lock_guard lock(waitsMutex);
            while (!waits.empty() && waits.top().getExpirationTime() <= startTime) {
                logMachError("semaphore_signal", semaphore_signal(waits.top().getSemaphore()));
                waits.pop();
            }
        }
        
        // Give another thread a chance to terminate this one
        boost::this_thread::interruption_point();
        
        // Sleep until the next work cycle
        startTime += period;
        if (mach_absolute_time() < startTime) {
            logMachError("mach_wait_until", mach_wait_until(startTime));
        }
    }
}
Пример #15
0
void *Consumer(void *arg)
{
	int fd, hit=0;

	for (;;) {
#if ApplePositive
		semaphore_wait(shared.full);
#else
		sem_wait(&shared.full);
#endif
		pthread_mutex_lock(&shared.mutex);
		fd = shared.buff[shared.out];
		shared.buff[shared.out] = 0;
		shared.out = (shared.out+1)%shared.buffSize;
		/* Release the buffer */
		pthread_mutex_unlock(&shared.mutex);
		ftp(fd, hit);
		/* Increment the number of full slots */
#if ApplePositive
		semaphore_signal(shared.empty);
#else
		sem_post(&shared.empty);
#endif
		hit++;
	}
	return NULL;
}
Пример #16
0
DISPATCH_NOINLINE
long
_dispatch_semaphore_signal_slow(dispatch_semaphore_t dsema)
{
	// Before dsema_sent_ksignals is incremented we can rely on the reference
	// held by the waiter. However, once this value is incremented the waiter
	// may return between the atomic increment and the semaphore_signal(),
	// therefore an explicit reference must be held in order to safely access
	// dsema after the atomic increment.
	_dispatch_retain(dsema);

	(void)dispatch_atomic_inc2o(dsema, dsema_sent_ksignals);

#if USE_MACH_SEM
	_dispatch_semaphore_create_port(&dsema->dsema_port);
	kern_return_t kr = semaphore_signal(dsema->dsema_port);
	DISPATCH_SEMAPHORE_VERIFY_KR(kr);
#elif USE_POSIX_SEM
	int ret = sem_post(&dsema->dsema_sem);
	DISPATCH_SEMAPHORE_VERIFY_RET(ret);
#endif

	_dispatch_release(dsema);
	return 1;
}
Пример #17
0
/*
 * Signal a condition variable, waking up all threads waiting for it.
 */
int       
pthread_cond_broadcast(pthread_cond_t *cond)
{
	kern_return_t kern_res;
	if (cond->sig == _PTHREAD_COND_SIG_init)
	{
		int res;
		if (res = pthread_cond_init(cond, NULL))
			return (res);
	}
	if (cond->sig == _PTHREAD_COND_SIG)
	{ 
		LOCK(cond->lock);
		if (cond->waiters == 0)
		{ /* Avoid kernel call since there are no waiters... */
			UNLOCK(cond->lock);	
			return (ESUCCESS);
		}
		UNLOCK(cond->lock);
		MACH_CALL(semaphore_signal(cond->sem), kern_res);
		MACH_CALL(semaphore_signal_all(cond->sem), kern_res);
		if (kern_res == KERN_SUCCESS)
		{
			return (ESUCCESS);
		} else
		{
			return (EINVAL);
		}
	} else
		return (EINVAL); /* Not a condition variable */
}
Пример #18
0
int XLinuxTask_preemptive::SemWrapper::Post()
{
	kern_return_t kres=semaphore_signal(fSem);
	xbox_assert(kres==KERN_SUCCESS);

	return static_cast<int>(kres);
}
Пример #19
0
void handler ()
{
  printf("%s:%d - %s, name [%s]\n", __FILE__, __LINE__, __FUNCTION__, handlerName);
    int i;
    for(i=0; i < 5; i++)
    {
        /* If you remove this protection, you should be able to see different
         * out of every time you run this program.  With this protection, you
         * should always be able to see result to be 151402.656521 */
        semaphore_wait(counter_mutex);       /* down semaphore */

        /* START CRITICAL REGION */
	/*
        int j;
        for (j = 0; j < 1000; j++) {
            result = result + sin(counter) * tan(counter);
        }
	*/
	result += counter;
        counter++;
	// sleep(1);

        /* END CRITICAL REGION */    

        semaphore_signal(counter_mutex);       /* up semaphore */
    }
    
    exit_my_thread(); /* exit thread */
}
/**
 * Signals a monitor condition variable.
 * cond is the zero based index of the condition variable which
 * must be less than the number of condition variables the monitor
 * was created with.
 */
void monitor_cond_signal(monitor_t *monitor, int cond) {
  if (monitor->x_count[cond] > 0) {
    monitor->next_count++;
    semaphore_signal(monitor->sem_id, MONITOR_SEM_COUNT + cond);
    semaphore_wait(monitor->sem_id, NEXT_SEM);
    monitor->next_count--;
  }
}
Пример #21
0
static VOID _UnWaitCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{
#if defined __APPLE__
	semaphore_signal(*((winpr_sem_t*) lpCriticalSection->LockSemaphore));
#else
	sem_post((winpr_sem_t*) lpCriticalSection->LockSemaphore);
#endif
}
void
_dispatch_sema4_signal(_dispatch_sema4_t *sema, long count)
{
	do {
		kern_return_t kr = semaphore_signal(*sema);
		DISPATCH_SEMAPHORE_VERIFY_KR(kr);
	} while (--count);
}
Пример #23
0
void *
second_thread(void *args)
{
	struct second_thread_args *secargs = (struct second_thread_args *)args;
	int res;
	uint64_t i;
	kern_return_t kret;
	uint64_t wake_time;
	int cpuno;

	/* Set scheduling policy */
	res = thread_setup(secargs->pol);
	if (res != 0) {
		printf("Couldn't set thread policy.\n");
		exit(1);
	}

	/* 
	 * Repeatedly pick a random timer length and 
	 * try to sleep exactly that long 
	 */
	for (i = 0; i < secargs->iterations; i++) {

		/* Wake up when poked by main thread */
		kret = semaphore_wait(secargs->wakeup_semaphore);
		if (kret != KERN_SUCCESS) {
			errx(1, "semaphore_wait %d", kret);
		}

		wake_time = mach_absolute_time();
		cpuno = cpu_number();
		if (wake_time < secargs->last_poke_time) {
			/* Woke in past, unsynchronized mach_absolute_time()? */
			
			errx(1, "woke in past %llu (%d) < %llu (%d)", wake_time, cpuno, secargs->last_poke_time, secargs->cpuno);
		}

		if (cpuno == secargs->cpuno) {
			secargs->woke_on_same_cpu++;
		}

		secargs->wakeup_second_jitter_arr[i] = (double)(wake_time - secargs->last_poke_time);
		
		/* Too much: cut a tracepoint for a debugger */
		if (secargs->wakeup_second_jitter_arr[i] >= secargs->too_much) {
			kdebug_trace(0xeeeee4 | DBG_FUNC_NONE, 0, 0, 0, 0);
		}

		kret = semaphore_signal(secargs->return_semaphore);
		if (kret != KERN_SUCCESS) {
			errx(1, "semaphore_signal %d", kret);
		}

	}

	return NULL;
}
Пример #24
0
BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount)
{
#if defined __APPLE__
	semaphore_signal(*((winpr_sem_t*) hSemaphore));
#else
	sem_post((winpr_sem_t*) hSemaphore);
#endif

	return 1;
}
Пример #25
0
bool Semaphore::Signal()
{
	kern_return_t result = semaphore_signal(mSemaphore);

	if(KERN_SUCCESS != result) {
		LOGGER_WARNING("org.sbooth.AudioEngine.Semaphore", "Couldn't signal the semaphore: " << mach_error_string(result));
		return false;
	}

	return true;
}
Пример #26
0
/*
 * Add an integer to the buffer.  Yield control to another
 * thread if the buffer is full.
 */
void bounded_buffer_add(BoundedBuffer *bufp, const BufferElem *elem) {
    // We only want one thread modifying the buffer at a time
    __sthread_lock();
    // Decrement the count of available space (we're adding)
    // Blocks when available count = 0 (buffer is full)
    semaphore_wait(bufp->avail);
    bufp->buffer[(bufp->first + bufp->count) % bufp->length] = *elem;
    bufp->count++;
    // Increment the count of used space
    semaphore_signal(bufp->used);
    __sthread_unlock();
}
Пример #27
0
void
_dispatch_thread_semaphore_signal(_dispatch_thread_semaphore_t sema)
{
#if USE_MACH_SEM
	semaphore_t s4 = (semaphore_t)sema;
	kern_return_t kr = semaphore_signal(s4);
	DISPATCH_SEMAPHORE_VERIFY_KR(kr);
#elif USE_POSIX_SEM
	int ret = sem_post((sem_t *)sema);
	DISPATCH_SEMAPHORE_VERIFY_RET(ret);
#endif
}
Пример #28
0
inline int
fast_recv_m (u32 *arg1, u32 *arg2)
{
  quest_tss *cur;
  semaphore_signal (&lookup_TSS (str ())->Msem, 1);
  asm volatile ("call *%1"
                :"=D" (cur)
                :"m" (schedule)
                :"eax", "ebx", "ecx", "edx", "esi", "cc", "memory");
  *arg1 = cur->M[0];
  *arg2 = cur->M[1];
  return 0;
}
Пример #29
0
/* Increment the value of a semaphore.
 * returns 0 on success, EOVERFLOW in case of integer overflow */
int vlc_sem_post (vlc_sem_t *sem)
{
    int val;

    if (likely(semaphore_signal(*sem) == KERN_SUCCESS))
        return 0;

    val = EINVAL;

    if (unlikely(val != EOVERFLOW))
        VLC_THREAD_ASSERT ("unlocking semaphore");
    return val;
}
Пример #30
0
int
mono_sem_post (MonoSemType *sem)
{
	int res;
#ifndef USE_MACH_SEMA
	while ((res = sem_post (sem)) == -1 && errno == EINTR);
#else
	res = semaphore_signal (*sem);
	/* OSX might return > 0 for error */
	if (res != KERN_SUCCESS)
		res = -1;
#endif
	return res;
}