Home | History | Annotate | Download | only in pthreads-win32
      1 /*
      2  * autostatic.c
      3  *
      4  * Description:
      5  * This translation unit implements static auto-init and auto-exit logic.
      6  *
      7  * --------------------------------------------------------------------------
      8  *
      9  *      Pthreads-win32 - POSIX Threads Library for Win32
     10  *      Copyright(C) 1998 John E. Bossom
     11  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
     12  *
     13  *      Contact Email: rpj (at) callisto.canberra.edu.au
     14  *
     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
     20  *
     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.
     25  *
     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.
     30  *
     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
     35  */
     36 
     37 #include "pthread.h"
     38 #include "implement.h"
     39 
     40 #if defined(PTW32_STATIC_LIB)
     41 
     42 #if defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER)
     43 
     44 /* For an explanation of this code (at least the MSVC parts), refer to
     45  *
     46  * http://www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c6945/
     47  * ("Running Code Before and After Main")
     48  *
     49  * Compatibility with MSVC8 was cribbed from Boost:
     50  *
     51  * http://svn.boost.org/svn/boost/trunk/libs/thread/src/win32/tss_pe.cpp
     52  *
     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.
     58  */
     59 
     60 static int on_process_init(void)
     61 {
     62     pthread_win32_process_attach_np ();
     63     return 0;
     64 }
     65 
     66 static int on_process_exit(void)
     67 {
     68     pthread_win32_thread_detach_np  ();
     69     pthread_win32_process_detach_np ();
     70     return 0;
     71 }
     72 
     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;
     82 #  else
     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 */
     88 #  endif
     89 #endif
     90 
     91 #endif /* defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER) */
     92 
     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.
     96  */
     97 void ptw32_autostatic_anchor(void) { abort(); }
     98 
     99 #endif /* PTW32_STATIC_LIB */
    100 
    101 #if ! defined(PTW32_BUILD_INLINED)
    102 /*
    103  * Avoid "translation unit is empty" warnings
    104  */
    105 typedef int foo;
    106 #endif
    107