diff options
author | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-03-03 09:26:42 +0100 |
---|---|---|
committer | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-03-03 13:20:42 +0100 |
commit | 9a3a2b03b8b718409eb26252d742d48091756ef7 (patch) | |
tree | 94cc5ae822288ec8b1e098773338498f865b5b77 /drivers | |
parent | c9768f15f7bb194622b9020ab2614d47ac7e63dd (diff) |
Drop old semaphore implementation
- Removed platform-specific implementations.
- Now all semaphores are in-object, unless they need to be conditionally created.
- Similarly to `Mutex`, provided a dummy implementation for when `NO_THREADS` is defined.
- Similarly to `Mutex`, methods are made `const` for easy use in such contexts.
- Language bindings updated: `wait()` and `post()` are now `void`.
- Language bindings updated: `try_wait()` added.
Bonus:
- Rewritten the `#ifdef` in `mutex.h` to meet the code style.
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/unix/os_unix.cpp | 5 | ||||
-rw-r--r-- | drivers/unix/semaphore_posix.cpp | 87 | ||||
-rw-r--r-- | drivers/unix/semaphore_posix.h | 58 | ||||
-rw-r--r-- | drivers/windows/semaphore_windows.cpp | 98 | ||||
-rw-r--r-- | drivers/windows/semaphore_windows.h | 58 |
5 files changed, 0 insertions, 306 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 1d94b9618d..f7da18a7f9 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -38,7 +38,6 @@ #include "drivers/unix/file_access_unix.h" #include "drivers/unix/net_socket_posix.h" #include "drivers/unix/rw_lock_posix.h" -#include "drivers/unix/semaphore_posix.h" #include "drivers/unix/thread_posix.h" #include "servers/visual_server.h" @@ -121,13 +120,9 @@ void OS_Unix::initialize_core() { #ifdef NO_THREADS ThreadDummy::make_default(); - SemaphoreDummy::make_default(); RWLockDummy::make_default(); #else ThreadPosix::make_default(); -#if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED) - SemaphorePosix::make_default(); -#endif RWLockPosix::make_default(); #endif FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES); diff --git a/drivers/unix/semaphore_posix.cpp b/drivers/unix/semaphore_posix.cpp deleted file mode 100644 index b532b09cd6..0000000000 --- a/drivers/unix/semaphore_posix.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/*************************************************************************/ -/* semaphore_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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; -} - -SemaphoreOld *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 diff --git a/drivers/unix/semaphore_posix.h b/drivers/unix/semaphore_posix.h deleted file mode 100644 index 2bffe6933d..0000000000 --- a/drivers/unix/semaphore_posix.h +++ /dev/null @@ -1,58 +0,0 @@ -/*************************************************************************/ -/* semaphore_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 SemaphoreOld { - - mutable sem_t sem; - - static SemaphoreOld *create_semaphore_posix(); - -public: - virtual Error wait(); - virtual Error post(); - virtual int get() const; - - static void make_default(); - SemaphorePosix(); - - ~SemaphorePosix(); -}; - -#endif -#endif diff --git a/drivers/windows/semaphore_windows.cpp b/drivers/windows/semaphore_windows.cpp deleted file mode 100644 index 1b53e311ff..0000000000 --- a/drivers/windows/semaphore_windows.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/*************************************************************************/ -/* semaphore_windows.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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); -} - -SemaphoreOld *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 diff --git a/drivers/windows/semaphore_windows.h b/drivers/windows/semaphore_windows.h deleted file mode 100644 index 159e8b3b96..0000000000 --- a/drivers/windows/semaphore_windows.h +++ /dev/null @@ -1,58 +0,0 @@ -/*************************************************************************/ -/* semaphore_windows.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 SemaphoreOld { - - mutable HANDLE semaphore; - - static SemaphoreOld *create_semaphore_windows(); - -public: - virtual Error wait(); - virtual Error post(); - virtual int get() const; - - static void make_default(); - SemaphoreWindows(); - - ~SemaphoreWindows(); -}; - -#endif -#endif |