]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ucln_cmn.c
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / common / ucln_cmn.c
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
4388f060 3* Copyright (C) 2001-2011, 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
A
18#include "ucln_cmn.h"
19#include "umutex.h"
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
4388f060
A
28U_CDECL_BEGIN
29
30UBool gICUInitialized = FALSE;
31UMTX gICUInitMutex = NULL;
32
33U_CDECL_END
34
374ca955 35static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT];
73c04bcf 36static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON];
b75a7d8f 37
4388f060
A
38/************************************************
39 The cleanup order is important in this function.
40 Please be sure that you have read ucln.h
41 ************************************************/
42U_CAPI void U_EXPORT2
43u_cleanup(void)
44{
45 UTRACE_ENTRY_OC(UTRACE_U_CLEANUP);
46 umtx_lock(NULL); /* Force a memory barrier, so that we are sure to see */
47 umtx_unlock(NULL); /* all state left around by any other threads. */
729e4ab9 48
4388f060 49 ucln_lib_cleanup();
729e4ab9 50
4388f060
A
51 umtx_destroy(&gICUInitMutex);
52 umtx_cleanup();
53 cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */
54 gICUInitialized = FALSE;
55 UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */
56/*#if U_ENABLE_TRACING*/
57 utrace_cleanup();
58/*#endif*/
59}
729e4ab9 60
4388f060 61U_CAPI void U_EXPORT2 ucln_cleanupOne(ECleanupLibraryType libType)
729e4ab9
A
62{
63 if (gLibCleanupFunctions[libType])
64 {
65 gLibCleanupFunctions[libType]();
66 gLibCleanupFunctions[libType] = NULL;
67 }
68}
69
73c04bcf
A
70U_CFUNC void
71ucln_common_registerCleanup(ECleanupCommonType type,
72 cleanupFunc *func)
b75a7d8f 73{
374ca955
A
74 U_ASSERT(UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT);
75 if (UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT)
b75a7d8f 76 {
374ca955 77 gCommonCleanupFunctions[type] = func;
b75a7d8f 78 }
729e4ab9
A
79#if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL))
80 ucln_registerAutomaticCleanup();
81#endif
b75a7d8f
A
82}
83
73c04bcf
A
84U_CAPI void U_EXPORT2
85ucln_registerCleanup(ECleanupLibraryType type,
86 cleanupFunc *func)
87{
88 U_ASSERT(UCLN_START < type && type < UCLN_COMMON);
89 if (UCLN_START < type && type < UCLN_COMMON)
90 {
91 gLibCleanupFunctions[type] = func;
92 }
93}
94
95U_CFUNC UBool ucln_lib_cleanup(void) {
96 ECleanupLibraryType libType = UCLN_START;
97 ECleanupCommonType commonFunc = UCLN_COMMON_START;
98
99 for (libType++; libType<UCLN_COMMON; libType++) {
4388f060 100 ucln_cleanupOne(libType);
73c04bcf 101 }
b75a7d8f 102
73c04bcf 103 for (commonFunc++; commonFunc<UCLN_COMMON_COUNT; commonFunc++) {
374ca955 104 if (gCommonCleanupFunctions[commonFunc])
b75a7d8f 105 {
374ca955
A
106 gCommonCleanupFunctions[commonFunc]();
107 gCommonCleanupFunctions[commonFunc] = NULL;
b75a7d8f 108 }
b75a7d8f 109 }
729e4ab9
A
110#if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL))
111 ucln_unRegisterAutomaticCleanup();
112#endif
374ca955 113 return TRUE;
b75a7d8f 114}