From 4e837093157939ec53ec2865aefa9cb21c7e27cb Mon Sep 17 00:00:00 2001 From: Alessandro Pignotti Date: Fri, 5 Oct 2012 16:49:36 -0700 Subject: [PATCH] Improve thread safety of localization --- src/xlibi18n/lcWrap.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/xlibi18n/lcWrap.c b/src/xlibi18n/lcWrap.c index c3f5ca5..6809da2 100644 --- a/src/xlibi18n/lcWrap.c +++ b/src/xlibi18n/lcWrap.c @@ -57,6 +57,7 @@ from The Open Group. #include #endif #include +#include #include "Xlibint.h" #include "Xlcint.h" #include @@ -173,6 +174,7 @@ typedef struct _XLCdListRec { } XLCdListRec, *XLCdList; static XLCdList lcd_list = NULL; +static pthread_mutex_t lcd_list_mutex = PTHREAD_MUTEX_INITIALIZER; typedef struct _XlcLoaderListRec { struct _XlcLoaderListRec *next; @@ -180,6 +182,8 @@ typedef struct _XlcLoaderListRec { } XlcLoaderListRec, *XlcLoaderList; static XlcLoaderList loader_list = NULL; +static pthread_mutex_t loader_list_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t opencloseLC_mutex = PTHREAD_MUTEX_INITIALIZER; void _XlcRemoveLoader( @@ -187,13 +191,18 @@ _XlcRemoveLoader( { XlcLoaderList loader, prev; + pthread_mutex_lock(&loader_list_mutex); if (loader_list == NULL) + { + pthread_mutex_unlock(&loader_list_mutex); return; + } prev = loader = loader_list; if (loader->proc == proc) { loader_list = loader->next; Xfree(loader); + pthread_mutex_unlock(&loader_list_mutex); return; } @@ -201,11 +210,13 @@ _XlcRemoveLoader( if (loader->proc == proc) { prev->next = loader->next; Xfree(loader); + pthread_mutex_unlock(&loader_list_mutex); return; } prev = loader; } + pthread_mutex_unlock(&loader_list_mutex); return; } @@ -224,6 +235,7 @@ _XlcAddLoader( loader->proc = proc; + pthread_mutex_lock(&loader_list_mutex); if (loader_list == NULL) position = XlcHead; @@ -238,6 +250,7 @@ _XlcAddLoader( loader->next = NULL; last->next = loader; } + pthread_mutex_unlock(&loader_list_mutex); return True; } @@ -276,17 +289,23 @@ _XOpenLC( /* * search for needed lcd, if found return it */ + pthread_mutex_lock(&lcd_list_mutex); for (cur = lcd_list; cur; cur = cur->next) { if (!strcmp (cur->lcd->core->name, name)) { lcd = cur->lcd; cur->ref_count++; + pthread_mutex_unlock(&lcd_list_mutex); goto found; } } + pthread_mutex_unlock(&lcd_list_mutex); + pthread_mutex_lock(&opencloseLC_mutex); if (!loader_list) _XlcInitLoader(); + pthread_mutex_unlock(&opencloseLC_mutex); + pthread_mutex_lock(&loader_list_mutex); /* * not there, so try to get and add to list */ @@ -297,16 +316,20 @@ _XOpenLC( if (cur) { cur->lcd = lcd; cur->ref_count = 1; + pthread_mutex_lock(&lcd_list_mutex); cur->next = lcd_list; lcd_list = cur; + pthread_mutex_unlock(&lcd_list_mutex); } else { (*lcd->methods->close)(lcd); lcd = (XLCd) NULL; } + pthread_mutex_unlock(&loader_list_mutex); goto found; } } + pthread_mutex_unlock(&loader_list_mutex); lcd = NULL; found: @@ -325,6 +348,7 @@ _XCloseLC( { XLCdList cur, *prev; + pthread_mutex_lock(&lcd_list_mutex); for (prev = &lcd_list; (cur = *prev); prev = &cur->next) { if (cur->lcd == lcd) { if (--cur->ref_count < 1) { @@ -335,11 +359,14 @@ _XCloseLC( break; } } + pthread_mutex_unlock(&lcd_list_mutex); + pthread_mutex_lock(&opencloseLC_mutex); if(loader_list) { _XlcDeInitLoader(); loader_list = NULL; } + pthread_mutex_unlock(&opencloseLC_mutex); } /* -- 1.7.9.5