Exemplo n.º 1
0
s32 sys_rwlock_trywlock(PPUThread& ppu, u32 rw_lock_id)
{
	sys_rwlock.Log("sys_rwlock_trywlock(rw_lock_id=0x%x)", rw_lock_id);

	LV2_LOCK;

	const auto rwlock = idm::get<lv2_rwlock_t>(rw_lock_id);

	if (!rwlock)
	{
		return CELL_ESRCH;
	}

	if (rwlock->writer.get() == &ppu)
	{
		return CELL_EDEADLK;
	}

	if (rwlock->readers || rwlock->writer || rwlock->wsq.size())
	{
		return CELL_EBUSY;
	}

	rwlock->writer = std::static_pointer_cast<CPUThread>(ppu.shared_from_this());

	return CELL_OK;
}
Exemplo n.º 2
0
s32 sys_mutex_trylock(PPUThread& ppu, u32 mutex_id)
{
	sys_mutex.Log("sys_mutex_trylock(mutex_id=0x%x)", mutex_id);

	LV2_LOCK;

	const auto mutex = idm::get<lv2_mutex_t>(mutex_id);

	if (!mutex)
	{
		return CELL_ESRCH;
	}

	// check current ownership
	if (mutex->owner.get() == &ppu)
	{
		if (mutex->recursive)
		{
			if (mutex->recursive_count == 0xffffffffu)
			{
				return CELL_EKRESOURCE;
			}

			mutex->recursive_count++;

			return CELL_OK;
		}

		return CELL_EDEADLK;
	}

	if (mutex->owner)
	{
		return CELL_EBUSY;
	}

	// own the mutex if free
	mutex->owner = std::static_pointer_cast<CPUThread>(ppu.shared_from_this());

	return CELL_OK;
}
Exemplo n.º 3
0
s32 sys_rwlock_wlock(PPUThread& ppu, u32 rw_lock_id, u64 timeout)
{
	sys_rwlock.Log("sys_rwlock_wlock(rw_lock_id=0x%x, timeout=0x%llx)", rw_lock_id, timeout);

	const u64 start_time = get_system_time();

	LV2_LOCK;

	const auto rwlock = idm::get<lv2_rwlock_t>(rw_lock_id);

	if (!rwlock)
	{
		return CELL_ESRCH;
	}

	if (rwlock->writer.get() == &ppu)
	{
		return CELL_EDEADLK;
	}

	if (!rwlock->readers && !rwlock->writer)
	{
		rwlock->writer = std::static_pointer_cast<CPUThread>(ppu.shared_from_this());

		return CELL_OK;
	}

	// add waiter; protocol is ignored in current implementation
	sleep_queue_entry_t waiter(ppu, rwlock->wsq);

	while (!ppu.unsignal())
	{
		CHECK_EMU_STATUS;

		if (timeout)
		{
			const u64 passed = get_system_time() - start_time;

			if (passed >= timeout)
			{
				// if the last waiter quit the writer sleep queue, readers must acquire the lock
				if (!rwlock->writer && rwlock->wsq.size() == 1)
				{
					if (rwlock->wsq.front().get() != &ppu)
					{
						throw EXCEPTION("Unexpected");
					}

					rwlock->wsq.clear();
					rwlock->notify_all(lv2_lock);
				}

				return CELL_ETIMEDOUT;
			}

			ppu.cv.wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
		}
		else
		{
			ppu.cv.wait(lv2_lock);
		}
	}

	if (rwlock->readers || rwlock->writer.get() != &ppu)
	{
		throw EXCEPTION("Unexpected");
	}

	return CELL_OK;
}
Exemplo n.º 4
0
s32 sys_mutex_lock(PPUThread& ppu, u32 mutex_id, u64 timeout)
{
	sys_mutex.Log("sys_mutex_lock(mutex_id=0x%x, timeout=0x%llx)", mutex_id, timeout);

	const u64 start_time = get_system_time();

	LV2_LOCK;

	const auto mutex = idm::get<lv2_mutex_t>(mutex_id);

	if (!mutex)
	{
		return CELL_ESRCH;
	}

	// check current ownership
	if (mutex->owner.get() == &ppu)
	{
		if (mutex->recursive)
		{
			if (mutex->recursive_count == 0xffffffffu)
			{
				return CELL_EKRESOURCE;
			}

			mutex->recursive_count++;

			return CELL_OK;
		}

		return CELL_EDEADLK;
	}

	// lock immediately if not locked
	if (!mutex->owner)
	{
		mutex->owner = std::static_pointer_cast<CPUThread>(ppu.shared_from_this());

		return CELL_OK;
	}

	// add waiter; protocol is ignored in current implementation
	sleep_queue_entry_t waiter(ppu, mutex->sq);

	while (!ppu.unsignal())
	{
		CHECK_EMU_STATUS;

		if (timeout)
		{
			const u64 passed = get_system_time() - start_time;

			if (passed >= timeout)
			{
				return CELL_ETIMEDOUT;
			}

			ppu.cv.wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
		}
		else
		{
			ppu.cv.wait(lv2_lock);
		}
	}

	// new owner must be set when unlocked
	if (mutex->owner.get() != &ppu)
	{
		throw EXCEPTION("Unexpected mutex owner");
	}

	return CELL_OK;
}
Exemplo n.º 5
0
s32 sys_cond_wait(PPUThread& ppu, u32 cond_id, u64 timeout)
{
	sys_cond.Log("sys_cond_wait(cond_id=0x%x, timeout=%lld)", cond_id, timeout);

	const u64 start_time = get_system_time();

	LV2_LOCK;

	const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);

	if (!cond)
	{
		return CELL_ESRCH;
	}

	// check current ownership
	if (cond->mutex->owner.get() != &ppu)
	{
		return CELL_EPERM;
	}

	// save the recursive value
	const u32 recursive_value = cond->mutex->recursive_count.exchange(0);

	// unlock the mutex
	cond->mutex->unlock(lv2_lock);

	// add waiter; protocol is ignored in current implementation
	sleep_queue_entry_t waiter(ppu, cond->sq);

	// potential mutex waiter (not added immediately)
	sleep_queue_entry_t mutex_waiter(ppu, cond->mutex->sq, defer_sleep);

	while (!ppu.unsignal())
	{
		CHECK_EMU_STATUS;

		// timeout is ignored if waiting on the cond var is already dropped
		if (timeout && waiter)
		{
			const u64 passed = get_system_time() - start_time;

			if (passed >= timeout)
			{
				// try to reown mutex and exit if timed out
				if (!cond->mutex->owner)
				{
					cond->mutex->owner = ppu.shared_from_this();
					break;
				}

				// drop condition variable and start waiting on the mutex queue
				mutex_waiter.enter();
				waiter.leave();
				continue;
			}

			ppu.cv.wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
		}
		else
		{
			ppu.cv.wait(lv2_lock);
		}
	}

	// mutex owner is restored after notification or unlocking
	if (cond->mutex->owner.get() != &ppu)
	{
		throw EXCEPTION("Unexpected mutex owner");
	}

	// restore the recursive value
	cond->mutex->recursive_count = recursive_value;

	// check timeout (unclear)
	if (timeout && get_system_time() - start_time > timeout)
	{
		return CELL_ETIMEDOUT;
	}

	return CELL_OK;
}