Modernize Semaphore

- Based on C++11's `mutex` and `condition_variable`
- No more need to allocate-deallocate or check for null
- No pointer anymore, just a member variable
- Platform-specific implementations no longer needed
- Simpler for `NO_THREADS`
This commit is contained in:
Pedro J. Estébanez 2021-01-27 13:41:10 +01:00
parent 4ddcdc031b
commit 8f6a636ae7
37 changed files with 99 additions and 836 deletions

View file

@ -2689,12 +2689,14 @@ void _Marshalls::_bind_methods() {
Error _Semaphore::wait() {
return semaphore->wait();
semaphore.wait();
return OK; // Can't fail anymore; keep compat
}
Error _Semaphore::post() {
return semaphore->post();
semaphore.post();
return OK; // Can't fail anymore; keep compat
}
void _Semaphore::_bind_methods() {
@ -2703,16 +2705,6 @@ void _Semaphore::_bind_methods() {
ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
}
_Semaphore::_Semaphore() {
semaphore = Semaphore::create();
}
_Semaphore::~_Semaphore() {
memdelete(semaphore);
}
///////////////
void _Mutex::lock() {

View file

@ -665,16 +665,13 @@ public:
class _Semaphore : public Reference {
GDCLASS(_Semaphore, Reference);
Semaphore *semaphore;
Semaphore semaphore;
static void _bind_methods();
public:
Error wait();
Error post();
_Semaphore();
~_Semaphore();
};
class _Thread : public Reference {

View file

@ -113,11 +113,10 @@ CommandQueueMT::CommandQueueMT(bool p_sync) {
for (int i = 0; i < SYNC_SEMAPHORES; i++) {
sync_sems[i].sem = Semaphore::create();
sync_sems[i].in_use = false;
}
if (p_sync) {
sync = Semaphore::create();
sync = memnew(Semaphore);
} else {
sync = NULL;
}
@ -127,9 +126,5 @@ CommandQueueMT::~CommandQueueMT() {
if (sync)
memdelete(sync);
for (int i = 0; i < SYNC_SEMAPHORES; i++) {
memdelete(sync_sems[i].sem);
}
memfree(command_mem);
}

View file

@ -250,7 +250,7 @@
cmd->sync_sem = ss; \
unlock(); \
if (sync) sync->post(); \
ss->sem->wait(); \
ss->sem.wait(); \
ss->in_use = false; \
}
@ -267,7 +267,7 @@
cmd->sync_sem = ss; \
unlock(); \
if (sync) sync->post(); \
ss->sem->wait(); \
ss->sem.wait(); \
ss->in_use = false; \
}
@ -277,7 +277,7 @@ class CommandQueueMT {
struct SyncSemaphore {
Semaphore *sem;
Semaphore sem;
bool in_use;
};
@ -293,7 +293,7 @@ class CommandQueueMT {
SyncSemaphore *sync_sem;
virtual void post() {
sync_sem->sem->post();
sync_sem->sem.post();
}
};

View file

@ -88,9 +88,7 @@ void FileAccessNetworkClient::_thread_func() {
while (!quit) {
DEBUG_PRINT("SEM WAIT - " + itos(sem->get()));
Error err = sem->wait();
if (err != OK)
ERR_PRINT("sem->wait() failed");
sem.wait();
DEBUG_TIME("sem_unlock");
//DEBUG_PRINT("semwait returned "+itos(werr));
DEBUG_PRINT("MUTEX LOCK " + itos(lockcount));
@ -140,7 +138,7 @@ void FileAccessNetworkClient::_thread_func() {
fa->_respond(len, Error(status));
}
fa->sem->post();
fa->sem.post();
} break;
case FileAccessNetwork::RESPONSE_DATA: {
@ -160,14 +158,14 @@ void FileAccessNetworkClient::_thread_func() {
int status = get_32();
fa->exists_modtime = status != 0;
fa->sem->post();
fa->sem.post();
} break;
case FileAccessNetwork::RESPONSE_GET_MODTIME: {
uint64_t status = get_64();
fa->exists_modtime = status;
fa->sem->post();
fa->sem.post();
} break;
}
@ -229,7 +227,6 @@ FileAccessNetworkClient::FileAccessNetworkClient() {
singleton = this;
last_id = 0;
client.instance();
sem = Semaphore::create();
lockcount = 0;
}
@ -237,12 +234,10 @@ FileAccessNetworkClient::~FileAccessNetworkClient() {
if (thread) {
quit = true;
sem->post();
sem.post();
Thread::wait_to_finish(thread);
memdelete(thread);
}
memdelete(sem);
}
void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) {
@ -262,7 +257,7 @@ void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block)
if (waiting_on_page == page) {
waiting_on_page = -1;
page_sem->post();
page_sem.post();
}
}
@ -304,9 +299,9 @@ Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) {
nc->unlock_mutex();
DEBUG_PRINT("OPEN POST");
DEBUG_TIME("open_post");
nc->sem->post(); //awaiting answer
nc->sem.post(); //awaiting answer
DEBUG_PRINT("WAIT...");
sem->wait();
sem.wait();
DEBUG_TIME("open_end");
DEBUG_PRINT("WAIT ENDED...");
@ -390,7 +385,7 @@ void FileAccessNetwork::_queue_page(int p_page) const {
pages.write[p_page].queued = true;
nc->blockrequest_mutex.unlock();
DEBUG_PRINT("QUEUE PAGE POST");
nc->sem->post();
nc->sem.post();
DEBUG_PRINT("queued " + itos(p_page));
}
}
@ -423,7 +418,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const {
}
buffer_mutex.unlock();
DEBUG_PRINT("wait");
page_sem->wait();
page_sem.wait();
DEBUG_PRINT("done");
} else {
@ -472,8 +467,8 @@ bool FileAccessNetwork::file_exists(const String &p_path) {
nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
nc->unlock_mutex();
DEBUG_PRINT("FILE EXISTS POST");
nc->sem->post();
sem->wait();
nc->sem.post();
sem.wait();
return exists_modtime != 0;
}
@ -489,8 +484,8 @@ uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) {
nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
nc->unlock_mutex();
DEBUG_PRINT("MODTIME POST");
nc->sem->post();
sem->wait();
nc->sem.post();
sem.wait();
return exists_modtime;
}
@ -518,8 +513,6 @@ FileAccessNetwork::FileAccessNetwork() {
eof_flag = false;
opened = false;
pos = 0;
sem = Semaphore::create();
page_sem = Semaphore::create();
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex();
id = nc->last_id++;
@ -535,8 +528,6 @@ FileAccessNetwork::FileAccessNetwork() {
FileAccessNetwork::~FileAccessNetwork() {
close();
memdelete(sem);
memdelete(page_sem);
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
nc->lock_mutex();

View file

@ -49,7 +49,7 @@ class FileAccessNetworkClient {
List<BlockRequest> block_requests;
Semaphore *sem;
Semaphore sem;
Thread *thread;
bool quit;
Mutex mutex;
@ -85,8 +85,8 @@ public:
class FileAccessNetwork : public FileAccess {
Semaphore *sem;
Semaphore *page_sem;
Semaphore sem;
Semaphore page_sem;
Mutex buffer_mutex;
bool opened;
size_t total_size;

View file

@ -71,7 +71,7 @@ struct _IP_ResolverPrivate {
}
Mutex mutex;
Semaphore *sem;
Semaphore sem;
Thread *thread;
//Semaphore* semaphore;
@ -98,7 +98,7 @@ struct _IP_ResolverPrivate {
while (!ipr->thread_abort) {
ipr->sem->wait();
ipr->sem.wait();
ipr->mutex.lock();
ipr->resolve_queues();
@ -152,7 +152,7 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ
resolver->queue[id].response = IP_Address();
resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
if (resolver->thread)
resolver->sem->post();
resolver->sem.post();
else
resolver->resolve_queues();
}
@ -314,23 +314,11 @@ IP::IP() {
singleton = this;
resolver = memnew(_IP_ResolverPrivate);
resolver->sem = NULL;
#ifndef NO_THREADS
resolver->sem = Semaphore::create();
if (resolver->sem) {
resolver->thread_abort = false;
resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
if (!resolver->thread)
memdelete(resolver->sem); //wtf
} else {
resolver->thread = NULL;
}
resolver->thread_abort = false;
resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
#else
resolver->sem = NULL;
resolver->thread = NULL;
#endif
}
@ -340,10 +328,9 @@ IP::~IP() {
#ifndef NO_THREADS
if (resolver->thread) {
resolver->thread_abort = true;
resolver->sem->post();
resolver->sem.post();
Thread::wait_to_finish(resolver->thread);
memdelete(resolver->thread);
memdelete(resolver->sem);
}
#endif

View file

@ -1,45 +0,0 @@
/*************************************************************************/
/* semaphore.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "semaphore.h"
#include "core/error_macros.h"
Semaphore *(*Semaphore::create_func)() = 0;
Semaphore *Semaphore::create() {
ERR_FAIL_COND_V(!create_func, 0);
return create_func();
}
Semaphore::~Semaphore() {
}

View file

@ -32,19 +32,59 @@
#define SEMAPHORE_H
#include "core/error_list.h"
#include "core/typedefs.h"
#if !defined(NO_THREADS)
#include <condition_variable>
#include <mutex>
class Semaphore {
protected:
static Semaphore *(*create_func)();
private:
mutable std::mutex mutex_;
mutable std::condition_variable condition_;
mutable unsigned long count_ = 0; // Initialized as locked.
public:
virtual Error wait() = 0; ///< wait until semaphore has positive value, then decrement and pass
virtual Error post() = 0; ///< unlock the semaphore, incrementing the value
virtual int get() const = 0; ///< get semaphore value
_ALWAYS_INLINE_ void post() const {
std::lock_guard<decltype(mutex_)> lock(mutex_);
++count_;
condition_.notify_one();
}
static Semaphore *create(); ///< Create a mutex
_ALWAYS_INLINE_ void wait() const {
std::unique_lock<decltype(mutex_)> lock(mutex_);
while (!count_) { // Handle spurious wake-ups.
condition_.wait(lock);
}
--count_;
}
virtual ~Semaphore();
_ALWAYS_INLINE_ bool try_wait() const {
std::lock_guard<decltype(mutex_)> lock(mutex_);
if (count_) {
--count_;
return true;
}
return false;
}
_ALWAYS_INLINE_ int get() const {
std::lock_guard<decltype(mutex_)> lock(mutex_);
return count_;
}
};
#else
class Semaphore {
public:
_ALWAYS_INLINE_ void post() const {}
_ALWAYS_INLINE_ void wait() const {}
_ALWAYS_INLINE_ bool try_wait() const { return true; }
_ALWAYS_INLINE_ int get() const { return 1; }
};
#endif
#endif // SEMAPHORE_H

View file

@ -39,11 +39,3 @@ Thread *ThreadDummy::create(ThreadCreateCallback p_callback, void *p_user, const
void ThreadDummy::make_default() {
Thread::create_func = &ThreadDummy::create;
};
Semaphore *SemaphoreDummy::create() {
return memnew(SemaphoreDummy);
};
void SemaphoreDummy::make_default() {
Semaphore::create_func = &SemaphoreDummy::create;
};

View file

@ -45,16 +45,4 @@ public:
static void make_default();
};
class SemaphoreDummy : public Semaphore {
static Semaphore *create();
public:
virtual Error wait() { return OK; };
virtual Error post() { return OK; };
virtual int get() const { return 0; }; ///< get semaphore value
static void make_default();
};
#endif

View file

@ -37,7 +37,6 @@
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "drivers/unix/net_socket_posix.h"
#include "drivers/unix/semaphore_posix.h"
#include "drivers/unix/thread_posix.h"
#include "servers/visual_server.h"
@ -121,12 +120,8 @@ void OS_Unix::initialize_core() {
#ifdef NO_THREADS
ThreadDummy::make_default();
SemaphoreDummy::make_default();
#else
ThreadPosix::make_default();
#if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
SemaphorePosix::make_default();
#endif
#endif
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);

View file

@ -1,87 +0,0 @@
/*************************************************************************/
/* semaphore_posix.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "semaphore_posix.h"
#if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
#include "core/os/memory.h"
#include <errno.h>
#include <stdio.h>
Error SemaphorePosix::wait() {
while (sem_wait(&sem)) {
if (errno == EINTR) {
errno = 0;
continue;
} else {
perror("sem waiting");
return ERR_BUSY;
}
}
return OK;
}
Error SemaphorePosix::post() {
return (sem_post(&sem) == 0) ? OK : ERR_BUSY;
}
int SemaphorePosix::get() const {
int val;
sem_getvalue(&sem, &val);
return val;
}
Semaphore *SemaphorePosix::create_semaphore_posix() {
return memnew(SemaphorePosix);
}
void SemaphorePosix::make_default() {
create_func = create_semaphore_posix;
}
SemaphorePosix::SemaphorePosix() {
int r = sem_init(&sem, 0, 0);
if (r != 0)
perror("sem creating");
}
SemaphorePosix::~SemaphorePosix() {
sem_destroy(&sem);
}
#endif

View file

@ -1,58 +0,0 @@
/*************************************************************************/
/* semaphore_posix.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef SEMAPHORE_POSIX_H
#define SEMAPHORE_POSIX_H
#include "core/os/semaphore.h"
#if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
#include <semaphore.h>
class SemaphorePosix : public Semaphore {
mutable sem_t sem;
static Semaphore *create_semaphore_posix();
public:
virtual Error wait();
virtual Error post();
virtual int get() const;
static void make_default();
SemaphorePosix();
~SemaphorePosix();
};
#endif
#endif

View file

@ -1,98 +0,0 @@
/*************************************************************************/
/* semaphore_windows.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "semaphore_windows.h"
#if defined(WINDOWS_ENABLED)
#include "core/os/memory.h"
Error SemaphoreWindows::wait() {
WaitForSingleObjectEx(semaphore, INFINITE, false);
return OK;
}
Error SemaphoreWindows::post() {
ReleaseSemaphore(semaphore, 1, NULL);
return OK;
}
int SemaphoreWindows::get() const {
long previous;
switch (WaitForSingleObjectEx(semaphore, 0, false)) {
case WAIT_OBJECT_0: {
ERR_FAIL_COND_V(!ReleaseSemaphore(semaphore, 1, &previous), -1);
return previous + 1;
} break;
case WAIT_TIMEOUT: {
return 0;
} break;
default: {
}
}
ERR_FAIL_V(-1);
}
Semaphore *SemaphoreWindows::create_semaphore_windows() {
return memnew(SemaphoreWindows);
}
void SemaphoreWindows::make_default() {
create_func = create_semaphore_windows;
}
SemaphoreWindows::SemaphoreWindows() {
#ifdef UWP_ENABLED
semaphore = CreateSemaphoreEx(
NULL,
0,
0xFFFFFFF, //wathever
NULL,
0,
SEMAPHORE_ALL_ACCESS);
#else
semaphore = CreateSemaphore(
NULL,
0,
0xFFFFFFF, //wathever
NULL);
#endif
}
SemaphoreWindows::~SemaphoreWindows() {
CloseHandle(semaphore);
}
#endif

View file

@ -1,58 +0,0 @@
/*************************************************************************/
/* semaphore_windows.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef SEMAPHORE_WINDOWS_H
#define SEMAPHORE_WINDOWS_H
#include "core/os/semaphore.h"
#ifdef WINDOWS_ENABLED
#include <windows.h>
class SemaphoreWindows : public Semaphore {
mutable HANDLE semaphore;
static Semaphore *create_semaphore_windows();
public:
virtual Error wait();
virtual Error post();
virtual int get() const;
static void make_default();
SemaphoreWindows();
~SemaphoreWindows();
};
#endif
#endif

View file

@ -220,7 +220,7 @@ void EditorResourcePreview::_thread() {
exited = false;
while (!exit) {
preview_sem->wait();
preview_sem.wait();
preview_mutex.lock();
if (queue.size()) {
@ -381,7 +381,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
queue.push_back(item);
preview_mutex.unlock();
preview_sem->post();
preview_sem.post();
}
void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
@ -403,7 +403,7 @@ void EditorResourcePreview::queue_resource_preview(const String &p_path, Object
queue.push_back(item);
preview_mutex.unlock();
preview_sem->post();
preview_sem.post();
}
void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
@ -463,7 +463,7 @@ void EditorResourcePreview::start() {
void EditorResourcePreview::stop() {
if (thread) {
exit = true;
preview_sem->post();
preview_sem.post();
while (!exited) {
OS::get_singleton()->delay_usec(10000);
VisualServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
@ -477,7 +477,6 @@ void EditorResourcePreview::stop() {
EditorResourcePreview::EditorResourcePreview() {
thread = NULL;
singleton = this;
preview_sem = Semaphore::create();
order = 0;
exit = false;
exited = false;
@ -486,5 +485,4 @@ EditorResourcePreview::EditorResourcePreview() {
EditorResourcePreview::~EditorResourcePreview() {
stop();
memdelete(preview_sem);
}

View file

@ -71,7 +71,7 @@ class EditorResourcePreview : public Node {
List<QueueItem> queue;
Mutex preview_mutex;
Semaphore *preview_sem;
Semaphore preview_sem;
Thread *thread;
volatile bool exit;
volatile bool exited;

View file

@ -44,7 +44,7 @@ int VideoStreamPlaybackTheora::buffer_data() {
int read;
do {
thread_sem->post();
thread_sem.post();
read = MIN(ring_buffer.data_left(), 4096);
if (read) {
ring_buffer.read((uint8_t *)buffer, read);
@ -141,7 +141,7 @@ void VideoStreamPlaybackTheora::clear() {
#ifdef THEORA_USE_THREAD_STREAMING
thread_exit = true;
thread_sem->post(); //just in case
thread_sem.post(); //just in case
Thread::wait_to_finish(thread);
memdelete(thread);
thread = NULL;
@ -385,7 +385,7 @@ void VideoStreamPlaybackTheora::update(float p_delta) {
};
#ifdef THEORA_USE_THREAD_STREAMING
thread_sem->post();
thread_sem.post();
#endif
time += p_delta;
@ -664,7 +664,7 @@ void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
}
}
vs->thread_sem->wait();
vs->thread_sem.wait();
}
}
@ -693,7 +693,6 @@ VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
int rb_power = nearest_shift(RB_SIZE_KB * 1024);
ring_buffer.resize(rb_power);
read_buffer.resize(RB_SIZE_KB * 1024);
thread_sem = Semaphore::create();
thread = NULL;
thread_exit = false;
thread_eof = false;
@ -703,10 +702,6 @@ VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
#ifdef THEORA_USE_THREAD_STREAMING
memdelete(thread_sem);
#endif
clear();
if (file)

View file

@ -112,7 +112,7 @@ class VideoStreamPlaybackTheora : public VideoStreamPlayback {
RingBuffer<uint8_t> ring_buffer;
Vector<uint8_t> read_buffer;
bool thread_eof;
Semaphore *thread_sem;
Semaphore thread_sem;
Thread *thread;
volatile bool thread_exit;

View file

@ -3,7 +3,6 @@
Import("env")
iphone_lib = [
"semaphore_iphone.cpp",
"godot_iphone.mm",
"os_iphone.mm",
"main.m",

View file

@ -45,8 +45,6 @@
#include "core/project_settings.h"
#include "drivers/unix/syslog_logger.h"
#include "semaphore_iphone.h"
#import "app_delegate.h"
#import "device_metrics.h"
#import "godot_view.h"
@ -104,7 +102,6 @@ String OSIPhone::get_unique_id() const {
void OSIPhone::initialize_core() {
OS_Unix::initialize_core();
SemaphoreIphone::make_default();
set_data_dir(data_dir);
};

View file

@ -1,112 +0,0 @@
/*************************************************************************/
/* semaphore_iphone.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "semaphore_iphone.h"
#include <fcntl.h>
#include <unistd.h>
void cgsem_init(cgsem_t *);
void cgsem_post(cgsem_t *);
void cgsem_wait(cgsem_t *);
void cgsem_destroy(cgsem_t *);
void cgsem_init(cgsem_t *cgsem) {
int flags, fd, i;
pipe(cgsem->pipefd);
/* Make the pipes FD_CLOEXEC to allow them to close should we call
* execv on restart. */
for (i = 0; i < 2; i++) {
fd = cgsem->pipefd[i];
flags = fcntl(fd, F_GETFD, 0);
flags |= FD_CLOEXEC;
fcntl(fd, F_SETFD, flags);
}
}
void cgsem_post(cgsem_t *cgsem) {
const char buf = 1;
write(cgsem->pipefd[1], &buf, 1);
}
void cgsem_wait(cgsem_t *cgsem) {
char buf;
read(cgsem->pipefd[0], &buf, 1);
}
void cgsem_destroy(cgsem_t *cgsem) {
close(cgsem->pipefd[1]);
close(cgsem->pipefd[0]);
}
#include "core/os/memory.h"
#include <errno.h>
Error SemaphoreIphone::wait() {
cgsem_wait(&sem);
return OK;
}
Error SemaphoreIphone::post() {
cgsem_post(&sem);
return OK;
}
int SemaphoreIphone::get() const {
return 0;
}
Semaphore *SemaphoreIphone::create_semaphore_iphone() {
return memnew(SemaphoreIphone);
}
void SemaphoreIphone::make_default() {
create_func = create_semaphore_iphone;
}
SemaphoreIphone::SemaphoreIphone() {
cgsem_init(&sem);
}
SemaphoreIphone::~SemaphoreIphone() {
cgsem_destroy(&sem);
}

View file

@ -1,59 +0,0 @@
/*************************************************************************/
/* semaphore_iphone.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef SEMAPHORE_IPHONE_H
#define SEMAPHORE_IPHONE_H
struct cgsem {
int pipefd[2];
};
typedef struct cgsem cgsem_t;
#include "core/os/semaphore.h"
class SemaphoreIphone : public Semaphore {
mutable cgsem_t sem;
static Semaphore *create_semaphore_iphone();
public:
virtual Error wait();
virtual Error post();
virtual int get() const;
static void make_default();
SemaphoreIphone();
~SemaphoreIphone();
};
#endif // SEMAPHORE_IPHONE_H

View file

@ -9,7 +9,6 @@ files = [
"crash_handler_osx.mm",
"os_osx.mm",
"godot_main_osx.mm",
"semaphore_osx.cpp",
"dir_access_osx.mm",
"joypad_osx.cpp",
"power_osx.cpp",

View file

@ -38,7 +38,6 @@
#include "drivers/gles2/rasterizer_gles2.h"
#include "drivers/gles3/rasterizer_gles3.h"
#include "main/main.h"
#include "semaphore_osx.h"
#include "servers/visual/visual_server_raster.h"
#include <mach-o/dyld.h>
@ -1527,8 +1526,6 @@ void OS_OSX::initialize_core() {
DirAccess::make_default<DirAccessOSX>(DirAccess::ACCESS_RESOURCES);
DirAccess::make_default<DirAccessOSX>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessOSX>(DirAccess::ACCESS_FILESYSTEM);
SemaphoreOSX::make_default();
}
struct LayoutInfo {

View file

@ -1,107 +0,0 @@
/*************************************************************************/
/* semaphore_osx.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "semaphore_osx.h"
#include <fcntl.h>
#include <unistd.h>
void cgsem_init(cgsem_t *cgsem) {
int flags, fd, i;
pipe(cgsem->pipefd);
/* Make the pipes FD_CLOEXEC to allow them to close should we call
* execv on restart. */
for (i = 0; i < 2; i++) {
fd = cgsem->pipefd[i];
flags = fcntl(fd, F_GETFD, 0);
flags |= FD_CLOEXEC;
fcntl(fd, F_SETFD, flags);
}
}
void cgsem_post(cgsem_t *cgsem) {
const char buf = 1;
write(cgsem->pipefd[1], &buf, 1);
}
void cgsem_wait(cgsem_t *cgsem) {
char buf;
read(cgsem->pipefd[0], &buf, 1);
}
void cgsem_destroy(cgsem_t *cgsem) {
close(cgsem->pipefd[1]);
close(cgsem->pipefd[0]);
}
#include "core/os/memory.h"
#include <errno.h>
Error SemaphoreOSX::wait() {
cgsem_wait(&sem);
return OK;
}
Error SemaphoreOSX::post() {
cgsem_post(&sem);
return OK;
}
int SemaphoreOSX::get() const {
return 0;
}
Semaphore *SemaphoreOSX::create_semaphore_osx() {
return memnew(SemaphoreOSX);
}
void SemaphoreOSX::make_default() {
create_func = create_semaphore_osx;
}
SemaphoreOSX::SemaphoreOSX() {
cgsem_init(&sem);
}
SemaphoreOSX::~SemaphoreOSX() {
cgsem_destroy(&sem);
}

View file

@ -1,59 +0,0 @@
/*************************************************************************/
/* semaphore_osx.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef SEMAPHORE_OSX_H
#define SEMAPHORE_OSX_H
struct cgsem {
int pipefd[2];
};
typedef struct cgsem cgsem_t;
#include "core/os/semaphore.h"
class SemaphoreOSX : public Semaphore {
mutable cgsem_t sem;
static Semaphore *create_semaphore_osx();
public:
virtual Error wait();
virtual Error post();
virtual int get() const;
static void make_default();
SemaphoreOSX();
~SemaphoreOSX();
};
#endif // SEMAPHORE_OSX_H

View file

@ -11,7 +11,6 @@ common_server = [
if sys.platform == "darwin":
common_server.append("#platform/osx/crash_handler_osx.mm")
common_server.append("#platform/osx/power_osx.cpp")
common_server.append("#platform/osx/semaphore_osx.cpp")
else:
common_server.append("#platform/x11/crash_handler_x11.cpp")
common_server.append("#platform/x11/power_x11.cpp")

View file

@ -68,10 +68,6 @@ void OS_Server::initialize_core() {
crash_handler.initialize();
OS_Unix::initialize_core();
#ifdef __APPLE__
SemaphoreOSX::make_default();
#endif
}
Error OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {

View file

@ -37,7 +37,6 @@
#ifdef __APPLE__
#include "platform/osx/crash_handler_osx.h"
#include "platform/osx/power_osx.h"
#include "platform/osx/semaphore_osx.h"
#else
#include "platform/x11/crash_handler_x11.h"
#include "platform/x11/power_x11.h"

View file

@ -40,7 +40,6 @@
#include "drivers/unix/ip_unix.h"
#include "drivers/windows/dir_access_windows.h"
#include "drivers/windows/file_access_windows.h"
#include "drivers/windows/semaphore_windows.h"
#include "main/main.h"
#include "platform/windows/windows_terminal_logger.h"
#include "servers/audio_server.h"
@ -139,7 +138,6 @@ void OS_UWP::initialize_core() {
//RedirectIOToConsole();
ThreadUWP::make_default();
SemaphoreWindows::make_default();
FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES);
FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA);

View file

@ -40,7 +40,6 @@
#include "drivers/gles3/rasterizer_gles3.h"
#include "drivers/windows/dir_access_windows.h"
#include "drivers/windows/file_access_windows.h"
#include "drivers/windows/semaphore_windows.h"
#include "drivers/windows/thread_windows.h"
#include "joypad_windows.h"
#include "lang_table.h"
@ -226,7 +225,6 @@ void OS_Windows::initialize_core() {
borderless = false;
ThreadWindows::make_default();
SemaphoreWindows::make_default();
FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES);
FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA);

View file

@ -40,7 +40,7 @@ void Physics2DServerWrapMT::thread_exit() {
void Physics2DServerWrapMT::thread_step(real_t p_delta) {
physics_2d_server->step(p_delta);
step_sem->post();
step_sem.post();
}
void Physics2DServerWrapMT::_thread_callback(void *_instance) {
@ -84,12 +84,13 @@ void Physics2DServerWrapMT::step(real_t p_step) {
void Physics2DServerWrapMT::sync() {
if (step_sem) {
if (create_thread) {
if (first_frame)
first_frame = false;
else
step_sem->wait(); //must not wait if a step was not issued
step_sem.wait(); //must not wait if a step was not issued
}
physics_2d_server->sync();
}
@ -107,7 +108,6 @@ void Physics2DServerWrapMT::init() {
if (create_thread) {
step_sem = Semaphore::create();
//OS::get_singleton()->release_rendering_thread();
if (create_thread) {
thread = Thread::create(_thread_callback, this);
@ -146,9 +146,6 @@ void Physics2DServerWrapMT::finish() {
space_free_cached_ids();
area_free_cached_ids();
body_free_cached_ids();
if (step_sem)
memdelete(step_sem);
}
Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
@ -157,7 +154,6 @@ Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool
physics_2d_server = p_contained;
create_thread = p_create_thread;
thread = NULL;
step_sem = NULL;
step_pending = 0;
step_thread_up = false;

View file

@ -58,7 +58,7 @@ class Physics2DServerWrapMT : public Physics2DServer {
volatile bool step_thread_up;
bool create_thread;
Semaphore *step_sem;
Semaphore step_sem;
int step_pending;
void thread_step(real_t p_delta);
void thread_flush();

View file

@ -2711,7 +2711,7 @@ void VisualServerScene::_gi_probe_bake_thread() {
while (true) {
probe_bake_sem->wait();
probe_bake_sem.wait();
if (probe_bake_thread_exit) {
break;
}
@ -3410,7 +3410,7 @@ void VisualServerScene::render_probes() {
probe->dynamic.updating_stage = GI_UPDATE_STAGE_LIGHTING;
probe_bake_list.push_back(instance_probe);
probe_bake_mutex.unlock();
probe_bake_sem->post();
probe_bake_sem.post();
#else
@ -3683,7 +3683,6 @@ VisualServerScene *VisualServerScene::singleton = NULL;
VisualServerScene::VisualServerScene() {
#ifndef NO_THREADS
probe_bake_sem = Semaphore::create();
probe_bake_thread = Thread::create(_gi_probe_bake_threads, this);
probe_bake_thread_exit = false;
#endif
@ -3697,9 +3696,8 @@ VisualServerScene::~VisualServerScene() {
#ifndef NO_THREADS
probe_bake_thread_exit = true;
probe_bake_sem->post();
probe_bake_sem.post();
Thread::wait_to_finish(probe_bake_thread);
memdelete(probe_bake_thread);
memdelete(probe_bake_sem);
#endif
}

View file

@ -593,7 +593,7 @@ public:
volatile bool probe_bake_thread_exit;
Thread *probe_bake_thread;
Semaphore *probe_bake_sem;
Semaphore probe_bake_sem;
Mutex probe_bake_mutex;
List<Instance *> probe_bake_list;