Skip to content

Commit 1cd4938

Browse files
committed
- remove double lock (tween colors cache is created in each thread, the
cache mutex is already locked earlier) - #40858, other TS improvements for gd freetype cache management cache initialization and shutdown is now done in MINIT and MSHUTDOWN.
1 parent 02f5db0 commit 1cd4938

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

ext/gd/gd.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,7 @@ zend_module_entry gd_module_entry = {
10781078
"gd",
10791079
gd_functions,
10801080
PHP_MINIT(gd),
1081-
#if HAVE_LIBT1
10821081
PHP_MSHUTDOWN(gd),
1083-
#else
1084-
NULL,
1085-
#endif
10861082
NULL,
10871083
#if HAVE_GD_STRINGFT && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE)
10881084
PHP_RSHUTDOWN(gd),
@@ -1126,16 +1122,20 @@ static void php_free_gd_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
11261122
}
11271123
/* }}} */
11281124

1129-
#if HAVE_LIBT1
1125+
11301126
/* {{{ PHP_MSHUTDOWN_FUNCTION
11311127
*/
11321128
PHP_MSHUTDOWN_FUNCTION(gd)
11331129
{
1130+
#if HAVE_LIBT1
11341131
T1_CloseLib();
1132+
#endif
1133+
#if HAVE_GD_FONTMUTEX
1134+
gdFontCacheMutexShutdown();
1135+
#endif
11351136
return SUCCESS;
11361137
}
11371138
/* }}} */
1138-
#endif
11391139

11401140

11411141
/* {{{ PHP_MINIT_FUNCTION
@@ -1144,6 +1144,9 @@ PHP_MINIT_FUNCTION(gd)
11441144
{
11451145
le_gd = zend_register_list_destructors_ex(php_free_gd_image, NULL, "gd", module_number);
11461146
le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number);
1147+
#if HAVE_GD_FONTMUTEX
1148+
gdFontCacheMutexSetup();
1149+
#endif
11471150
#if HAVE_LIBT1
11481151
T1_SetBitmapPad(8);
11491152
T1_InitLib(NO_LOGFILE | IGNORE_CONFIGFILE | IGNORE_FONTDATABASE);

ext/gd/libgd/gd.h

+8
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s,
297297
void gdImageString16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
298298
void gdImageStringUp16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
299299

300+
/*
301+
* The following functions are required to be called prior to the
302+
* use of any sort of threads in a module load / shutdown function
303+
* respectively.
304+
*/
305+
void gdFontCacheMutexSetup();
306+
void gdFontCacheMutexShutdown();
307+
300308
/* 2.0.16: for thread-safe use of gdImageStringFT and friends,
301309
* call this before allowing any thread to call gdImageStringFT.
302310
* Otherwise it is invoked by the first thread to invoke

ext/gd/libgd/gdft.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,8 @@ static char * gdft_draw_bitmap (gdCache_head_t *tc_cache, gdImage * im, int fg,
698698
} else {
699699
/* find antialised color */
700700
tc_key.bgcolor = *pixel;
701-
gdMutexLock(gdFontCacheMutex);
702701
tc_elem = (tweencolor_t *) gdCacheGet(tc_cache, &tc_key);
703702
*pixel = tc_elem->tweencolor;
704-
gdMutexUnlock(gdFontCacheMutex);
705703
}
706704
}
707705
}
@@ -722,7 +720,6 @@ void gdFontCacheShutdown()
722720
gdCacheDelete(fontCache);
723721
fontCache = NULL;
724722
gdMutexUnlock(gdFontCacheMutex);
725-
gdMutexShutdown(gdFontCacheMutex);
726723
FT_Done_FreeType(library);
727724
}
728725
}
@@ -732,15 +729,23 @@ void gdFreeFontCache()
732729
gdFontCacheShutdown();
733730
}
734731

732+
void gdFontCacheMutexSetup()
733+
{
734+
gdMutexSetup(gdFontCacheMutex);
735+
}
736+
737+
void gdFontCacheMutexShutdown()
738+
{
739+
gdMutexShutdown(gdFontCacheMutex);
740+
}
741+
735742
int gdFontCacheSetup(void)
736743
{
737744
if (fontCache) {
738745
/* Already set up */
739746
return 0;
740747
}
741-
gdMutexSetup(gdFontCacheMutex);
742748
if (FT_Init_FreeType(&library)) {
743-
gdMutexShutdown(gdFontCacheMutex);
744749
return -1;
745750
}
746751
fontCache = gdCacheCreate (FONTCACHESIZE, fontTest, fontFetch, fontRelease);
@@ -803,15 +808,16 @@ gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsi
803808

804809
/***** initialize font library and font cache on first call ******/
805810

811+
gdMutexLock(gdFontCacheMutex);
806812
if (!fontCache) {
807813
if (gdFontCacheSetup() != 0) {
808814
gdCacheDelete(tc_cache);
815+
gdMutexUnlock(gdFontCacheMutex);
809816
return "Failure to initialize font library";
810817
}
811818
}
812819
/*****/
813820

814-
gdMutexLock(gdFontCacheMutex);
815821
/* get the font (via font cache) */
816822
fontkey.fontlist = fontlist;
817823
fontkey.library = &library;

0 commit comments

Comments
 (0)