ReactOS  0.4.14-dev-342-gdc047f9
context.c File Reference
#include "precomp.h"
#include <ws2help.h>
Include dependency graph for context.c:

Go to the source code of this file.

Macros

#define WSH_SEARCH_TABLE_FROM_HANDLE(h, t)   (&t->SearchTables[(((ULONG_PTR)h >> 2) & t->Mask)])
 
#define WSH_HASH_FROM_HANDLE(h, hs)   (hs->Handles[((ULONG_PTR)h % hs->Size)])
 
#define AcquireWriteLock(t)   EnterCriticalSection(&t->Lock);
 
#define ReleaseWriteLock(t)   LeaveCriticalSection(&t->Lock);
 

Typedefs

typedef volatile LONG VLONG
 
typedef VLONGPVLONG
 

Functions

static __inline VOID AcquireReadLock (IN PWAH_SEARCH_TABLE Table, IN PVLONG *Count)
 
static __inline VOID ReleaseReadLock (IN PWAH_SEARCH_TABLE Table, IN PVLONG Count)
 
VOID WINAPI DoWaitForReaders (IN PWAH_SEARCH_TABLE Table, IN PVLONG Counter)
 
static __inline VOID TryWaitForReaders (IN PWAH_SEARCH_TABLE Table)
 
DWORD WINAPI WahCreateHandleContextTable (OUT PWAH_HANDLE_TABLE *Table)
 
DWORD WINAPI WahDestroyHandleContextTable (IN PWAH_HANDLE_TABLE Table)
 
BOOL WINAPI WahEnumerateHandleContexts (IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE_ENUMERATE_PROC Callback, IN PVOID Context)
 
PWAH_HANDLE WINAPI WahInsertHandleContext (IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
 
PWAH_HANDLE WINAPI WahReferenceContextByHandle (IN PWAH_HANDLE_TABLE Table, IN HANDLE Handle)
 
DWORD WINAPI WahRemoveHandleContext (IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
 

Variables

CRITICAL_SECTION WshHandleTableLock
 
HANDLE ghWriterEvent
 
DWORD gdwSpinCount = 0
 
DWORD gHandleToIndexMask
 
CONST DWORD SockPrimes []
 

Macro Definition Documentation

◆ AcquireWriteLock

#define AcquireWriteLock (   t)    EnterCriticalSection(&t->Lock);

Definition at line 40 of file context.c.

◆ ReleaseWriteLock

#define ReleaseWriteLock (   t)    LeaveCriticalSection(&t->Lock);

Definition at line 43 of file context.c.

◆ WSH_HASH_FROM_HANDLE

#define WSH_HASH_FROM_HANDLE (   h,
  hs 
)    (hs->Handles[((ULONG_PTR)h % hs->Size)])

Definition at line 37 of file context.c.

◆ WSH_SEARCH_TABLE_FROM_HANDLE

#define WSH_SEARCH_TABLE_FROM_HANDLE (   h,
  t 
)    (&t->SearchTables[(((ULONG_PTR)h >> 2) & t->Mask)])

Definition at line 34 of file context.c.

Typedef Documentation

◆ PVLONG

typedef VLONG* PVLONG

Definition at line 29 of file context.c.

◆ VLONG

typedef volatile LONG VLONG

Definition at line 28 of file context.c.

Function Documentation

◆ AcquireReadLock()

static __inline VOID AcquireReadLock ( IN PWAH_SEARCH_TABLE  Table,
IN PVLONG Count 
)
static

Definition at line 50 of file context.c.

52 {
53  LONG OldCount;
54 
55  /* Start acquire loop */
56  do
57  {
58  /* Write and save count value */
59  *Count = Table->CurrentCount;
60  OldCount = **Count;
61 
62  /* Check if it's valid and try to increment it */
63  if ((OldCount > 0) && (InterlockedCompareExchange(*Count,
64  OldCount + 1,
65  OldCount) == OldCount))
66  {
67  /* Everything went OK */
68  break;
69  }
70  } while (TRUE);
71 }
ASMGENDATA Table[]
Definition: genincdata.c:61
#define TRUE
Definition: types.h:120
#define InterlockedCompareExchange
Definition: interlocked.h:104
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
long LONG
Definition: pedump.c:60

