1/*
2******************************************************************************
3*                                                                            *
4* Copyright (C) 2009, 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 setting UCLN_NO_AUTO_CLEANUP to 0 in pwin32.h (For Visual Studio
36 * solution file builds)
37 * For option 2, follow option 1 and also define UCLN_AUTO_ATEXIT
38 * For option 3, follow option 1 and also define UCLN_AUTO_LOCAL (see below for more information)
39 */
40
41#if !UCLN_NO_AUTO_CLEANUP
42
43/*
44 * The following declarations are for when UCLN_AUTO_LOCAL or UCLN_AUTO_ATEXIT
45 * are defined. They are commented out because they are static and will be defined
46 * later. The information is still here to provide some guidance for the developer
47 * who chooses to use UCLN_AUTO_LOCAL.
48 */
49/**
50 * Give the library an opportunity to register an automatic cleanup.
51 * This may be called more than once.
52 */
53/*static void ucln_registerAutomaticCleanup();*/
54/**
55 * Unregister an automatic cleanup, if possible. Called from cleanup.
56 */
57/*static void ucln_unRegisterAutomaticCleanup();*/
58
59/* ------------ automatic cleanup: registration. Choose ONE ------- */
60#if defined(UCLN_AUTO_LOCAL)
61/* To use:
62 *  1. define UCLN_AUTO_LOCAL,
63 *  2. create ucln_local_hook.c containing implementations of
64 *           static void ucln_registerAutomaticCleanup()
65 *           static void ucln_unRegisterAutomaticCleanup()
66 */
67#include "ucln_local_hook.c"
68
69#elif defined(UCLN_AUTO_ATEXIT)
70/*
71 * Use the ANSI C 'atexit' function. Note that this mechanism does not
72 * guarantee the order of cleanup relative to other users of ICU!
73 */
74static UBool gAutoCleanRegistered = FALSE;
75
76static void ucln_atexit_handler()
77{
78    ucln_cleanupOne(UCLN_TYPE);
79}
80
81static void ucln_registerAutomaticCleanup()
82{
83    if(!gAutoCleanRegistered) {
84        gAutoCleanRegistered = TRUE;
85        atexit(&ucln_atexit_handler);
86    }
87}
88
89static void ucln_unRegisterAutomaticCleanup () {
90}
91/* ------------end of automatic cleanup: registration. ------- */
92
93#elif defined (UCLN_FINI)
94/**
95 * If UCLN_FINI is defined, it is the (versioned, etc) name of a cleanup
96 * entrypoint. Add a stub to call ucln_cleanupOne
97 * Used on AIX, Solaris, and HP-UX
98 */
99U_CAPI void U_EXPORT2 UCLN_FINI (void);
100
101U_CAPI void U_EXPORT2 UCLN_FINI ()
102{
103    /* This function must be defined, if UCLN_FINI is defined, else link error. */
104     ucln_cleanupOne(UCLN_TYPE);
105}
106#elif defined(__GNUC__)
107/* GCC - use __attribute((destructor)) */
108static void ucln_destructor()   __attribute__((destructor)) ;
109
110static void ucln_destructor()
111{
112    ucln_cleanupOne(UCLN_TYPE);
113}
114
115/* Windows: DllMain */
116#elif defined (U_WINDOWS)
117/*
118 * ICU's own DllMain.
119 */
120
121/* these are from putil.c */
122/* READ READ READ READ!    Are you getting compilation errors from windows.h?
123          Any source file which includes this (ucln_imp.h) header MUST
124          be defined with language extensions ON. */
125#   define WIN32_LEAN_AND_MEAN
126#   define VC_EXTRALEAN
127#   define NOUSER
128#   define NOSERVICE
129#   define NOIME
130#   define NOMCX
131#   include <windows.h>
132/*
133 * This is a stub DllMain function with icu specific process handling code.
134 */
135BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
136{
137    BOOL status = TRUE;
138
139    switch(fdwReason) {
140        case DLL_PROCESS_ATTACH:
141             /* ICU does not trap process attach, but must pass these through properly. */
142            /* ICU specific process attach could go here */
143            break;
144
145        case DLL_PROCESS_DETACH:
146            /* Here is the one we actually care about. */
147
148            ucln_cleanupOne(UCLN_TYPE);
149
150            break;
151
152        case DLL_THREAD_ATTACH:
153            /* ICU does not trap thread attach, but must pass these through properly. */
154            /* ICU specific thread attach could go here */
155            break;
156
157        case DLL_THREAD_DETACH:
158            /* ICU does not trap thread detach, but must pass these through properly. */
159            /* ICU specific thread detach could go here */
160            break;
161
162    }
163    return status;
164}
165#endif
166
167#endif /* UCLN_NO_AUTO_CLEANUP */
168
169#else
170#error This file can only be included once.
171#endif
172