Use std::accumulate to find the max channel count
[openal-soft.git] / common / threads.cpp
blob69989ae4bb2298b2558a160ff7fcb37c8da376b1
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 "threads.h"
25 #include <limits>
26 #include <system_error>
29 #ifdef _WIN32
31 void althrd_setname(const char *name)
33 #if defined(_MSC_VER)
34 #define MS_VC_EXCEPTION 0x406D1388
35 #pragma pack(push,8)
36 struct {
37 DWORD dwType; // Must be 0x1000.
38 LPCSTR szName; // Pointer to name (in user addr space).
39 DWORD dwThreadID; // Thread ID (-1=caller thread).
40 DWORD dwFlags; // Reserved for future use, must be zero.
41 } info;
42 #pragma pack(pop)
43 info.dwType = 0x1000;
44 info.szName = name;
45 info.dwThreadID = -1;
46 info.dwFlags = 0;
48 __try {
49 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
51 __except(EXCEPTION_CONTINUE_EXECUTION) {
53 #undef MS_VC_EXCEPTION
54 #else
55 (void)name;
56 #endif
59 namespace al {
61 semaphore::semaphore(unsigned int initial)
63 if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
64 throw std::system_error(std::make_error_code(std::errc::value_too_large));
65 mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
66 if(mSem == nullptr)
67 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
70 semaphore::~semaphore()
71 { CloseHandle(mSem); }
73 void semaphore::post()
75 if(!ReleaseSemaphore(mSem, 1, nullptr))
76 throw std::system_error(std::make_error_code(std::errc::value_too_large));
79 void semaphore::wait() noexcept
80 { WaitForSingleObject(mSem, INFINITE); }
82 bool semaphore::try_wait() noexcept
83 { return WaitForSingleObject(mSem, 0) == WAIT_OBJECT_0; }
85 } // namespace al
87 #else
89 #include <errno.h>
90 #include <pthread.h>
91 #ifdef HAVE_PTHREAD_NP_H
92 #include <pthread_np.h>
93 #endif
95 void althrd_setname(const char *name)
97 #if defined(HAVE_PTHREAD_SETNAME_NP)
98 #if defined(PTHREAD_SETNAME_NP_ONE_PARAM)
99 pthread_setname_np(name);
100 #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
101 pthread_setname_np(pthread_self(), "%s", (void*)name);
102 #else
103 pthread_setname_np(pthread_self(), name);
104 #endif
105 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
106 pthread_set_name_np(pthread_self(), name);
107 #else
108 (void)name;
109 #endif
112 namespace al {
114 #ifdef __APPLE__
116 semaphore::semaphore(unsigned int initial)
118 mSem = dispatch_semaphore_create(initial);
119 if(!mSem)
120 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
123 semaphore::~semaphore()
124 { dispatch_release(mSem); }
126 void semaphore::post()
127 { dispatch_semaphore_signal(mSem); }
129 void semaphore::wait() noexcept
130 { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
132 bool semaphore::try_wait() noexcept
133 { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
135 #else /* !__APPLE__ */
137 semaphore::semaphore(unsigned int initial)
139 if(sem_init(&mSem, 0, initial) != 0)
140 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
143 semaphore::~semaphore()
144 { sem_destroy(&mSem); }
146 void semaphore::post()
148 if(sem_post(&mSem) != 0)
149 throw std::system_error(std::make_error_code(std::errc::value_too_large));
152 void semaphore::wait() noexcept
154 while(sem_wait(&mSem) == -1 && errno == EINTR) {
158 bool semaphore::try_wait() noexcept
159 { return sem_trywait(&mSem) == 0; }
161 #endif /* __APPLE__ */
163 } // namespace al
165 #endif /* _WIN32 */