Referenced by WahInsertHandleContext(), and WahReferenceContextByHandle().

◆ DoWaitForReaders()

VOID WINAPI DoWaitForReaders ( IN PWAH_SEARCH_TABLE  Table,
IN PVLONG  Counter 
)

Definition at line 88 of file context.c.

90 {
92 
93  /* Do a context switch */
95 
96  /* Check if the counter is above one */
97  if (*Counter > 0)
98  {
99  /*
100  * This shouldn't happen unless priorities are messed up. Do a wait so
101  * that the threads with lower priority will get their chance now.
102  */
103  if (!ghWriterEvent)
104  {
105  /* We don't even have an event! Allocate one manually... */
107  if (EventHandle)
108  {
109  /* Save the event handle atomically */
111  EventHandle,
112  NULL)))
113  {
114  /* Someone beat us to it, close ours */
116  }
117  }
118  else
119  {
120  /* Things couldn't get worse for us. Do a last-resort hack */
121  while (*Counter > 0) Sleep(10);
122  }
123  }
124 
125  /*
126  * Our event is ready. Tell the others to signal us by making sure
127  * that the last counter will be -1, notifying the last thread of our
128  * request.
129  */
130  if (InterlockedDecrement(Counter) >= 0)
131  {
132  /* Keep looping */
133  do
134  {
135  /* Wait in tiny bursts, so we can catch the PulseEvent */
137  } while (*Counter >= 0);
138  }
139  }
140 }
#define CreateEvent
Definition: winbase.h:3572
BOOL WINAPI SwitchToThread(VOID)
Definition: thread.c:447
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
smooth NULL
Definition: ftsmooth.c:416
#define InterlockedDecrement
Definition: armddk.h:52
static LARGE_INTEGER Counter
Definition: clock.c:43
HANDLE ghWriterEvent
Definition: context.c:17
_Out_ PHANDLE EventHandle
Definition: iofuncs.h:855

Referenced by TryWaitForReaders().

◆ ReleaseReadLock()

static __inline VOID ReleaseReadLock ( IN PWAH_SEARCH_TABLE  Table,
IN PVLONG  Count 
)
static

Definition at line 75 of file context.c.

77 {
78  /* Decrement the count. If we went below 0, someone is waiting... */
79  if (InterlockedDecrement(Count) < 0)
80  {
81  /* We use pulse since this is a shared event */
83  }
84 }
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent(IN HANDLE hEvent)
Definition: synch.c:695
#define InterlockedDecrement
Definition: armddk.h:52
HANDLE ghWriterEvent
Definition: context.c:17

Referenced by WahInsertHandleContext(), and WahReferenceContextByHandle().

◆ TryWaitForReaders()

static __inline VOID TryWaitForReaders ( IN PWAH_SEARCH_TABLE  Table)
static

Definition at line 144 of file context.c.

145 {
146  PVLONG OldCount = Table->CurrentCount;
147  LONG SpinCount;
148 
149  /* See which counter is being used */
150  if (OldCount == &Table->Count1)
151  {
152  /* Use counter 2 now */
153  Table->Count2 = 1;
154  Table->CurrentCount = &Table->Count2;
155  }
156  else
157  {
158  /* Use counter 1 now */
159  Table->Count1 = 1;
160  Table->CurrentCount = &Table->Count1;
161  }
162 
163  /* Decrease the old count to block new readers */
164  if (InterlockedDecrement(OldCount) > 0)
165  {
166  /* On an MP machine, spin a bit first */
167  if (Table->SpinCount)
168  {
169  /* Get the spincount and loop it */
170  SpinCount = Table->SpinCount;
171  while (*OldCount > 0)
172  {
173  /* Check if the spin failed */
174  if (--SpinCount <= 0) break;
175  }
176  }
177 
178  /* Check one last time if someone is still active */
179  if (*OldCount > 0)
180  {
181  /* Yep, we'll have to do a blocking (slow) wait */
182  DoWaitForReaders(Table, OldCount);
183  }
184  }
185 }
ASMGENDATA Table[]
Definition: genincdata.c:61
VLONG * PVLONG
Definition: context.c:29
long LONG
Definition: pedump.c:60
#define InterlockedDecrement
Definition: armddk.h:52
VOID WINAPI DoWaitForReaders(IN PWAH_SEARCH_TABLE Table, IN PVLONG Counter)
Definition: context.c:88

