1/*
2******************************************************************************
3*
4* Copyright (C) 2009-2011, International Business Machines
5*                Corporation and others. All Rights Reserved.
6*
7******************************************************************************
8*   file name:  ucln_imp.h
9*   encoding:   US-ASCII
10*   tab size:   8 (not used)
11*   indentation:4
12*
13*   This file contains the platform specific implementation of per-library cleanup.
14*
15*/
16
17
18#ifndef __UCLN_IMP_H__
19#define __UCLN_IMP_H__
20
21#include "ucln.h"
22#include <stdlib.h>
23
24/**
25 * Auto cleanup of ICU libraries
26 * There are several methods in per library cleanup of icu libraries:
27 * 1) Compiler/Platform based cleanup:
28 *   a) Windows MSVC uses DllMain()
29 *   b) GCC uses destructor function attribute
30 *   c) Sun Studio, AIX VA, and HP-UX aCC uses a linker option to set the exit function
31 * 2) Using atexit()
32 * 3) Implementing own automatic cleanup functions
33 *
34 * For option 1, ensure that UCLN_NO_AUTO_CLEANUP is set to 0 by using --enable-auto-cleanup
35 * configure option or by otherwise setting UCLN_NO_AUTO_CLEANUP to 0
36 * For option 2, follow option 1 and also define UCLN_AUTO_ATEXIT
37 * For option 3, follow option 1 and also define UCLN_AUTO_LOCAL (see below for more information)
38 */
39
40#if !UCLN_NO_AUTO_CLEANUP
41
42/*
43 * The following declarations are for when UCLN_AUTO_LOCAL or UCLN_AUTO_ATEXIT
44 * are defined. They are commented out because they are static and will be defined
45 * later. The information is still here to provide some guidance for the developer
46 * who chooses to use UCLN_AUTO_LOCAL.
47 */
48/**
49 * Give the library an opportunity to register an automatic cleanup.
50 * This may be called more than once.
51 */
52/*static void ucln_registerAutomaticCleanup();*/
53/**
54 * Unregister an automatic cleanup, if possible. Called from cleanup.
55 */
56/*static void ucln_unRegisterAutomaticCleanup();*/
57
58#ifdef UCLN_TYPE_IS_COMMON
59#   define UCLN_CLEAN_ME_UP u_cleanup()
60#else
61#   define UCLN_CLEAN_ME_UP ucln_cleanupOne(UCLN_TYPE)
62#endif
63
64/* ------------ automatic cleanup: registration. Choose ONE ------- */
65#if defined(UCLN_AUTO_LOCAL)
66/* To use:
67 *  1. define UCLN_AUTO_LOCAL,
68 *  2. create ucln_local_hook.c containing implementations of
69 *           static void ucln_registerAutomaticCleanup()
70 *           static void ucln_unRegisterAutomaticCleanup()
71 */
72#include "ucln_local_hook.c"
73
74#elif defined(UCLN_AUTO_ATEXIT)
75/*
76 * Use the ANSI C 'atexit' function. Note that this mechanism does not
77 * guarantee the order of cleanup relative to other users of ICU!
78 */
79static UBool gAutoCleanRegistered = FALSE;
80
81static void ucln_atexit_handler()
82{
83    UCLN_CLEAN_ME_UP;
84}
85
86static void ucln_registerAutomaticCleanup()
87{
88    if(!gAutoCleanRegistered) {
89        gAutoCleanRegistered = TRUE;
90        atexit(&ucln_atexit_handler);
91    }
92}
93
94static void ucln_unRegisterAutomaticCleanup () {
95}
96/* ------------end of automatic cleanup: registration. ------- */
97
98#elif defined (UCLN_FINI)
99/**
100 * If UCLN_FINI is defined, it is the (versioned, etc) name of a cleanup
101 * entrypoint. Add a stub to call ucln_cleanupOne
102 * Used on AIX, Solaris, and HP-UX
103 */
104U_CAPI void U_EXPORT2 UCLN_FINI (void);
105
106U_CAPI void U_EXPORT2 UCLN_FINI ()
107{
108    /* This function must be defined, if UCLN_FINI is defined, else link error. */
109     UCLN_CLEAN_ME_UP;
110}
111
112/* Windows: DllMain */
113#elif U_PLATFORM_HAS_WIN32_API
114/*
115 * ICU's own DllMain.
116 */
117
118/* these are from putil.c */
119/* READ READ READ READ!    Are you getting compilation errors from windows.h?
120          Any source file which includes this (ucln_imp.h) header MUST
121          be defined with language extensions ON. */
122#   define WIN32_LEAN_AND_MEAN
123#   define VC_EXTRALEAN
124#   define NOUSER
125#   define NOSERVICE
126#   define NOIME
127#   define NOMCX
128#   include <windows.h>
129/*
130 * This is a stub DllMain function with icu specific process handling code.
131 */
132BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
133{
134    BOOL status = TRUE;
135
136    switch(fdwReason) {
137        case DLL_PROCESS_ATTACH:
138             /* ICU does not trap process attach, but must pass these through properly. */
139            /* ICU specific process attach could go here */
140            break;
141
142        case DLL_PROCESS_DETACH:
143            /* Here is the one we actually care about. */
144
145            UCLN_CLEAN_ME_UP;
146
147            break;
148
149        case DLL_THREAD_ATTACH:
150            /* ICU does not trap thread attach, but must pass these through properly. */
151            /* ICU specific thread attach could go here */
152            break;
153
154        case DLL_THREAD_DETACH:
155            /* ICU does not trap thread detach, but must pass these through properly. */
156            /* ICU specific thread detach could go here */
157            break;
158
159    }
160    return status;
161}
162
163#elif defined(__GNUC__)
164/* GCC - use __attribute((destructor)) */
165static void ucln_destructor()   __attribute__((destructor)) ;
166
167static void ucln_destructor()
168{
169    UCLN_CLEAN_ME_UP;
170}
171
172#endif
173
174#endif /* UCLN_NO_AUTO_CLEANUP */
175
176#else
177#error This file can only be included once.
178#endif
179