Use an array of pointers for the UHJ encoder input
[openal-soft.git] / common / threads.cpp
blobc782dc35cda2ce0fef273d0b5fb3ce752d3c0504
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 "opthelpers.h"
24 #include "threads.h"
26 #include <system_error>
29 #ifdef _WIN32
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
33 #include <limits>
35 void althrd_setname(const char *name)
37 #if defined(_MSC_VER)
38 #define MS_VC_EXCEPTION 0x406D1388
39 #pragma pack(push,8)
40 struct {
41 DWORD dwType; // Must be 0x1000.
42 LPCSTR szName; // Pointer to name (in user addr space).
43 DWORD dwThreadID; // Thread ID (-1=caller thread).
44 DWORD dwFlags; // Reserved for future use, must be zero.
45 } info;
46 #pragma pack(pop)
47 info.dwType = 0x1000;
48 info.szName = name;
49 info.dwThreadID = ~DWORD{0};
50 info.dwFlags = 0;
52 __try {
53 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
55 __except(EXCEPTION_CONTINUE_EXECUTION) {
57 #undef MS_VC_EXCEPTION
58 #else
59 (void)name;
60 #endif
63 namespace al {
65 semaphore::semaphore(unsigned int initial)
67 if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
68 throw std::system_error(std::make_error_code(std::errc::value_too_large));
69 mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
70 if(mSem == nullptr)
71 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
74 semaphore::~semaphore()
75 { CloseHandle(mSem); }
77 void semaphore::post()
79 if UNLIKELY(!ReleaseSemaphore(static_cast<HANDLE>(mSem), 1, nullptr))
80 throw std::system_error(std::make_error_code(std::errc::value_too_large));
83 void semaphore::wait() noexcept
84 { WaitForSingleObject(static_cast<HANDLE>(mSem), INFINITE); }
86 bool semaphore::try_wait() noexcept
87 { return WaitForSingleObject(static_cast<HANDLE>(mSem), 0) == WAIT_OBJECT_0; }
89 } // namespace al
91 #else
93 #include <pthread.h>
94 #ifdef HAVE_PTHREAD_NP_H
95 #include <pthread_np.h>
96 #endif
97 #include <tuple>
99 namespace {
101 using setname_t1 = int(*)(const char*);
102 using setname_t2 = int(*)(pthread_t, const char*);
103 using setname_t3 = int(*)(pthread_t, const char*, void*);
105 void setname_caller(setname_t1 func, const char *name)
106 { func(name); }
108 void setname_caller(setname_t2 func, const char *name)
109 { func(pthread_self(), name); }
111 void setname_caller(setname_t3 func, const char *name)
112 { func(pthread_self(), "%s", static_cast<void*>(const_cast<char*>(name))); }
114 } // namespace
116 void althrd_setname(const char *name)
118 #if defined(HAVE_PTHREAD_SET_NAME_NP)
119 setname_caller(pthread_set_name_np, name);
120 #elif defined(HAVE_PTHREAD_SETNAME_NP)
121 setname_caller(pthread_setname_np, name);
122 #endif
123 /* Avoid unused function/parameter warnings. */
124 std::ignore = name;
125 std::ignore = static_cast<void(*)(setname_t1,const char*)>(&setname_caller);
126 std::ignore = static_cast<void(*)(setname_t2,const char*)>(&setname_caller);
127 std::ignore = static_cast<void(*)(setname_t3,const char*)>(&setname_caller);
130 #ifdef __APPLE__
132 namespace al {
134 semaphore::semaphore(unsigned int initial)
136 mSem = dispatch_semaphore_create(initial);
137 if(!mSem)
138 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
141 semaphore::~semaphore()
142 { dispatch_release(mSem); }
144 void semaphore::post()
145 { dispatch_semaphore_signal(mSem); }
147 void semaphore::wait() noexcept
148 { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
150 bool semaphore::try_wait() noexcept
151 { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
153 } // namespace al
155 #else /* !__APPLE__ */
157 #include <cerrno>
159 namespace al {
161 semaphore::semaphore(unsigned int initial)
163 if(sem_init(&mSem, 0, initial) != 0)
164 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
167 semaphore::~semaphore()
168 { sem_destroy(&mSem); }
170 void semaphore::post()
172 if(sem_post(&mSem) != 0)
173 throw std::system_error(std::make_error_code(std::errc::value_too_large));
176 void semaphore::wait() noexcept
178 while(sem_wait(&mSem) == -1 && errno == EINTR) {
182 bool semaphore::try_wait() noexcept
183 { return sem_trywait(&mSem) == 0; }
185 } // namespace al
187 #endif /* __APPLE__ */
189 #endif /* _WIN32 */