Referenced by WahEnumerateHandleContexts(), WahInsertHandleContext(), and WahRemoveHandleContext().

◆ WahCreateHandleContextTable()

DWORD WINAPI WahCreateHandleContextTable ( OUT PWAH_HANDLE_TABLE Table)

Definition at line 189 of file context.c.

190 {
192  PWAH_HANDLE_TABLE LocalTable;
193  DWORD i;
194 
195  /* Enter the prolog, make sure we're initialized */
197  if (ErrorCode != ERROR_SUCCESS) return ErrorCode;
198 
199  /* Assume NULL */
200  *Table = NULL;
201 
202  /* Allocate enough tables */
203  LocalTable = HeapAlloc(GlobalHeap,
204  0,
206  SearchTables[gHandleToIndexMask + 1]));
207 
208  /* Make sure it was allocated */
209  if (!LocalTable) return WSA_NOT_ENOUGH_MEMORY;
210 
211  /* Set the mask for the table */
212  LocalTable->Mask = gHandleToIndexMask;
213 
214  /* Now initialize every table */
215  for (i = 0; i <= gHandleToIndexMask; i++)
216  {
217  /* No hash table yet */
218  LocalTable->SearchTables[i].HashTable = NULL;
219 
220  /* Set the current count */
221  LocalTable->SearchTables[i].CurrentCount = &LocalTable->SearchTables[i].Count1;
222 
223  /* Initialize the counts */
224  LocalTable->SearchTables[i].Count1 = 1;
225  LocalTable->SearchTables[i].Count2 = 0;
226 
227  /* Set expanding state and spin count */
228  LocalTable->SearchTables[i].Expanding = FALSE;
229  LocalTable->SearchTables[i].SpinCount = gdwSpinCount;
230 
231  /* Initialize the lock */
233  SearchTables[i].Lock,
234  gdwSpinCount);
235  }
236 
237  /* Return pointer */
238  *Table = LocalTable;
239 
240  /* Return success */
241  return ERROR_SUCCESS;
242 }
volatile PLONG CurrentCount
Definition: ws2help.h:27
ASMGENDATA Table[]
Definition: genincdata.c:61
BOOL WINAPI InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount)
Definition: synch.c:765
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define ERROR_SUCCESS
Definition: deptool.c:10
volatile PWAH_HASH_TABLE HashTable
Definition: ws2help.h:26
#define WS2HELP_PROLOG()
Definition: precomp.h:47
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
smooth NULL
Definition: ftsmooth.c:416
WSH_SEARCH_TABLE SearchTables[1]
Definition: ws2help.h:38
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WSA_NOT_ENOUGH_MEMORY
Definition: winsock2.h:593
DWORD gHandleToIndexMask
Definition: context.c:19
#define VOID
Definition: acefi.h:82
HANDLE GlobalHeap
Definition: dllmain.c:19
DWORD gdwSpinCount
Definition: context.c:18
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254

Referenced by WsSockStartup().

◆ WahDestroyHandleContextTable()

DWORD WINAPI WahDestroyHandleContextTable ( IN PWAH_HANDLE_TABLE  Table)

Definition at line 246 of file context.c.

247 {
248  DWORD i;
249 
250  /* Make sure the table is valid */
251  if (!Table)
252  {
253  /* No valid table */
255  }
256 
257  /* Loop each search table */
258  for (i = 0; i <= Table->Mask; i++)
259  {
260  /* Check if there's a table here */
261  if (Table->SearchTables[i].HashTable)
262  {
263  /* Free it */
264  HeapFree(GlobalHeap, 0, Table->SearchTables[i].HashTable);
265  }
266 
267  /* Delete the lock */
268  DeleteCriticalSection(&Table->SearchTables[i].Lock);
269  }
270 
271  /* Delete the table */
273 
274  /* Return success */
275  return ERROR_SUCCESS;
276 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
ASMGENDATA Table[]
Definition: genincdata.c:61
#define ERROR_SUCCESS
Definition: deptool.c:10
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
unsigned long DWORD
Definition: ntddk_ex.h:95
HANDLE GlobalHeap
Definition: dllmain.c:19
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by WsSockCleanup().

◆ WahEnumerateHandleContexts()

BOOL WINAPI WahEnumerateHandleContexts ( IN PWAH_HANDLE_TABLE  Table,
IN PWAH_HANDLE_ENUMERATE_PROC  Callback,
IN PVOID  Context 
)

Definition at line 280 of file context.c.

283 {
284  DWORD i, j;
285  PWAH_SEARCH_TABLE SearchTable;
288  BOOL GoOn = TRUE;
289 
290  /* Loop the table */
291  for (i = 0; i <= Table->Mask; i++)
292  {
293  /* Get the Search table */
294  SearchTable = &Table->SearchTables[i];
295 
296  /* Lock it */
297  AcquireWriteLock(SearchTable);
298 
299  /* Mark us as expanding and wait for everyone */
300  SearchTable->Expanding = TRUE;
301  TryWaitForReaders(SearchTable);
302 
303  /* Get the hash table */
304  HashTable = SearchTable->HashTable;
305 
306  /* Make sure it exists */
307  if (HashTable)
308  {
309  /* Loop every handle in it */
310  for (j = 0; j < HashTable->Size; j++)
311  {
312  /* Get this handle */
313  Handle = HashTable->Handles[j];
314  if (!Handle) continue;
315 
316  /* Call the callback proc */
317  GoOn = Callback(Context, Handle);
318  if (!GoOn) break;
319  }
320  }
321 
322  /* Disable the expansion bit and release the lock */
323  SearchTable->Expanding = FALSE;
324  ReleaseWriteLock(SearchTable);
325 
326  /* Check again if we should leave */
327  if (!GoOn) break;
328  }
329 
330  /* return */
331  return GoOn;
332 }
ASMGENDATA Table[]
Definition: genincdata.c:61
#define TRUE
Definition: types.h:120
Definition: hash.c:67
volatile PWAH_HASH_TABLE HashTable
Definition: ws2help.h:26
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
static __inline VOID TryWaitForReaders(IN PWAH_SEARCH_TABLE Table)
Definition: context.c:144
_In_ HANDLE Handle
Definition: extypes.h:390
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ReleaseWriteLock(t)
Definition: context.c:43
LPFNPSPCALLBACK Callback
Definition: desk.c:111
#define AcquireWriteLock(t)
Definition: context.c:40

Referenced by WsProcDelete().

◆ WahInsertHandleContext()

PWAH_HANDLE WINAPI WahInsertHandleContext ( IN PWAH_HANDLE_TABLE  Table,
IN PWAH_HANDLE  Handle 
)

Definition at line 336 of file context.c.

338 {
339  PWAH_HANDLE *HashHandle, OldHandle;
340  PVLONG Count;
342  DWORD HandleCount, i;
343  PWAH_SEARCH_TABLE SearchTable;
344 
345  /* Get the current Search Table */
346  SearchTable = WSH_SEARCH_TABLE_FROM_HANDLE(Handle->Handle, Table);
347 
348  /* Start loop */
349  do
350  {
351  /* Get reader lock */
352  AcquireReadLock(SearchTable, &Count);
353 
354  /* Get the hash table */
355  HashTable = SearchTable->HashTable;
356 
357  /* Make sure we are not expanding, and that the table is there */
358  if (!(SearchTable->Expanding) && (HashTable))
359  {
360  /* Get the hash handle */
361  HashHandle = &WSH_HASH_FROM_HANDLE(Handle->Handle, HashTable);
362 
363  /* Do the insert */
364  if (InterlockedCompareExchangePointer((PVOID*)HashHandle,
365  Handle,
366  NULL) == NULL)
367  {
368  /* Success, release the reader lock */
369  ReleaseReadLock(SearchTable, Count);
370 
371  /* Save old handle */
372  OldHandle = Handle;
373  break;
374  }
375  }
376 
377  /* Release the read lock since we're done with it now */
378  ReleaseReadLock(SearchTable, Count);
379 
380  /* We need the writer lock to expand/create the table */
381  AcquireWriteLock(SearchTable);
382 
383  /* Mark the table in use */
384  SearchTable->Expanding = TRUE;
385 
386  /* Wait for all the readers to finish */
387  TryWaitForReaders(SearchTable);
388 
389  /* Start loop */
390  do
391  {
392  /* Get the hash table again */
393  HashTable = SearchTable->HashTable;
394 
395  /* Check if exists now */
396  if (HashTable)
397  {
398  /* It does! Do what we wanted to do earlier. Get the hash... */
399  HashHandle = &WSH_HASH_FROM_HANDLE(Handle->Handle, HashTable);
400 
401  /* Check if it doesn't exist */
402  if (!(*HashHandle))
403  {
404  /* Write it (no need for interlock, we have the RW lock) */
405  OldHandle = Handle;
406  *HashHandle = Handle;
407  break;
408  }
409  else if ((*HashHandle)->Handle == Handle->Handle)
410  {
411  /* Handle matches, write it (see comment above) */
412  OldHandle = *HashHandle;
413  *HashHandle = Handle;
414  break;
415  }
416 
417  /* No go, we need to expand the table. Remember the size now */
418  HandleCount = HashTable->Size;
419  }
420  else
421  {
422  /* Table is empty, we have to create it */
423  HandleCount = 0;
424  }
425 
426 ExpandTable:
427  /* Find a free prime */
428  for (i = 0; HandleCount >= SockPrimes[i]; i++);
429 
430  /* Check if we found one */
431  if (SockPrimes[i] != 0xFFFFFFFF)
432  {
433  /* Use the prime */
434  HandleCount = SockPrimes[i];
435  }
436  else
437  {
438  /* No primes left. Table is quite large, so simply double it */
439  HandleCount *= 2;
440  }
441 
442  /* Allocate the table */
444  0,
446  Handles[HandleCount]));
447 
448  /* Hopefully we have one now */
449  if (NewHashTable)
450  {
451  /* Set its size */
452  NewHashTable->Size = HandleCount;
453 
454  /* Initialize it */
455  RtlZeroMemory(NewHashTable->Handles, HandleCount * sizeof(PVOID));
456 
457  /* Insert us first */
459 
460  /* Now check if our old table had entries in it */
461  if (HashTable)
462  {
463  /* We need to move them */
464  for (i = 0; i < HashTable->Size; i++)
465  {
466  /* Make sure the hash handle exists */
467  if (HashTable->Handles[i])
468  {
469  /* Get it */
470  HashHandle = &WSH_HASH_FROM_HANDLE(HashTable->
471  Handles[i]->Handle,
472  NewHashTable);
473 
474  /* Check if it has a value */
475  if (!(*HashHandle))
476  {
477  /* It's empty, so just write the handle */
478  *HashHandle = HashTable->Handles[i];
479  }
480  else
481  {
482  /* Not empty :/... that implies a collision */
484  goto ExpandTable;
485  }
486  }
487  }
488 
489  /* Write the new hash table */
490  SearchTable->HashTable = NewHashTable;
491 
492  /* Wait for everyone to be done with it, then free it */
493  TryWaitForReaders(SearchTable);
495  }
496  else
497  {
498  /* It was empty, nothing to worry about */
499  SearchTable->HashTable = NewHashTable;
500  }
501 
502  /* Save the old handle */
503  OldHandle = Handle;
504  }
505  else
506  {
507  /* There was no old handle */
508  OldHandle = Handle;
509  }
510  } while (0);
511 
512  /* Mark us as free, and release the write lock */
513  SearchTable->Expanding = FALSE;
514  ReleaseWriteLock(SearchTable);
515  break;
516  } while (1);
517 
518  /* Return the old handle */
519  return OldHandle;
520 }
ASMGENDATA Table[]
Definition: genincdata.c:61
#define TRUE
Definition: types.h:120
Definition: hash.c:67
VLONG * PVLONG
Definition: context.c:29
static __inline VOID AcquireReadLock(IN PWAH_SEARCH_TABLE Table, IN PVLONG *Count)
Definition: context.c:50
#define WSH_HASH_FROM_HANDLE(h, hs)
Definition: context.c:37
volatile PWAH_HASH_TABLE HashTable
Definition: ws2help.h:26
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
CONST DWORD SockPrimes[]
Definition: context.c:21
#define WSH_SEARCH_TABLE_FROM_HANDLE(h, t)
Definition: context.c:34
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
smooth NULL
Definition: ftsmooth.c:416
static __inline VOID TryWaitForReaders(IN PWAH_SEARCH_TABLE Table)
Definition: context.c:144
_In_ HANDLE Handle
Definition: extypes.h:390
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
HANDLE GlobalHeap
Definition: dllmain.c:19
struct HashTable * NewHashTable(void)
Definition: hash.c:77
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define ReleaseWriteLock(t)
Definition: context.c:43
#define HeapFree(x, y, z)
Definition: compat.h:402
#define AcquireWriteLock(t)
Definition: context.c:40
static __inline VOID ReleaseReadLock(IN PWAH_SEARCH_TABLE Table, IN PVLONG Count)
Definition: context.c:75

Referenced by WsSockAssociateHandle().

◆ WahReferenceContextByHandle()

PWAH_HANDLE WINAPI WahReferenceContextByHandle ( IN PWAH_HANDLE_TABLE  Table,
IN HANDLE  Handle 
)

Definition at line 524 of file context.c.

526 {
527  PWAH_HANDLE HashHandle;
528  PWAH_SEARCH_TABLE SearchTable;
530  PVLONG Count;
531 
532  /* Get the current Search Table */
533  SearchTable = WSH_SEARCH_TABLE_FROM_HANDLE(Handle, Table);
534 
535  /* Lock it */
536  AcquireReadLock(SearchTable, &Count);
537 
538  /* Get the hash table and handle */
539  HashTable = SearchTable->HashTable;
540 
541  /* Check if it's valid, and if it's the one we want */
542  if ((HashTable) &&
543  (HashHandle = WSH_HASH_FROM_HANDLE(Handle, HashTable)) &&
544  (HashHandle->Handle == Handle))
545  {
546  /* Reference the handle */
547  InterlockedIncrement(&HashHandle->RefCount);
548  }
549  else
550  {
551  /* Invalid handle */
552  HashHandle = NULL;
553  }
554 
555  /* Release the lock */
556  ReleaseReadLock(SearchTable, Count);
557 
558  /* Return */
559  return HashHandle;
560 }
ASMGENDATA Table[]
Definition: genincdata.c:61
Definition: hash.c:67
VLONG * PVLONG
Definition: context.c:29
static __inline VOID AcquireReadLock(IN PWAH_SEARCH_TABLE Table, IN PVLONG *Count)
Definition: context.c:50
#define WSH_HASH_FROM_HANDLE(h, hs)
Definition: context.c:37
volatile PWAH_HASH_TABLE HashTable
Definition: ws2help.h:26
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define WSH_SEARCH_TABLE_FROM_HANDLE(h, t)
Definition: context.c:34
smooth NULL
Definition: ftsmooth.c:416
_In_ HANDLE Handle
Definition: extypes.h:390
LONG RefCount
Definition: ws2help.h:14
#define InterlockedIncrement
Definition: armddk.h:53
HANDLE Handle
Definition: ws2help.h:15
static __inline VOID ReleaseReadLock(IN PWAH_SEARCH_TABLE Table, IN PVLONG Count)
Definition: context.c:75

