1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* FluidSynth - A Software Synthesizer
*
* Copyright (C) 2003 Peter Hanappe and others.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <https://www.gnu.org/licenses/>.
*/
#include "fluid_sys.h"
#include <chrono>
#include <condition_variable>
#include <map>
#include <mutex>
#include <new>
#include <thread>
static std::mutex atomic_lock;
fluid_mutex_t _atomic_lock = &atomic_lock;
static thread_local std::map<fluid_private_t, void *> private_data;
void fluid_msleep(unsigned int msecs)
{
std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
}
double fluid_utime()
{
auto now = std::chrono::steady_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::microseconds>(now).count();
}
static void thread_wrapper(fluid_thread_func_t func, void *data)
{
try
{
func(data);
}
catch (...)
{
FLUID_LOG(FLUID_ERR, "Exception thrown in thread function");
}
}
fluid_thread_t *
new_fluid_thread(const char *name, fluid_thread_func_t func, void *data, int prio_level, int detach)
{
if (func == nullptr)
return nullptr;
fluid_thread_info_t *info = nullptr;
try
{
if (prio_level > 0)
{
info = new fluid_thread_info_t;
info->func = func;
info->data = data;
info->prio_level = prio_level;
func = fluid_thread_high_prio;
data = info;
}
std::thread *thread = new std::thread(thread_wrapper, func, data);
if (detach)
thread->detach();
return thread;
}
catch (const std::bad_alloc &)
{
FLUID_LOG(FLUID_PANIC, "Out of memory on thread allocation");
}
catch (...)
{
FLUID_LOG(FLUID_ERR, "Failed to create thread");
}
delete info;
return nullptr;
}
void delete_fluid_thread(fluid_thread_t *_thread)
{
std::thread *thread = static_cast<std::thread *>(_thread);
if (thread->joinable())
{
if (thread->get_id() == std::this_thread::get_id())
{
// Thread is deleting itself, detach from the thread object
thread->detach();
}
else
{
FLUID_LOG(FLUID_ERR, "deleting thread that is still joinable");
}
}
delete thread;
}
int fluid_thread_join(fluid_thread_t *thread)
{
static_cast<std::thread *>(thread)->join();
return FLUID_OK;
}
void _fluid_mutex_init(fluid_mutex_t *mutex)
{
*mutex = new(std::nothrow) std::mutex();
if (*mutex == nullptr)
FLUID_LOG(FLUID_PANIC, "Out of memory on mutex allocation");
}
void fluid_mutex_destroy(fluid_mutex_t mutex)
{
delete static_cast<std::mutex *>(mutex);
}
template<class T>
static void ensure_lock_mutex(T *mutex)
{
do
{
try
{
mutex->lock();
}
catch (...)
{
continue;
}
}
while (false);
}
void _fluid_mutex_lock(fluid_mutex_t *mutex)
{
if (*mutex == nullptr)
{
// First use of a statically initialized mutex
fluid_mutex_lock(_atomic_lock);
if (*mutex == nullptr)
_fluid_mutex_init(mutex);
fluid_mutex_unlock(_atomic_lock);
}
ensure_lock_mutex(static_cast<std::mutex *>(*mutex));
}
void fluid_mutex_unlock(fluid_mutex_t mutex)
{
static_cast<std::mutex *>(mutex)->unlock();
}
void _fluid_rec_mutex_init(fluid_rec_mutex_t *mutex)
{
*mutex = new(std::nothrow) std::recursive_mutex();
if (*mutex == nullptr)
FLUID_LOG(FLUID_PANIC, "Out of memory on recursive mutex allocation");
}
void fluid_rec_mutex_destroy(fluid_rec_mutex_t mutex)
{
delete static_cast<std::recursive_mutex *>(mutex);
}
void fluid_rec_mutex_lock(fluid_rec_mutex_t mutex)
{
ensure_lock_mutex(static_cast<std::recursive_mutex *>(mutex));
}
void fluid_rec_mutex_unlock(fluid_rec_mutex_t mutex)
{
static_cast<std::recursive_mutex *>(mutex)->unlock();
}
void fluid_cond_mutex_lock(fluid_cond_mutex_t *mutex)
{
ensure_lock_mutex(static_cast<std::mutex *>(mutex));
}
void fluid_cond_mutex_unlock(fluid_cond_mutex_t *mutex)
{
static_cast<std::mutex *>(mutex)->unlock();
}
fluid_cond_mutex_t *new_fluid_cond_mutex(void)
{
std::mutex *mutex = new(std::nothrow) std::mutex();
if (mutex == nullptr)
FLUID_LOG(FLUID_PANIC, "Out of memory on condition mutex allocation");
return mutex;
}
void delete_fluid_cond_mutex(fluid_cond_mutex_t *mutex)
{
delete static_cast<std::mutex *>(mutex);
}
void fluid_cond_signal(fluid_cond_t cond)
{
static_cast<std::condition_variable *>(cond)->notify_one();
}
void fluid_cond_broadcast(fluid_cond_t cond)
{
static_cast<std::condition_variable *>(cond)->notify_all();
}
void fluid_cond_wait(fluid_cond_t cond, fluid_cond_mutex_t *mutex)
{
std::unique_lock<std::mutex> lock(*static_cast<std::mutex *>(mutex), std::adopt_lock);
static_cast<std::condition_variable *>(cond)->wait(lock);
lock.release();
}
fluid_cond_t new_fluid_cond(void)
{
std::condition_variable *cond = new(std::nothrow) std::condition_variable();
if (cond == nullptr)
FLUID_LOG(FLUID_PANIC, "Out of memory on condition variable allocation");
return cond;
}
void delete_fluid_cond(fluid_cond_t cond)
{
delete static_cast<std::condition_variable *>(cond);
}
void _fluid_private_init(fluid_private_t *priv)
{
*priv = priv;
}
void fluid_private_free(fluid_private_t priv)
{
private_data.erase(priv);
}
void *fluid_private_get(fluid_private_t priv)
{
return private_data[priv];
}
void fluid_private_set(fluid_private_t priv, void *value)
{
private_data[priv] = value;
}
#if HAVE_CXX_FILESYSTEM
#include <filesystem>
int fluid_stat(const char *_path, fluid_stat_buf_t *buffer)
{
try
{
std::filesystem::path path = std::filesystem::u8path(_path);
auto mtime = std::filesystem::last_write_time(path).time_since_epoch();
buffer->st_mtime = std::chrono::duration_cast<std::chrono::seconds>(mtime).count();
return FLUID_OK;
}
catch (...)
{
}
#else
int fluid_stat(const char *_path, fluid_stat_buf_t *buffer)
{
FLUID_LOG(FLUID_ERR, "fluid_stat is unavailable, returning -1");
#endif
buffer->st_mtime = 0;
return FLUID_FAILED;
}
|