Skip to content

Commit

Permalink
Merge pull request #458 from cryos/mutex
Browse files Browse the repository at this point in the history
Mutex
  • Loading branch information
cryos authored Apr 27, 2020
2 parents 31445db + d3e3a1f commit 4393fae
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 8 deletions.
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ project(AvogadroLibs)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# Request C++11 standard, using new CMake variables.
set(CMAKE_CXX_STANDARD 11)
# Request C++11 standard when building Python wheels due to old compiler/glibc.
# Use C++17 otherwise, and hopefully globally once the pip infra is updated.
option(PYTHON_WHEEL_BUILD "Is this a Python wheel build?" OFF)
mark_as_advanced(PYTHON_WHEEL_BUILD)
if(PYTHON_WHEEL_BUILD)
set(CMAKE_CXX_STANDARD 11)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS False)
# Set symbol visibility defaults for all targets.
Expand Down
17 changes: 16 additions & 1 deletion avogadro/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ set(SOURCES
version.cpp
)

# We currently build core without shared_mutex for Python wheels.
if(NOT PYTHON_WHEEL_BUILD)
list(APPEND HEADERS sharedmutex.h)
list(APPEND SOURCES sharedmutex.cpp)
endif()

if(USE_SPGLIB)
list(APPEND HEADERS avospglib.h)
list(APPEND SOURCES avospglib.cpp)
endif()

# The std::shared_mutex class needs pthreads on Linux.
if(UNIX AND NOT APPLE AND NOT PYTHON_WHEEL_BUILD)
find_package(Threads)
set(EXTRA_LINK_LIB ${CMAKE_THREAD_LIBS_INIT})
else()
set(EXTRA_LINK_LIB "")
endif()

avogadro_add_library(AvogadroCore ${HEADERS} ${SOURCES})
target_link_libraries(AvogadroCore LINK_PRIVATE ${SPGLIB_LIBRARY})
target_link_libraries(AvogadroCore
LINK_PRIVATE ${SPGLIB_LIBRARY} ${EXTRA_LINK_LIB})
8 changes: 3 additions & 5 deletions avogadro/core/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@

#include "avogadrocore.h"

#include <vector>

namespace Avogadro {
namespace Core {

/**
* @class Mutex mutex.h <avogadro/core/mutex.h>
* @brief The Mutex class provides a simple wrapper for the C++11 or Boost mutex
* @brief The Mutex class provides a simple wrapper for the C++11 mutex
* class
* @author Marcus D. Hanwell
*
* A very simple, and thin wrapper around the C++11 (or Boost fallback) mutex
* class, allowing for lock, tryLock and unlock.
* A very simple, and thin wrapper around the C++11 mutex class, allowing for
* lock, tryLock and unlock.
*/

class AVOGADROCORE_EXPORT Mutex
Expand Down
61 changes: 61 additions & 0 deletions avogadro/core/sharedmutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "sharedmutex.h"

#include <shared_mutex>

namespace Avogadro {
namespace Core {

using std::shared_mutex;

class SharedMutex::PIMPL
{
public:
PIMPL() {}

shared_mutex lock;
};

SharedMutex::SharedMutex() : d(new PIMPL) {}

SharedMutex::~SharedMutex()
{
delete d;
}

void SharedMutex::lockForRead()
{
d->lock.lock_shared();
}

bool SharedMutex::tryLockForRead()
{
return d->lock.try_lock_shared();
}

void SharedMutex::unlockForRead()
{
d->lock.unlock_shared();
}

void SharedMutex::lockForWrite()
{
d->lock.lock();
}

bool SharedMutex::tryLockForWrite()
{
return d->lock.try_lock();
}

void SharedMutex::unlockForWrite()
{
d->lock.unlock();
}

} // namespace Core
} // namespace Avogadro
69 changes: 69 additions & 0 deletions avogadro/core/sharedmutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_CORE_SHAREDMUTEX_H
#define AVOGADRO_CORE_SHAREDMUTEX_H

#include "avogadrocore.h"

namespace Avogadro {
namespace Core {

/**
* @class SharedMutex sharedmutex.h <avogadro/core/sharedmutex.h>
* @brief The SharedMutex class provides a simple wrapper for the C++17
* shared_mutex class
* @author Marcus D. Hanwell
*
* A very simple, and thin wrapper around the C++17 shared_mutex class, allowing
* for lock, tryLock and unlock.
*/

class AVOGADROCORE_EXPORT SharedMutex
{
public:
SharedMutex();
~SharedMutex();

/**
* @brief Obtain a shared read lock.
*/
void lockForRead();

/**
* @brief Attempt to obtain a shared read lock.
* @return True on success, false on failure.
*/
bool tryLockForRead();

/**
* @brief Unlocks the exclusive write lock.
*/
void unlockForRead();

/**
* @brief Obtain an exclusive write lock.
*/
void lockForWrite();

/**
* @brief Attempt to obtain an exclusive write lock.
* @return True on success, false on failure.
*/
bool tryLockForWrite();

/**
* @brief Unlocks the exclusive write lock.
*/
void unlockForWrite();

private:
class PIMPL;
PIMPL* d;
};
} // namespace Core
} // namespace Avogadro

#endif // AVOGADRO_CORE_SHAREDMUTEX_H
2 changes: 2 additions & 0 deletions avogadro/qtplugins/coloropacitymap/vtkChartHistogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#ifndef tomvizvtkChartHistogram_h
#define tomvizvtkChartHistogram_h

#include <bits/stdc++.h>

#include <vtkChartXY.h>

#include <vtkNew.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "vtkChartHistogramColorOpacityEditor.h"

#include <bits/stdc++.h>

#include <vtkAxis.h>
#include <vtkChart.h>
#include <vtkColorTransferControlPointsItem.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#ifndef tomvizvtkCustomPiecewiseControlPointsItem_h
#define tomvizvtkCustomPiecewiseControlPointsItem_h

#include <bits/stdc++.h>

#include <vtkPiecewiseControlPointsItem.h>

class vtkContextMouseEvent;
Expand Down
2 changes: 2 additions & 0 deletions avogadro/vtk/vtkplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <iostream>
#include <string>

#include <bits/stdc++.h>

#include <vtkAxis.h>
#include <vtkChartXY.h>
#include <vtkContextScene.h>
Expand Down
1 change: 1 addition & 0 deletions scripts/circleci/manylinux-build-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ for PYBIN in "${PYBINARIES[@]}"; do
-DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE} \
-DPYTHON_INCLUDE_DIR:PATH=${PYTHON_INCLUDE_DIR} \
-DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY} \
-DPYTHON_WHEEL_BUILD:BOOL=TRUE \
|| exit 1
${PYBIN}/python setup.py clean
done
Expand Down

0 comments on commit 4393fae

Please sign in to comment.