ICU-6481 fix threading issues in calendar initialization code

X-SVN-Rev: 24445
This commit is contained in:
Andy Heninger 2008-08-05 22:19:05 +00:00
parent 3d1a4a3fce
commit 38b7c3e8a7
9 changed files with 103 additions and 126 deletions

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2003-2007, International Business Machines Corporation and *
* Copyright (C) 2003-2008, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -187,27 +187,23 @@ BuddhistCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
if (U_SUCCESS(status))
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}

View File

@ -837,27 +837,24 @@ ChineseCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
ChineseCalendar calendar(Locale("@calendar=chinese"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
ChineseCalendar calendar(Locale("@calendar=chinese"),status);
if (U_SUCCESS(status))
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChineseCalendar)

View File

@ -141,8 +141,8 @@ CopticCalendar::initializeSystemDefaultCentury()
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
umtx_unlock(NULL);
}
}

View File

@ -178,8 +178,8 @@ EthiopicCalendar::initializeSystemDefaultCentury()
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
umtx_unlock(NULL);
}
}

View File

@ -1313,29 +1313,26 @@ GregorianCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
Calendar *calendar = new GregorianCalendar(status);
if (calendar != NULL && U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
Calendar *calendar = new GregorianCalendar(status);
if (calendar != NULL && U_SUCCESS(status))
{
calendar->setTime(Calendar::getNow(), status);
calendar->add(UCAL_YEAR, -80, status);
calendar->setTime(Calendar::getNow(), status);
calendar->add(UCAL_YEAR, -80, status);
UDate newStart = calendar->getTime(status);
int32_t newYear = calendar->get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
delete calendar;
UDate newStart = calendar->getTime(status);
int32_t newYear = calendar->get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
{
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
delete calendar;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}

View File

@ -731,27 +731,23 @@ HebrewCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
HebrewCalendar calendar(Locale("@calendar=hebrew"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
HebrewCalendar calendar(Locale("@calendar=hebrew"),status);
if (U_SUCCESS(status))
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(HebrewCalendar)

View File

@ -517,27 +517,24 @@ IslamicCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status);
if (U_SUCCESS(status))
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar)

View File

@ -437,27 +437,24 @@ PersianCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
PersianCalendar calendar(Locale("@calendar=persian"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
PersianCalendar calendar(Locale("@calendar=persian"),status);
if (U_SUCCESS(status))
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)

View File

@ -194,27 +194,24 @@ TaiwanCalendar::initializeSystemDefaultCentury()
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
// No point in locking as it should be idempotent.
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
UErrorCode status = U_ZERO_ERROR;
TaiwanCalendar calendar(Locale("@calendar=roc"),status);
if (U_SUCCESS(status))
{
UErrorCode status = U_ZERO_ERROR;
TaiwanCalendar calendar(Locale("@calendar=roc"),status);
if (U_SUCCESS(status))
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
umtx_lock(NULL);
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
{
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
{
umtx_lock(NULL);
fgSystemDefaultCenturyStart = newStart;
fgSystemDefaultCenturyStartYear = newYear;
umtx_unlock(NULL);
}
fgSystemDefaultCenturyStartYear = newYear;
fgSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
umtx_unlock(NULL);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}