您的位置: 首页 > IT文章 > 使用event与无锁算法实现windows平台上的critical section 使用event与无锁算法实现windows平台上的critical section 分类: IT文章 • 2024-03-22 09:18:25 使用event与无锁算法实现windows平台下的critical section//#include "own_cs.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <assert.h> #include <malloc.h> #include <stdlib.h> #include <stdio.h> struct own_critical_section { long own_lock_count; // count of locked thread, -1 means unlock , other means lock. HANDLE own_event; // auto-reset DWORD own_owning_thread_id; // owner thread of lock long own_recursion_count; // count of recursion }; void InitialOwnCriticalSection( own_critical_section** own_cs) { *own_cs = (own_critical_section*)malloc( sizeof( own_critical_section ) ); (*own_cs)->own_lock_count = -1; (*own_cs)->own_event = CreateEventW( NULL, FALSE, FALSE, NULL ); (*own_cs)->own_owning_thread_id = 0; (*own_cs)->own_recursion_count = 0; } void DeleteOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs != NULL ); CloseHandle( own_cs->own_event ); free( own_cs ); } void EnterOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); for ( int spin_count = 0; spin_count < 500; ++ spin_count ) {//spinlock if ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, -1L, -1L ) ) break; Sleep(0); } DWORD current_thread_id = ::GetCurrentThreadId(); if( 0 < InterlockedIncrement( &own_cs->own_lock_count ) && ( own_cs->own_owning_thread_id != current_thread_id ) ) { //locked WaitForSingleObject( own_cs->own_event, INFINITE ); } own_cs->own_owning_thread_id = current_thread_id; ++own_cs->own_recursion_count; } void LeaveOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); if ( --own_cs->own_recursion_count == 0 ) own_cs->own_owning_thread_id = 0; if( -1L != InterlockedDecrement( &own_cs->own_lock_count ) && (::GetCurrentThreadId() == own_cs->own_owning_thread_id ) ) { SetEvent( own_cs->own_event ); } } bool TryEnterOwnCriticalSection( own_critical_section* own_cs ) { assert( own_cs->own_lock_count>=-1L ); DWORD current_thread_id = GetCurrentThreadId(); if ( current_thread_id == own_cs->own_owning_thread_id ) { // inside the lock InterlockedIncrement( &own_cs->own_lock_count ); return true; } else{ if ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, 0, -1L ) ) return false; own_cs->own_owning_thread_id = current_thread_id; } ++own_cs->own_recursion_count; return true; }