Use thread_local statics in dtoa.c to be thread-safe

We don't use MULTIPLE_THREADS for that to avoid performance
penalty due to locking and memory allocation.

Use dtoa.cxx stub to compile dtoa.c as C++, to allow using
thread_local keyword in MSVC, which still implements pre-C99.

Also don't expose dtoa() and other symbols - make them static.
And define IEEE_MC68k, not IEEE_8087 on big endian platforms.

Change-Id: Ie59fe0cf057b043556a5ac15ec6b11a1a5b41f47
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89629
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Mike Kaganski
2020-03-02 23:40:06 +03:00
parent 131d5cdbb9
commit 657d8c3e32
5 changed files with 103 additions and 14 deletions

View File

@@ -11,13 +11,20 @@ $(eval $(call gb_StaticLibrary_StaticLibrary,dtoa))
$(eval $(call gb_StaticLibrary_use_unpacked,dtoa,dtoa))
# A place that duplicates these settings is jurt/source/pipe/staticsalhack.cxx:
$(eval $(call gb_StaticLibrary_add_cflags,dtoa,-DIEEE_8087))
# A place that duplicates these settings is jurt/Library_jpipe.mk
$(eval $(call gb_StaticLibrary_add_defs,dtoa,\
$(if $(filter little,$(ENDIANNESS)),-DIEEE_8087,-DIEEE_MC68k)\
))
$(eval $(call gb_StaticLibrary_set_warnings_disabled,dtoa))
$(eval $(call gb_StaticLibrary_add_generated_cobjects,dtoa,\
UnpackedTarball/dtoa/src/dtoa \
$(eval $(call gb_StaticLibrary_set_include,dtoa,\
-I$(call gb_UnpackedTarball_get_dir,dtoa/src/)\
$$(INCLUDE)\
))
$(eval $(call gb_StaticLibrary_add_exception_objects,dtoa,\
external/dtoa/source/dtoa \
))
# vim: set noet sw=4 ts=4:

View File

@@ -1,23 +1,57 @@
--- /dev/null
+++ dtoa/include/dtoa.h
@@ -0,0 +1,3 @@
@@ -0,0 +1,1 @@
+extern "C" double strtod_nolocale(const char *s00, char **se);
+extern "C" char *dtoa_nolocale(double d, int mode, int ndigits,
+ int *decpt, int *sign, char **rve);
--- dtoa/src/dtoa.c.orig
+++ dtoa/src/dtoa.c
@@ -1502,8 +1502,8 @@ static unsigned int maxthreads = 0;
@@ -268,7 +268,7 @@ #ifndef PRIVATE_MEM
#define PRIVATE_MEM 2304
#endif
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
-static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
+static thread_local double private_mem[PRIVATE_mem], *pmem_next = private_mem;
#endif
#undef IEEE_Arith
@@ -1502,9 +1502,7 @@ static unsigned int maxthreads = 0;
#define Kmax 7
#ifdef __cplusplus
-extern "C" double strtod(const char *s00, char **se);
-extern "C" char *dtoa(double d, int mode, int ndigits,
- int *decpt, int *sign, char **rve);
+extern "C" double strtod_nolocale(const char *s00, char **se);
+extern "C" char *dtoa_nolocale(double d, int mode, int ndigits,
int *decpt, int *sign, char **rve);
#endif
@@ -3429,7 +3429,7 @@ retlow1:
struct
@@ -1521,7 +1519,7 @@ ThInfo {
Bigint *P5s;
} ThInfo;
- static ThInfo TI0;
+ static thread_local ThInfo TI0;
#ifdef MULTIPLE_THREADS
static ThInfo *TI1;
@@ -1529,7 +1527,7 @@ #ifdef MULTIPLE_THREADS
static ThInfo *TI1;
static int TI0_used;
- void
+ static void
set_max_dtoa_threads(unsigned int n)
{
size_t L;
@@ -2717,7 +2715,7 @@ enum { /* rounding values: same as FLT_ROUNDS */
Round_down = 3
};
- void
+ static void
gethex( const char **sp, U *rvp, int rounding, int sign MTd)
{
Bigint *b;
@@ -3429,7 +3427,7 @@ retlow1:
#endif /* NO_STRTOD_BIGCOMP */
double
@@ -26,11 +60,40 @@
{
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
@@ -6185,7 +6185,7 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
@@ -4851,7 +4849,7 @@ strtod_nolocale(const char *s00, char **se)
}
char *
#ifndef MULTIPLE_THREADS
- static char *dtoa_result;
+ static thread_local char *dtoa_result;
#endif
static char *
@@ -4902,7 +4900,7 @@ nrv_alloc(const char *s, char *s0, size_t s0len, char **rve, int n MTd)
* when MULTIPLE_THREADS is not defined.
*/
- void
+ static void
freedtoa(char *s)
{
#ifdef MULTIPLE_THREADS
@@ -4951,7 +4949,7 @@ freedtoa(char *s)
* calculation.
*/
- char *
+ static char *
dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char *buf, size_t blen)
{
/* Arguments ndigits, decpt, sign are similar to those
@@ -6184,8 +6182,8 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
return buf;
}
- char *
-dtoa(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
+ static char *
+dtoa_nolocale(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
{
/* Sufficient space is allocated to the return value

15
external/dtoa/source/dtoa.cxx vendored Normal file
View File

@@ -0,0 +1,15 @@
/* -*- 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/.
*/
#include <sal/config.h>
// build this as C++ code, to allow e.g. thread_local keyword in MSVC
#include <dtoa.c>
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */

View File

@@ -46,6 +46,11 @@ $(eval $(call gb_Library_add_cobjects,jpipe, \
jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection \
))
# see external/dtoa/StaticLibrary_dtoa.mk
$(eval $(call gb_Library_add_defs,jpipe,\
$(if $(filter little,$(ENDIANNESS)),-DIEEE_8087,-DIEEE_MC68k)\
))
$(eval $(call gb_Library_add_exception_objects,jpipe, \
jurt/source/pipe/staticsalhack \
))

View File

@@ -76,7 +76,6 @@ extern "C" ImplTextEncodingData const * sal_getFullTextEncodingData(
std::abort();
}
#define IEEE_8087 // see external/dtoa/StaticLibrary_dtoa.mk
#pragma clang diagnostic ignored "-Weverything"
extern "C" {
#include <src/dtoa.c> // workdir/UnpackedTarball/dtoa/src/dtoa.c