//
#include <assert.h>
-#include <pthread.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <search.h>
*/
struct SortHandle
{
- pthread_mutex_t collatorsLockObject;
UCollator* collatorsPerOption[CompareOptionsMask + 1];
};
}
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)
if (U_FAILURE(err))
{
- pthread_mutex_destroy(&(*ppSortHandle)->collatorsLockObject);
free(*ppSortHandle);
(*ppSortHandle) = NULL;
}
}
}
- 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)