]> git.tdb.fi Git - ext/openal.git/blob - common/threads.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / common / threads.cpp
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.
8  *
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.
13  *
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
19  */
20
21 #include "config.h"
22
23 #include "opthelpers.h"
24 #include "threads.h"
25
26 #include <system_error>
27
28
29 #ifdef _WIN32
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
32
33 #include <limits>
34
35 void althrd_setname(const char *name)
36 {
37 #if defined(_MSC_VER) && !defined(_M_ARM)
38
39 #define MS_VC_EXCEPTION 0x406D1388
40 #pragma pack(push,8)
41     struct {
42         DWORD dwType;     // Must be 0x1000.
43         LPCSTR szName;    // Pointer to name (in user addr space).
44         DWORD dwThreadID; // Thread ID (-1=caller thread).
45         DWORD dwFlags;    // Reserved for future use, must be zero.
46     } info;
47 #pragma pack(pop)
48     info.dwType = 0x1000;
49     info.szName = name;
50     info.dwThreadID = ~DWORD{0};
51     info.dwFlags = 0;
52
53     __try {
54         RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
55     }
56     __except(EXCEPTION_CONTINUE_EXECUTION) {
57     }
58 #undef MS_VC_EXCEPTION
59
60 #else
61
62     (void)name;
63 #endif
64 }
65
66 namespace al {
67
68 semaphore::semaphore(unsigned int initial)
69 {
70     if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
71         throw std::system_error(std::make_error_code(std::errc::value_too_large));
72     mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
73     if(mSem == nullptr)
74         throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
75 }
76
77 semaphore::~semaphore()
78 { CloseHandle(mSem); }
79
80 void semaphore::post()
81 {
82     if(!ReleaseSemaphore(static_cast<HANDLE>(mSem), 1, nullptr))
83         throw std::system_error(std::make_error_code(std::errc::value_too_large));
84 }
85
86 void semaphore::wait() noexcept
87 { WaitForSingleObject(static_cast<HANDLE>(mSem), INFINITE); }
88
89 bool semaphore::try_wait() noexcept
90 { return WaitForSingleObject(static_cast<HANDLE>(mSem), 0) == WAIT_OBJECT_0; }
91
92 } // namespace al
93
94 #else
95
96 #include <pthread.h>
97 #ifdef HAVE_PTHREAD_NP_H
98 #include <pthread_np.h>
99 #endif
100 #include <tuple>
101
102 namespace {
103
104 using setname_t1 = int(*)(const char*);
105 using setname_t2 = int(*)(pthread_t, const char*);
106 using setname_t3 = void(*)(pthread_t, const char*);
107 using setname_t4 = int(*)(pthread_t, const char*, void*);
108
109 void setname_caller(setname_t1 func, const char *name)
110 { func(name); }
111
112 void setname_caller(setname_t2 func, const char *name)
113 { func(pthread_self(), name); }
114
115 void setname_caller(setname_t3 func, const char *name)
116 { func(pthread_self(), name); }
117
118 void setname_caller(setname_t4 func, const char *name)
119 { func(pthread_self(), "%s", static_cast<void*>(const_cast<char*>(name))); }
120
121 } // namespace
122
123 void althrd_setname(const char *name)
124 {
125 #if defined(HAVE_PTHREAD_SET_NAME_NP)
126     setname_caller(pthread_set_name_np, name);
127 #elif defined(HAVE_PTHREAD_SETNAME_NP)
128     setname_caller(pthread_setname_np, name);
129 #endif
130     /* Avoid unused function/parameter warnings. */
131     std::ignore = name;
132     std::ignore = static_cast<void(*)(setname_t1,const char*)>(&setname_caller);
133     std::ignore = static_cast<void(*)(setname_t2,const char*)>(&setname_caller);
134     std::ignore = static_cast<void(*)(setname_t3,const char*)>(&setname_caller);
135     std::ignore = static_cast<void(*)(setname_t4,const char*)>(&setname_caller);
136 }
137
138 #ifdef __APPLE__
139
140 namespace al {
141
142 semaphore::semaphore(unsigned int initial)
143 {
144     mSem = dispatch_semaphore_create(initial);
145     if(!mSem)
146         throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
147 }
148
149 semaphore::~semaphore()
150 { dispatch_release(mSem); }
151
152 void semaphore::post()
153 { dispatch_semaphore_signal(mSem); }
154
155 void semaphore::wait() noexcept
156 { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
157
158 bool semaphore::try_wait() noexcept
159 { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
160
161 } // namespace al
162
163 #else /* !__APPLE__ */
164
165 #include <cerrno>
166
167 namespace al {
168
169 semaphore::semaphore(unsigned int initial)
170 {
171     if(sem_init(&mSem, 0, initial) != 0)
172         throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
173 }
174
175 semaphore::~semaphore()
176 { sem_destroy(&mSem); }
177
178 void semaphore::post()
179 {
180     if(sem_post(&mSem) != 0)
181         throw std::system_error(std::make_error_code(std::errc::value_too_large));
182 }
183
184 void semaphore::wait() noexcept
185 {
186     while(sem_wait(&mSem) == -1 && errno == EINTR) {
187     }
188 }
189
190 bool semaphore::try_wait() noexcept
191 { return sem_trywait(&mSem) == 0; }
192
193 } // namespace al
194
195 #endif /* __APPLE__ */
196
197 #endif /* _WIN32 */