Path: blob/master/thirdparty/embree/common/sys/mutex.cpp
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#include "mutex.h"4#include "regression.h"56#if defined(__WIN32__) && !defined(PTHREADS_WIN32)78#define WIN32_LEAN_AND_MEAN9#include <windows.h>1011namespace embree12{13MutexSys::MutexSys() { mutex = new CRITICAL_SECTION; InitializeCriticalSection((CRITICAL_SECTION*)mutex); }14MutexSys::~MutexSys() { DeleteCriticalSection((CRITICAL_SECTION*)mutex); delete (CRITICAL_SECTION*)mutex; }15void MutexSys::lock() { EnterCriticalSection((CRITICAL_SECTION*)mutex); }16bool MutexSys::try_lock() { return TryEnterCriticalSection((CRITICAL_SECTION*)mutex) != 0; }17void MutexSys::unlock() { LeaveCriticalSection((CRITICAL_SECTION*)mutex); }18}19#endif2021#if defined(__UNIX__) || defined(PTHREADS_WIN32)22#include <pthread.h>23namespace embree24{25/*! system mutex using pthreads */26MutexSys::MutexSys()27{28mutex = new pthread_mutex_t;29if (pthread_mutex_init((pthread_mutex_t*)mutex, nullptr) != 0)30THROW_RUNTIME_ERROR("pthread_mutex_init failed");31}3233MutexSys::~MutexSys()34{35MAYBE_UNUSED bool ok = pthread_mutex_destroy((pthread_mutex_t*)mutex) == 0;36assert(ok);37delete (pthread_mutex_t*)mutex;38mutex = nullptr;39}4041void MutexSys::lock()42{43if (pthread_mutex_lock((pthread_mutex_t*)mutex) != 0)44THROW_RUNTIME_ERROR("pthread_mutex_lock failed");45}4647bool MutexSys::try_lock() {48return pthread_mutex_trylock((pthread_mutex_t*)mutex) == 0;49}5051void MutexSys::unlock()52{53if (pthread_mutex_unlock((pthread_mutex_t*)mutex) != 0)54THROW_RUNTIME_ERROR("pthread_mutex_unlock failed");55}56};57#endif585960