5 * This translation unit implements static auto-init and auto-exit logic.
7 * --------------------------------------------------------------------------
9 * Pthreads-win32 - POSIX Threads Library for Win32
10 * Copyright(C) 1998 John E. Bossom
11 * Copyright(C) 1999,2005 Pthreads-win32 contributors
13 * Contact Email: rpj@callisto.canberra.edu.au
15 * The current list of contributors is contained
16 * in the file CONTRIBUTORS included with the source
17 * code distribution. The list can also be seen at the
18 * following World Wide Web location:
19 * http://sources.redhat.com/pthreads-win32/contributors.html
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library in the file COPYING.LIB;
33 * if not, write to the Free Software Foundation, Inc.,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
38 #include "implement.h"
40 #if defined(PTW32_STATIC_LIB)
42 #if defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER)
44 /* For an explanation of this code (at least the MSVC parts), refer to
46 * http://www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c6945/
47 * ("Running Code Before and After Main")
49 * Compatibility with MSVC8 was cribbed from Boost:
51 * http://svn.boost.org/svn/boost/trunk/libs/thread/src/win32/tss_pe.cpp
53 * In addition to that, because we are in a static library, and the linker
54 * can't tell that the constructor/destructor functions are actually
55 * needed, we need a way to prevent the linker from optimizing away this
56 * module. The pthread_win32_autostatic_anchor() hack below (and in
57 * implement.h) does the job in a portable manner.
60 static int on_process_init(void)
62 pthread_win32_process_attach_np ();
66 static int on_process_exit(void)
68 pthread_win32_thread_detach_np ();
69 pthread_win32_process_detach_np ();
73 #if defined(__MINGW64__) || defined(__MINGW32__)
74 __attribute__((section(".ctors"), used
)) static int (*gcc_ctor
)(void) = on_process_init
;
75 __attribute__((section(".dtors"), used
)) static int (*gcc_dtor
)(void) = on_process_exit
;
76 #elif defined(_MSC_VER)
77 # if _MSC_VER >= 1400 /* MSVC8 */
78 # pragma section(".CRT$XCU", long, read)
79 # pragma section(".CRT$XPU", long, read)
80 __declspec(allocate(".CRT$XCU")) static int (*msc_ctor
)(void) = on_process_init
;
81 __declspec(allocate(".CRT$XPU")) static int (*msc_dtor
)(void) = on_process_exit
;
83 # pragma data_seg(".CRT$XCU")
84 static int (*msc_ctor
)(void) = on_process_init
;
85 # pragma data_seg(".CRT$XPU")
86 static int (*msc_dtor
)(void) = on_process_exit
;
87 # pragma data_seg() /* reset data segment */
91 #endif /* defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER) */
93 /* This dummy function exists solely to be referenced by other modules
94 * (specifically, in implement.h), so that the linker can't optimize away
95 * this module. Don't call it.
97 void ptw32_autostatic_anchor(void) { abort(); }
99 #endif /* PTW32_STATIC_LIB */
101 #if ! defined(PTW32_BUILD_INLINED)
103 * Avoid "translation unit is empty" warnings