1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #pragma warning(push,1) /* disable warnings within system headers */
37 #include <systools/win32/uwinapi.h>
39 #include <osl/diagnose.h>
40 #include <sal/types.h>
43 #include <osl/diagnose.h>
44 #include <osl/mutex.h>
45 #include <sal/types.h>
47 #include "internal/rtllifecycle.h"
49 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
53 extern DWORD g_dwTLSTextEncodingIndex
;
54 extern void SAL_CALL
_osl_callThreadKeyCallbackOnThreadDetach(void);
55 extern CRITICAL_SECTION g_ThreadKeyListCS
;
56 extern oslMutex g_Mutex
;
57 extern oslMutex g_CurrentDirectoryMutex
;
61 typedef void (*func_ptr
) (void);
62 extern func_ptr __CTOR_LIST__
[];
63 extern func_ptr __DTOR_LIST__
[];
65 static void do_startup(void);
66 static void do_cleanup(void);
71 This is needed because DllMain is called after static constructors. A DLL's
72 startup and shutdown sequence looks like this:
84 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
);
85 extern BOOL (WINAPI
*_pRawDllMain
)(HANDLE
, DWORD
, LPVOID
) = _RawDllMain
;
89 //------------------------------------------------------------------------------
91 //------------------------------------------------------------------------------
92 int osl_isSingleCPU
= 0;
97 __do_global_dtors (void)
99 static func_ptr
*p
= __DTOR_LIST__
+ 1;
102 * Call each destructor in the destructor list until a null pointer
113 __do_global_ctors (void)
115 unsigned long nptrs
= (unsigned long) __CTOR_LIST__
[0];
119 * If the first entry in the constructor list is -1 then the list
120 * is terminated with a null entry. Otherwise the first entry was
121 * the number of pointers in the list.
123 if (nptrs
== (unsigned long)-1)
125 for (nptrs
= 0; __CTOR_LIST__
[nptrs
+ 1] != 0; nptrs
++)
130 * Go through the list backwards calling constructors.
132 for (i
= nptrs
; i
>= 1; i
--)
138 * Register the destructors for processing on exit.
140 atexit (__do_global_dtors
);
143 static int initialized
= 0;
152 __do_global_ctors ();
156 static void do_startup( void )
159 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
161 (void)hinstDLL
; /* avoid warnings */
162 (void)lpvReserved
; /* avoid warnings */
166 case DLL_PROCESS_ATTACH
:
170 SYSTEM_INFO SystemInfo
;
172 GetSystemInfo(&SystemInfo
);
174 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
176 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
177 * comes with a cost and is especially expensive on pre HT x86 single processor
178 * systems, where it isn't needed at all.
180 if ( SystemInfo
.dwNumberOfProcessors
== 1 ) {
184 #if OSL_DEBUG_LEVEL < 2
185 /* Suppress file error messages from system like "Floppy A: not inserted" */
186 SetErrorMode( SEM_NOOPENFILEERRORBOX
| SEM_FAILCRITICALERRORS
);
189 /* initialize global mutex */
190 g_Mutex
= osl_createMutex();
192 /* initialize "current directory" mutex */
193 g_CurrentDirectoryMutex
= osl_createMutex();
195 g_dwTLSTextEncodingIndex
= TlsAlloc();
196 InitializeCriticalSection( &g_ThreadKeyListCS
);
198 //We disable floating point exceptions. This is the usual state at program startup
199 //but on Windows 98 and ME this is not always the case.
200 _control87(_MCW_EM
, _MCW_EM
);
205 void do_cleanup( void )
211 case DLL_PROCESS_DETACH
:
216 TlsFree( g_dwTLSTextEncodingIndex
);
217 DeleteCriticalSection( &g_ThreadKeyListCS
);
219 osl_destroyMutex( g_Mutex
);
221 osl_destroyMutex( g_CurrentDirectoryMutex
);
227 On a product build memory management finalization might
228 cause a crash without assertion (assertions off) if heap is
229 corrupted. But a crash report won't help here because at
230 this point all other threads have been terminated and only
231 ntdll is on the stack. No chance to find the reason for the
232 corrupted heap if so.
234 So annoying the user with a crash report is completly useless.
242 /* cleanup locale hashtable */
245 /* finalize memory management */
251 __except( EXCEPTION_EXECUTE_HANDLER
)
262 static DWORD
GetParentProcessId()
264 DWORD dwParentProcessId
= 0;
265 HANDLE hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
267 if ( IsValidHandle( hSnapshot
) )
272 ZeroMemory( &pe
, sizeof(pe
) );
273 pe
.dwSize
= sizeof(pe
);
274 fSuccess
= Process32First( hSnapshot
, &pe
);
278 if ( GetCurrentProcessId() == pe
.th32ProcessID
)
280 dwParentProcessId
= pe
.th32ParentProcessID
;
284 fSuccess
= Process32Next( hSnapshot
, &pe
);
287 CloseHandle( hSnapshot
);
290 return dwParentProcessId
;
293 static DWORD WINAPI
ParentMonitorThreadProc( LPVOID lpParam
)
295 DWORD dwParentProcessId
= (DWORD
)lpParam
;
297 HANDLE hParentProcess
= OpenProcess( SYNCHRONIZE
, FALSE
, dwParentProcessId
);
298 if ( IsValidHandle( hParentProcess
) )
300 if ( WAIT_OBJECT_0
== WaitForSingleObject( hParentProcess
, INFINITE
) )
302 TerminateProcess( GetCurrentProcess(), 0 );
304 CloseHandle( hParentProcess
);
309 BOOL WINAPI
DllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
311 (void)hinstDLL
; /* avoid warning */
312 (void)lpvReserved
; /* avoid warning */
315 case DLL_PROCESS_ATTACH
:
319 // This code will attach the process to it's parent process
320 // if the parent process had set the environment variable.
321 // The corresponding code (setting the environment variable)
322 // is is desktop/win32/source/officeloader.cxx
325 DWORD dwResult
= GetEnvironmentVariable( "ATTACHED_PARENT_PROCESSID", szBuffer
, sizeof(szBuffer
) );
327 if ( dwResult
&& dwResult
< sizeof(szBuffer
) )
329 DWORD dwThreadId
= 0;
331 DWORD dwParentProcessId
= (DWORD
)atol( szBuffer
);
333 if ( dwParentProcessId
&& GetParentProcessId() == dwParentProcessId
)
335 // No error check, it works or it does not
336 // Thread should only be started for headless mode, see desktop/win32/source/officeloader.cxx
337 CreateThread( NULL
, 0, ParentMonitorThreadProc
, (LPVOID
)dwParentProcessId
, 0, &dwThreadId
); //
344 case DLL_THREAD_ATTACH
:
347 case DLL_THREAD_DETACH
:
348 _osl_callThreadKeyCallbackOnThreadDetach( );
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */