1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * The DLL entry point (DllMain) for NSPR.
8  *
9  * The only reason we use DLLMain() now is to find out whether
10  * the NSPR DLL is statically or dynamically loaded.  When
11  * dynamically loaded, we cannot use static thread-local storage.
12  * However, static TLS is faster than the TlsXXX() functions.
13  * So we want to use static TLS whenever we can.  A global
14  * variable _pr_use_static_tls is set in DllMain() during process
15  * attachment to indicate whether it is safe to use static TLS
16  * or not.
17  */
18 
19 #include <windows.h>
20 #include <primpl.h>
21 
22 extern BOOL _pr_use_static_tls;  /* defined in ntthread.c */
23 
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)24 BOOL WINAPI DllMain(
25     HINSTANCE hinstDLL,
26     DWORD fdwReason,
27     LPVOID lpvReserved)
28 {
29     PRThread *me;
30 
31     switch (fdwReason) {
32         case DLL_PROCESS_ATTACH:
33             /*
34              * If lpvReserved is NULL, we are dynamically loaded
35              * and therefore can't use static thread-local storage.
36              */
37             if (lpvReserved == NULL) {
38                 _pr_use_static_tls = FALSE;
39             } else {
40                 _pr_use_static_tls = TRUE;
41             }
42             break;
43         case DLL_THREAD_ATTACH:
44             break;
45         case DLL_THREAD_DETACH:
46             if (_pr_initialized) {
47                 me = _MD_GET_ATTACHED_THREAD();
48                 if ((me != NULL) && (me->flags & _PR_ATTACHED)) {
49                     _PRI_DetachThread();
50                 }
51             }
52             break;
53         case DLL_PROCESS_DETACH:
54             break;
55     }
56     return TRUE;
57 }
58