BVB Source Codes

mars Show tss_pe.cpp Source code

Return Download mars: download tss_pe.cpp Source code - Download mars Source code - Type:.cpp
  1. // $Id$
  2. // (C) Copyright Aaron W. LaFramboise, Roland Schwarz, Michael Glassford 2004.
  3. // (C) Copyright 2007 Roland Schwarz
  4. // (C) Copyright 2007 Anthony Williams
  5. // (C) Copyright 2007 David Deakins
  6. // Use, modification and distribution are subject to the
  7. // Boost Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9.  
  10. #include <boost/thread/detail/config.hpp>
  11.  
  12. #if defined(BOOST_HAS_WINTHREADS) && defined(BOOST_THREAD_BUILD_LIB)
  13.  
  14. #if (defined(__MINGW32__) && !defined(_WIN64)) || defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR)
  15.  
  16. #include <boost/thread/detail/tss_hooks.hpp>
  17.  
  18. #include <windows.h>
  19.  
  20. #include <cstdlib>
  21.  
  22. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost
  23. {
  24.     void tss_cleanup_implemented() {}
  25. }
  26.  
  27. namespace {
  28.     void NTAPI on_tls_callback(void* , DWORD dwReason, PVOID )
  29.     {
  30.         switch (dwReason)
  31.         {
  32.         case DLL_THREAD_DETACH:
  33.         {
  34.             mars_boost::mars_booston_thread_exit();
  35.             break;
  36.         }
  37.         }
  38.     }
  39. }
  40.  
  41. #if defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR) || (__MINGW32_MAJOR_VERSION >3) ||             \
  42.     ((__MINGW32_MAJOR_VERSION==3) && (__MINGW32_MINOR_VERSION>=18))
  43. extern "C"
  44. {
  45.     PIMAGE_TLS_CALLBACK __crt_xl_tls_callback__ __attribute__ ((section(".CRT$XLB"))) = on_tls_callback;
  46. }
  47. #else
  48. extern "C" {
  49.  
  50.     void (* after_ctors )() __attribute__((section(".ctors")))     = mars_boost::on_process_enter;
  51.     void (* before_dtors)() __attribute__((section(".dtors")))     = mars_boost::on_thread_exit;
  52.     void (* after_dtors )() __attribute__((section(".dtors.zzz"))) = mars_boost::on_process_exit;
  53.  
  54.     ULONG __tls_index__ = 0;
  55.     char __tls_end__ __attribute__((section(".tls$zzz"))) = 0;
  56.     char __tls_start__ __attribute__((section(".tls"))) = 0;
  57.  
  58.  
  59.     PIMAGE_TLS_CALLBACK __crt_xl_start__ __attribute__ ((section(".CRT$XLA"))) = 0;
  60.     PIMAGE_TLS_CALLBACK __crt_xl_end__ __attribute__ ((section(".CRT$XLZ"))) = 0;
  61. }
  62. extern "C" const IMAGE_TLS_DIRECTORY32 _tls_used __attribute__ ((section(".rdata$T"))) =
  63. {
  64.         (DWORD) &__tls_start__,
  65.         (DWORD) &__tls_end__,
  66.         (DWORD) &__tls_index__,
  67.         (DWORD) (&__crt_xl_start__+1),
  68.         (DWORD) 0,
  69.         (DWORD) 0
  70. };
  71. #endif
  72.  
  73.  
  74. #elif  defined(_MSC_VER) && !defined(UNDER_CE)
  75.  
  76.     #include <boost/thread/detail/tss_hooks.hpp>
  77.  
  78.     #include <stdlib.h>
  79.  
  80.     #define WIN32_LEAN_AND_MEAN
  81.     #include <windows.h>
  82.  
  83.  
  84. // _pRawDllMainOrig can be defined by including boost/thread/win32/mfc_thread_init.hpp
  85. // into your dll; it ensures that MFC-Dll-initialization will be done properly
  86. // The following code is adapted from the MFC-Dll-init code
  87. /*
  88.  * _pRawDllMainOrig MUST be an extern const variable, which will be aliased to
  89.  * _pDefaultRawDllMainOrig if no real user definition is present, thanks to the
  90.  * alternatename directive.
  91.  */
  92.  
  93. // work at least with _MSC_VER 1500 (MSVC++ 9.0, VS 2008)
  94. #if (_MSC_VER >= 1500)
  95.  
  96. extern "C" {
  97. extern BOOL (WINAPI * const _pRawDllMainOrig)(HANDLE, DWORD, LPVOID);
  98. extern BOOL (WINAPI * const _pDefaultRawDllMainOrig)(HANDLE, DWORD, LPVOID) = NULL;
  99. #if defined (_M_IX86)
  100. #pragma comment(linker, "/alternatename:__pRawDllMainOrig=__pDefaultRawDllMainOrig")
  101. #elif defined (_M_X64) || defined (_M_ARM)
  102. #pragma comment(linker, "/alternatename:_pRawDllMainOrig=_pDefaultRawDllMainOrig")
  103. #else  /* defined (_M_X64) || defined (_M_ARM) */
  104. #error Unsupported platform
  105. #endif  /* defined (_M_X64) || defined (_M_ARM) */
  106. }
  107.  
  108. #endif
  109.  
  110.  
  111.  
  112.  
  113.     //Definitions required by implementation
  114.  
  115.     #if (_MSC_VER < 1300) // 1300 == VC++ 7.0
  116.         typedef void (__cdecl *_PVFV)();
  117.         #define INIRETSUCCESS
  118.         #define PVAPI void __cdecl
  119.     #else
  120.         typedef int (__cdecl *_PVFV)();
  121.         #define INIRETSUCCESS 0
  122.         #define PVAPI int __cdecl
  123.     #endif
  124.  
  125.     typedef void (NTAPI* _TLSCB)(HINSTANCE, DWORD, PVOID);
  126.  
  127.     //Symbols for connection to the runtime environment
  128.  
  129.     extern "C"
  130.     {
  131.         extern DWORD _tls_used; //the tls directory (located in .rdata segment)
  132.         extern _TLSCB __xl_a[], __xl_z[];    //tls initializers */
  133.     }
  134.  
  135.     namespace
  136.     {
  137.         //Forward declarations
  138.  
  139.         static PVAPI on_tls_prepare();
  140.         static PVAPI on_process_init();
  141.         static PVAPI on_process_term();
  142.         static void NTAPI on_tls_callback(HINSTANCE, DWORD, PVOID);
  143.  
  144.         //The .CRT$Xxx information is taken from Codeguru:
  145.         //http://www.codeguru.com/Cpp/misc/misc/threadsprocesses/article.php/c6945__2/
  146.  
  147. #if (_MSC_VER >= 1400)
  148. #pragma section(".CRT$XIU",long,read)
  149. #pragma section(".CRT$XCU",long,read)
  150. #pragma section(".CRT$XTU",long,read)
  151. #pragma section(".CRT$XLC",long,read)
  152.         __declspec(allocate(".CRT$XLC")) _TLSCB __xl_ca=on_tls_callback;
  153.         __declspec(allocate(".CRT$XIU"))_PVFV p_tls_prepare = on_tls_prepare;
  154.         __declspec(allocate(".CRT$XCU"))_PVFV p_process_init = on_process_init;
  155.         __declspec(allocate(".CRT$XTU"))_PVFV p_process_term = on_process_term;
  156. #else
  157.         #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
  158.         #   pragma data_seg(push, old_seg)
  159.         #endif
  160.             //Callback to run tls glue code first.
  161.             //I don't think it is necessary to run it
  162.             //at .CRT$XIB level, since we are only
  163.             //interested in thread detachement. But
  164.             //this could be changed easily if required.
  165.  
  166.             #pragma data_seg(".CRT$XIU")
  167.             static _PVFV p_tls_prepare = on_tls_prepare;
  168.             #pragma data_seg()
  169.  
  170.             //Callback after all global ctors.
  171.  
  172.             #pragma data_seg(".CRT$XCU")
  173.             static _PVFV p_process_init = on_process_init;
  174.             #pragma data_seg()
  175.  
  176.             //Callback for tls notifications.
  177.  
  178.             #pragma data_seg(".CRT$XLB")
  179.             _TLSCB p_thread_callback = on_tls_callback;
  180.             #pragma data_seg()
  181.             //Callback for termination.
  182.  
  183.             #pragma data_seg(".CRT$XTU")
  184.             static _PVFV p_process_term = on_process_term;
  185.             #pragma data_seg()
  186.         #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
  187.         #   pragma data_seg(pop, old_seg)
  188.         #endif
  189. #endif
  190.  
  191. #ifdef BOOST_MSVC
  192. #pragma warning(push)
  193. #pragma warning(disable:4189)
  194. #endif
  195.  
  196.         PVAPI on_tls_prepare()
  197.         {
  198.             //The following line has an important side effect:
  199.             //if the TLS directory is not already there, it will
  200.             //be created by the linker. In other words, it forces a tls
  201.             //directory to be generated by the linker even when static tls
  202.             //(i.e. __declspec(thread)) is not used.
  203.             //The volatile should prevent the optimizer
  204.             //from removing the reference.
  205.  
  206.             DWORD volatile dw = _tls_used;
  207.  
  208.             #if (_MSC_VER < 1300) // 1300 == VC++ 7.0
  209.                 _TLSCB* pfbegin = __xl_a;
  210.                 _TLSCB* pfend = __xl_z;
  211.                 _TLSCB* pfdst = pfbegin;
  212.                 //pfdst = (_TLSCB*)_tls_used.AddressOfCallBacks;
  213.  
  214.                 //The following loop will merge the address pointers
  215.                 //into a contiguous area, since the tlssup code seems
  216.                 //to require this (at least on MSVC 6)
  217.  
  218.                 while (pfbegin < pfend)
  219.                 {
  220.                     if (*pfbegin != 0)
  221.                     {
  222.                         *pfdst = *pfbegin;
  223.                         ++pfdst;
  224.                     }
  225.                     ++pfbegin;
  226.                 }
  227.  
  228.                 *pfdst = 0;
  229.             #endif
  230.  
  231.             return INIRETSUCCESS;
  232.         }
  233. #ifdef BOOST_MSVC
  234. #pragma warning(pop)
  235. #endif
  236.  
  237.         PVAPI on_process_init()
  238.         {
  239.             //Schedule on_thread_exit() to be called for the main
  240.             //thread before destructors of global objects have been
  241.             //called.
  242.  
  243.             //It will not be run when 'quick' exiting the
  244.             //library; however, this is the standard behaviour
  245.             //for destructors of global objects, so that
  246.             //shouldn't be a problem.
  247.  
  248.             atexit(mars_boost::on_thread_exit);
  249.  
  250.             //Call Boost process entry callback here
  251.  
  252.             mars_boost::on_process_enter();
  253.  
  254.             return INIRETSUCCESS;
  255.         }
  256.  
  257.         PVAPI on_process_term()
  258.         {
  259.             mars_boost::on_process_exit();
  260.             return INIRETSUCCESS;
  261.         }
  262.  
  263.         void NTAPI on_tls_callback(HINSTANCE /*h*/, DWORD dwReason, PVOID /*pv*/)
  264.         {
  265.             switch (dwReason)
  266.             {
  267.             case DLL_THREAD_DETACH:
  268.                 mars_boost::on_thread_exit();
  269.                 break;
  270.             }
  271.         }
  272.  
  273. #if (_MSC_VER >= 1500)
  274.         BOOL WINAPI dll_callback(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
  275. #else
  276.         BOOL WINAPI dll_callback(HANDLE, DWORD dwReason, LPVOID)
  277. #endif
  278.         {
  279.             switch (dwReason)
  280.             {
  281.             case DLL_THREAD_DETACH:
  282.                 mars_boost::on_thread_exit();
  283.                 break;
  284.             case DLL_PROCESS_DETACH:
  285.                 mars_boost::on_process_exit();
  286.                 break;
  287.             }
  288.  
  289. #if (_MSC_VER >= 1500)
  290.             if( _pRawDllMainOrig )
  291.             {
  292.                 return _pRawDllMainOrig(hInstance, dwReason, lpReserved);
  293.             }
  294. #endif
  295.             return true;
  296.         }
  297.     } //namespace
  298.  
  299. extern "C"
  300. {
  301.     extern BOOL (WINAPI * const _pRawDllMain)(HANDLE, DWORD, LPVOID)=&dll_callback;
  302. }
  303. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost
  304. {
  305.     void tss_cleanup_implemented()
  306.     {
  307.         /*
  308.         This function's sole purpose is to cause a link error in cases where
  309.         automatic tss cleanup is not implemented by Boost.Threads as a
  310.         reminder that user code is responsible for calling the necessary
  311.         functions at the appropriate times (and for implementing an a
  312.         tss_cleanup_implemented() function to eliminate the linker's
  313.         missing symbol error).
  314.  
  315.         If Boost.Threads later implements automatic tss cleanup in cases
  316.         where it currently doesn't (which is the plan), the duplicate
  317.         symbol error will warn the user that their custom solution is no
  318.         longer needed and can be removed.
  319.         */
  320.     }
  321. }
  322.  
  323. #endif //defined(_MSC_VER) && !defined(UNDER_CE)
  324.  
  325. #endif //defined(BOOST_HAS_WINTHREADS) && defined(BOOST_THREAD_BUILD_LIB)
  326.  
downloadtss_pe.cpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top