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
23 #include "opthelpers.h"
26 #include <system_error>
30 #define WIN32_LEAN_AND_MEAN
35 void althrd_setname(const char *name
)
38 #define MS_VC_EXCEPTION 0x406D1388
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.
49 info
.dwThreadID
= ~DWORD
{0};
53 RaiseException(MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(ULONG_PTR
), (ULONG_PTR
*)&info
);
55 __except(EXCEPTION_CONTINUE_EXECUTION
) {
57 #undef MS_VC_EXCEPTION
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);
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
; }
93 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
95 #ifdef HAVE_PTHREAD_NP_H
96 #include <pthread_np.h>
99 void althrd_setname(const char *name
)
101 #if defined(HAVE_PTHREAD_SET_NAME_NP)
102 pthread_set_name_np(pthread_self(), name
);
103 #elif defined(PTHREAD_SETNAME_NP_ONE_PARAM)
104 pthread_setname_np(name
);
105 #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
106 pthread_setname_np(pthread_self(), "%s", (void*)name
);
108 pthread_setname_np(pthread_self(), name
);
114 void althrd_setname(const char*) { }
121 semaphore::semaphore(unsigned int initial
)
123 mSem
= dispatch_semaphore_create(initial
);
125 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again
));
128 semaphore::~semaphore()
129 { dispatch_release(mSem
); }
131 void semaphore::post()
132 { dispatch_semaphore_signal(mSem
); }
134 void semaphore::wait() noexcept
135 { dispatch_semaphore_wait(mSem
, DISPATCH_TIME_FOREVER
); }
137 bool semaphore::try_wait() noexcept
138 { return dispatch_semaphore_wait(mSem
, DISPATCH_TIME_NOW
) == 0; }
142 #else /* !__APPLE__ */
148 semaphore::semaphore(unsigned int initial
)
150 if(sem_init(&mSem
, 0, initial
) != 0)
151 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again
));
154 semaphore::~semaphore()
155 { sem_destroy(&mSem
); }
157 void semaphore::post()
159 if(sem_post(&mSem
) != 0)
160 throw std::system_error(std::make_error_code(std::errc::value_too_large
));
163 void semaphore::wait() noexcept
165 while(sem_wait(&mSem
) == -1 && errno
== EINTR
) {
169 bool semaphore::try_wait() noexcept
170 { return sem_trywait(&mSem
) == 0; }
174 #endif /* __APPLE__ */