File diff r23496:661d21df67d7 → r23497:a0ab44ebd2fa
src/thread/thread_pthread.cpp
Show inline comments
 
@@ -42,20 +42,20 @@ public:
 
		self_destruct(self_destruct),
 
		name(name)
 
	{
 
		pthread_create(&this->thread, NULL, &stThreadProc, this);
 
	}
 

	
 
	/* virtual */ bool Exit()
 
	bool Exit() override
 
	{
 
		assert(pthread_self() == this->thread);
 
		/* For now we terminate by throwing an error, gives much cleaner cleanup */
 
		throw OTTDThreadExitSignal();
 
	}
 

	
 
	/* virtual */ void Join()
 
	void Join() override
 
	{
 
		/* You cannot join yourself */
 
		assert(pthread_self() != this->thread);
 
		pthread_join(this->thread, NULL);
 
		this->thread = 0;
 
	}
 
@@ -126,26 +126,26 @@ public:
 
		pthread_mutexattr_init(&this->attr);
 
		pthread_mutexattr_settype(&this->attr, PTHREAD_MUTEX_ERRORCHECK);
 
		pthread_mutex_init(&this->mutex, &this->attr);
 
		pthread_cond_init(&this->condition, NULL);
 
	}
 

	
 
	/* virtual */ ~ThreadMutex_pthread()
 
	~ThreadMutex_pthread() override
 
	{
 
		int err = pthread_cond_destroy(&this->condition);
 
		assert(err != EBUSY);
 
		err = pthread_mutex_destroy(&this->mutex);
 
		assert(err != EBUSY);
 
	}
 

	
 
	bool IsOwnedByCurrentThread() const
 
	{
 
		return this->owner == pthread_self();
 
	}
 

	
 
	/* virtual */ void BeginCritical(bool allow_recursive = false)
 
	void BeginCritical(bool allow_recursive = false) override
 
	{
 
		/* pthread mutex is not recursive by itself */
 
		if (this->IsOwnedByCurrentThread()) {
 
			if (!allow_recursive) NOT_REACHED();
 
		} else {
 
			int err = pthread_mutex_lock(&this->mutex);
 
@@ -153,35 +153,35 @@ public:
 
			assert(this->recursive_count == 0);
 
			this->owner = pthread_self();
 
		}
 
		this->recursive_count++;
 
	}
 

	
 
	/* virtual */ void EndCritical(bool allow_recursive = false)
 
	void EndCritical(bool allow_recursive = false) override
 
	{
 
		assert(this->IsOwnedByCurrentThread());
 
		if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
 
		this->recursive_count--;
 
		if (this->recursive_count != 0) return;
 
		this->owner = 0;
 
		int err = pthread_mutex_unlock(&this->mutex);
 
		assert(err == 0);
 
	}
 

	
 
	/* virtual */ void WaitForSignal()
 
	void WaitForSignal() override
 
	{
 
		uint old_recursive_count = this->recursive_count;
 
		this->recursive_count = 0;
 
		this->owner = 0;
 
		int err = pthread_cond_wait(&this->condition, &this->mutex);
 
		assert(err == 0);
 
		this->owner = pthread_self();
 
		this->recursive_count = old_recursive_count;
 
	}
 

	
 
	/* virtual */ void SendSignal()
 
	void SendSignal() override
 
	{
 
		int err = pthread_cond_signal(&this->condition);
 
		assert(err == 0);
 
	}
 
};