kernel/eka/euser/us_exec.cpp
changeset 177 a232af6b0b1f
parent 90 947f0dc9f7a8
child 245 647ab20fee2e
equal deleted inserted replaced
176:af6ec97d9189 177:a232af6b0b1f
  3766 free. If the mutex is already held by the current thread a count is maintained
  3766 free. If the mutex is already held by the current thread a count is maintained
  3767 of how many times the thread has acquired the mutex.
  3767 of how many times the thread has acquired the mutex.
  3768 */
  3768 */
  3769 EXPORT_C void RMutex::Wait()
  3769 EXPORT_C void RMutex::Wait()
  3770 	{
  3770 	{
  3771 
  3771 	Exec::MutexWait(iHandle, 0);
  3772 	Exec::MutexWait(iHandle);
  3772 	}
       
  3773 
       
  3774 
       
  3775 
       
  3776 
       
  3777 /**
       
  3778 Acquire the mutex if it is currently free, but don't wait for it.
       
  3779 
       
  3780 This function checks if the mutex is currently held. If not the mutex is marked
       
  3781 as held by the current thread and the call returns immediately indicating
       
  3782 success. If the mutex is held by another thread the call returns immediately
       
  3783 indicating failure. If the mutex is already held by the current thread a count
       
  3784 is maintained of how many times the thread has acquired the mutex.
       
  3785 
       
  3786 @return	KErrNone if the mutex was acquired
       
  3787 		KErrTimedOut if the mutex could not be acquired
       
  3788         KErrGeneral if the semaphore is being reset, i.e the semaphore
       
  3789         is about to  be deleted.
       
  3790 */
       
  3791 EXPORT_C TInt RMutex::Poll()
       
  3792 	{
       
  3793 	return Exec::MutexWait(iHandle, -1);
       
  3794 	}
       
  3795 
       
  3796 
       
  3797 
       
  3798 
       
  3799 /**
       
  3800 Acquire the mutex, if necessary waiting up to a specified maximum amount of time
       
  3801 for it to become free.
       
  3802 
       
  3803 This function checks if the mutex is currently held. If not the mutex is marked
       
  3804 as held by the current thread and the call returns immediately. If the mutex is
       
  3805 held by another thread the current thread will suspend until the mutex becomes
       
  3806 free or until the specified timeout period has elapsed. If the mutex is already
       
  3807 held by the current thread a count is maintained of how many times the thread
       
  3808 has acquired the mutex.
       
  3809 
       
  3810 @param aTimeout The timeout value in microseconds
       
  3811 
       
  3812 @return KErrNone if the mutex was acquired successfully.
       
  3813         KErrTimedOut if the timeout has expired.
       
  3814         KErrGeneral if the mutex is being reset, i.e the mutex
       
  3815         is about to  be deleted.
       
  3816         KErrArgument if aTimeout is negative;
       
  3817         otherwise one of the other system wide error codes.
       
  3818 */
       
  3819 EXPORT_C TInt RMutex::Wait(TInt aTimeout)
       
  3820 	{
       
  3821 	if (aTimeout>=0)
       
  3822 		return Exec::MutexWait(iHandle, aTimeout);
       
  3823 	return KErrArgument;
  3773 	}
  3824 	}
  3774 
  3825 
  3775 
  3826 
  3776 
  3827 
  3777 
  3828 
  4306 	}
  4357 	}
  4307 
  4358 
  4308 
  4359 
  4309 
  4360 
  4310 
  4361 
  4311 EXPORT_C void RSemaphore::Wait()
       
  4312 /**
  4362 /**
  4313 Waits for a signal on the semaphore.
  4363 Waits for a signal on the semaphore.
  4314 
  4364 
  4315 The function decrements the semaphore count by one and returns immediately 
  4365 The function decrements the semaphore count by one and returns immediately 
  4316 if it is zero or positive.
  4366 if it is zero or positive.
  4322 be waiting on a particular semaphore at a time. When there are multiple threads 
  4372 be waiting on a particular semaphore at a time. When there are multiple threads 
  4323 waiting on a semaphore, they are released in priority order.
  4373 waiting on a semaphore, they are released in priority order.
  4324 
  4374 
  4325 If the semaphore is deleted, all threads waiting on that semaphore are released.
  4375 If the semaphore is deleted, all threads waiting on that semaphore are released.
  4326 */
  4376 */
  4327 	{
  4377 EXPORT_C void RSemaphore::Wait()
  4328 
  4378 	{
  4329 	Exec::SemaphoreWait(iHandle, 0);
  4379 	Exec::SemaphoreWait(iHandle, 0);
  4330 	}
  4380 	}
  4331 
  4381 
  4332 
  4382 
  4333 
       
  4334 
       
  4335 EXPORT_C TInt RSemaphore::Wait(TInt aTimeout)
       
  4336 /**
  4383 /**
  4337 Waits for a signal on the semaphore, or a timeout.
  4384 Waits for a signal on the semaphore, or a timeout.
  4338 
  4385 
  4339 @param aTimeout The timeout value in micoseconds
  4386 @param aTimeout The timeout value in microseconds
  4340 
  4387 
  4341 @return KErrNone if the wait has completed normally.
  4388 @return KErrNone if the wait has completed normally.
  4342         KErrTimedOut if the timeout has expired.
  4389         KErrTimedOut if the timeout has expired.
  4343         KErrGeneral if the semaphore is being reset, i.e the semaphore
  4390         KErrGeneral if the semaphore is being reset, i.e the semaphore
  4344         is about to  be deleted.
  4391         is about to  be deleted.
  4345         KErrArgument if aTimeout is negative;
  4392         KErrArgument if aTimeout is negative;
  4346         otherwise one of the other system wide error codes.
  4393         otherwise one of the other system wide error codes.
  4347 */
  4394 */
  4348 	{
  4395 EXPORT_C TInt RSemaphore::Wait(TInt aTimeout)
  4349 
  4396 	{
  4350 	return Exec::SemaphoreWait(iHandle, aTimeout);
  4397 	if (aTimeout>=0)
  4351 	}
  4398 		return Exec::SemaphoreWait(iHandle, aTimeout);
  4352 
  4399 	return KErrArgument;
  4353 
  4400 	}
       
  4401 
       
  4402 
       
  4403 /**
       
  4404 Acquires the semaphore if that is possible without waiting.
       
  4405 
       
  4406 @return KErrNone if the semaphore was acquired successfully
       
  4407         KErrTimedOut if the semaphore could not be acquired
       
  4408         KErrGeneral if the semaphore is being reset, i.e the semaphore
       
  4409         is about to  be deleted.
       
  4410 */
       
  4411 EXPORT_C TInt RSemaphore::Poll()
       
  4412 	{
       
  4413 	return Exec::SemaphoreWait(iHandle, -1);
       
  4414 	}
  4354 
  4415 
  4355 
  4416 
  4356 EXPORT_C void RSemaphore::Signal()
  4417 EXPORT_C void RSemaphore::Signal()
  4357 /**
  4418 /**
  4358 Signals the semaphore once.
  4419 Signals the semaphore once.
  4387 	Exec::SemaphoreSignalN(iHandle,aCount);
  4448 	Exec::SemaphoreSignalN(iHandle,aCount);
  4388 	}
  4449 	}
  4389 
  4450 
  4390 
  4451 
  4391 
  4452 
       
  4453 #ifndef __CPU_ARM
       
  4454 /**
       
  4455 Acquire the lock, if necessary waiting up to a specified maximum amount of time
       
  4456 for it to become free.
       
  4457 
       
  4458 This function checks if the lock is currently held. If not the lock is marked
       
  4459 as held by the current thread and the call returns immediately. If the lock is
       
  4460 held by another thread the current thread will suspend until the lock becomes
       
  4461 free or until the specified timeout period has elapsed.
       
  4462 
       
  4463 @param aTimeout The timeout value in microseconds
       
  4464 
       
  4465 @return KErrNone if the lock was acquired successfully.
       
  4466         KErrTimedOut if the timeout has expired.
       
  4467         KErrGeneral if the lock is being reset, i.e the lock
       
  4468         is about to  be deleted.
       
  4469         KErrArgument if aTimeout is negative;
       
  4470         otherwise one of the other system wide error codes.
       
  4471 */
       
  4472 EXPORT_C TInt RFastLock::Wait(TInt aTimeout)
       
  4473 	{
       
  4474 	if (aTimeout<=0)
       
  4475 		return KErrArgument;
       
  4476 	TInt orig = __e32_atomic_add_acq32(&iCount, TUint32(-1));
       
  4477 	if (orig == 0)
       
  4478 		return KErrNone;
       
  4479 	FOREVER
       
  4480 		{
       
  4481 		TInt r = Exec::SemaphoreWait(iHandle, aTimeout);
       
  4482 		if (r != KErrTimedOut)	// got lock OK or lock deleted
       
  4483 			return r;
       
  4484 		// Before we can return KErrTimedOut we must increment iCount (since we
       
  4485 		// previously decremented it in anticipation of acquiring the lock.
       
  4486 		// However we must not increment iCount if it would become zero, since
       
  4487 		// the semaphore will have been signalled (to counterbalance the Wait()
       
  4488 		// which timed out and thus never happened). This would result in two
       
  4489 		// threads being able to acquire the lock simultaneously - one by
       
  4490 		// decrementing iCount from 0 to -1 without looking at the semaphore,
       
  4491 		// and the other by decrementing iCount from -1 to -2 and then absorbing
       
  4492 		// the spurious semaphore signal.
       
  4493 		orig = __e32_atomic_tas_ord32(&iCount, -1, 0, 1);	// don't release lock completely
       
  4494 		if (orig < -1)
       
  4495 			return KErrTimedOut;	// count corrected - don't need to touch semaphore
       
  4496 		// lock is actually free at this point, try again to claim it
       
  4497 		aTimeout = 1;
       
  4498 		}
       
  4499 	}
       
  4500 #endif
  4392 
  4501 
  4393 EXPORT_C RCriticalSection::RCriticalSection()
  4502 EXPORT_C RCriticalSection::RCriticalSection()
  4394 	: iBlocked(1)
  4503 	: iBlocked(1)
  4395 /**
  4504 /**
  4396 Default constructor.
  4505 Default constructor.