Presentation is loading. Please wait.

Presentation is loading. Please wait.

Win32 Synchronization CS470 -- Spring 2002. Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.

Similar presentations


Presentation on theme: "Win32 Synchronization CS470 -- Spring 2002. Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations."— Presentation transcript:

1 Win32 Synchronization CS470 -- Spring 2002

2 Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations Dispatcher Objects - Events, Semaphores, Mutex objects, Waitable Timers

3 Kernel Synchronization Manipulating IRQL selectively disables interrupts -- controls context switching IRQL only works on a single processor Spinlocks, based e.g. on test & set instructions, work for multiprocessors Busy-wait wastes cycles - so holder is never preempted, is not allowed to cause page faults, do system calls, cause interrupts or exceptions, etc.

4 Executive Synchronization Executive also uses spinlocks Also uses dispatcher objects Dispatcher objects also available to user programs - Win32 calls include WaitForSingleObject and WaitForMultipleObjects applicable to processes, threads, events, semaphores, mutex objects, waitable timers, file objects, etc.

5 Thread Dispatcher States Terminated-4 Ready-1 Standby-3 Transition-6 Waiting-5 Running-2 Initialized-0 Create Destroy Done Wait for Object Reinitialize Preempt Select Context-Switch Need Resource Get resource All Set Ready to run

6 Dispatcher Database Thread 1 Thread 2 Thread 3 Thread 4 Process 5 Timer 0 KWAIT_BLOCKS Dispatchers

7 Wait Operations (1 of 2) DWORD WaitForSingleObjectEx( HANDLE hHandle, DWORD dwMilliseconds BOOL bAlertable); Returns WAIT_OBJECT_0 (zero), WAIT_TIMEOUT, WAIT_FAILED, or WAIT_IO_COMPLETION If object non-signaled, block thread. When signaled, one or more threads are readied - can change state of object.

8 Wait Operations (2 of 2) DWORD WaitForMultipleObjectsEx( DWORD nCount, CONST HANDLE *lpHandles BOOL fWaitAll, DWORD dwMilliseconds, BOOL bAlertable); WAIT_OBJECT_0 to WAIT_OBJECT_n-1 to specify first signaled handle (if fWaitAll if false).

9 Events (1 of 2) HANDLE CreateEvent ( LPSECURITY_ATTRIBUTES lpSA, BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName); HANDLE OpenEvent ( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName);

10 Events (2 of 2) BOOL SetEvent ( HANDLE hEvent ); BOOL ResetEvent ( HANDLE hEvent ); BOOL PulseEvent ( HANDLE hEvent ); manual-reset versus auto-reset events setting versus pulsing CloseHandle used to close events

11 Mutual Exclusion (Events) Initialization: hEvent = CreateEvent( NULL, FALSE, TRUE, NULL); Critical Section Entry: WaitForSingleObjectEx( hEvent, INFINITE, FALSE); Critical Section Exit: SetEvent(hEvent);

12 Semaphores (1 of 2) HANDLE CreateSemaphore ( LPSECURITY_ATTRIBUTES lpSA, LONG lInitialCount, LONG lMaximumCount, LPCTSTR lpName ); HANDLE OpenSemaphore ( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName );

13 Semaphores (2 of 2) BOOL ReleaseSemaphore ( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount ); lReleaseCount is 1 to release semaphore or larger in order to set an initial value. CloseHandle to close semaphore handle

14 Mutual Exclusion (Semaphore) Initialization: hSemaphore = CreateSemaphore( NULL, 1, 1, NULL); Critical Section Entry: WaitForSingleObjectEx( hSemaphore, INFINITE, FALSE); Critical Section Exit: ReleaseSemaphore( hSemaphore, 1, &previous);

15 Mutex Objects (1 of 2) HANDLE CreateMutex ( LPSECURITY_ATTRIBUTES lpSA, BOOL bInitialOwner, LPCTSTR lpName ); HANDLE OpenMutex ( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName );

16 Mutex Objects (2 of 2) BOOL ReleaseMutex ( HANDLE hMutex ); Owner can make additional calls Wait calls without blocking, but must call ReleaseMutex the same number of times. Use CloseHandle to close mutex handles.

17 Mutual Exclusion (Mutex) Initialization: hMutex = CreateMutex( NULL, FALSE, NULL); Critical Section Entry: WaitForSingleObject( hMutex, INFINITE, FALSE); Critical Section Exit: ReleaseMutex(hMutex);

18 Waitable Timers (1 of 2) HANDLE CreateWaitableTimer ( LPSECURITY_ATTRIBUTES lpSA, BOOL bManualReset, LPCTSTR lpTimerName ); HANDLE OpenWaitableTimer ( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpTimerName );

19 Waitable Timers (2 of 2) BOOL SetWaitableTimer ( HANDLE hTimer, const LARGE_INTEGER *pDueTime, LONG lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, BOOL fResume ); BOOL CancelWaitableTimer (HANDLE);

20 Mutual Exclusion (Timer) 1 of 2 Initialization: LARGE_INTEGER dueTime = {-1, -1}; hTimer = (HANDLE) CreateWaitableTimer( NULL, FALSE, NULL); SetWaitableTimer( hTimer, &dueTime, 0, NULL, NULL, FALSE);

21 Mutual Exclusion (Timer) 2 of 2 Critical Section Entry: WaitForSingleObjectEx(hTimer, INFINITE, FALSE); Critical Section Exit: LARGE_INTEGER dueTime = {-1, -1}; SetWaitableTimer(hTimer, &dueTime, 0, NULL, NULL, FALSE);


Download ppt "Win32 Synchronization CS470 -- Spring 2002. Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations."

Similar presentations


Ads by Google