Files
OCCT/src/OSD/OSD_Parallel_Threads.cxx
kgv 6f498847fa 0029935: Foundation Classes - introduce OSD_ThreadPool class defining a thread pool
New class OSD_ThreadPool has been introduced to define a Thread Pool for multi-threading algorithm.
Thread Pool assigns a serial number for each thread allowing Multi-Threading algorithm to allocate thread-local storage variables as an array whose size is the same as the number of threads.

OSD_ThreadPool also redirects exceptions to a thread calling parallel execution and consistently initializes FPE exception handling.

New class Standard_Condition provides a platform-independent  tool similar to Event in WinAPI.

A new auxiliary function Standard_Atomic_CompareAndSwap() has been introduced
for performing atomic compare and swap of integer number.
Standard_Atomic_Increment/Standard_Atomic_Decrement fallback implementation
using ASM code for x86 processors for GCC has been dropped;
instead, it is expected that GCC should be properly configured targeting modern x86 architectures.

OSD_Signal now declares fFltExceptions as thread_local variable accessible through OSD::ToCatchFloatingSignals() property.
Standard_THREADLOCAL macro (wrapping thread_local attribute) has been moved to public header Standard_Macro.hxx.

OSD_Parallel::ForEach() has been extended with new optional parameter theNbItems and uses OSD_ThreadPool::DefaultPool().
2018-07-13 16:38:06 +03:00

160 lines
5.2 KiB
C++

// Created on: 2014-08-19
// Created by: Alexander Zaikin
// Copyright (c) 1996-1999 Matra Datavision
// Copyright (c) 2013-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
// Version of parallel executor used when TBB is not available
#ifndef HAVE_TBB
#include <OSD_Parallel.hxx>
#include <OSD_ThreadPool.hxx>
#include <NCollection_Array1.hxx>
#include <Standard_Mutex.hxx>
#include <OSD_Thread.hxx>
namespace
{
//! Class implementing tools for parallel processing
//! using threads (when TBB is not available);
//! it is derived from OSD_Parallel to get access to
//! Iterator and FunctorInterface nested types.
class OSD_Parallel_Threads : public OSD_ThreadPool, public OSD_Parallel
{
public:
//! Auxiliary class which ensures exclusive
//! access to iterators of processed data pool.
class Range
{
public: //! @name public methods
//! Constructor
Range(const OSD_Parallel::UniversalIterator& theBegin,
const OSD_Parallel::UniversalIterator& theEnd)
: myBegin(theBegin),
myEnd(theEnd),
myIt(theBegin)
{
}
//! Returns const link on the first element.
inline const OSD_Parallel::UniversalIterator& Begin() const
{
return myBegin;
}
//! Returns const link on the last element.
inline const OSD_Parallel::UniversalIterator& End() const
{
return myEnd;
}
//! Returns first non processed element or end.
//! Thread-safe method.
inline OSD_Parallel::UniversalIterator It() const
{
Standard_Mutex::Sentry aMutex(myMutex);
return (myIt != myEnd) ? myIt++ : myEnd;
}
private: //! @name private methods
//! Empty copy constructor
Range(const Range& theCopy);
//! Empty copy operator.
Range& operator=(const Range& theCopy);
private: //! @name private fields
const OSD_Parallel::UniversalIterator& myBegin; //!< Fisrt element of range.
const OSD_Parallel::UniversalIterator& myEnd; //!< Last element of range.
mutable OSD_Parallel::UniversalIterator myIt; //!< First non processed element of range.
mutable Standard_Mutex myMutex; //!< Access controller for the first non processed element.
};
//! Auxiliary wrapper class for thread function.
class Task : public JobInterface
{
public: //! @name public methods
//! Constructor.
Task(const OSD_Parallel::FunctorInterface& thePerformer, Range& theRange)
: myPerformer(thePerformer),
myRange(theRange)
{
}
//! Method is executed in the context of thread,
//! so this method defines the main calculations.
virtual void Perform (int ) Standard_OVERRIDE
{
for (OSD_Parallel::UniversalIterator anIter = myRange.It(); anIter != myRange.End(); anIter = myRange.It())
{
myPerformer (anIter);
}
}
private: //! @name private methods
//! Empty copy constructor.
Task(const Task& theCopy);
//! Empty copy operator.
Task& operator=(const Task& theCopy);
private: //! @name private fields
const FunctorInterface& myPerformer; //!< Link on functor
const Range& myRange; //!< Link on processed data block
};
//! Launcher specialization.
class UniversalLauncher : public Launcher
{
public:
//! Constructor.
UniversalLauncher (OSD_ThreadPool& thePool, int theMaxThreads = -1)
: Launcher (thePool, theMaxThreads) {}
//! Primitive for parallelization of "for" loops.
void Perform (OSD_Parallel::UniversalIterator& theBegin,
OSD_Parallel::UniversalIterator& theEnd,
const OSD_Parallel::FunctorInterface& theFunctor)
{
Range aData (theBegin, theEnd);
Task aJob (theFunctor, aData);
perform (aJob);
}
};
};
}
//=======================================================================
//function : forEach
//purpose :
//=======================================================================
void OSD_Parallel::forEach (UniversalIterator& theBegin,
UniversalIterator& theEnd,
const FunctorInterface& theFunctor,
Standard_Integer theNbItems)
{
const Handle(OSD_ThreadPool)& aThreadPool = OSD_ThreadPool::DefaultPool();
const Standard_Integer aNbThreads = theNbItems != -1 ? Min (theNbItems, aThreadPool->NbDefaultThreadsToLaunch()) : -1;
OSD_Parallel_Threads::UniversalLauncher aLauncher (*aThreadPool, aNbThreads);
aLauncher.Perform (theBegin, theEnd, theFunctor);
}
#endif /* ! HAVE_TBB */