Added context functions for FreeBSD.
[wine/multimedia.git] / scheduler / critsection.c
blob0c8d6cc3291925f98dbe3e73e8331d975afcb2df
1 /*
2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include "winerror.h"
12 #include "winbase.h"
13 #include "ntddk.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "thread.h"
18 DEFAULT_DEBUG_CHANNEL(win32)
19 DECLARE_DEBUG_CHANNEL(relay)
22 /***********************************************************************
23 * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
25 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
27 crit->LockCount = -1;
28 crit->RecursionCount = 0;
29 crit->OwningThread = 0;
30 crit->LockSemaphore = CreateSemaphoreA( NULL, 0, 1, NULL );
31 crit->Reserved = GetCurrentProcessId();
35 /***********************************************************************
36 * DeleteCriticalSection (KERNEL32.185) (NTDLL.327)
38 void WINAPI DeleteCriticalSection( CRITICAL_SECTION *crit )
40 if (crit->LockSemaphore)
42 if (crit->RecursionCount) /* Should not happen */
43 ERR("Deleting owned critical section (%p)\n", crit );
45 crit->LockCount = -1;
46 crit->RecursionCount = 0;
47 crit->OwningThread = 0;
48 CloseHandle( crit->LockSemaphore );
49 crit->LockSemaphore = 0;
50 crit->Reserved = (DWORD)-1;
55 /***********************************************************************
56 * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
58 void WINAPI EnterCriticalSection( CRITICAL_SECTION *crit )
60 DWORD res;
62 if (!crit->LockSemaphore)
64 FIXME("entering uninitialized section(%p)?\n",crit);
65 InitializeCriticalSection(crit);
67 if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
69 FIXME("Crst %p belongs to process %ld, current is %ld!\n",
70 crit, crit->Reserved, GetCurrentProcessId() );
71 return;
73 if (InterlockedIncrement( &crit->LockCount ))
75 if (crit->OwningThread == GetCurrentThreadId())
77 crit->RecursionCount++;
78 return;
81 /* Now wait for it */
82 for (;;)
84 EXCEPTION_RECORD rec;
86 res = WaitForSingleObject( crit->LockSemaphore, 5000L );
87 if ( res == WAIT_TIMEOUT )
89 ERR("Critical section %p wait timed out, retrying (60 sec)\n", crit );
90 res = WaitForSingleObject( crit->LockSemaphore, 60000L );
91 if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
93 ERR("Critical section %p wait timed out, retrying (5 min)\n", crit );
94 res = WaitForSingleObject( crit->LockSemaphore, 300000L );
97 if (res == STATUS_WAIT_0) break;
99 rec.ExceptionCode = EXCEPTION_CRITICAL_SECTION_WAIT;
100 rec.ExceptionFlags = 0;
101 rec.ExceptionRecord = NULL;
102 rec.ExceptionAddress = RtlRaiseException; /* sic */
103 rec.NumberParameters = 1;
104 rec.ExceptionInformation[0] = (DWORD)crit;
105 RtlRaiseException( &rec );
108 crit->OwningThread = GetCurrentThreadId();
109 crit->RecursionCount = 1;
113 /***********************************************************************
114 * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
116 BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
118 if (InterlockedIncrement( &crit->LockCount ))
120 if (crit->OwningThread == GetCurrentThreadId())
122 crit->RecursionCount++;
123 return TRUE;
125 /* FIXME: this doesn't work */
126 InterlockedDecrement( &crit->LockCount );
127 return FALSE;
129 crit->OwningThread = GetCurrentThreadId();
130 crit->RecursionCount = 1;
131 return TRUE;
135 /***********************************************************************
136 * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
138 void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
140 if (crit->OwningThread != GetCurrentThreadId()) return;
142 if (--crit->RecursionCount)
144 InterlockedDecrement( &crit->LockCount );
145 return;
147 crit->OwningThread = 0;
148 if (InterlockedDecrement( &crit->LockCount ) >= 0)
150 /* Someone is waiting */
151 ReleaseSemaphore( crit->LockSemaphore, 1, NULL );
156 /***********************************************************************
157 * MakeCriticalSectionGlobal (KERNEL32.515)
159 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
161 crit->LockSemaphore = ConvertToGlobalHandle( crit->LockSemaphore );
162 crit->Reserved = 0L;
166 /***********************************************************************
167 * ReinitializeCriticalSection (KERNEL32.581)
169 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
171 if ( !crit->LockSemaphore )
172 InitializeCriticalSection( crit );
174 else if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
176 FIXME("(%p) called for %08lx first, %08lx now: making global\n",
177 crit, crit->Reserved, GetCurrentProcessId() );
179 MakeCriticalSectionGlobal( crit );
184 /***********************************************************************
185 * UninitializeCriticalSection (KERNEL32.703)
187 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
189 if ( crit->LockSemaphore )
191 if ( crit->Reserved ) /* not global */
192 DeleteCriticalSection( crit );
193 else
194 FIXME("(%p) for %08lx: Crst is global, don't know whether to delete\n",
195 crit, GetCurrentProcessId() );