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