Don't assert for unexpected FX slot IDs
[openal-soft.git] / common / alsem.cpp
blob2eeed5929d8473a7885227c5e70ab06a4a5761c2
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "alsem.h"
25 #include <system_error>
28 #ifdef _WIN32
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
32 #include <limits>
34 namespace al {
36 semaphore::semaphore(unsigned int initial)
38 if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
39 throw std::system_error(std::make_error_code(std::errc::value_too_large));
40 mSem = CreateSemaphoreW(nullptr, static_cast<LONG>(initial), std::numeric_limits<int>::max(),
41 nullptr);
42 if(mSem == nullptr)
43 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
46 semaphore::~semaphore()
47 { CloseHandle(mSem); }
49 void semaphore::post()
51 if(!ReleaseSemaphore(static_cast<HANDLE>(mSem), 1, nullptr))
52 throw std::system_error(std::make_error_code(std::errc::value_too_large));
55 void semaphore::wait() noexcept
56 { WaitForSingleObject(static_cast<HANDLE>(mSem), INFINITE); }
58 bool semaphore::try_wait() noexcept
59 { return WaitForSingleObject(static_cast<HANDLE>(mSem), 0) == WAIT_OBJECT_0; }
61 } // namespace al
63 #else
65 /* Do not try using libdispatch on systems where it is absent. */
66 #if defined(AL_APPLE_HAVE_DISPATCH)
68 namespace al {
70 semaphore::semaphore(unsigned int initial)
72 mSem = dispatch_semaphore_create(initial);
73 if(!mSem)
74 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
77 semaphore::~semaphore()
78 { dispatch_release(mSem); }
80 void semaphore::post()
81 { dispatch_semaphore_signal(mSem); }
83 void semaphore::wait() noexcept
84 { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
86 bool semaphore::try_wait() noexcept
87 { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
89 } // namespace al
91 #else /* !__APPLE__ */
93 #include <cerrno>
95 namespace al {
97 semaphore::semaphore(unsigned int initial)
99 if(sem_init(&mSem, 0, initial) != 0)
100 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
103 semaphore::~semaphore()
104 { sem_destroy(&mSem); }
106 void semaphore::post()
108 if(sem_post(&mSem) != 0)
109 throw std::system_error(std::make_error_code(std::errc::value_too_large));
112 void semaphore::wait() noexcept
114 while(sem_wait(&mSem) == -1 && errno == EINTR) {
118 bool semaphore::try_wait() noexcept
119 { return sem_trywait(&mSem) == 0; }
121 } // namespace al
123 #endif /* __APPLE__ */
125 #endif /* _WIN32 */