xref: /reactos/dll/win32/ws2help/dllmain.c (revision 4561998a)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 DLL
4  * FILE:        include/ws2_32.h
5  * PURPOSE:     WinSock 2 DLL header
6  */
7 
8 /* INCLUDES ******************************************************************/
9 
10 #include "precomp.h"
11 
12 /* DATA **********************************************************************/
13 
14 HANDLE GlobalHeap;
15 BOOL Ws2helpInitialized = FALSE;
16 CRITICAL_SECTION StartupSynchronization;
17 HINSTANCE LibraryHdl;
18 
19 /* FUNCTIONS *****************************************************************/
20 
21 VOID
22 WINAPI
23 NewCtxInit(VOID)
24 {
25     NT_PRODUCT_TYPE ProductType = NtProductWinNt;
26     SYSTEM_INFO SystemInfo;
27     DWORD NumHandleBuckets;
28     HKEY KeyHandle;
29     DWORD RegSize = sizeof(DWORD);
30     DWORD RegType;
31     DWORD Mask;
32 
33     /* Try to figure out if this is a workstation or server install */
34     RtlGetNtProductType(&ProductType);
35 
36     /* Get the system info */
37     GetSystemInfo(&SystemInfo);
38 
39     /* If this is an MP machine, set the default spinlock */
40     if (SystemInfo.dwNumberOfProcessors > 1) gdwSpinCount = 2000;
41 
42     /* Figure how many "Handle Buckets" we'll use. Start with the default */
43     NumHandleBuckets = ProductType == NtProductWinNt ? 8 : 32;
44 
45     /* Open the registry settings */
46     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
47                      "System\\CurrentControlSet\\Services\\Winsock2\\Parameters",
48                      0,
49                      KEY_QUERY_VALUE,
50                      &KeyHandle) == ERROR_SUCCESS)
51     {
52         /* Query the key */
53         RegQueryValueEx(KeyHandle,
54                         "Ws2_32NumHandleBuckets",
55                         0,
56                         &RegType,
57                         (LPBYTE)&NumHandleBuckets,
58                         &RegSize);
59 
60         /* Are we on MP? */
61         if (SystemInfo.dwNumberOfProcessors > 1)
62         {
63             /* Also check for a custom spinlock setting */
64             RegQueryValueEx(KeyHandle,
65                             "Ws2_32SpinCount",
66                             0,
67                             &RegType,
68                             (LPBYTE)&gdwSpinCount,
69                             &RegSize);
70         }
71 
72         /* Close the key, we're done */
73         RegCloseKey(KeyHandle);
74     }
75 
76     /* Now get the bucket count and normalize it to be log2 and within 256 */
77     for (Mask = 256; !(Mask & NumHandleBuckets); Mask >>= 1);
78     NumHandleBuckets = Mask;
79 
80     /* Normalize it again, to be within OS parameters */
81     if (ProductType == NtProductWinNt)
82     {
83         /* Is it within norms for non-server editions? */
84         if (NumHandleBuckets > 32) NumHandleBuckets = 32;
85         else if (NumHandleBuckets < 8) NumHandleBuckets = 8;
86     }
87     else
88     {
89         /* Is it within norms for server editions? */
90         if (NumHandleBuckets > 256) NumHandleBuckets = 256;
91         else if (NumHandleBuckets < 32) NumHandleBuckets = 32;
92     }
93 
94     /* Normalize the spincount */
95     if (gdwSpinCount > 8000) gdwSpinCount = 8000;
96 
97     /* Set the final mask */
98     gHandleToIndexMask = NumHandleBuckets -1;
99 }
100 
101 DWORD
102 WINAPI
103 Ws2helpInitialize(VOID)
104 {
105     /* Enter the startup CS */
106     EnterCriticalSection(&StartupSynchronization);
107 
108     /* Check again for init */
109     if (!Ws2helpInitialized)
110     {
111         /* Initialize us */
112         NewCtxInit();
113         Ws2helpInitialized = TRUE;
114     }
115 
116     /* Leave the CS and return */
117     LeaveCriticalSection(&StartupSynchronization);
118     return ERROR_SUCCESS;
119 }
120 
121 BOOL
122 APIENTRY
123 DllMain(HANDLE hModule,
124         DWORD  dwReason,
125         LPVOID lpReserved)
126 {
127     switch (dwReason)
128     {
129         case DLL_PROCESS_ATTACH:
130 
131             /* Save our handle */
132             LibraryHdl = hModule;
133 
134             /* Improve Performance */
135             DisableThreadLibraryCalls(hModule);
136 
137             /* Initialize startup CS */
138             InitializeCriticalSection(&StartupSynchronization);
139 
140             /* Get Global Heap */
141             GlobalHeap = GetProcessHeap();
142         	break;
143 
144         case DLL_THREAD_ATTACH:
145 	    case DLL_THREAD_DETACH:
146             break;
147 
148 	    case DLL_PROCESS_DETACH:
149 
150             /* Make sure we loaded */
151             if (!LibraryHdl) break;
152 
153             /* Check if we are cleaning up */
154             if (lpReserved)
155             {
156                 /* Free the security descriptor */
157                 if (pSDPipe) HeapFree(GlobalHeap, 0, pSDPipe);
158 
159                 /* Close the event */
160                 if (ghWriterEvent) CloseHandle(ghWriterEvent);
161 
162                 /* Delete the startup CS */
163                 DeleteCriticalSection(&StartupSynchronization);
164                 Ws2helpInitialized = FALSE;
165             }
166 			break;
167     }
168 
169     return TRUE;
170 }
171