From 38b7c3e8a7f08fcebc616f90c23a55126d80fb97 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Tue, 5 Aug 2008 22:19:05 +0000 Subject: [PATCH] ICU-6481 fix threading issues in calendar initialization code X-SVN-Rev: 24445 --- icu4c/source/i18n/buddhcal.cpp | 34 +++++++++++++++------------------ icu4c/source/i18n/chnsecal.cpp | 31 ++++++++++++++---------------- icu4c/source/i18n/coptccal.cpp | 2 +- icu4c/source/i18n/ethpccal.cpp | 2 +- icu4c/source/i18n/gregocal.cpp | 35 ++++++++++++++++------------------ icu4c/source/i18n/hebrwcal.cpp | 32 ++++++++++++++----------------- icu4c/source/i18n/islamcal.cpp | 31 ++++++++++++++---------------- icu4c/source/i18n/persncal.cpp | 31 ++++++++++++++---------------- icu4c/source/i18n/taiwncal.cpp | 31 ++++++++++++++---------------- 9 files changed, 103 insertions(+), 126 deletions(-) diff --git a/icu4c/source/i18n/buddhcal.cpp b/icu4c/source/i18n/buddhcal.cpp index a887cbff02..92f726c302 100644 --- a/icu4c/source/i18n/buddhcal.cpp +++ b/icu4c/source/i18n/buddhcal.cpp @@ -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. } diff --git a/icu4c/source/i18n/chnsecal.cpp b/icu4c/source/i18n/chnsecal.cpp index 52cb6c86d9..337903189f 100644 --- a/icu4c/source/i18n/chnsecal.cpp +++ b/icu4c/source/i18n/chnsecal.cpp @@ -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) diff --git a/icu4c/source/i18n/coptccal.cpp b/icu4c/source/i18n/coptccal.cpp index eb33aec8ec..a5d0412820 100644 --- a/icu4c/source/i18n/coptccal.cpp +++ b/icu4c/source/i18n/coptccal.cpp @@ -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); } } diff --git a/icu4c/source/i18n/ethpccal.cpp b/icu4c/source/i18n/ethpccal.cpp index 2ab69bdcc4..44cd7f470f 100644 --- a/icu4c/source/i18n/ethpccal.cpp +++ b/icu4c/source/i18n/ethpccal.cpp @@ -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); } } diff --git a/icu4c/source/i18n/gregocal.cpp b/icu4c/source/i18n/gregocal.cpp index a3e6423142..842ff769ed 100644 --- a/icu4c/source/i18n/gregocal.cpp +++ b/icu4c/source/i18n/gregocal.cpp @@ -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. } diff --git a/icu4c/source/i18n/hebrwcal.cpp b/icu4c/source/i18n/hebrwcal.cpp index abdf17fd89..3a6ef43941 100644 --- a/icu4c/source/i18n/hebrwcal.cpp +++ b/icu4c/source/i18n/hebrwcal.cpp @@ -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) diff --git a/icu4c/source/i18n/islamcal.cpp b/icu4c/source/i18n/islamcal.cpp index 84c787ffee..c70dbe7087 100644 --- a/icu4c/source/i18n/islamcal.cpp +++ b/icu4c/source/i18n/islamcal.cpp @@ -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) diff --git a/icu4c/source/i18n/persncal.cpp b/icu4c/source/i18n/persncal.cpp index cc7439c739..51303417aa 100644 --- a/icu4c/source/i18n/persncal.cpp +++ b/icu4c/source/i18n/persncal.cpp @@ -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) diff --git a/icu4c/source/i18n/taiwncal.cpp b/icu4c/source/i18n/taiwncal.cpp index d5b72e47aa..4a895251df 100644 --- a/icu4c/source/i18n/taiwncal.cpp +++ b/icu4c/source/i18n/taiwncal.cpp @@ -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. }