diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2023-02-21 12:58:11 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2023-02-24 18:27:25 +0000 |
commit | e7bad15bc13292defd99cb561f867d8b77692e97 (patch) | |
tree | ed4c1642999c8ec97f4de27517193cb16a505c32 | |
parent | d7fd46b5155bda021b09faecdefba26f6a59e997 (diff) |
Unify osl_{g,s}etThreadTextEncoding implementation across platforms
... using a static thread_local variable, as already was used on Windows;
this replaces the non-Windows implementation based on pthread_setspecific
and friends.
Change-Id: Iba42510dea90a9e7d1983ba4af674667055f6dfc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147624
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r-- | sal/Library_sal.mk | 1 | ||||
-rw-r--r-- | sal/inc/thread_internal.hxx | 18 | ||||
-rw-r--r-- | sal/osl/all/threadshared.cxx | 45 | ||||
-rw-r--r-- | sal/osl/unx/thread.cxx | 82 | ||||
-rw-r--r-- | sal/osl/w32/thread.cxx | 18 |
5 files changed, 79 insertions, 85 deletions
diff --git a/sal/Library_sal.mk b/sal/Library_sal.mk index decfa39ce304..e0bf325e38cd 100644 --- a/sal/Library_sal.mk +++ b/sal/Library_sal.mk @@ -94,6 +94,7 @@ $(eval $(call gb_Library_add_exception_objects,sal,\ sal/osl/all/log \ sal/osl/all/mutexshared \ sal/osl/all/signalshared \ + sal/osl/all/threadshared \ sal/osl/all/utility \ sal/rtl/alloc_arena \ sal/rtl/alloc_cache \ diff --git a/sal/inc/thread_internal.hxx b/sal/inc/thread_internal.hxx new file mode 100644 index 000000000000..09c60a5d3ed8 --- /dev/null +++ b/sal/inc/thread_internal.hxx @@ -0,0 +1,18 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include <sal/config.h> + +#include <rtl/textenc.h> + +rtl_TextEncoding getThreadTextEncodingForInitialization(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sal/osl/all/threadshared.cxx b/sal/osl/all/threadshared.cxx new file mode 100644 index 000000000000..136369435235 --- /dev/null +++ b/sal/osl/all/threadshared.cxx @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <osl/thread.h> + +#include <thread_internal.hxx> + +#include <utility> + +namespace +{ +rtl_TextEncoding& getThreadTextEncodingImpl() +{ + // Use OS-specific initial value + static thread_local rtl_TextEncoding s_enc = getThreadTextEncodingForInitialization(); + return s_enc; +} +} + +rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding() { return getThreadTextEncodingImpl(); } + +rtl_TextEncoding SAL_CALL osl_setThreadTextEncoding(rtl_TextEncoding Encoding) +{ + return std::exchange(getThreadTextEncodingImpl(), Encoding); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sal/osl/unx/thread.cxx b/sal/osl/unx/thread.cxx index b122c5f31f3f..ee34731a4456 100644 --- a/sal/osl/unx/thread.cxx +++ b/sal/osl/unx/thread.cxx @@ -27,6 +27,8 @@ #include "system.hxx" #include "unixerrnostring.hxx" +#include <thread_internal.hxx> + #include <string.h> #if defined(OPENBSD) #include <sched.h> @@ -106,38 +108,19 @@ struct osl_thread_priority_st int m_Below_Normal; int m_Lowest; }; -#endif - -} - -#if !defined NO_PTHREAD_PRIORITY #define OSL_THREAD_PRIORITY_INITIALIZER { 127, 96, 64, 32, 0 } #endif -static void osl_thread_priority_init_Impl(); - -namespace { - -struct osl_thread_textencoding_st -{ - pthread_key_t m_key; /* key to store thread local text encoding */ - rtl_TextEncoding m_default; /* the default text encoding */ -}; - } -#define OSL_THREAD_TEXTENCODING_INITIALIZER { 0, RTL_TEXTENCODING_DONTKNOW } -static void osl_thread_textencoding_init_Impl(); +#if !defined NO_PTHREAD_PRIORITY namespace { struct osl_thread_global_st { pthread_once_t m_once; -#if !defined NO_PTHREAD_PRIORITY struct osl_thread_priority_st m_priority; -#endif - struct osl_thread_textencoding_st m_textencoding; }; } @@ -145,13 +128,10 @@ struct osl_thread_global_st static struct osl_thread_global_st g_thread = { PTHREAD_ONCE_INIT, -#if !defined NO_PTHREAD_PRIORITY - OSL_THREAD_PRIORITY_INITIALIZER, -#endif - OSL_THREAD_TEXTENCODING_INITIALIZER + OSL_THREAD_PRIORITY_INITIALIZER }; -static void osl_thread_init_Impl(); +#endif // !defined NO_PTHREAD_PRIORITY static Thread_Impl* osl_thread_construct_Impl(); static void osl_thread_destruct_Impl (Thread_Impl ** ppImpl); @@ -167,12 +147,6 @@ static oslThreadIdentifier insertThreadId (pthread_t hThread); static oslThreadIdentifier lookupThreadId (pthread_t hThread); static void removeThreadId (pthread_t hThread); -static void osl_thread_init_Impl() -{ - osl_thread_priority_init_Impl(); - osl_thread_textencoding_init_Impl(); -} - Thread_Impl* osl_thread_construct_Impl() { Thread_Impl* pImpl = new Thread_Impl; @@ -745,6 +719,7 @@ oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread) return Ident; } +#ifndef NO_PTHREAD_PRIORITY /***************************************************************************** @@@ see TODO @@@ osl_thread_priority_init_Impl @@ -759,7 +734,6 @@ oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread) *****************************************************************************/ static void osl_thread_priority_init_Impl() { -#ifndef NO_PTHREAD_PRIORITY struct sched_param param; int policy=0; int nRet=0; @@ -834,8 +808,8 @@ static void osl_thread_priority_init_Impl() << ", Priority " << param.sched_priority); } -#endif /* NO_PTHREAD_PRIORITY */ } +#endif /* NO_PTHREAD_PRIORITY */ /** Impl-Notes: contrary to solaris-docu, which claims @@ -879,7 +853,7 @@ void SAL_CALL osl_setThreadPriority ( } #endif /* __sun */ - pthread_once (&(g_thread.m_once), osl_thread_init_Impl); + pthread_once (&(g_thread.m_once), osl_thread_priority_init_Impl); switch(Priority) { @@ -950,7 +924,7 @@ oslThreadPriority SAL_CALL osl_getThreadPriority(const oslThread Thread) if (pthread_getschedparam(pImpl->m_hThread, &Policy, &Param) != 0) return osl_Thread_PriorityUnknown; /* ESRCH */ - pthread_once (&(g_thread.m_once), osl_thread_init_Impl); + pthread_once (&(g_thread.m_once), osl_thread_priority_init_Impl); /* map pthread priority to enum */ if (Param.sched_priority==g_thread.m_priority.m_Highest) @@ -1052,15 +1026,10 @@ sal_Bool SAL_CALL osl_setThreadKeyData(oslThreadKey Key, void *pData) return bRet; } -static void osl_thread_textencoding_init_Impl() +rtl_TextEncoding getThreadTextEncodingForInitialization() { - rtl_TextEncoding defaultEncoding; - - /* create thread specific data key */ - pthread_key_create (&(g_thread.m_textencoding.m_key), nullptr); - /* determine default text encoding */ - defaultEncoding = osl_getTextEncodingFromLocale(nullptr); + rtl_TextEncoding defaultEncoding = osl_getTextEncodingFromLocale(nullptr); // Tools string functions call abort() on an unknown encoding so ASCII is a // meaningful fallback: if ( RTL_TEXTENCODING_DONTKNOW == defaultEncoding ) @@ -1069,34 +1038,7 @@ static void osl_thread_textencoding_init_Impl() defaultEncoding = RTL_TEXTENCODING_ASCII_US; } - g_thread.m_textencoding.m_default = defaultEncoding; -} - -rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding() -{ - rtl_TextEncoding threadEncoding; - - pthread_once (&(g_thread.m_once), osl_thread_init_Impl); - - /* check for thread specific encoding, use default if not set */ - threadEncoding = static_cast<rtl_TextEncoding>( - reinterpret_cast<sal_uIntPtr>(pthread_getspecific(g_thread.m_textencoding.m_key))); - if (threadEncoding == 0) - threadEncoding = g_thread.m_textencoding.m_default; - - return threadEncoding; -} - -rtl_TextEncoding osl_setThreadTextEncoding(rtl_TextEncoding Encoding) -{ - rtl_TextEncoding oldThreadEncoding = osl_getThreadTextEncoding(); - - /* save encoding in thread local storage */ - pthread_setspecific ( - g_thread.m_textencoding.m_key, - reinterpret_cast<void*>(static_cast<sal_uIntPtr>(Encoding))); - - return oldThreadEncoding; + return defaultEncoding; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/osl/w32/thread.cxx b/sal/osl/w32/thread.cxx index 194c4c6e5930..f6b3109adfb9 100644 --- a/sal/osl/w32/thread.cxx +++ b/sal/osl/w32/thread.cxx @@ -19,6 +19,7 @@ #include "system.h" #include "thread.hxx" +#include <thread_internal.hxx> #include <comphelper/windowserrorstring.hxx> #include <osl/diagnose.h> @@ -520,22 +521,9 @@ sal_Bool SAL_CALL osl_setThreadKeyData(oslThreadKey Key, void *pData) return false; } -namespace +rtl_TextEncoding getThreadTextEncodingForInitialization() { -rtl_TextEncoding& getThreadTextEncodingImpl() -{ - static thread_local rtl_TextEncoding s_enc = rtl_getTextEncodingFromWindowsCodePage(GetACP()); - return s_enc; -} -} - -rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding(void) { return getThreadTextEncodingImpl(); } - -rtl_TextEncoding SAL_CALL osl_setThreadTextEncoding( rtl_TextEncoding Encoding ) -{ - rtl_TextEncoding oldEncoding = getThreadTextEncodingImpl(); - getThreadTextEncodingImpl() = Encoding; - return oldEncoding; + return rtl_getTextEncodingFromWindowsCodePage(GetACP()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |