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
8 /* INCLUDES ******************************************************************/
12 /* DATA **********************************************************************/
15 BOOL Ws2helpInitialized
= FALSE
;
16 CRITICAL_SECTION StartupSyncronization
;
19 /* FUNCTIONS *****************************************************************/
25 NT_PRODUCT_TYPE ProductType
= NtProductWinNt
;
26 SYSTEM_INFO SystemInfo
;
27 DWORD NumHandleBuckets
;
29 DWORD RegSize
= sizeof(DWORD
);
33 /* Try to figure out if this is a workstation or server install */
34 RtlGetNtProductType(&ProductType
);
36 /* Get the system info */
37 GetSystemInfo(&SystemInfo
);
39 /* If this is an MP machine, set the default spinlock */
40 if (SystemInfo
.dwNumberOfProcessors
> 1) gdwSpinCount
= 2000;
42 /* Figure how many "Handle Buckets" we'll use. Start with the default */
43 NumHandleBuckets
= ProductType
== NtProductWinNt
? 8 : 32;
45 /* Open the registry settings */
46 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
47 "System\\CurrentControlSet\\Services\\Winsock2\\Parameters",
50 &KeyHandle
) == ERROR_SUCCESS
)
53 RegQueryValueEx(KeyHandle
,
54 "Ws2_32NumHandleBuckets",
57 (LPBYTE
)&NumHandleBuckets
,
61 if (SystemInfo
.dwNumberOfProcessors
> 1)
63 /* Also check for a custom spinlock setting */
64 RegQueryValueEx(KeyHandle
,
68 (LPBYTE
)&gdwSpinCount
,
72 /* Close the key, we're done */
73 RegCloseKey(KeyHandle
);
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
;
80 /* Normalize it again, to be within OS parameters */
81 if (ProductType
== NtProductWinNt
)
83 /* Is it within norms for non-server editions? */
84 if (NumHandleBuckets
> 32) NumHandleBuckets
= 32;
85 else if (NumHandleBuckets
< 8) NumHandleBuckets
= 8;
89 /* Is it within norms for server editions? */
90 if (NumHandleBuckets
> 256) NumHandleBuckets
= 256;
91 else if (NumHandleBuckets
< 32) NumHandleBuckets
= 32;
94 /* Normalize the spincount */
95 if (gdwSpinCount
> 8000) gdwSpinCount
= 8000;
97 /* Set the final mask */
98 gHandleToIndexMask
= NumHandleBuckets
-1;
103 Ws2helpInitialize(VOID
)
105 /* Enter the startup CS */
106 EnterCriticalSection(&StartupSyncronization
);
108 /* Check again for init */
109 if (!Ws2helpInitialized
)
113 Ws2helpInitialized
= TRUE
;
116 /* Leave the CS and return */
117 LeaveCriticalSection(&StartupSyncronization
);
118 return ERROR_SUCCESS
;
123 DllMain(HANDLE hModule
,
129 case DLL_PROCESS_ATTACH
:
131 /* Save our handle */
132 LibraryHdl
= hModule
;
134 /* Improve Performance */
135 DisableThreadLibraryCalls(hModule
);
137 /* Initialize startup CS */
138 InitializeCriticalSection(&StartupSyncronization
);
140 /* Get Global Heap */
141 GlobalHeap
= GetProcessHeap();
144 case DLL_THREAD_ATTACH
:
145 case DLL_THREAD_DETACH
:
148 case DLL_PROCESS_DETACH
:
150 /* Make sure we loaded */
151 if (!LibraryHdl
) break;
153 /* Check if we are cleaning up */
156 /* Free the security descriptor */
157 if (pSDPipe
) HeapFree(GlobalHeap
, 0, pSDPipe
);
159 /* Close the event */
160 if (ghWriterEvent
) CloseHandle(ghWriterEvent
);
162 /* Delete the startup CS */
163 DeleteCriticalSection(&StartupSyncronization
);
164 Ws2helpInitialized
= FALSE
;