diff options
author | Adam Sitnik <adam.sitnik@gmail.com> | 2019-06-09 13:39:29 +0200 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2019-06-09 04:39:29 -0700 |
commit | 563d5c4570575b25bd86cb14be2bad5f23238f95 (patch) | |
tree | c0ee2ab2c8c0347c1a05af8575cde3f4b366c80b /src/corefx | |
parent | b1a841249bd23d94a5c88684d57a0dbdd92cac86 (diff) | |
download | coreclr-563d5c4570575b25bd86cb14be2bad5f23238f95.tar.gz coreclr-563d5c4570575b25bd86cb14be2bad5f23238f95.tar.bz2 coreclr-563d5c4570575b25bd86cb14be2bad5f23238f95.zip |
don't acquire lock if the collator has been already created (#24973)
* don't acquire lock if the collator has been already created
* use atomic_compare_exchange_strong instead of __sync_bool_compare_and_swap
* don't try to workaround clang 5.0 bug, just use __atomic_compare_exchange_n
Diffstat (limited to 'src/corefx')
-rw-r--r-- | src/corefx/System.Globalization.Native/config.h.in | 2 | ||||
-rw-r--r-- | src/corefx/System.Globalization.Native/pal_collation.c | 39 |
2 files changed, 16 insertions, 25 deletions
diff --git a/src/corefx/System.Globalization.Native/config.h.in b/src/corefx/System.Globalization.Native/config.h.in index 633bcfbd0f..89399903e8 100644 --- a/src/corefx/System.Globalization.Native/config.h.in +++ b/src/corefx/System.Globalization.Native/config.h.in @@ -1,4 +1,4 @@ #pragma once #cmakedefine01 HAVE_UDAT_STANDALONE_SHORTER_WEEKDAYS -#cmakedefine01 HAVE_SET_MAX_VARIABLE +#cmakedefine01 HAVE_SET_MAX_VARIABLE
\ No newline at end of file diff --git a/src/corefx/System.Globalization.Native/pal_collation.c b/src/corefx/System.Globalization.Native/pal_collation.c index 5b270e659c..675d80521b 100644 --- a/src/corefx/System.Globalization.Native/pal_collation.c +++ b/src/corefx/System.Globalization.Native/pal_collation.c @@ -4,7 +4,7 @@ // #include <assert.h> -#include <pthread.h> +#include <stdbool.h> #include <stdlib.h> #include <stdint.h> #include <search.h> @@ -42,7 +42,6 @@ typedef struct { int32_t key; UCollator* UCollator; } TCollatorMap; */ struct SortHandle { - pthread_mutex_t collatorsLockObject; UCollator* collatorsPerOption[CompareOptionsMask + 1]; }; @@ -342,12 +341,6 @@ void CreateSortHandle(SortHandle** ppSortHandle) } memset(*ppSortHandle, 0, sizeof(SortHandle)); - - int result = pthread_mutex_init(&(*ppSortHandle)->collatorsLockObject, NULL); - if (result != 0) - { - assert(FALSE && "Unexpected pthread_mutex_init return value."); - } } ResultCode GlobalizationNative_GetSortHandle(const char* lpLocaleName, SortHandle** ppSortHandle) @@ -366,7 +359,6 @@ ResultCode GlobalizationNative_GetSortHandle(const char* lpLocaleName, SortHandl if (U_FAILURE(err)) { - pthread_mutex_destroy(&(*ppSortHandle)->collatorsLockObject); free(*ppSortHandle); (*ppSortHandle) = NULL; } @@ -385,38 +377,37 @@ void GlobalizationNative_CloseSortHandle(SortHandle* pSortHandle) } } - pthread_mutex_destroy(&pSortHandle->collatorsLockObject); - free(pSortHandle); } const UCollator* GetCollatorFromSortHandle(SortHandle* pSortHandle, int32_t options, UErrorCode* pErr) { - UCollator* pCollator; if (options == 0) { - pCollator = pSortHandle->collatorsPerOption[0]; + return pSortHandle->collatorsPerOption[0]; } else { - int lockResult = pthread_mutex_lock(&pSortHandle->collatorsLockObject); - if (lockResult != 0) + options &= CompareOptionsMask; + UCollator* pCollator = pSortHandle->collatorsPerOption[options]; + if (pCollator != NULL) { - assert(FALSE && "Unexpected pthread_mutex_lock return value."); + return pCollator; } - options &= CompareOptionsMask; - pCollator = pSortHandle->collatorsPerOption[options]; - if (pCollator == NULL) + pCollator = CloneCollatorWithOptions(pSortHandle->collatorsPerOption[0], options, pErr); + UCollator* pNull = NULL; + + // we are not using the standard atomic_compare_exchange_strong to workaround bugs in clang 5.0 (https://bugs.llvm.org/show_bug.cgi?id=37457) + if (!__atomic_compare_exchange_n(&pSortHandle->collatorsPerOption[options], &pNull, pCollator, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { - pCollator = CloneCollatorWithOptions(pSortHandle->collatorsPerOption[0], options, pErr); - pSortHandle->collatorsPerOption[options] = pCollator; + ucol_close(pCollator); + pCollator = pSortHandle->collatorsPerOption[options]; + assert(pCollator != NULL && "pCollator not expected to be null here."); } - pthread_mutex_unlock(&pSortHandle->collatorsLockObject); + return pCollator; } - - return pCollator; } int32_t GlobalizationNative_GetSortVersion(SortHandle* pSortHandle) |