Skip to content

Commit 077891f

Browse files
committed
Remove zend_strtod mutex
1 parent 887ed94 commit 077891f

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

Zend/zend.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,10 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
823823
executor_globals->pid = 0;
824824
executor_globals->oldact = (struct sigaction){0};
825825
#endif
826+
memset(executor_globals->strtod_state.freelist, 0,
827+
sizeof(executor_globals->strtod_state.freelist));
828+
executor_globals->strtod_state.p5s = NULL;
829+
executor_globals->strtod_state.result = NULL;
826830
}
827831
/* }}} */
828832

@@ -918,7 +922,6 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
918922
#endif
919923

920924
zend_startup_hrtime();
921-
zend_startup_strtod();
922925
zend_startup_extensions_mechanism();
923926

924927
/* Set up utility functions and values */
@@ -1158,7 +1161,6 @@ void zend_shutdown(void) /* {{{ */
11581161

11591162
zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
11601163
free(GLOBAL_CONSTANTS_TABLE);
1161-
zend_shutdown_strtod();
11621164
zend_attributes_shutdown();
11631165

11641166
#ifdef ZTS
@@ -1275,6 +1277,7 @@ ZEND_API void zend_activate(void) /* {{{ */
12751277
#ifdef ZTS
12761278
virtual_cwd_activate();
12771279
#endif
1280+
zend_strtod_activate();
12781281
gc_reset();
12791282
init_compiler();
12801283
init_executor();
@@ -1345,6 +1348,8 @@ ZEND_API void zend_deactivate(void) /* {{{ */
13451348
zend_map_ptr_reset();
13461349
}
13471350

1351+
zend_strtod_deactivate();
1352+
13481353
#if GC_BENCH
13491354
gc_bench_print();
13501355
#endif

Zend/zend_globals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "zend_arena.h"
4242
#include "zend_call_stack.h"
4343
#include "zend_max_execution_timer.h"
44+
#include "zend_strtod.h"
4445

4546
/* Define ZTS if you want a thread-safe Zend */
4647
/*#undef ZTS*/
@@ -304,6 +305,8 @@ struct _zend_executor_globals {
304305
struct sigaction oldact;
305306
#endif
306307

308+
zend_strtod_state strtod_state;
309+
307310
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
308311
};
309312

Zend/zend_strtod.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
#include <zend_operators.h>
190190
#include <zend_strtod.h>
191191
#include "zend_strtod_int.h"
192+
#include "zend_globals.h"
192193

193194
#ifndef Long
194195
#define Long int32_t
@@ -197,6 +198,16 @@
197198
#define ULong uint32_t
198199
#endif
199200

