/src/icu/source/common/ucln_cmn.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ****************************************************************************** |
5 | | * Copyright (C) 2001-2014, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ****************************************************************************** |
8 | | * file name: ucln_cmn.cpp |
9 | | * encoding: UTF-8 |
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 "cmemory.h" |
20 | | #include "mutex.h" |
21 | | #include "uassert.h" |
22 | | #include "ucln.h" |
23 | | #include "ucln_cmn.h" |
24 | | #include "utracimp.h" |
25 | | #include "umutex.h" |
26 | | |
27 | | /** Auto-client for UCLN_COMMON **/ |
28 | | #define UCLN_TYPE_IS_COMMON |
29 | | #include "ucln_imp.h" |
30 | | |
31 | | static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT]; |
32 | | static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON]; |
33 | | |
34 | | |
35 | | /************************************************ |
36 | | The cleanup order is important in this function. |
37 | | Please be sure that you have read ucln.h |
38 | | ************************************************/ |
39 | | U_CAPI void U_EXPORT2 |
40 | | u_cleanup(void) |
41 | 1.35k | { |
42 | 1.35k | UTRACE_ENTRY_OC(UTRACE_U_CLEANUP); |
43 | 1.35k | umtx_lock(NULL); /* Force a memory barrier, so that we are sure to see */ |
44 | 1.35k | umtx_unlock(NULL); /* all state left around by any other threads. */ |
45 | | |
46 | 1.35k | ucln_lib_cleanup(); |
47 | | |
48 | 1.35k | cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */ |
49 | 1.35k | UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */ |
50 | | /*#if U_ENABLE_TRACING*/ |
51 | 1.35k | utrace_cleanup(); |
52 | | /*#endif*/ |
53 | 1.35k | } |
54 | | |
55 | | U_CAPI void U_EXPORT2 ucln_cleanupOne(ECleanupLibraryType libType) |
56 | 10.8k | { |
57 | 10.8k | if (gLibCleanupFunctions[libType]) |
58 | 0 | { |
59 | 0 | gLibCleanupFunctions[libType](); |
60 | 0 | gLibCleanupFunctions[libType] = NULL; |
61 | 0 | } |
62 | 10.8k | } |
63 | | |
64 | | U_CFUNC void |
65 | | ucln_common_registerCleanup(ECleanupCommonType type, |
66 | | cleanupFunc *func) |
67 | 6.70k | { |
68 | 6.70k | U_ASSERT(UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT); |
69 | 6.70k | if (UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT) |
70 | 6.70k | { |
71 | 6.70k | icu::Mutex m; // See ticket 10295 for discussion. |
72 | 6.70k | gCommonCleanupFunctions[type] = func; |
73 | 6.70k | } |
74 | | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
75 | | ucln_registerAutomaticCleanup(); |
76 | | #endif |
77 | 6.70k | } |
78 | | |
79 | | // Note: ucln_registerCleanup() is called with the ICU global mutex locked. |
80 | | // Be aware if adding anything to the function. |
81 | | // See ticket 10295 for discussion. |
82 | | |
83 | | U_CAPI void U_EXPORT2 |
84 | | ucln_registerCleanup(ECleanupLibraryType type, |
85 | | cleanupFunc *func) |
86 | 0 | { |
87 | 0 | U_ASSERT(UCLN_START < type && type < UCLN_COMMON); |
88 | 0 | if (UCLN_START < type && type < UCLN_COMMON) |
89 | 0 | { |
90 | 0 | gLibCleanupFunctions[type] = func; |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 1.35k | U_CFUNC UBool ucln_lib_cleanup(void) { |
95 | 1.35k | int32_t libType = UCLN_START; |
96 | 1.35k | int32_t commonFunc = UCLN_COMMON_START; |
97 | | |
98 | 12.1k | for (libType++; libType<UCLN_COMMON; libType++) { |
99 | 10.8k | ucln_cleanupOne(static_cast<ECleanupLibraryType>(libType)); |
100 | 10.8k | } |
101 | | |
102 | 31.0k | for (commonFunc++; commonFunc<UCLN_COMMON_COUNT; commonFunc++) { |
103 | 29.7k | if (gCommonCleanupFunctions[commonFunc]) |
104 | 5.58k | { |
105 | 5.58k | gCommonCleanupFunctions[commonFunc](); |
106 | 5.58k | gCommonCleanupFunctions[commonFunc] = NULL; |
107 | 5.58k | } |
108 | 29.7k | } |
109 | | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
110 | | ucln_unRegisterAutomaticCleanup(); |
111 | | #endif |
112 | 1.35k | return TRUE; |
113 | 1.35k | } |