1/*
2******************************************************************************
3*                                                                            *
4* Copyright (C) 2001-2010, International Business Machines                   *
5*                Corporation and others. All Rights Reserved.                *
6*                                                                            *
7******************************************************************************
8*   file name:  uinit.c
9*   encoding:   US-ASCII
10*   tab size:   8 (not used)
11*   indentation:4
12*
13*   created on: 2001July05
14*   created by: George Rhoten
15*/
16
17#include "unicode/utypes.h"
18#include "unicode/uclean.h"
19#include "utracimp.h"
20#include "ustr_imp.h"
21#include "unormimp.h"
22#include "ucln_cmn.h"
23#include "ucnv_io.h"
24#include "umutex.h"
25#include "ucln.h"
26#include "cmemory.h"
27#include "uassert.h"
28#include "unicode/icuplug.h"
29#include "icuplugimp.h"
30
31
32static UBool gICUInitialized = FALSE;
33static UMTX  gICUInitMutex   = NULL;
34
35
36/************************************************
37 The cleanup order is important in this function.
38 Please be sure that you have read ucln.h
39 ************************************************/
40U_CAPI void U_EXPORT2
41u_cleanup(void)
42{
43    UTRACE_ENTRY_OC(UTRACE_U_CLEANUP);
44    umtx_lock(NULL);     /* Force a memory barrier, so that we are sure to see   */
45    umtx_unlock(NULL);   /*   all state left around by any other threads.        */
46
47    ucln_lib_cleanup();
48
49    umtx_destroy(&gICUInitMutex);
50    umtx_cleanup();
51    cmemory_cleanup();       /* undo any heap functions set by u_setMemoryFunctions(). */
52    gICUInitialized = FALSE;
53    UTRACE_EXIT();           /* Must be before utrace_cleanup(), which turns off tracing. */
54/*#if U_ENABLE_TRACING*/
55    utrace_cleanup();
56/*#endif*/
57}
58
59/*
60 *
61 *   ICU Initialization Function.  Force loading and/or initialization of
62 *           any shared data that could potentially be used concurrently
63 *           by multiple threads.
64 */
65U_CAPI void U_EXPORT2
66u_init(UErrorCode *status) {
67    UTRACE_ENTRY_OC(UTRACE_U_INIT);
68    /* initialize plugins */
69    uplug_init(status);
70
71    umtx_lock(&gICUInitMutex);
72    if (gICUInitialized || U_FAILURE(*status)) {
73        umtx_unlock(&gICUInitMutex);
74        UTRACE_EXIT_STATUS(*status);
75        return;
76    }
77
78#if 1
79    /*
80     * 2005-may-02
81     *
82     * ICU4C 3.4 (jitterbug 4497) hardcodes the data for Unicode character
83     * properties for APIs that want to be fast.
84     * Therefore, we need not load them here nor check for errors.
85     * Instead, we load the converter alias table to see if any ICU data
86     * is available.
87     * Users should really open the service objects they need and check
88     * for errors there, to make sure that the actual items they need are
89     * available.
90     */
91#if !UCONFIG_NO_CONVERSION
92    ucnv_io_countKnownConverters(status);
93#endif
94#else
95    /* Do any required init for services that don't have open operations
96     * and use "only" the double-check initialization method for performance
97     * reasons (avoiding a mutex lock even for _checking_ whether the
98     * initialization had occurred).
99     */
100
101    /* TODO:  Completely remove this section.  u_init() is now publicly being
102     *        advertised as never being required for thread safety, and we cannot
103     *        bring back this requirement.
104     */
105
106    /* Char Properties */
107    uprv_haveProperties(status);
108
109    /* load the case and bidi properties but don't fail if they are not available */
110    u_isULowercase(0x61);
111    u_getIntPropertyValue(0x200D, UCHAR_JOINING_TYPE); /* ZERO WIDTH JOINER: Join_Causing */
112
113#if !UCONFIG_NO_NORMALIZATION
114    /*  Normalization  */
115    unorm_haveData(status);
116#endif
117#endif
118
119    gICUInitialized = TRUE;    /* TODO:  don't set if U_FAILURE? */
120    umtx_unlock(&gICUInitMutex);
121    UTRACE_EXIT_STATUS(*status);
122}
123
124