OpenMC/include/openmc/openmp_interface.h
2021-08-11 11:41:49 -05:00

76 lines
1.5 KiB
C++

#ifndef OPENMC_OPENMP_INTERFACE_H
#define OPENMC_OPENMP_INTERFACE_H
#ifdef _OPENMP
#include <omp.h>
#endif
namespace openmc {
//==============================================================================
//! An object used to prevent concurrent access to a piece of data.
//
//! This type meets the C++ "Lockable" requirements.
//==============================================================================
class OpenMPMutex {
public:
OpenMPMutex()
{
#ifdef _OPENMP
omp_init_lock(&mutex_);
#endif
}
~OpenMPMutex()
{
#ifdef _OPENMP
omp_destroy_lock(&mutex_);
#endif
}
// Mutexes cannot be copied. We need to explicitly delete the copy
// constructor and copy assignment operator to ensure the compiler doesn't
// "help" us by implicitly trying to copy the underlying mutexes.
OpenMPMutex(const OpenMPMutex&) = delete;
OpenMPMutex& operator=(const OpenMPMutex&) = delete;
//! Lock the mutex.
//
//! This function blocks execution until the lock succeeds.
void lock()
{
#ifdef _OPENMP
omp_set_lock(&mutex_);
#endif
}
//! Try to lock the mutex and indicate success.
//
//! This function does not block. It returns immediately and gives false if
//! the lock is unavailable.
bool try_lock() noexcept
{
#ifdef _OPENMP
return omp_test_lock(&mutex_);
#else
return true;
#endif
}
//! Unlock the mutex.
void unlock() noexcept
{
#ifdef _OPENMP
omp_unset_lock(&mutex_);
#endif
}
private:
#ifdef _OPENMP
omp_lock_t mutex_;
#endif
};
} // namespace openmc
#endif // OPENMC_OPENMP_INTERFACE_H