Referenced by WsSockGetSocket(), and WsSockGetSocketNoExport().

◆ WahRemoveHandleContext()

DWORD WINAPI WahRemoveHandleContext ( IN PWAH_HANDLE_TABLE  Table,
IN PWAH_HANDLE  Handle 
)

Definition at line 564 of file context.c.

566 {
567  PWAH_HANDLE *HashHandle;
568  PWAH_SEARCH_TABLE SearchTable;
571 
572  /* Get the current Search Table */
573  SearchTable = WSH_SEARCH_TABLE_FROM_HANDLE(Handle->Handle, Table);
574 
575  /* Lock it */
576  AcquireWriteLock(SearchTable);
577 
578  /* Get the hash table and handle */
579  HashTable = SearchTable->HashTable;
580  HashHandle = &WSH_HASH_FROM_HANDLE(Handle->Handle, HashTable);
581 
582  /* Make sure we have a handle, and write the new pointer */
583  if (HashHandle && (InterlockedCompareExchangePointer((PVOID*)HashHandle,
584  NULL,
585  Handle) == Handle))
586  {
587  /* Wait for everyone to be done with it */
588  TryWaitForReaders(SearchTable);
589  }
590  else
591  {
592  /* Invalid handle */
594  }
595 
596  /* Release the lock */
597  ReleaseWriteLock(SearchTable);
598 
599  /* Return */
600  return ErrorCode;
601 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
ASMGENDATA Table[]
Definition: genincdata.c:61
Definition: hash.c:67
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define ERROR_SUCCESS
Definition: deptool.c:10
#define WSH_HASH_FROM_HANDLE(h, hs)
Definition: context.c:37
volatile PWAH_HASH_TABLE HashTable
Definition: ws2help.h:26
#define WSH_SEARCH_TABLE_FROM_HANDLE(h, t)
Definition: context.c:34
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
smooth NULL
Definition: ftsmooth.c:416
static __inline VOID TryWaitForReaders(IN PWAH_SEARCH_TABLE Table)
Definition: context.c:144
_In_ HANDLE Handle
Definition: extypes.h:390
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ReleaseWriteLock(t)
Definition: context.c:43
#define AcquireWriteLock(t)
Definition: context.c:40

Referenced by WsProcDetachSocket(), and WsSockDisassociateHandle().

Variable Documentation

◆ gdwSpinCount

DWORD gdwSpinCount = 0

Definition at line 18 of file context.c.

Referenced by NewCtxInit(), and WahCreateHandleContextTable().

◆ gHandleToIndexMask

DWORD gHandleToIndexMask

Definition at line 19 of file context.c.

Referenced by NewCtxInit(), and WahCreateHandleContextTable().

◆ ghWriterEvent

HANDLE ghWriterEvent

Definition at line 17 of file context.c.

Referenced by DllMain(), DoWaitForReaders(), and ReleaseReadLock().

◆ SockPrimes

CONST DWORD SockPrimes[]
Initial value:
=
{
31, 61, 127, 257, 521, 1031, 2053,
4099, 8191, 16381, 32749, 65537, 131071, 261983,
-1
}

Definition at line 21 of file context.c.

Referenced by WahInsertHandleContext().

◆ WshHandleTableLock

CRITICAL_SECTION WshHandleTableLock

Definition at line 16 of file context.c.