Home | History | Annotate | Download | only in common
      1 /*
      2 ******************************************************************************
      3 * Copyright (C) 2001-2014, International Business Machines
      4 *                Corporation and others. All Rights Reserved.
      5 ******************************************************************************
      6 *   file name:  ucln_cmn.cpp
      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"
     17 #include "cmemory.h"
     18 #include "mutex.h"
     19 #include "uassert.h"
     20 #include "ucln.h"
     21 #include "ucln_cmn.h"
     22 #include "utracimp.h"
     23 #include "umutex.h"
     24 
     25 /**  Auto-client for UCLN_COMMON **/
     26 #define UCLN_TYPE_IS_COMMON
     27 #include "ucln_imp.h"
     28 
     29 static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT];
     30 static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON];
     31 
     32 
     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.        */
     43 
     44     ucln_lib_cleanup();
     45 
     46     cmemory_cleanup();       /* undo any heap functions set by u_setMemoryFunctions(). */
     47     UTRACE_EXIT();           /* Must be before utrace_cleanup(), which turns off tracing. */
     48 /*#if U_ENABLE_TRACING*/
     49     utrace_cleanup();
     50 /*#endif*/
     51 }
     52 
     53 U_CAPI void U_EXPORT2 ucln_cleanupOne(ECleanupLibraryType libType)
     54 {
     55     if (gLibCleanupFunctions[libType])
     56     {
     57         gLibCleanupFunctions[libType]();
     58         gLibCleanupFunctions[libType] = NULL;
     59     }
     60 }
     61 
     62 U_CFUNC void
     63 ucln_common_registerCleanup(ECleanupCommonType type,
     64                             cleanupFunc *func)
     65 {
     66     U_ASSERT(UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT);
     67     if (UCLN_COMMON_START < type && type < UCLN_COMMON_COUNT)
     68     {
     69         icu::Mutex m;     // See ticket 10295 for discussion.
     70         gCommonCleanupFunctions[type] = func;
     71     }
     72 #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL))
     73     ucln_registerAutomaticCleanup();
     74 #endif
     75 }
     76 
     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 
     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) {
     93     int32_t libType = UCLN_START;
     94     int32_t commonFunc = UCLN_COMMON_START;
     95 
     96     for (libType++; libType<UCLN_COMMON; libType++) {
     97         ucln_cleanupOne(static_cast<ECleanupLibraryType>(libType));
     98     }
     99 
    100     for (commonFunc++; commonFunc<UCLN_COMMON_COUNT; commonFunc++) {
    101         if (gCommonCleanupFunctions[commonFunc])
    102         {
    103             gCommonCleanupFunctions[commonFunc]();
    104             gCommonCleanupFunctions[commonFunc] = NULL;
    105         }
    106     }
    107 #if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL))
    108     ucln_unRegisterAutomaticCleanup();
    109 #endif
    110     return TRUE;
    111 }
    112