/* Static version just for mprAlloc which needs locks that don't allocate memory. */ PUBLIC MprSpin *mprInitSpinLock(MprSpin *lock) { #if USE_MPR_LOCK mprInitLock(&lock->cs); #elif MACOSX lock->cs = OS_SPINLOCK_INIT; #elif ME_UNIX_LIKE && ME_COMPILER_HAS_SPINLOCK pthread_spin_init(&lock->cs, 0); #elif ME_UNIX_LIKE pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); pthread_mutex_init(&lock->cs, &attr); pthread_mutexattr_destroy(&attr); #elif ME_WIN_LIKE && !ME_DEBUG && CRITICAL_SECTION_NO_DEBUG_INFO && ME_64 && _WIN32_WINNT >= 0x0600 InitializeCriticalSectionEx(&lock->cs, ME_MPR_SPIN_COUNT, CRITICAL_SECTION_NO_DEBUG_INFO); lock->freed = 0; #elif ME_WIN_LIKE InitializeCriticalSectionAndSpinCount(&lock->cs, ME_MPR_SPIN_COUNT); lock->freed = 0; #elif VXWORKS lock->cs = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE); #endif /* VXWORKS */ #if ME_DEBUG lock->owner = 0; #endif return lock; }
mutex::mutex() { #if defined(EA_PLATFORM_MICROSOFT) static_assert(sizeof(mMutexBuffer) == sizeof(CRITICAL_SECTION), "mMutexBuffer size failure"); //static_assert(EA_ALIGN_OF(mMutexBuffer) >= EA_ALIGN_OF(CRITICAL_SECTION), "mMutexBuffer alignment failure"); // Enabling this causes the VS2012 compiler to crash. #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0403) InitializeCriticalSection((CRITICAL_SECTION*)mMutexBuffer); #elif !EA_WINAPI_FAMILY_PARTITION(EA_WINAPI_PARTITION_DESKTOP) BOOL result = InitializeCriticalSectionEx((CRITICAL_SECTION*)mMutexBuffer, 10, 0); EASTL_ASSERT(result != 0); EA_UNUSED(result); #else BOOL result = InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION*)mMutexBuffer, 10); EASTL_ASSERT(result != 0); EA_UNUSED(result); #endif #elif defined(EA_PLATFORM_POSIX) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mMutex, &attr); pthread_mutexattr_destroy(&attr); #endif }
void NTAPI pthread_tls_init(void* dll, DWORD reason, void* reserved) { UNREFERENCED_PARAMETER(dll); UNREFERENCED_PARAMETER(reserved); switch(reason) { case DLL_PROCESS_ATTACH: slots_allocated = 0; destructors = NULL; InitializeCriticalSectionEx(&destructor_lock, 0, 0); // fall through case DLL_THREAD_ATTACH: _pthread_tls_attach_thread(); break; case DLL_THREAD_DETACH: _pthread_tls_detach_thread(); break; case DLL_PROCESS_DETACH: /* For WINOBJC - Don't detach the last thread, as it is likely executing from within a block and will complain about the queue not being empty */ #ifndef WINOBJC _pthread_tls_detach_thread(); #endif free(destructors); DeleteCriticalSection(&destructor_lock); break; } }
// Constructors Lock::Lock(unsigned spinCount) { #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility InitializeCriticalSectionEx(&cs, spinCount, OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO)); #else // Spin count init critical section function prototype for Window NT typedef BOOL (WINAPI *Function_InitializeCriticalSectionAndSpinCount) (LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount); // Try to load function dynamically so that we don't require NT // On Windows NT we will use InitializeCriticalSectionAndSpinCount static bool initTried = 0; static Function_InitializeCriticalSectionAndSpinCount pInitFn = 0; if (!initTried) { HMODULE hmodule = ::LoadLibrary(OVR_STR("kernel32.dll")); pInitFn = (Function_InitializeCriticalSectionAndSpinCount) ::GetProcAddress(hmodule, "InitializeCriticalSectionAndSpinCount"); initTried = true; } // Initialize the critical section if (pInitFn) pInitFn(&cs, spinCount); else ::InitializeCriticalSection(&cs); #endif }
/* ** The sqlite3_mutex_alloc() routine allocates a new ** mutex and returns a pointer to it. If it returns NULL ** that means that a mutex could not be allocated. SQLite ** will unwind its stack and return an error. The argument ** to sqlite3_mutex_alloc() is one of these integer constants: ** ** <ul> ** <li> SQLITE_MUTEX_FAST ** <li> SQLITE_MUTEX_RECURSIVE ** <li> SQLITE_MUTEX_STATIC_MASTER ** <li> SQLITE_MUTEX_STATIC_MEM ** <li> SQLITE_MUTEX_STATIC_MEM2 ** <li> SQLITE_MUTEX_STATIC_PRNG ** <li> SQLITE_MUTEX_STATIC_LRU ** <li> SQLITE_MUTEX_STATIC_PMEM ** </ul> ** ** The first two constants cause sqlite3_mutex_alloc() to create ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. ** The mutex implementation does not need to make a distinction ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does ** not want to. But SQLite will only request a recursive mutex in ** cases where it really needs one. If a faster non-recursive mutex ** implementation is available on the host platform, the mutex subsystem ** might return such a mutex in response to SQLITE_MUTEX_FAST. ** ** The other allowed parameters to sqlite3_mutex_alloc() each return ** a pointer to a static preexisting mutex. Six static mutexes are ** used by the current version of SQLite. Future versions of SQLite ** may add additional static mutexes. Static mutexes are for internal ** use by SQLite only. Applications that use SQLite mutexes should ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or ** SQLITE_MUTEX_RECURSIVE. ** ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() ** returns a different mutex on every call. But for the static ** mutex types, the same mutex is returned on every call that has ** the same type number. */ static sqlite3_mutex *winMutexAlloc(int iType) { sqlite3_mutex *p; switch( iType ) { case SQLITE_MUTEX_FAST: case SQLITE_MUTEX_RECURSIVE: { p = sqlite3MallocZero( sizeof(*p) ); if( p ) { #ifdef SQLITE_DEBUG p->id = iType; #endif #if SQLITE_OS_WINRT InitializeCriticalSectionEx(&p->mutex, 0, 0); #else InitializeCriticalSection(&p->mutex); #endif } break; } default: { assert( winMutex_isInit==1 ); assert( iType-2 >= 0 ); assert( iType-2 < ArraySize(winMutex_staticMutexes) ); p = &winMutex_staticMutexes[iType-2]; #ifdef SQLITE_DEBUG p->id = iType; #endif break; } } return p; }
void EbrLockInit(EbrLock* pLock) { CRITICAL_SECTION* pCrit = (CRITICAL_SECTION*)IwMalloc(sizeof(CRITICAL_SECTION)); InitializeCriticalSectionEx(pCrit, 0, 0); *pLock = (EbrLock)pCrit; }
void *lck_init(void) { #ifdef __SINGLE_THREAD return NULL; #else TLckData *lck = malloc(sizeof(TLckData)); if ( lck != NULL ) { #ifdef _WIN32 InitializeCriticalSectionEx(&lck->critSec, 4000, CRITICAL_SECTION_NO_DEBUG_INFO); #else pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&lck->mutex, &attr); #endif } return lck; #endif }
static void objc_global_mutex_new(void) { if (!InitializeCriticalSectionEx(&global_mutex, 0, 0)) OBJC_ERROR("Failed to create global mutex!"); global_mutex_init = YES; }
boolean Port_InitializeCriticalSection(CRITICAL_SECTION * criticalSection) { if (!InitializeCriticalSectionEx(criticalSection, 4000UL, RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN)) { KnxEt_Error("InitializeCriticalSectionAndSpinCount"); return FALSE; } return TRUE; }
/** @Status Caveat @Notes Ignores return value errors */ extern "C" int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) { CRITICAL_SECTION* crit = new CRITICAL_SECTION(); memset(crit, 0, sizeof(CRITICAL_SECTION)); InitializeCriticalSectionEx(crit, 0, 0); *mutex = (pthread_mutex*)crit; return 0; }
void NativeMutexInitialize(NativeMutexType *mutex) { #ifndef Q_OS_WINRT InitializeCriticalSection(mutex); #else InitializeCriticalSectionEx(mutex, 0, 0); #endif }
static void __cdecl __scrt_initialize_thread_safe_statics_platform_specific() throw() { InitializeCriticalSectionEx(&_Tss_mutex, 4000, 0); InitializeConditionVariable(&_Tss_cv); //encoded_sleep_condition_variable_cs = __crt_fast_encode_pointer(&SleepConditionVariableCS); //encoded_wake_all_condition_variable = __crt_fast_encode_pointer(&WakeAllConditionVariable); }
// Mutex static int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) { (void)mutexattr; #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/); #else InitializeCriticalSection(mutex); #endif return 0; }
void *__PHYSFS_platformCreateMutex(void) { LPCRITICAL_SECTION lpcs; lpcs = (LPCRITICAL_SECTION)allocator.Malloc(sizeof(CRITICAL_SECTION)); BAIL_IF_MACRO(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL); //InitializeCriticalSection(lpcs); InitializeCriticalSectionEx(lpcs, 2000, 0); return lpcs; } /* __PHYSFS_platformCreateMutex */
// Constructors Lock::Lock(unsigned spinCount) { #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility InitializeCriticalSectionEx(&cs, (DWORD)spinCount, OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO)); #else ::InitializeCriticalSectionAndSpinCount(&cs, (DWORD)spinCount); // This is available with WindowsXP+. #endif }
//---------------------------------------------------------------------------------------- amf_handle AMF_CDECL_CALL amf_create_critical_section() { CRITICAL_SECTION* cs = (CRITICAL_SECTION*)malloc(sizeof(CRITICAL_SECTION)); #if defined(METRO_APP) InitializeCriticalSectionEx(cs, 0, CRITICAL_SECTION_NO_DEBUG_INFO); #else InitializeCriticalSection(cs); #endif return (amf_handle)cs; // in Win32 - no errors }
void mutex::internal_construct() { #if _WIN32||_WIN64 InitializeCriticalSectionEx(&impl, 4000, 0); state = INITIALIZED; #else int error_code = pthread_mutex_init(&impl,NULL); if( error_code ) tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed"); #endif /* _WIN32||_WIN64*/ ITT_SYNC_CREATE(&impl, _T("tbb::mutex"), _T("")); }
//============================================================================== // Brief : コンストラクタ // Return : : なし // Arg : void : なし //============================================================================== CExecutorPushBack::CExecutorPushBack( void ) { // メンバ変数の初期化 m_idCurrent = 0; m_pItemTop = nullptr; m_isEnable = true; ZeroMemory( &m_criticalSection, sizeof( CRITICAL_SECTION ) ); // クリティカルセクションの初期化 InitializeCriticalSectionEx( &m_criticalSection, 0, 0 ); }
CriticalSectionLock::CriticalSectionLock(unsigned int spinCount) { #if defined(_WIN32) || defined(_WIN64) #if (_WIN32_WINNT >= 0x0600) // version >= vista. InitializeCriticalSectionEx(&m_key, spinCount, CRITICAL_SECTION_NO_DEBUG_INFO); #else InitializeCriticalSectionAndSpinCount(&m_key, spinCount); #endif #else pthread_mutex_init(&m_key, nullptr); #endif }
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName) { WINPR_EVENT* event; event = (WINPR_EVENT*) calloc(1, sizeof(WINPR_EVENT)); if (!event) return NULL; event->bAttached = FALSE; event->bManualReset = bManualReset; event->ops = &ops; WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT); if (!event->bManualReset) { WLog_ERR(TAG, "auto-reset events not yet implemented"); } event->pipe_fd[0] = -1; event->pipe_fd[1] = -1; #ifdef HAVE_EVENTFD_H event->pipe_fd[0] = eventfd(0, EFD_NONBLOCK); if (event->pipe_fd[0] < 0) { WLog_ERR(TAG, "failed to create event"); free(event); return NULL; } #else if (pipe(event->pipe_fd) < 0) { WLog_ERR(TAG, "failed to create event"); free(event); return NULL; } #endif if (bInitialState) SetEvent(event); if (!cs.LockSemaphore && !InitializeCriticalSectionEx(&cs, 0, 0)) { if (event->pipe_fd[0] != -1) close(event->pipe_fd[0]); if (event->pipe_fd[1] != -1) close(event->pipe_fd[1]); free(event); return NULL; } return (HANDLE)event; }
UNITEX_FUNC SYNC_Mutex_OBJECT UNITEX_CALL SyncBuildMutex() { SYNC_MUTEX_OBJECT_INTERNAL* pMoi = (SYNC_MUTEX_OBJECT_INTERNAL*)malloc(sizeof(SYNC_MUTEX_OBJECT_INTERNAL)); if (pMoi == NULL) return NULL; #ifdef UNITEX_USING_WINRT_API InitializeCriticalSectionEx(&(pMoi->cs),0,0); #else InitializeCriticalSection(&(pMoi->cs)); #endif return (SYNC_Mutex_OBJECT)pMoi; }
static void _HandleCreatorsInit() { /* NB: error management to be done outside of this function */ assert(_HandleCreators == NULL); _HandleCreators = (HANDLE_CREATOR**)calloc(HANDLE_CREATOR_MAX+1, sizeof(HANDLE_CREATOR*)); if (!_HandleCreators) return; if (!InitializeCriticalSectionEx(&_HandleCreatorsLock, 0, 0)) { free(_HandleCreators); _HandleCreators = NULL; } }
CFSMutex::CFSMutex() { #if defined (WINRT) InitializeCriticalSectionEx(&m_hMutex, 0, 0); #elif defined (WIN32) InitializeCriticalSection(&m_hMutex); #elif defined (UNIX) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutex_init(&m_hMutex, &attr); pthread_mutexattr_destroy(&attr); #elif defined (MAC) MPCreateCriticalRegion(&m_hMutex); #endif }
asCThreadCriticalSection::asCThreadCriticalSection() { #if defined AS_POSIX_THREADS pthread_mutex_init(&cs, 0); #elif defined AS_WINDOWS_THREADS #if defined(_MSC_VER) && (WINAPI_FAMILY & WINAPI_FAMILY_PHONE_APP) // Only the Ex version is available on Windows Store InitializeCriticalSectionEx(&cs, 4000, 0); #else // Only the non-Ex version is available on WinXP and older // MinGW also only defines this version InitializeCriticalSection(&cs); #endif #endif }
void CSimpleMutex::Init(void) { #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) InitializeCriticalSectionEx(&criticalSection,0,CRITICAL_SECTION_NO_DEBUG_INFO); #elif defined(_WIN32) // hMutex = CreateMutex(NULL, FALSE, 0); // RakAssert(hMutex); InitializeCriticalSection(&criticalSection); #else int error = pthread_mutex_init(&hMutex, 0); (void) error; RakAssert(error==0); #endif }
void pthread_cond_init(pthread_cond_t *cv, const void * attr) { cv->waiters_count_ = 0; cv->was_broadcast_ = 0; cv->sema_ = CreateSemaphoreEx(NULL, // no security 0, // initially 0 0x7fffffff, // max count NULL, // unnamed 0, EVENT_ALL_ACCESS); InitializeCriticalSectionEx(&cv->waiters_count_lock_, 10, 0); cv->waiters_done_ = CreateEventEx(NULL, // no security NULL, // unnamed 0, // auto-reset, unsignalled EVENT_ALL_ACCESS); }
HRESULT MuPDFDoc::InitContext() { InitializeCriticalSectionEx(&m_critSec, 0, 0); locks.user = &m_critSec; locks.lock = lock_mutex; locks.unlock = unlock_mutex; m_context = fz_new_context(nullptr, &locks, FZ_STORE_DEFAULT); if (!m_context) { return E_OUTOFMEMORY; } else { m_cts = (fz_cookie*)CoTaskMemAlloc(sizeof(fz_cookie)); return S_OK; } }
void recursive_mutex::internal_construct() { #if _WIN32||_WIN64 InitializeCriticalSectionEx(&impl, 4000, 0); state = INITIALIZED; #else pthread_mutexattr_t mtx_attr; int error_code = pthread_mutexattr_init( &mtx_attr ); if( error_code ) tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed"); pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE ); error_code = pthread_mutex_init( &impl, &mtx_attr ); if( error_code ) tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed"); pthread_mutexattr_destroy( &mtx_attr ); #endif /* _WIN32||_WIN64*/ ITT_SYNC_CREATE(&impl, _T("tbb::recursive_mutex"), _T("")); }
static void _CommInit() { /* NB: error management to be done outside of this function */ assert(_Log == NULL); assert(_CommDevices == NULL); assert(_CommHandleCreator == NULL); _CommDevices = (COMM_DEVICE**)calloc(COMM_DEVICE_MAX+1, sizeof(COMM_DEVICE*)); if (!_CommDevices) return; if (!InitializeCriticalSectionEx(&_CommDevicesLock, 0, 0)) { free(_CommDevices); _CommDevices = NULL; return; } _CommHandleCreator = (HANDLE_CREATOR*)malloc(sizeof(HANDLE_CREATOR)); if (!_CommHandleCreator) { DeleteCriticalSection(&_CommDevicesLock); free(_CommDevices); _CommDevices = NULL; return; } _CommHandleCreator->IsHandled = IsCommDevice; _CommHandleCreator->CreateFileA = CommCreateFileA; if (!RegisterHandleCreator(_CommHandleCreator)) { DeleteCriticalSection(&_CommDevicesLock); free(_CommDevices); free(_CommHandleCreator); _CommDevices = NULL; _CommHandleCreator = NULL; return; } _Log = WLog_Get("com.winpr.comm"); assert(_Log != NULL); }
static DVCMAN_CHANNEL* dvcman_channel_new(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr, UINT32 ChannelId, const char* ChannelName) { DVCMAN_CHANNEL* channel; if (dvcman_find_channel_by_id(pChannelMgr, ChannelId)) { WLog_Print(drdynvc->log, WLOG_ERROR, "Protocol error: Duplicated ChannelId %"PRIu32" (%s)!", ChannelId, ChannelName); return NULL; } channel = (DVCMAN_CHANNEL*) calloc(1, sizeof(DVCMAN_CHANNEL)); if (!channel) { WLog_Print(drdynvc->log, WLOG_ERROR, "calloc failed!"); return NULL; } channel->dvcman = (DVCMAN*) pChannelMgr; channel->channel_id = ChannelId; channel->channel_name = _strdup(ChannelName); if (!channel->channel_name) { WLog_Print(drdynvc->log, WLOG_ERROR, "_strdup failed!"); free(channel); return NULL; } if (!InitializeCriticalSectionEx(&(channel->lock), 0, 0)) { WLog_Print(drdynvc->log, WLOG_ERROR, "InitializeCriticalSectionEx failed!"); free(channel->channel_name); free(channel); return NULL; } return channel; }