Modernize Thread

- Based on C++11's `thread` and `thread_local`
- No more need to allocate-deallocate or check for null
- No pointer anymore, just a member variable
- Platform-specific implementations no longer needed (except for the few cases of non-portable functions)
- Simpler for `NO_THREADS`
- Thread ids are now the same across platforms (main is 1; others follow)
This commit is contained in:
Pedro J. Estébanez
2021-01-19 13:29:41 +01:00
parent 6ddfc8e718
commit 99fe462452
87 changed files with 385 additions and 1056 deletions

View File

@ -71,7 +71,7 @@ struct _IP_ResolverPrivate {
Mutex mutex;
Semaphore sem;
Thread *thread;
Thread thread;
//Semaphore* semaphore;
bool thread_abort;
@ -141,7 +141,7 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ
} else {
resolver->queue[id].response = IP_Address();
resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
if (resolver->thread) {
if (resolver->thread.is_started()) {
resolver->sem.post();
} else {
resolver->resolve_queues();
@ -285,26 +285,14 @@ IP::IP() {
singleton = this;
resolver = memnew(_IP_ResolverPrivate);
#ifndef NO_THREADS
resolver->thread_abort = false;
resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
#else
resolver->thread = nullptr;
#endif
resolver->thread.start(_IP_ResolverPrivate::_thread_function, resolver);
}
IP::~IP() {
#ifndef NO_THREADS
if (resolver->thread) {
resolver->thread_abort = true;
resolver->sem.post();
Thread::wait_to_finish(resolver->thread);
memdelete(resolver->thread);
}
#endif
resolver->thread_abort = true;
resolver->sem.post();
resolver->thread.wait_to_finish();
memdelete(resolver);
}