/*
************************************************************************************
- * Copyright (C) 2006-2014, International Business Machines Corporation
+ * Copyright (C) 2006-2016, International Business Machines Corporation
* and others. All Rights Reserved.
************************************************************************************
*/
#if !UCONFIG_NO_BREAK_ITERATION
#include "brkeng.h"
+#include "cmemory.h"
#include "dictbe.h"
#include "unicode/uchar.h"
#include "unicode/uniset.h"
#include "unicode/bytestrie.h"
#include "charstr.h"
#include "dictionarydata.h"
+#include "mutex.h"
#include "uvector.h"
#include "umutex.h"
#include "uresimp.h"
*/
UnhandledEngine::UnhandledEngine(UErrorCode &/*status*/) {
- for (int32_t i = 0; i < (int32_t)(sizeof(fHandled)/sizeof(fHandled[0])); ++i) {
+ for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
fHandled[i] = 0;
}
}
UnhandledEngine::~UnhandledEngine() {
- for (int32_t i = 0; i < (int32_t)(sizeof(fHandled)/sizeof(fHandled[0])); ++i) {
+ for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
if (fHandled[i] != 0) {
delete fHandled[i];
}
UBool
UnhandledEngine::handles(UChar32 c, int32_t breakType) const {
- return (breakType >= 0 && breakType < (int32_t)(sizeof(fHandled)/sizeof(fHandled[0]))
+ return (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)
&& fHandled[breakType] != 0 && fHandled[breakType]->contains(c));
}
UBool reverse,
int32_t breakType,
UStack &/*foundBreaks*/ ) const {
- if (breakType >= 0 && breakType < (int32_t)(sizeof(fHandled)/sizeof(fHandled[0]))) {
+ if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
UChar32 c = utext_current32(text);
if (reverse) {
while((int32_t)utext_getNativeIndex(text) > startPos && fHandled[breakType]->contains(c)) {
void
UnhandledEngine::handleCharacter(UChar32 c, int32_t breakType) {
- if (breakType >= 0 && breakType < (int32_t)(sizeof(fHandled)/sizeof(fHandled[0]))) {
+ if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
if (fHandled[breakType] == 0) {
fHandled[breakType] = new UnicodeSet();
if (fHandled[breakType] == 0) {
U_CDECL_END
U_NAMESPACE_BEGIN
+static UMutex gBreakEngineMutex = U_MUTEX_INITIALIZER;
+
const LanguageBreakEngine *
ICULanguageBreakFactory::getEngineFor(UChar32 c, int32_t breakType) {
- UBool needsInit;
- int32_t i;
const LanguageBreakEngine *lbe = NULL;
UErrorCode status = U_ZERO_ERROR;
- // TODO: The global mutex should not be used.
- // The global mutex should only be used for short periods.
- // A ICULanguageBreakFactory specific mutex should be used.
- umtx_lock(NULL);
- needsInit = (UBool)(fEngines == NULL);
- if (!needsInit) {
- i = fEngines->size();
+ Mutex m(&gBreakEngineMutex);
+
+ if (fEngines == NULL) {
+ UStack *engines = new UStack(_deleteEngine, NULL, status);
+ if (U_FAILURE(status) || engines == NULL) {
+ // Note: no way to return error code to caller.
+ delete engines;
+ return NULL;
+ }
+ fEngines = engines;
+ } else {
+ int32_t i = fEngines->size();
while (--i >= 0) {
lbe = (const LanguageBreakEngine *)(fEngines->elementAt(i));
if (lbe != NULL && lbe->handles(c, breakType)) {
- break;
+ return lbe;
}
- lbe = NULL;
}
}
- umtx_unlock(NULL);
+ // We didn't find an engine. Create one.
+ lbe = loadEngineFor(c, breakType);
if (lbe != NULL) {
- return lbe;
- }
-
- if (needsInit) {
- UStack *engines = new UStack(_deleteEngine, NULL, status);
- if (U_SUCCESS(status) && engines == NULL) {
- status = U_MEMORY_ALLOCATION_ERROR;
- }
- else if (U_FAILURE(status)) {
- delete engines;
- engines = NULL;
- }
- else {
- umtx_lock(NULL);
- if (fEngines == NULL) {
- fEngines = engines;
- engines = NULL;
- }
- umtx_unlock(NULL);
- delete engines;
- }
- }
-
- if (fEngines == NULL) {
- return NULL;
+ fEngines->push((void *)lbe, status);
}
-
- // We didn't find an engine the first time through, or there was no
- // stack. Create an engine.
- const LanguageBreakEngine *newlbe = loadEngineFor(c, breakType);
-
- // Now get the lock, and see if someone else has created it in the
- // meantime
- umtx_lock(NULL);
- i = fEngines->size();
- while (--i >= 0) {
- lbe = (const LanguageBreakEngine *)(fEngines->elementAt(i));
- if (lbe != NULL && lbe->handles(c, breakType)) {
- break;
- }
- lbe = NULL;
- }
- if (lbe == NULL && newlbe != NULL) {
- fEngines->push((void *)newlbe, status);
- lbe = newlbe;
- newlbe = NULL;
- }
- umtx_unlock(NULL);
-
- delete newlbe;
-
return lbe;
}