201+
#undef Bigint
202+
#undef freelist
203+
#undef p5s
204+
#undef dtoa_result
205+
206+
#define Bigint _zend_strtod_bigint
207+
#define freelist (EG(strtod_state).freelist)
208+
#define p5s (EG(strtod_state).p5s)
209+
#define dtoa_result (EG(strtod_state).result)
210+
200211
#ifdef DEBUG
201212
static void Bug(const char *message) {
202213
fprintf(stderr, "%s\n", message);
@@ -223,7 +234,8 @@ extern char *MALLOC();
223234
extern void *MALLOC(size_t);
224235
#endif
225236
#else
226-
#define MALLOC malloc
237+
#define MALLOC emalloc
238+
#define FREE efree
227239
#endif
228240

229241
#ifndef Omit_Private_Memory
@@ -522,7 +534,7 @@ BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflc
522534
#define FREE_DTOA_LOCK(n) /*nothing*/
523535
#endif
524536

525-
#define Kmax 7
537+
#define Kmax ZEND_STRTOD_K_MAX
526538

527539
struct
528540
Bigint {
@@ -533,37 +545,28 @@ Bigint {
533545

534546
typedef struct Bigint Bigint;
535547

548+
#ifndef Bigint
536549
static Bigint *freelist[Kmax+1];
550+
#endif
537551

538552
static void destroy_freelist(void);
539553
static void free_p5s(void);
540554

541-
#ifdef ZTS
555+
#ifdef MULTIPLE_THREADS
542556
static MUTEX_T dtoa_mutex;
543557
static MUTEX_T pow5mult_mutex;
544558
#endif /* ZTS */
545559

546-
ZEND_API int zend_startup_strtod(void) /* {{{ */
560+
ZEND_API int zend_strtod_activate(void) /* {{{ */
547561
{
548-
#ifdef ZTS
549-
dtoa_mutex = tsrm_mutex_alloc();
550-
pow5mult_mutex = tsrm_mutex_alloc();
551-
#endif
552562
return 1;
553563
}
554564
/* }}} */
555-
ZEND_API int zend_shutdown_strtod(void) /* {{{ */
565+
ZEND_API int zend_strtod_deactivate(void) /* {{{ */
556566
{
557567
destroy_freelist();
558568
free_p5s();
559569

560-
#ifdef ZTS
561-
tsrm_mutex_free(dtoa_mutex);
562-
dtoa_mutex = NULL;
563-
564-
tsrm_mutex_free(pow5mult_mutex);
565-
pow5mult_mutex = NULL;
566-
#endif
567570
return 1;
568571
}
569572
/* }}} */
@@ -627,11 +630,7 @@ Bfree
627630
{
628631
if (v) {
629632
if (v->k > Kmax)
630-
#ifdef FREE
631633
FREE((void*)v);
632-
#else
633-
free((void*)v);
634-
#endif
635634
else {
636635
ACQUIRE_DTOA_LOCK(0);
637636
v->next = freelist[v->k];
@@ -947,7 +946,9 @@ mult
947946
return c;
948947
}
949948

949+
#ifndef p5s
950950
static Bigint *p5s;
951+
#endif
951952

952953
static Bigint *
953954
pow5mult
@@ -3602,7 +3603,7 @@ zend_strtod
36023603
return sign ? -dval(&rv) : dval(&rv);
36033604
}
36043605

3605-
#ifndef MULTIPLE_THREADS
3606+
#if !defined(MULTIPLE_THREADS) && !defined(dtoa_result)
36063607
ZEND_TLS char *dtoa_result;
36073608
#endif
36083609

@@ -4616,7 +4617,7 @@ static void destroy_freelist(void)
46164617
Bigint **listp = &freelist[i];
46174618
while ((tmp = *listp) != NULL) {
46184619
*listp = tmp->next;
4619-
free(tmp);
4620+
FREE(tmp);
46204621
}
46214622
freelist[i] = NULL;
46224623
}
@@ -4631,7 +4632,8 @@ static void free_p5s(void)
46314632
listp = &p5s;
46324633
while ((tmp = *listp) != NULL) {
46334634
*listp = tmp->next;
4634-
free(tmp);
4635+
FREE(tmp);
46354636
}
4637+
p5s = NULL;
46364638
FREE_DTOA_LOCK(1)
46374639
}

Zend/zend_strtod.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,22 @@
2323
#include <zend.h>
2424

2525
BEGIN_EXTERN_C()
26+
#define ZEND_STRTOD_K_MAX 7
27+
typedef struct _zend_strtod_bigint zend_strtod_bigint;
28+
typedef struct _zend_strtod_state {
29+
zend_strtod_bigint *freelist[ZEND_STRTOD_K_MAX+1];
30+
zend_strtod_bigint *p5s;
31+
char *result;
32+
} zend_strtod_state;
2633
ZEND_API void zend_freedtoa(char *s);
2734
ZEND_API char *zend_dtoa(double _d, int mode, int ndigits, int *decpt, bool *sign, char **rve);
2835
ZEND_API char *zend_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
2936
ZEND_API double zend_strtod(const char *s00, const char **se);
3037
ZEND_API double zend_hex_strtod(const char *str, const char **endptr);
3138
ZEND_API double zend_oct_strtod(const char *str, const char **endptr);
3239
ZEND_API double zend_bin_strtod(const char *str, const char **endptr);
33-
ZEND_API int zend_startup_strtod(void);
34-
ZEND_API int zend_shutdown_strtod(void);
40+
ZEND_API int zend_strtod_activate(void);
41+
ZEND_API int zend_strtod_deactivate(void);
3542
END_EXTERN_C()
3643

3744
/* double limits */

Zend/zend_strtod_int.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,4 @@
104104
#endif
105105
#endif
106106

107-
#ifdef ZTS
108-
#define MULTIPLE_THREADS 1
109-
110-
#define ACQUIRE_DTOA_LOCK(x) \
111-
if (0 == x) { \
112-
tsrm_mutex_lock(dtoa_mutex); \
113-
} else if (1 == x) { \
114-
tsrm_mutex_lock(pow5mult_mutex); \
115-
}
116-
117-
#define FREE_DTOA_LOCK(x) \
118-
if (0 == x) { \
119-
tsrm_mutex_unlock(dtoa_mutex); \
120-
} else if (1 == x) { \
121-
tsrm_mutex_unlock(pow5mult_mutex); \
122-
}
123-
124-
125-
#endif
126-
127107
#endif /* ZEND_STRTOD_INT_H */

0 commit comments

Comments
 (0)