Skip to content

Commit

Permalink
Revert "Iconv: Ensure that OTTD2FS/FS2OTTD are thread-safe"
Browse files Browse the repository at this point in the history
This reverts commit 94d326b.
  • Loading branch information
JGRennison committed Feb 21, 2024
1 parent 4469925 commit c2d581a
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions src/os/unix/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <atomic>

#ifdef WITH_SDL2
#include <SDL.h>
Expand Down Expand Up @@ -161,34 +160,24 @@ static std::string convert_tofrom_fs(iconv_t convd, const std::string &name)
return buf;
}

std::mutex _iconv_convd_mutex;

/**
* Convert from OpenTTD's encoding to that of the local environment
* @param name pointer to a valid string that will be converted
* @return pointer to a new stringbuffer that contains the converted string
*/
std::string OTTD2FS(const std::string &name)
{
static std::atomic<iconv_t> convd{(iconv_t)(-1)};
iconv_t local_convd = convd.load(std::memory_order_relaxed);
if (local_convd == (iconv_t)(-1)) {
{
std::unique_lock<std::mutex> lock(_iconv_convd_mutex);
local_convd = convd.load(); // Load a second time now that the lock is acquired
if (local_convd == (iconv_t)(-1)) {
const char *env = GetLocalCode();
local_convd = iconv_open(env, INTERNALCODE);
if (local_convd != (iconv_t)(-1)) convd.store(local_convd);
}
}
if (local_convd == (iconv_t)(-1)) {
static iconv_t convd = (iconv_t)(-1);
if (convd == (iconv_t)(-1)) {
const char *env = GetLocalCode();
convd = iconv_open(env, INTERNALCODE);
if (convd == (iconv_t)(-1)) {
DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
return name;
}
}

return convert_tofrom_fs(local_convd, name);
return convert_tofrom_fs(convd, name);
}

/**
Expand All @@ -198,25 +187,17 @@ std::string OTTD2FS(const std::string &name)
*/
std::string FS2OTTD(const std::string &name)
{
static std::atomic<iconv_t> convd{(iconv_t)(-1)};
iconv_t local_convd = convd.load(std::memory_order_relaxed);
if (local_convd == (iconv_t)(-1)) {
{
std::unique_lock<std::mutex> lock(_iconv_convd_mutex);
local_convd = convd.load(); // Load a second time now that the lock is acquired
if (local_convd == (iconv_t)(-1)) {
const char *env = GetLocalCode();
local_convd = iconv_open(INTERNALCODE, env);
if (local_convd != (iconv_t)(-1)) convd.store(local_convd);
}
}
if (local_convd == (iconv_t)(-1)) {
static iconv_t convd = (iconv_t)(-1);
if (convd == (iconv_t)(-1)) {
const char *env = GetLocalCode();
convd = iconv_open(INTERNALCODE, env);
if (convd == (iconv_t)(-1)) {
DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
return name;
}
}

return convert_tofrom_fs(local_convd, name);
return convert_tofrom_fs(convd, name);
}

#endif /* WITH_ICONV */
Expand Down

0 comments on commit c2d581a

Please sign in to comment.