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
26 #include <system_error>
31 void althrd_setname(const char *name
)
34 #define MS_VC_EXCEPTION 0x406D1388
37 DWORD dwType
; // Must be 0x1000.
38 LPCSTR szName
; // Pointer to name (in user addr space).
39 DWORD dwThreadID
; // Thread ID (-1=caller thread).
40 DWORD dwFlags
; // Reserved for future use, must be zero.
49 RaiseException(MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(ULONG_PTR
), (ULONG_PTR
*)&info
);
51 __except(EXCEPTION_CONTINUE_EXECUTION
) {
53 #undef MS_VC_EXCEPTION
61 semaphore::semaphore(unsigned int initial
)
63 if(initial
> static_cast<unsigned int>(std::numeric_limits
<int>::max()))
64 throw std::system_error(std::make_error_code(std::errc::value_too_large
));
65 mSem
= CreateSemaphore(nullptr, initial
, std::numeric_limits
<int>::max(), nullptr);
67 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again
));
70 semaphore::~semaphore()
71 { CloseHandle(mSem
); }
73 void semaphore::post()
75 if(!ReleaseSemaphore(mSem
, 1, nullptr))
76 throw std::system_error(std::make_error_code(std::errc::value_too_large
));
79 void semaphore::wait() noexcept
80 { WaitForSingleObject(mSem
, INFINITE
); }
82 bool semaphore::try_wait() noexcept
83 { return WaitForSingleObject(mSem
, 0) == WAIT_OBJECT_0
; }
91 #ifdef HAVE_PTHREAD_NP_H
92 #include <pthread_np.h>
95 void althrd_setname(const char *name
)
97 #if defined(HAVE_PTHREAD_SETNAME_NP)
98 #if defined(PTHREAD_SETNAME_NP_ONE_PARAM)
99 pthread_setname_np(name
);
100 #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
101 pthread_setname_np(pthread_self(), "%s", (void*)name
);
103 pthread_setname_np(pthread_self(), name
);
105 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
106 pthread_set_name_np(pthread_self(), name
);
116 semaphore::semaphore(unsigned int initial
)
118 mSem
= dispatch_semaphore_create(initial
);
120 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again
));
123 semaphore::~semaphore()
124 { dispatch_release(mSem
); }
126 void semaphore::post()
127 { dispatch_semaphore_signal(mSem
); }
129 void semaphore::wait() noexcept
130 { dispatch_semaphore_wait(mSem
, DISPATCH_TIME_FOREVER
); }
132 bool semaphore::try_wait() noexcept
133 { return dispatch_semaphore_wait(mSem
, DISPATCH_TIME_NOW
) == 0; }
135 #else /* !__APPLE__ */
137 semaphore::semaphore(unsigned int initial
)
139 if(sem_init(&mSem
, 0, initial
) != 0)
140 throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again
));
143 semaphore::~semaphore()
144 { sem_destroy(&mSem
); }
146 void semaphore::post()
148 if(sem_post(&mSem
) != 0)
149 throw std::system_error(std::make_error_code(std::errc::value_too_large
));
152 void semaphore::wait() noexcept
154 while(sem_wait(&mSem
) == -1 && errno
== EINTR
) {
158 bool semaphore::try_wait() noexcept
159 { return sem_trywait(&mSem
) == 0; }
161 #endif /* __APPLE__ */