]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
57a6839d | 3 | * Copyright (C) 2001-2013, International Business Machines |
729e4ab9 | 4 | * Corporation and others. All Rights Reserved. |
b75a7d8f A |
5 | ****************************************************************************** |
6 | * file name: ucln_cmn.c | |
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 "utracimp.h" |
b75a7d8f | 18 | #include "ucln_cmn.h" |
57a6839d | 19 | #include "cmutex.h" |
b75a7d8f | 20 | #include "ucln.h" |
374ca955 A |
21 | #include "cmemory.h" |
22 | #include "uassert.h" | |
b75a7d8f | 23 | |
729e4ab9 | 24 | /** Auto-client for UCLN_COMMON **/ |
4388f060 | 25 | #define UCLN_TYPE_IS_COMMON |
729e4ab9 A |
26 | #include "ucln_imp.h" |
27 | ||
374ca955 | 28 | static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT]; |
73c04bcf | 29 | static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON]; |
b75a7d8f | 30 | |
51004dcb | 31 | |
4388f060 A |
32 | /************************************************ |
33 | The cleanup order is important in this function. | |
34 | Please be sure that you have read ucln.h | |
35 | ************************************************/ | |
36 | U_CAPI void U_EXPORT2 | |
37 | u_cleanup(void) | |
38 | { | |
39 | UTRACE_ENTRY_OC(UTRACE_U_CLEANUP); | |
40 | umtx_lock(NULL); /* Force a memory barrier, so that we are sure to see */ | |
41 | umtx_unlock(NULL); /* all state left around by any other threads. */ | |
729e4ab9 | 42 | |
4388f060 | 43 | ucln_lib_cleanup(); |
729e4ab9 | 44 | |
4388f060 | 45 | cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */ |
4388f060 A |
46 | UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */ |
47 | /*#if U_ENABLE_TRACING*/ | |
48 | utrace_cleanup(); | |
49 | /*#endif*/ | |
50 | } | |
729e4ab9 | 51 | |
4388f060 | 52 | U_CAPI void U_EXPORT2 ucln_cleanupOne(ECleanupLibraryType libType) |
729e4ab9 A |
53 | { |
54 | if (gLibCleanupFunctions[libType]) | |
55 | { | |
56 | gLibCleanupFunctions[libType](); | |
57 | gLibCleanupFunctions[libType] = NULL; | |
58 | } | |
59 | } | |
60 | ||
73c04bcf A |
61 | U_CFUNC void |
62 | ucln_common_registerCleanup(ECleanupCommonType type, | |
63 | cleanupFunc *func) | |
b75a7d8f | 64 | { |
374ca955 A |
65 | U_ASSERT(UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT); |
66 | if (UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT) | |
b75a7d8f | 67 | { |
374ca955 | 68 | gCommonCleanupFunctions[type] = func; |
b75a7d8f | 69 | } |
729e4ab9 A |
70 | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
71 | ucln_registerAutomaticCleanup(); | |
72 | #endif | |
b75a7d8f A |
73 | } |
74 | ||
73c04bcf A |
75 | U_CAPI void U_EXPORT2 |
76 | ucln_registerCleanup(ECleanupLibraryType type, | |
77 | cleanupFunc *func) | |
78 | { | |
79 | U_ASSERT(UCLN_START < type && type < UCLN_COMMON); | |
80 | if (UCLN_START < type && type < UCLN_COMMON) | |
81 | { | |
82 | gLibCleanupFunctions[type] = func; | |
83 | } | |
84 | } | |
85 | ||
86 | U_CFUNC UBool ucln_lib_cleanup(void) { | |
87 | ECleanupLibraryType libType = UCLN_START; | |
88 | ECleanupCommonType commonFunc = UCLN_COMMON_START; | |
89 | ||
90 | for (libType++; libType<UCLN_COMMON; libType++) { | |
4388f060 | 91 | ucln_cleanupOne(libType); |
73c04bcf | 92 | } |
b75a7d8f | 93 | |
73c04bcf | 94 | for (commonFunc++; commonFunc<UCLN_COMMON_COUNT; commonFunc++) { |
374ca955 | 95 | if (gCommonCleanupFunctions[commonFunc]) |
b75a7d8f | 96 | { |
374ca955 A |
97 | gCommonCleanupFunctions[commonFunc](); |
98 | gCommonCleanupFunctions[commonFunc] = NULL; | |
b75a7d8f | 99 | } |
b75a7d8f | 100 | } |
729e4ab9 A |
101 | #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) |
102 | ucln_unRegisterAutomaticCleanup(); | |
103 | #endif | |
374ca955 | 104 | return TRUE; |
b75a7d8f | 105 | } |