Commit 2a3f582e authored by Yuelong Yu's avatar Yuelong Yu
Browse files

Merge branch '3-replace-boost-with-standard-library' into 'master'

replace boost with standard library

Closes #3

See merge request !3
parents 60f438fd 5fadb773
Pipeline #24438 passed with stage
in 42 seconds
......@@ -2,9 +2,8 @@ cmake_minimum_required(VERSION 2.8)
project(fsdetcore)
enable_testing()
set(MAJOR_VERSION 0)
set(MINOR_VERSION 13)
set(MINOR_VERSION 14)
set(LIBRARY_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}")
set(SO_VERSION ${LIBRARY_VERSION})
# configure path
......@@ -30,14 +29,7 @@ message(STATUS "install path is:${CMAKE_INSTALL_PREFIX}")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
message(STATUS "module path is:${CMAKE_MODULE_PATH}")
#find necessary packages
find_package(LibBoost)
include_directories("${CMAKE_SOURCE_DIR}/include"
${Boost_INCLUDE_DIRS})
# get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
# message("-- inc_dirs = ${inc_dirs}")
include_directories("${CMAKE_SOURCE_DIR}/include")
# check compiler ID
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
......@@ -66,14 +58,13 @@ endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
#add shared linker flags
set(SHARED_LINKER_FLAG ${Boost_LIBRARIES} pthread)
set(PKG_LIBS "-lboost_system -lboost_thread -lpthread")
set(SHARED_LINKER_FLAG pthread)
set(PKG_LIBS "-lpthread")
#src
add_subdirectory("include")
add_subdirectory("src")
# unittest
add_subdirectory("test")
......@@ -8,7 +8,6 @@
## Installation
### Prerequists
- libboost >= 1.55
- cmake >= 3.0.2
- gcc >= 4.9.2
......
/*
* (c) Copyright 2014-2017 DESY, Yuelong Yu <yuelong.yu@desy.de>
* (c) Copyright 2014-2022 DESY
*
* This file is part of FS-DS detector library.
* This file is part of DESY software.
*
* This software is free: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -21,9 +21,6 @@
#pragma once
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/core/noncopyable.hpp>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
......@@ -48,10 +45,8 @@
#include <sys/types.h>
#include <time.h>
#include <vector>
#include <thread>
#include <chrono>
#include <ctime>
#include <mutex>
#include "Version.h"
namespace FSDetCoreNS
......
/*
* (c) Copyright 2014-2017 DESY, Yuelong Yu <yuelong.yu@desy.de>
* (c) Copyright 2014-2022 DESY
*
* This file is part of FS-DS detector library.
* This file is part of DESY software.
*
* This software is free: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -23,6 +23,8 @@
#include "Globals.h"
#include <mutex>
namespace FSDetCoreNS
{
/**
......@@ -31,7 +33,7 @@ namespace FSDetCoreNS
* This pool provides all the memory operations for the whole library
*/
template <class T>
class MemPool : public boost::noncopyable
class MemPool
{
public:
/**
......@@ -45,6 +47,9 @@ namespace FSDetCoreNS
* @brief destructor
*/
~MemPool();
MemPool( const MemPool & ) = delete;
MemPool& operator=( const MemPool& ) = delete;
void Reset();
......@@ -68,30 +73,30 @@ namespace FSDetCoreNS
* @brief get already stored images in the buffer
* @return stored images
*/
int32 GetStoredImageNumbers() const;
int32 GetStoredImageNumbers();
int32 GetFirstFrameNo() const;
int32 GetFirstFrameNo();
void SetFirstFrameNo(int32 lFrameNo);
int32 GetTotalReceivedFrames() const;
int32 GetTotalReceivedFrames();
/**
* @brief get latest arrived image no
* @return lastest image no
*/
int32 GetLastArrivedImageNo() const;
int32 GetLastArrivedImageNo();
/**
* @brief check if images are ready for reading
* @param nFrameNumbers how many frames needs to be read in one time
* @return true: ready for reading; false not ready yet
*/
bool IsImageReadyForReading(int32 nFrameNumbers) const;
bool IsImageReadyForReading(int32 nFrameNumbers);
void IsImageFinished();
......@@ -100,23 +105,23 @@ namespace FSDetCoreNS
/**
* @brief get start position of the buffer
*/
int32 GetStartPosOfBuffer() const;
int32 GetStartPosOfBuffer();
int32 GetTotalReceivedPackets() const;
int32 GetTotalReceivedPackets();
int32 GetFrameNoByIndex(const int32 lIdx) const;
int32 GetFrameNoByIndex(const int32 lIdx);
szt GetElementSize() const;
szt GetElementSize();
/**
* @brief tell if buffer is full
* @return true: full; false: not full
*/
bool IsFull() const;
bool IsFull();
void SetRequestedPacketNumber(int32 nPacketNumbers);
......@@ -176,19 +181,19 @@ namespace FSDetCoreNS
* @param lFrameNo frame no
* @return relative position
*/
int32 GetPosByFrameNo(int32 lFrameNo) const;
int32 GetPosByFrameNo(int32 lFrameNo);
/**
* @brief get start postision frame
* @return current frame no
*/
int32 GetCurrentFrameNo() const;
int32 GetCurrentFrameNo();
/**
* @brief get next consecutive frame no
* @return next frame no
*/
int32 GetNextFrameNo() const;
int32 GetNextFrameNo();
/**
......@@ -224,7 +229,7 @@ namespace FSDetCoreNS
vector<int32> m_vFrameNumber;
vector<int16> m_vErrorCode;
vector<int32> m_vDataSize;
mutable boost::mutex m_bstMtx;
std::mutex m_mtx;
vector<int32> m_vTaskMonitor;
vector<int32> m_vAcquiredPacketNumbers;
vector<string> vGlobalDebugInfo;
......
/*
* (c) Copyright 2014-2017 DESY, Yuelong Yu <yuelong.yu@desy.de>
* (c) Copyright 2014-2022 DESY
*
* This file is part of FS-DS detector library.
* This file is part of DESY software.
*
* This software is free: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -42,7 +42,7 @@ namespace FSDetCoreNS
m_lFirstFrameNumber(-1),
m_bFirstFrameSet(false),
m_nSafetyMargin(32),
m_bstMtx(),
m_mtx(),
m_bStartAcq(true),
m_bFrameStart(true)
{
......@@ -60,7 +60,7 @@ namespace FSDetCoreNS
template <class T>
MemPool<T>:: ~MemPool()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
LOG_TRACE(__FUNCTION__);
ReleaseMem();
m_vFrameNumber.clear();
......@@ -73,7 +73,7 @@ namespace FSDetCoreNS
template <class T>
void MemPool<T>::Reset()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
m_lLastArrived = 0;
m_lStartPos = 0;
m_lEndPos = 0;
......@@ -97,13 +97,13 @@ namespace FSDetCoreNS
m_bFirstFrameSet = false;
vGlobalDebugInfo.clear();
lock.unlock();
l.unlock();
}
template <class T>
void MemPool<T>::AllocateMem()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
// allocate the memory
for(szt i = 0; i < m_nSize; i++) {
T* objImg = new T[m_nElemSize];
......@@ -114,7 +114,7 @@ namespace FSDetCoreNS
template <class T>
void MemPool<T>::IncreaseMem(int32 nIncreasingSize)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
for(int i = 0; i < nIncreasingSize; i++) {
T* objImg = new T[m_nElemSize];
m_vImgBuff.push_back(objImg);
......@@ -128,7 +128,7 @@ namespace FSDetCoreNS
template <class T>
void MemPool<T>::AddTaskFrame(int32 nID, int32 nFrameNo)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
m_vTaskMonitor.push_back(-1);
}
......@@ -141,48 +141,44 @@ namespace FSDetCoreNS
}
template <class T>
int32 MemPool<T>::GetStoredImageNumbers() const
int32 MemPool<T>::GetStoredImageNumbers()
{
int32 lVal = m_lStoredImageNumbers;
return lVal;
return m_lStoredImageNumbers;
}
template <class T>
int32 MemPool<T>::GetFirstFrameNo() const
int32 MemPool<T>::GetFirstFrameNo()
{
int32 lVal = m_lFirstFrameNumber;
return lVal;
return m_lFirstFrameNumber;
}
template <class T>
void MemPool<T>::SetFirstFrameNo(int32 lFrameNo)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
m_lFirstFrameNumber = lFrameNo;
int32 lPos = GetPosByFrameNo(m_lFirstFrameNumber);
m_lStartPos = lPos;
m_lEndPos = lPos;
lock.unlock();
l.unlock();
}
template <class T>
int32 MemPool<T>::GetTotalReceivedFrames() const
int32 MemPool<T>::GetTotalReceivedFrames()
{
int32 lVal = m_lTotalReceivedFrames;
return lVal;
return m_lTotalReceivedFrames;
}
template <class T>
int32 MemPool<T>::GetLastArrivedImageNo() const
int32 MemPool<T>::GetLastArrivedImageNo()
{
int32 lVal = m_lLastArrived;
return lVal;
return m_lLastArrived;
}
template <class T>
bool MemPool<T>::IsImageReadyForReading(int32 nFrameNumbers) const
bool MemPool<T>::IsImageReadyForReading(int32 nFrameNumbers)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
bool bVal = false;
if(nFrameNumbers == 1)
bVal = (GetCurrentFrameNo() != int32min);
......@@ -197,7 +193,7 @@ namespace FSDetCoreNS
else
bVal = false;
}
lock.unlock();
l.unlock();
return bVal;
}
......@@ -205,9 +201,9 @@ namespace FSDetCoreNS
void MemPool<T>::IsImageFinished()
{
// frame finished, all packets received
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
if(m_vAcquiredPacketNumbers[m_lEndPos] == m_nRequestedPacketsNumber) {
lock.unlock();
l.unlock();
UpdateFrame(true);
}
else // check current working on frame
......@@ -216,7 +212,7 @@ namespace FSDetCoreNS
// all bigger than current lImgNo by at least 1
if(lVal > (m_lLastUnfinishedFrame + 1) && lVal != -1)
{
lock.unlock();
l.unlock();
UpdateFrame(false);
}
}
......@@ -225,7 +221,7 @@ namespace FSDetCoreNS
template <class T>
void MemPool<T>::UpdateFrame(bool bFullFinished)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
if(m_vAcquiredPacketNumbers[m_lEndPos] != 133)
{
string strTmp2 = "Raw image received incomplete: taskid:||"
......@@ -259,58 +255,51 @@ namespace FSDetCoreNS
}
template <class T>
int32 MemPool<T>::GetStartPosOfBuffer() const
int32 MemPool<T>::GetStartPosOfBuffer()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
int32 lVal = m_lStartPos;
lock.unlock();
return lVal;
std::lock_guard<std::mutex> l(m_mtx);
return m_lStartPos;
}
template <class T>
int32 MemPool<T>::GetTotalReceivedPackets() const
int32 MemPool<T>::GetTotalReceivedPackets()
{
return m_lTotalReceivedPackets;
}
template <class T>
int32 MemPool<T>::GetFrameNoByIndex(const int32 lIdx) const
int32 MemPool<T>::GetFrameNoByIndex(const int32 lIdx)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
int32 lVal = m_vFrameNumber[lIdx];
lock.unlock();
return lVal;
std::lock_guard<std::mutex> l(m_mtx);
return m_vFrameNumber[lIdx];
}
template <class T>
szt MemPool<T>::GetElementSize() const
szt MemPool<T>::GetElementSize()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::lock_guard<std::mutex> l(m_mtx);
return m_nElemSize;
}
template <class T>
bool MemPool<T>::IsFull() const
bool MemPool<T>::IsFull()
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
bool bVal = (static_cast<szt>(m_lStoredImageNumbers) >= m_nAllowedImages);
lock.unlock();
return bVal;
std::lock_guard<std::mutex> l(m_mtx);
return (static_cast<szt>(m_lStoredImageNumbers) >= m_nAllowedImages);
}
template <class T>
void MemPool<T>::SetRequestedPacketNumber(int32 nPacketNumbers)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::lock_guard<std::mutex> l(m_mtx);
m_nRequestedPacketsNumber = nPacketNumbers;
lock.unlock();
}
template <class T>
bool MemPool<T>::Get2Image(T*& objImg1, int32& lImgNo1, int16& shErrCode1,
T*& objImg2, int32& lImgNo2, int16& shErrCode2)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
bool bVal = false;
int32 lFrame1 = GetCurrentFrameNo();
......@@ -325,7 +314,7 @@ namespace FSDetCoreNS
if(!bVal) {
LOG_INFOS("No image is in buffer!!!");
lock.unlock();
l.unlock();
return false;
} else {
......@@ -378,13 +367,13 @@ namespace FSDetCoreNS
template <class T>
bool MemPool<T>::GetImage(T*& objImg, int32& lImgNo, int16& shErrCode, int32& nDataSize)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
bool bVal = false;
bVal = (GetCurrentFrameNo() != int32min);
if(!bVal) {
LOG_INFOS("No image is in buffer!!!");
lock.unlock();
l.unlock();
return false;
} else {
int32 nTempPos = m_lStartPos;
......@@ -426,7 +415,7 @@ namespace FSDetCoreNS
copy(objPacket + UDP_EXTRA_BYTES, objPacket + nLength, m_vImgBuff[lPos] + nPos);
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
if(m_bFrameStart) {
m_lStartPos = lPos;
......@@ -445,12 +434,12 @@ namespace FSDetCoreNS
template <class T>
bool MemPool<T>::SetImage(T* objImg, int32 lImgNo, int16 shErrCode)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
if(static_cast<szt>(m_lStoredImageNumbers) >= m_nAllowedImages) {
std::unique_lock<std::mutex> l(m_mtx);
if(static_cast<szt>(m_lStoredImageNumbers) >= m_nAllowedImages)
{
LOG_INFOS("Buffer is already full!!!");
lock.unlock();
l.unlock();
return false;
}
m_lLastArrived = lImgNo;
......@@ -469,7 +458,7 @@ namespace FSDetCoreNS
bool MemPool<T>::SetImage(T* objImg, int32 lImgNo, int16 shErrCode,
bool bFixedPosQueue, szt nDataSize)
{
boost::unique_lock<boost::mutex> lock(m_bstMtx);
std::unique_lock<std::mutex> l(m_mtx);
// Check if image falls into "safety margin" region - if so, don't allow insertion.
int32 lPos = GetPosByFrameNo(lImgNo);
int32 lSafetyTest = lPos - m_lStartPos;
......@@ -488,7 +477,7 @@ namespace FSDetCoreNS
// Rely on correct image numbering here
// Only update queue info etc after insertion,
// to prevent invalid images from causing problems.
lock.unlock();
l.unlock();
if(nDataSize == 0)
copy(objImg, objImg + m_nElemSize, m_vImgBuff[lPos]);
......@@ -498,7 +487,7 @@ namespace FSDetCoreNS
m_vDataSize[lPos] = nDataSize;
}
lock.lock();
l.lock();
m_lLastArrived = lImgNo; // Not guaranteed to be correct - depends on ordering
m_lStoredImageNumbers++;
......@@ -513,24 +502,21 @@ namespace FSDetCoreNS
}
template <class T>
int32 MemPool<T>::GetPosByFrameNo(int32 lFrameNo) const
int32 MemPool<T>::GetPosByFrameNo(int32 lFrameNo)
{
int32 lVal = (lFrameNo - 1) % m_nSize;
return lVal;
return (lFrameNo - 1) % m_nSize;
}
template <class T>
int32 MemPool<T>::GetCurrentFrameNo() const
int32 MemPool<T>::GetCurrentFrameNo()
{
int32 lVal = m_vFrameNumber[m_lStartPos];
return lVal;
return m_vFrameNumber[m_lStartPos];
}
template <class T>
int32 MemPool<T>::GetNextFrameNo() const
int32 MemPool<T>::GetNextFrameNo()
{
int32 lVal = m_vFrameNumber[((m_lStartPos + 1) % m_nSize)];
return lVal;
return m_vFrameNumber[((m_lStartPos + 1) % m_nSize)];
}
template <class T>
......
/*
* (c) Copyright 2014-2017 DESY, Yuelong Yu <yuelong.yu@desy.de>
* (c) Copyright 2014-2022 DESY
*
* This file is part of FS-DS detector library.
* This file is part of DESY software.
*
* This software is free: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -22,6 +22,10 @@
#pragma once
#include "Globals.h"
#include <thread>
#include <mutex>
#include <condition_variable>
namespace FSDetCoreNS
{
/**
......@@ -88,8 +92,8 @@ namespace FSDetCoreNS
int32 m_nID;
bool m_fStart,m_fExit;
boost::condition_variable m_bstCond;
boost::mutex m_bstSync;
std::condition_variable m_bstCond;
std::mutex m_bstSync;
};
/**
......@@ -138,12 +142,12 @@ namespace FSDetCoreNS
private:
int32 m_nThreadNumber;
boost::thread_group m_boostThGroup;
vector<std::thread> m_thread_pool;
int32 m_nAvailableThreads;
bool m_bRunning;
queue<Task*> m_qTaskList;
boost::mutex m_boostMtx;
boost::condition_variable m_boostCond;
std::mutex m_mtx;
std::condition_variable m_cond;
Enum_priority m_enumCurrentPriority;
};
}
......@@ -4,5 +4,5 @@
namespace FSDetCoreNS
{
const std::string FSDETCORE_VERSION="0.11";
const std::string FSDETCORE_VERSION="0.14";
}
/*
* (c) Copyright 2014-2022 DESY
*
* This file is part of DESY software.
*
* This software is free: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.