/*
*******************************************************************************
-* Copyright (C) 2003-2007, International Business Machines Corporation and *
+* Copyright (C) 2003-2013, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
-static const int32_t kMaxEra = 0; // only 1 era
+//static const int32_t kMaxEra = 0; // only 1 era
static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian)
}
#endif
-// default century
-const UDate BuddhistCalendar::fgSystemDefaultCentury = DBL_MIN;
-const int32_t BuddhistCalendar::fgSystemDefaultCenturyYear = -1;
-
-UDate BuddhistCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
-int32_t BuddhistCalendar::fgSystemDefaultCenturyStartYear = -1;
+/**
+ * The system maintains a static default century start date. This is initialized
+ * the first time it is used. Once the system default century date and year
+ * are set, they do not change.
+ */
+static UDate gSystemDefaultCenturyStart = DBL_MIN;
+static int32_t gSystemDefaultCenturyStartYear = -1;
+static icu::UInitOnce gBCInitOnce;
UBool BuddhistCalendar::haveDefaultCentury() const
return TRUE;
}
-UDate BuddhistCalendar::defaultCenturyStart() const
+static void U_CALLCONV
+initializeSystemDefaultCentury()
{
- return internalGetDefaultCenturyStart();
-}
-
-int32_t BuddhistCalendar::defaultCenturyStartYear() const
-{
- return internalGetDefaultCenturyStartYear();
-}
-
-UDate
-BuddhistCalendar::internalGetDefaultCenturyStart() const
-{
- // lazy-evaluate systemDefaultCenturyStart
- UBool needsUpdate;
- UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
-
- if (needsUpdate) {
- initializeSystemDefaultCentury();
+ // initialize systemDefaultCentury and systemDefaultCenturyYear based
+ // on the current time. They'll be set to 80 years before
+ // the current time.
+ 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);
+ gSystemDefaultCenturyStartYear = newYear;
+ gSystemDefaultCenturyStart = newStart;
}
-
- // use defaultCenturyStart unless it's the flag value;
- // then use systemDefaultCenturyStart
-
- return fgSystemDefaultCenturyStart;
+ // We have no recourse upon failure unless we want to propagate the failure
+ // out.
}
-int32_t
-BuddhistCalendar::internalGetDefaultCenturyStartYear() const
+UDate BuddhistCalendar::defaultCenturyStart() const
{
- // lazy-evaluate systemDefaultCenturyStartYear
- UBool needsUpdate;
- UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
-
- if (needsUpdate) {
- initializeSystemDefaultCentury();
- }
-
- // use defaultCenturyStart unless it's the flag value;
- // then use systemDefaultCenturyStartYear
-
- return fgSystemDefaultCenturyStartYear;
+ // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear
+ umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
+ return gSystemDefaultCenturyStart;
}
-void
-BuddhistCalendar::initializeSystemDefaultCentury()
+int32_t BuddhistCalendar::defaultCenturyStartYear() const
{
- // 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))
- {
- 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);
- }
- }
- // We have no recourse upon failure unless we want to propagate the failure
- // out.
- }
+ // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart
+ umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
+ return gSystemDefaultCenturyStartYear;
}