Browse Source

Use newlocale methods in CarlaScopedLocale to be thread-safe

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.2.0-RC1
falkTX 5 years ago
parent
commit
264ff11dd2
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
2 changed files with 44 additions and 6 deletions
  1. +3
    -0
      source/discovery/carla-discovery.cpp
  2. +41
    -6
      source/utils/CarlaScopeUtils.hpp

+ 3
- 0
source/discovery/carla-discovery.cpp View File

@@ -1596,6 +1596,7 @@ static void do_fluidsynth_check(const char* const filename, const PluginType typ


// unused // unused
(void)filename; (void)filename;
(void)type;
(void)doInit; (void)doInit;
#endif #endif
} }
@@ -1644,6 +1645,8 @@ int main(int argc, char* argv[])
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Initialize OS features // Initialize OS features


const CarlaScopedLocale csl;

#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
OleInitialize(nullptr); OleInitialize(nullptr);
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);


+ 41
- 6
source/utils/CarlaScopeUtils.hpp View File

@@ -2,7 +2,7 @@
* Carla Scope-related classes and tools (pointer and setter taken from JUCE v4) * Carla Scope-related classes and tools (pointer and setter taken from JUCE v4)
* Copyright (C) 2013 Raw Material Software Ltd. * Copyright (C) 2013 Raw Material Software Ltd.
* Copyright (c) 2016 ROLI Ltd. * Copyright (c) 2016 ROLI Ltd.
* Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2013-2020 Filipe Coelho <falktx@falktx.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as * modify it under the terms of the GNU General Public License as
@@ -25,6 +25,10 @@
#include <algorithm> #include <algorithm>
#include <clocale> #include <clocale>


#if defined(CARLA_PROPER_CPP11_SUPPORT) && ! defined(CARLA_OS_WIN)
# define CARLA_USE_NEWLOCALE
#endif

// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// CarlaScopedEnvVar class // CarlaScopedEnvVar class


@@ -89,24 +93,55 @@ private:
// CarlaScopedLocale class // CarlaScopedLocale class


class CarlaScopedLocale { class CarlaScopedLocale {
#ifdef CARLA_USE_NEWLOCALE
static constexpr locale_t kNullLocale = (locale_t)0;
#endif

public: public:
CarlaScopedLocale() noexcept CarlaScopedLocale() noexcept
: locale(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr)))
#ifdef CARLA_USE_NEWLOCALE
: newloc(::newlocale(LC_NUMERIC_MASK, "C", kNullLocale)),
oldloc(newloc != kNullLocale ? ::uselocale(newloc) : kNullLocale) {}
#else
# ifdef CARLA_OS_WIN
: oldthreadloc(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)),
# else
: oldthreadloc(-1),
# endif
oldloc(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr)))
{ {
::setlocale(LC_NUMERIC, "C"); ::setlocale(LC_NUMERIC, "C");
} }
#endif


~CarlaScopedLocale() noexcept ~CarlaScopedLocale() noexcept
{ {
if (locale != nullptr)
#ifdef CARLA_USE_NEWLOCALE
if (oldloc != kNullLocale)
::uselocale(oldloc);
if (newloc != kNullLocale)
::freelocale(newloc);
#else // CARLA_USE_NEWLOCALE
if (oldloc != nullptr)
{ {
::setlocale(LC_NUMERIC, locale);
delete[] locale;
::setlocale(LC_NUMERIC, oldloc);
delete[] oldloc;
} }

# ifdef CARLA_OS_WIN
if (oldthreadloc != -1)
_configthreadlocale(oldthreadloc);
# endif
#endif // CARLA_USE_NEWLOCALE
} }


private: private:
const char* const locale;
#ifdef CARLA_USE_NEWLOCALE
locale_t newloc, oldloc;
#else
const int oldthreadloc;
const char* const oldloc;
#endif


CARLA_DECLARE_NON_COPY_CLASS(CarlaScopedLocale) CARLA_DECLARE_NON_COPY_CLASS(CarlaScopedLocale)
CARLA_PREVENT_HEAP_ALLOCATION CARLA_PREVENT_HEAP_ALLOCATION


Loading…
Cancel
Save