]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
b331163b | 3 | * Copyright (C) 2001-2014, International Business Machines |
729e4ab9 | 4 | * Corporation and others. All Rights Reserved. |
b75a7d8f | 5 | ****************************************************************************** |
b331163b | 6 | * file name: ucln_cmn.cpp |
b75a7d8f A |
7 | * encoding: US-ASCII |
8 | * tab size: 8 (not used) | |
9 | * indentation:4 | |
10 | * | |
11 | * created on: 2001July05 | |
12 | * created by: George Rhoten | |
13 | */ | |
14 | ||
15 | #include "unicode/utypes.h" | |
16 | #include "unicode/uclean.h" | |
374ca955 | 17 | #include "cmemory.h" |
b331163b | 18 | #include "mutex.h" |
374ca955 | 19 | #include "uassert.h" |
b331163b A |
20 | #include "ucln.h" |
21 | #include "ucln_cmn.h" | |
22 | #include "utracimp.h" | |
23 | #include "umutex.h" | |
b75a7d8f | 24 | |
729e4ab9 | 25 | /** Auto-client for UCLN_COMMON **/ |
4388f060 | 26 | #define UCLN_TYPE_IS_COMMON |
729e4ab9 A |
27 | #include "ucln_imp.h" |
28 | ||
374ca955 | 29 | static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT]; |
73c04bcf | 30 | static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON]; |
b75a7d8f | 31 | |
51004dcb | 32 | |
4388f060 A |
33 | /************************************************ |
34 | The cleanup order is important in this function. | |
35 | Please be sure that you have read ucln.h | |
36 | ************************************************/ | |
37 | U_CAPI void U_EXPORT2 | |
38 | u_cleanup(void) | |
39 | { | |
40 | UTRACE_ENTRY_OC(UTRACE_U_CLEANUP); | |
41 | umtx_lock(NULL); /* Force a memory barrier, so that we are sure to see */ | |
42 | umtx_unlock(NULL); /* all state left around by any other threads. */ | |
729e4ab9 | 43 | |
4388f060 | 44 | ucln_lib_cleanup(); |
729e4ab9 | 45 | |
4388f060 | 46 | cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */ |
4388f060 A |
47 | UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */ |
48 | /*#if U_ENABLE_TRACING*/ | |
49 | utrace_cleanup(); | |
50 | /*#endif*/ | |
51 | } | |
729e4ab9 | 52 | |
4388f060 | 53 | U_CAPI void U_EXPORT2 ucln_cleanupOne(ECleanupLibraryType libType) |
729e4ab9 A |
54 | { |
55 | if (gLibCleanupFunctions[libType]) | |
56 | { | |
57 | gLibCleanupFunctions[libType](); | |
58 | gLibCleanupFunctions[libType] = NULL; | |
59 | } | |
60 | } | |
61 | ||
73c04bcf A |
62 | U_CFUNC void |
63 | ucln_common_registerCleanup(ECleanupCommonType type, | |
64 | cleanupFunc *func) | |
b75a7d8f | 65 | { |
374ca955 A |
66 | U_ASSERT(UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT); |
67 | if (UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT) | |
b75a7d8f | 68 | { |
b331163b | 69 | icu::Mutex m; // See ticket 10295 for discussion. |
374ca955 | 70 | gCommonCleanupFunctions[type] = func; |
b75a7d8f | 71 | } |
729e4ab9 A |
72 | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
73 | ucln_registerAutomaticCleanup(); | |
74 | #endif | |
b75a7d8f A |
75 | } |
76 | ||
b331163b A |
77 | // Note: ucln_registerCleanup() is called with the ICU global mutex locked. |
78 | // Be aware if adding anything to the function. | |
79 | // See ticket 10295 for discussion. | |
80 | ||
73c04bcf A |
81 | U_CAPI void U_EXPORT2 |
82 | ucln_registerCleanup(ECleanupLibraryType type, | |
83 | cleanupFunc *func) | |
84 | { | |
85 | U_ASSERT(UCLN_START < type && type < UCLN_COMMON); | |
86 | if (UCLN_START < type && type < UCLN_COMMON) | |
87 | { | |
88 | gLibCleanupFunctions[type] = func; | |
89 | } | |
90 | } | |
91 | ||
92 | U_CFUNC UBool ucln_lib_cleanup(void) { | |
b331163b A |
93 | int32_t libType = UCLN_START; |
94 | int32_t commonFunc = UCLN_COMMON_START; | |
73c04bcf A |
95 | |
96 | for (libType++; libType<UCLN_COMMON; libType++) { | |
b331163b | 97 | ucln_cleanupOne(static_cast<ECleanupLibraryType>(libType)); |
73c04bcf | 98 | } |
b75a7d8f | 99 | |
73c04bcf | 100 | for (commonFunc++; commonFunc<UCLN_COMMON_COUNT; commonFunc++) { |
374ca955 | 101 | if (gCommonCleanupFunctions[commonFunc]) |
b75a7d8f | 102 | { |
374ca955 A |
103 | gCommonCleanupFunctions[commonFunc](); |
104 | gCommonCleanupFunctions[commonFunc] = NULL; | |
b75a7d8f | 105 | } |
b75a7d8f | 106 | } |
729e4ab9 A |
107 | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
108 | ucln_unRegisterAutomaticCleanup(); | |
109 | #endif | |
374ca955 | 110 | return TRUE; |
b75a7d8f | 111 | } |