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
25 #include <system_error>
32 void althrd_setname(const char *name
)
35 #define MS_VC_EXCEPTION 0x406D1388
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.
46 info
.dwThreadID
= ~DWORD
{0};
50 RaiseException(MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(ULONG_PTR
), (ULONG_PTR
*)&info
);
52 __except(EXCEPTION_CONTINUE_EXECUTION
) {
54 #undef MS_VC_EXCEPTION
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);
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
; }
90 #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
92 #ifdef HAVE_PTHREAD_NP_H
93 #include <pthread_np.h>
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
);
105 pthread_setname_np(pthread_self(), name
);
111 void althrd_setname(const char*) { }
118 semaphore::semaphore(unsigned int initial
)
120 mSem
= dispatch_semaphore_create(initial
);
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__ */
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__ */