Make a couple more functions private
[openal-soft.git] / common / threads.cpp
blobff01de421b3397f9a9f7e27896e21a39606f9277
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 <system_error>
28 #ifdef _WIN32
30 #include <limits>
32 void althrd_setname(const char *name)
34 #if defined(_MSC_VER)
35 #define MS_VC_EXCEPTION 0x406D1388
36 #pragma pack(push,8)
37 struct {
38 DWORD dwType; // Must be 0x1000.
39 LPCSTR szName; // Pointer to name (in user addr space).
40 DWORD dwThreadID; // Thread ID (-1=caller thread).
41 DWORD dwFlags; // Reserved for future use, must be zero.
42 } info;
43 #pragma pack(pop)
44 info.dwType = 0x1000;
45 info.szName = name;
46 info.dwThreadID = ~DWORD{0};
47 info.dwFlags = 0;
49 __try {
50 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
52 __except(EXCEPTION_CONTINUE_EXECUTION) {
54 #undef MS_VC_EXCEPTION
55 #else
56 (void)name;
57 #endif
60 namespace al {
62 semaphore::semaphore(unsigned int initial)
64 if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
65 throw std::system_error(std::make_error_code(std::errc::value_too_large));
66 mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
67 if(mSem == nullptr)
68 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
71 semaphore::~semaphore()
72 { CloseHandle(mSem); }
74 void semaphore::post()
76 if(!ReleaseSemaphore(mSem, 1, nullptr))
77 throw std::system_error(std::make_error_code(std::errc::value_too_large));
80 void semaphore::wait() noexcept
81 { WaitForSingleObject(mSem, INFINITE); }
83 bool semaphore::try_wait() noexcept
84 { return WaitForSingleObject(mSem, 0) == WAIT_OBJECT_0; }
86 } // namespace al
88 #else
90 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
91 #include <pthread.h>
92 #ifdef HAVE_PTHREAD_NP_H
93 #include <pthread_np.h>
94 #endif
96 void althrd_setname(const char *name)
98 #if defined(HAVE_PTHREAD_SET_NAME_NP)
99 pthread_set_name_np(pthread_self(), name);
100 #elif defined(PTHREAD_SETNAME_NP_ONE_PARAM)
101 pthread_setname_np(name);
102 #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
103 pthread_setname_np(pthread_self(), "%s", (void*)name);
104 #else
105 pthread_setname_np(pthread_self(), name);
106 #endif
109 #else
111 void althrd_setname(const char*) { }
112 #endif
114 namespace al {
116 #ifdef __APPLE__
118 semaphore::semaphore(unsigned int initial)
120 mSem = dispatch_semaphore_create(initial);
121 if(!mSem)
122 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
125 semaphore::~semaphore()
126 { dispatch_release(mSem); }
128 void semaphore::post()
129 { dispatch_semaphore_signal(mSem); }
131 void semaphore::wait() noexcept
132 { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
134 bool semaphore::try_wait() noexcept
135 { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
137 #else /* !__APPLE__ */
139 #include <cerrno>
141 semaphore::semaphore(unsigned int initial)
143 if(sem_init(&mSem, 0, initial) != 0)
144 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
147 semaphore::~semaphore()
148 { sem_destroy(&mSem); }
150 void semaphore::post()
152 if(sem_post(&mSem) != 0)
153 throw std::system_error(std::make_error_code(std::errc::value_too_large));
156 void semaphore::wait() noexcept
158 while(sem_wait(&mSem) == -1 && errno == EINTR) {
162 bool semaphore::try_wait() noexcept
163 { return sem_trywait(&mSem) == 0; }
165 #endif /* __APPLE__ */
167 } // namespace al
169 #endif /* _WIN32 */