Program Listing for File ThreadPool.h

↰ Return to documentation for file (ADS/parallel/ThreadPool.h)

/*--------------------------------------------------------------------------*/
/*---------------------------- File ThreadPool.h ---------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_ThreadPool
 #define __ADS_ThreadPool
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"
#include "ADS/concurrent/EventCount.h"
#include "ADS/concurrent/WorkStealingDeque.h"
#include "ADS/parallel/Executor.h"

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS ThreadPool ----------------------------*/
/*--------------------------------------------------------------------------*/

class ThreadPool {

/*--------------------------------------------------------------------------*/
 public:

 explicit ThreadPool( std::size_t p = 0 , std::int64_t spin = 2000000 );
 ThreadPool( const ThreadPool & ) = delete;
 ThreadPool & operator=( const ThreadPool & ) = delete;

 ~ThreadPool();

 [[nodiscard]] std::size_t concurrency( void ) const {
  return( f_slot.size() );
  }

 template< class F >
 void bulk( std::size_t n , F && f );

 template< class F , class G >
 void invoke( F && f , G && g );

/*--------------------------------------------------------------------------*/
 private:

 struct Task {
  void ( * run )( Task * ) noexcept;
  };

 struct alignas( 128 ) Slot {
  WorkStealingDeque< Task * > deque{ 256 };
  std::uint64_t seed = 0;
  };

 struct Self {
  ThreadPool * pool;
  std::size_t slot;
  };

 class Enter;

 template< class F > struct BulkJob;
 template< class G > struct InvokeTask;

 void loop( std::size_t s );

 Task * find( std::size_t s );

 bool help( std::size_t s );

 static inline thread_local Self tl_self{ nullptr , 0 };

 std::vector< std::unique_ptr< Slot > > f_slot;
 std::vector< std::thread > f_thread;
 EventCount f_event;
 std::atomic< bool > f_stop{ false };
 std::mutex f_master;
 std::int64_t f_spin;
 };  // end( class ThreadPool )

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/parallel/impl/ThreadPool.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* ThreadPool.h included */

/*--------------------------------------------------------------------------*/
/*------------------------ End File ThreadPool.h ---------------------------*/
/*--------------------------------------------------------------------------*/