<commit_before>//===- OptimalEdgeProfiling.cpp - Insert counters for opt. edge profiling -===//
//
//                      The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass instruments the specified program with counters for edge profiling.
// Edge profiling can give a reasonable approximation of the hot paths through a
// program, and is used for a wide variety of program transformations.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "insert-optimal-edge-profiling"
#include "ProfilingUtils.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Analysis/ProfileInfoLoader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Statistic.h"
#include "MaximumSpanningTree.h"
#include <set>
using namespace llvm;

STATISTIC(NumEdgesInserted, "The # of edges inserted.");

namespace {
  class OptimalEdgeProfiler : public ModulePass {
    bool runOnModule(Module &M);
  public:
    static char ID; // Pass identification, replacement for typeid
    OptimalEdgeProfiler() : ModulePass(&ID) {}

    void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequiredID(ProfileEstimatorPassID);
      AU.addRequired<ProfileInfo>();
    }

    virtual const char *getPassName() const {
      return "Optimal Edge Profiler";
    }
  };
}

char OptimalEdgeProfiler::ID = 0;
static RegisterPass<OptimalEdgeProfiler>
X("insert-optimal-edge-profiling",
  "Insert optimal instrumentation for edge profiling");

ModulePass *llvm::createOptimalEdgeProfilerPass() {
  return new OptimalEdgeProfiler();
}

inline static void printEdgeCounter(ProfileInfo::Edge e,
                                    BasicBlock* b,
                                    unsigned i) {
  DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
               << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
}

bool OptimalEdgeProfiler::runOnModule(Module &M) {
  Function *Main = M.getFunction("main");
  if (Main == 0) {
    errs() << "WARNING: cannot insert edge profiling into a module"
           << " with no main function!\n";
    return false;  // No main, no instrumentation!
  }

  // NumEdges counts all the edges that may be instrumented. Later on its
  // decided which edges to actually instrument, to achieve optimal profiling.
  // For the entry block a virtual edge (0,entry) is reserved, for each block
  // with no successors an edge (BB,0) is reserved. These edges are necessary
  // to calculate a truly optimal maximum spanning tree and thus an optimal
  // instrumentation.
  unsigned NumEdges = 0;

  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
    if (F->isDeclaration()) continue;
    // Reserve space for (0,entry) edge.
    ++NumEdges;
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
      // Keep track of which blocks need to be instrumented.  We don't want to
      // instrument blocks that are added as the result of breaking critical
      // edges!
      if (BB->getTerminator()->getNumSuccessors() == 0) {
        // Reserve space for (BB,0) edge.
        ++NumEdges;
      } else {
        NumEdges += BB->getTerminator()->getNumSuccessors();
      }
    }
  }

  // In the profiling output a counter for each edge is reserved, but only few
  // are used. This is done to be able to read back in the profile without
  // calulating the maximum spanning tree again, instead each edge counter that
  // is not used is initialised with -1 to signal that this edge counter has to
  // be calculated from other edge counters on reading the profile info back
  // in.

  const Type *Int32 = Type::getInt32Ty(M.getContext());
  const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
  GlobalVariable *Counters =
    new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
                       Constant::getNullValue(ATy), "OptEdgeProfCounters");
  NumEdgesInserted = 0;

  std::vector<Constant*> Initializer(NumEdges);
  Constant* Zero = ConstantInt::get(Int32, 0);
  Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);

  // Instrument all of the edges not in MST...
  unsigned i = 0;
  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
    if (F->isDeclaration()) continue;
    DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");

    // Calculate a Maximum Spanning Tree with the edge weights determined by
    // ProfileEstimator. ProfileEstimator also assign weights to the virtual
    // edges (0,entry) and (BB,0) (for blocks with no successors) and this
    // edges also participate in the maximum spanning tree calculation.
    // The third parameter of MaximumSpanningTree() has the effect that not the
    // actual MST is returned but the edges _not_ in the MST.

    ProfileInfo::EdgeWeights ECs =
      getAnalysisID<ProfileInfo>(ProfileEstimatorPassID, *F).getEdgeWeights(F);
    std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
    MaximumSpanningTree<BasicBlock> MST (EdgeVector);
    std::stable_sort(MST.begin(),MST.end());

    // Check if (0,entry) not in the MST. If not, instrument edge
    // (IncrementCounterInBlock()) and set the counter initially to zero, if
    // the edge is in the MST the counter is initialised to -1.

    BasicBlock *entry = &(F->getEntryBlock());
    ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
    if (!std::binary_search(MST.begin(), MST.end(), edge)) {
      printEdgeCounter(edge,entry,i);
      IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
      Initializer[i++] = (Zero);
    } else{
      Initializer[i++] = (Uncounted);
    }

    // InsertedBlocks contains all blocks that were inserted for splitting an
    // edge, this blocks do not have to be instrumented.
    DenseSet<BasicBlock*> InsertedBlocks;
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
      // Check if block was not inserted and thus does not have to be
      // instrumented.
      if (InsertedBlocks.count(BB)) continue;

      // Okay, we have to add a counter of each outgoing edge not in MST. If
      // the outgoing edge is not critical don't split it, just insert the
      // counter in the source or destination of the edge. Also, if the block
      // has no successors, the virtual edge (BB,0) is processed.
      TerminatorInst *TI = BB->getTerminator();
      if (TI->getNumSuccessors() == 0) {
        ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
        if (!std::binary_search(MST.begin(), MST.end(), edge)) {
          printEdgeCounter(edge,BB,i);
          IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
          Initializer[i++] = (Zero);
        } else{
          Initializer[i++] = (Uncounted);
        }
      }
      for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
        BasicBlock *Succ = TI->getSuccessor(s);
        ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
        if (!std::binary_search(MST.begin(), MST.end(), edge)) {

          // If the edge is critical, split it.
          bool wasInserted = SplitCriticalEdge(TI, s, this);
          Succ = TI->getSuccessor(s);
          if (wasInserted)
            InsertedBlocks.insert(Succ);

          // Okay, we are guaranteed that the edge is no longer critical.  If
          // we only have a single successor, insert the counter in this block,
          // otherwise insert it in the successor block.
          if (TI->getNumSuccessors() == 1) {
            // Insert counter at the start of the block
            printEdgeCounter(edge,BB,i);
            IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
          } else {
            // Insert counter at the start of the block
            printEdgeCounter(edge,Succ,i);
            IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
          }
          Initializer[i++] = (Zero);
        } else {
          Initializer[i++] = (Uncounted);
        }
      }
    }
  }

  // Check if the number of edges counted at first was the number of edges we
  // considered for instrumentation.
  assert(i==NumEdges && "the number of edges in counting array is wrong");

  // Assing the now completely defined initialiser to the array.
  Constant *init = ConstantArray::get(ATy, Initializer);
  Counters->setInitializer(init);

  // Add the initialization call to main.
  InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
  return true;
}

<commit_msg>simplify this code.<commit_after>//===- OptimalEdgeProfiling.cpp - Insert counters for opt. edge profiling -===//
//
//                      The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass instruments the specified program with counters for edge profiling.
// Edge profiling can give a reasonable approximation of the hot paths through a
// program, and is used for a wide variety of program transformations.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "insert-optimal-edge-profiling"
#include "ProfilingUtils.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Analysis/ProfileInfoLoader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Statistic.h"
#include "MaximumSpanningTree.h"
#include <set>
using namespace llvm;

STATISTIC(NumEdgesInserted, "The # of edges inserted.");

namespace {
  class OptimalEdgeProfiler : public ModulePass {
    bool runOnModule(Module &M);
  public:
    static char ID; // Pass identification, replacement for typeid
    OptimalEdgeProfiler() : ModulePass(&ID) {}

    void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequiredID(ProfileEstimatorPassID);
      AU.addRequired<ProfileInfo>();
    }

    virtual const char *getPassName() const {
      return "Optimal Edge Profiler";
    }
  };
}

char OptimalEdgeProfiler::ID = 0;
static RegisterPass<OptimalEdgeProfiler>
X("insert-optimal-edge-profiling",
  "Insert optimal instrumentation for edge profiling");

ModulePass *llvm::createOptimalEdgeProfilerPass() {
  return new OptimalEdgeProfiler();
}

inline static void printEdgeCounter(ProfileInfo::Edge e,
                                    BasicBlock* b,
                                    unsigned i) {
  DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
               << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
}

bool OptimalEdgeProfiler::runOnModule(Module &M) {
  Function *Main = M.getFunction("main");
  if (Main == 0) {
    errs() << "WARNING: cannot insert edge profiling into a module"
           << " with no main function!\n";
    return false;  // No main, no instrumentation!
  }

  // NumEdges counts all the edges that may be instrumented. Later on its
  // decided which edges to actually instrument, to achieve optimal profiling.
  // For the entry block a virtual edge (0,entry) is reserved, for each block
  // with no successors an edge (BB,0) is reserved. These edges are necessary
  // to calculate a truly optimal maximum spanning tree and thus an optimal
  // instrumentation.
  unsigned NumEdges = 0;

  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
    if (F->isDeclaration()) continue;
    // Reserve space for (0,entry) edge.
    ++NumEdges;
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
      // Keep track of which blocks need to be instrumented.  We don't want to
      // instrument blocks that are added as the result of breaking critical
      // edges!
      if (BB->getTerminator()->getNumSuccessors() == 0) {
        // Reserve space for (BB,0) edge.
        ++NumEdges;
      } else {
        NumEdges += BB->getTerminator()->getNumSuccessors();
      }
    }
  }

  // In the profiling output a counter for each edge is reserved, but only few
  // are used. This is done to be able to read back in the profile without
  // calulating the maximum spanning tree again, instead each edge counter that
  // is not used is initialised with -1 to signal that this edge counter has to
  // be calculated from other edge counters on reading the profile info back
  // in.

  const Type *Int32 = Type::getInt32Ty(M.getContext());
  const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
  GlobalVariable *Counters =
    new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
                       Constant::getNullValue(ATy), "OptEdgeProfCounters");
  NumEdgesInserted = 0;

  std::vector<Constant*> Initializer(NumEdges);
  Constant* Zero = ConstantInt::get(Int32, 0);
  Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);

  // Instrument all of the edges not in MST...
  unsigned i = 0;
  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
    if (F->isDeclaration()) continue;
    DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");

    // Calculate a Maximum Spanning Tree with the edge weights determined by
    // ProfileEstimator. ProfileEstimator also assign weights to the virtual
    // edges (0,entry) and (BB,0) (for blocks with no successors) and this
    // edges also participate in the maximum spanning tree calculation.
    // The third parameter of MaximumSpanningTree() has the effect that not the
    // actual MST is returned but the edges _not_ in the MST.

    ProfileInfo::EdgeWeights ECs =
      getAnalysis<ProfileInfo>(*F).getEdgeWeights(F);
    std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
    MaximumSpanningTree<BasicBlock> MST (EdgeVector);
    std::stable_sort(MST.begin(),MST.end());

    // Check if (0,entry) not in the MST. If not, instrument edge
    // (IncrementCounterInBlock()) and set the counter initially to zero, if
    // the edge is in the MST the counter is initialised to -1.

    BasicBlock *entry = &(F->getEntryBlock());
    ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
    if (!std::binary_search(MST.begin(), MST.end(), edge)) {
      printEdgeCounter(edge,entry,i);
      IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
      Initializer[i++] = (Zero);
    } else{
      Initializer[i++] = (Uncounted);
    }

    // InsertedBlocks contains all blocks that were inserted for splitting an
    // edge, this blocks do not have to be instrumented.
    DenseSet<BasicBlock*> InsertedBlocks;
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
      // Check if block was not inserted and thus does not have to be
      // instrumented.
      if (InsertedBlocks.count(BB)) continue;

      // Okay, we have to add a counter of each outgoing edge not in MST. If
      // the outgoing edge is not critical don't split it, just insert the
      // counter in the source or destination of the edge. Also, if the block
      // has no successors, the virtual edge (BB,0) is processed.
      TerminatorInst *TI = BB->getTerminator();
      if (TI->getNumSuccessors() == 0) {
        ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
        if (!std::binary_search(MST.begin(), MST.end(), edge)) {
          printEdgeCounter(edge,BB,i);
          IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
          Initializer[i++] = (Zero);
        } else{
          Initializer[i++] = (Uncounted);
        }
      }
      for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
        BasicBlock *Succ = TI->getSuccessor(s);
        ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
        if (!std::binary_search(MST.begin(), MST.end(), edge)) {

          // If the edge is critical, split it.
          bool wasInserted = SplitCriticalEdge(TI, s, this);
          Succ = TI->getSuccessor(s);
          if (wasInserted)
            InsertedBlocks.insert(Succ);

          // Okay, we are guaranteed that the edge is no longer critical.  If
          // we only have a single successor, insert the counter in this block,
          // otherwise insert it in the successor block.
          if (TI->getNumSuccessors() == 1) {
            // Insert counter at the start of the block
            printEdgeCounter(edge,BB,i);
            IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
          } else {
            // Insert counter at the start of the block
            printEdgeCounter(edge,Succ,i);
            IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
          }
          Initializer[i++] = (Zero);
        } else {
          Initializer[i++] = (Uncounted);
        }
      }
    }
  }

  // Check if the number of edges counted at first was the number of edges we
  // considered for instrumentation.
  assert(i==NumEdges && "the number of edges in counting array is wrong");

  // Assing the now completely defined initialiser to the array.
  Constant *init = ConstantArray::get(ATy, Initializer);
  Counters->setInitializer(init);

  // Add the initialization call to main.
  InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
  return true;
}

<|endoftext|>


<commit_before>/**
 * \file FFT.cpp
 */

#include <cmath>
#include <complex>
#include <cstdlib>

#include "FFT.h"

namespace ATK
{
  template<class DataType_>
  FFT<DataType_>::FFT()
#if ATK_USE_FFTW == 1
  :size(0), fft_plan(nullptr), fft_reverse_plan(nullptr), input_data(nullptr), output_freqs(nullptr)
#endif
#if ATK_USE_ACCELERATE == 1
  :fftSetup(nullptr)
#endif
  {
#if ATK_USE_ACCELERATE == 1
    splitData.realp = nullptr;
    splitData.imagp = nullptr;
#endif
  }

  template<class DataType_>
  FFT<DataType_>::~FFT()
  {
#if ATK_USE_FFTW == 1
    fftw_free(input_data);
    fftw_free(output_freqs);
    fftw_destroy_plan(fft_plan);
    fftw_destroy_plan(fft_reverse_plan);
#endif
#if ATK_USE_ACCELERATE == 1
    delete[] splitData.realp;
    delete[] splitData.imagp;
    vDSP_destroy_fftsetupD(fftSetup);
    delete[] output_freqs;
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::set_size(int64_t size)
  {
    if(this->size == size)
      return;
    this->size = size;
    log2n = static_cast<int>(std::log(size) / std::log(2.));
#if ATK_USE_FFTW == 1
    fftw_free(input_data);
    fftw_free(output_freqs);
    fftw_destroy_plan(fft_plan);
    fftw_destroy_plan(fft_reverse_plan);

    input_data = fftw_alloc_complex(size);
    output_freqs = fftw_alloc_complex(size);

    fft_plan = fftw_plan_dft_1d(size, input_data, output_freqs, FFTW_FORWARD, FFTW_ESTIMATE);
    fft_reverse_plan = fftw_plan_dft_1d(size, output_freqs, input_data, FFTW_BACKWARD, FFTW_ESTIMATE);
#endif
#if ATK_USE_ACCELERATE == 1
    delete[] splitData.realp;
    delete[] splitData.imagp;
    vDSP_destroy_fftsetupD(fftSetup);
    splitData.realp = new double[size];
    splitData.imagp = new double[size];
    fftSetup = vDSP_create_fftsetupD(log2n, FFT_RADIX2);
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::process(const DataType_* input, int64_t input_size) const
  {
    double factor = 1 << log2n;
#if ATK_USE_FFTW == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      input_data[j][0] = input[j] / factor;
      input_data[j][1] = 0;
    }
    for(int j = std::min(input_size, size); j < size; ++j)
    {
      input_data[j][0] = 0;
      input_data[j][1] = 0;
    }
    fftw_execute(fft_plan);
#endif
#if ATK_USE_ACCELERATE == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      splitData.realp[j] = input[j] / factor;
      splitData.imagp[j] = 0;
    }
    for(int j = std::min(input_size, size); j < size; ++j)
    {
      splitData.realp[j] = 0;
      splitData.imagp[j] = 0;
    }
    vDSP_fft_zipD(fftSetup, &splitData, 1, log2n, FFT_FORWARD);
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::process_forward(const DataType_* input, std::complex<DataType_>* output, int64_t input_size) const
  {
    process(input, input_size);
    for(int j = 0; j < size; ++j)
    {
#if ATK_USE_FFTW == 1
      output[j] = std::complex<DataType_>(output_freqs[j][0], output_freqs[j][1]);
#endif
#if ATK_USE_ACCELERATE == 1
      output[j] = std::complex<DataType_>(splitData.realp[j], splitData.imagp[j]);
#endif
    }
  }

  template<class DataType_>
  void FFT<DataType_>::process_backward(const std::complex<DataType_>* input, DataType_* output, int64_t input_size) const
  {
#if ATK_USE_FFTW == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output_freqs[j][0] = input[j].real();
      output_freqs[j][1] = input[j].imag();
    }
    fftw_execute(fft_reverse_plan);
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output[j] = input_data[j][0];
    }
#endif
#if ATK_USE_ACCELERATE == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      splitData.realp[j] = input[j].real();
      splitData.imagp[j] = input[j].imag();
    }
    vDSP_fft_zipD(fftSetup, &splitData, 1, log2n, FFT_BACKWARD);
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output[j] = splitData.realp[j];
    }
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::get_amp(std::vector<DataType_>& amp) const
  {
    amp.assign(size / 2 + 1, 0);
    for(int64_t i = 0; i < size / 2 + 1; ++i)
    {
#if ATK_USE_FFTW == 1
      amp[i] = output_freqs[i][0] * output_freqs[i][0];
      amp[i] += output_freqs[i][1] * output_freqs[i][1];
      amp[i] = std::sqrt(amp[i]);
#endif
#if ATK_USE_ACCELERATE == 1
      amp[i] = splitData.realp[j] * splitData.realp[j];
      amp[i] += splitData.imagp[j] * splitData.imagp[j];
      amp[i] = std::sqrt(amp[i]);
#endif
    }
  }

  template<class DataType_>
  void FFT<DataType_>::get_angle(std::vector<DataType_>& angle) const
  {
    angle.assign(size / 2 + 1, 0);
    for(int64_t i = 0; i < size / 2 + 1; ++i)
    {
#if ATK_USE_FFTW == 1
      angle[i] = std::arg(std::complex<DataType_>(output_freqs[i][0], output_freqs[i][1]));
#endif
#if ATK_USE_ACCELERATE == 1
      angle[i] = std::arg(std::complex<DataType_>(splitData.realp[j], splitData.imagp[j]));
#endif
    }
  }

  template class FFT<double>;
}
<commit_msg>Fix VS2015 compilation<commit_after>/**
 * \file FFT.cpp
 */

#include <algorithm>
#include <complex>
#include <cstdlib>

#include "FFT.h"

namespace ATK
{
  template<class DataType_>
  FFT<DataType_>::FFT()
#if ATK_USE_FFTW == 1
  :size(0), fft_plan(nullptr), fft_reverse_plan(nullptr), input_data(nullptr), output_freqs(nullptr)
#endif
#if ATK_USE_ACCELERATE == 1
  :fftSetup(nullptr)
#endif
  {
#if ATK_USE_ACCELERATE == 1
    splitData.realp = nullptr;
    splitData.imagp = nullptr;
#endif
  }

  template<class DataType_>
  FFT<DataType_>::~FFT()
  {
#if ATK_USE_FFTW == 1
    fftw_free(input_data);
    fftw_free(output_freqs);
    fftw_destroy_plan(fft_plan);
    fftw_destroy_plan(fft_reverse_plan);
#endif
#if ATK_USE_ACCELERATE == 1
    delete[] splitData.realp;
    delete[] splitData.imagp;
    vDSP_destroy_fftsetupD(fftSetup);
    delete[] output_freqs;
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::set_size(int64_t size)
  {
    if(this->size == size)
      return;
    this->size = size;
    log2n = static_cast<int>(std::log(size) / std::log(2.));
#if ATK_USE_FFTW == 1
    fftw_free(input_data);
    fftw_free(output_freqs);
    fftw_destroy_plan(fft_plan);
    fftw_destroy_plan(fft_reverse_plan);

    input_data = fftw_alloc_complex(size);
    output_freqs = fftw_alloc_complex(size);

    fft_plan = fftw_plan_dft_1d(size, input_data, output_freqs, FFTW_FORWARD, FFTW_ESTIMATE);
    fft_reverse_plan = fftw_plan_dft_1d(size, output_freqs, input_data, FFTW_BACKWARD, FFTW_ESTIMATE);
#endif
#if ATK_USE_ACCELERATE == 1
    delete[] splitData.realp;
    delete[] splitData.imagp;
    vDSP_destroy_fftsetupD(fftSetup);
    splitData.realp = new double[size];
    splitData.imagp = new double[size];
    fftSetup = vDSP_create_fftsetupD(log2n, FFT_RADIX2);
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::process(const DataType_* input, int64_t input_size) const
  {
    double factor = 1 << log2n;
#if ATK_USE_FFTW == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      input_data[j][0] = input[j] / factor;
      input_data[j][1] = 0;
    }
    for(int j = std::min(input_size, size); j < size; ++j)
    {
      input_data[j][0] = 0;
      input_data[j][1] = 0;
    }
    fftw_execute(fft_plan);
#endif
#if ATK_USE_ACCELERATE == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      splitData.realp[j] = input[j] / factor;
      splitData.imagp[j] = 0;
    }
    for(int j = std::min(input_size, size); j < size; ++j)
    {
      splitData.realp[j] = 0;
      splitData.imagp[j] = 0;
    }
    vDSP_fft_zipD(fftSetup, &splitData, 1, log2n, FFT_FORWARD);
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::process_forward(const DataType_* input, std::complex<DataType_>* output, int64_t input_size) const
  {
    process(input, input_size);
    for(int j = 0; j < size; ++j)
    {
#if ATK_USE_FFTW == 1
      output[j] = std::complex<DataType_>(output_freqs[j][0], output_freqs[j][1]);
#endif
#if ATK_USE_ACCELERATE == 1
      output[j] = std::complex<DataType_>(splitData.realp[j], splitData.imagp[j]);
#endif
    }
  }

  template<class DataType_>
  void FFT<DataType_>::process_backward(const std::complex<DataType_>* input, DataType_* output, int64_t input_size) const
  {
#if ATK_USE_FFTW == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output_freqs[j][0] = input[j].real();
      output_freqs[j][1] = input[j].imag();
    }
    fftw_execute(fft_reverse_plan);
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output[j] = input_data[j][0];
    }
#endif
#if ATK_USE_ACCELERATE == 1
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      splitData.realp[j] = input[j].real();
      splitData.imagp[j] = input[j].imag();
    }
    vDSP_fft_zipD(fftSetup, &splitData, 1, log2n, FFT_BACKWARD);
    for(int j = 0; j < std::min(input_size, size); ++j)
    {
      output[j] = splitData.realp[j];
    }
#endif
  }

  template<class DataType_>
  void FFT<DataType_>::get_amp(std::vector<DataType_>& amp) const
  {
    amp.assign(size / 2 + 1, 0);
    for(int64_t i = 0; i < size / 2 + 1; ++i)
    {
#if ATK_USE_FFTW == 1
      amp[i] = output_freqs[i][0] * output_freqs[i][0];
      amp[i] += output_freqs[i][1] * output_freqs[i][1];
      amp[i] = std::sqrt(amp[i]);
#endif
#if ATK_USE_ACCELERATE == 1
      amp[i] = splitData.realp[j] * splitData.realp[j];
      amp[i] += splitData.imagp[j] * splitData.imagp[j];
      amp[i] = std::sqrt(amp[i]);
#endif
    }
  }

  template<class DataType_>
  void FFT<DataType_>::get_angle(std::vector<DataType_>& angle) const
  {
    angle.assign(size / 2 + 1, 0);
    for(int64_t i = 0; i < size / 2 + 1; ++i)
    {
#if ATK_USE_FFTW == 1
      angle[i] = std::arg(std::complex<DataType_>(output_freqs[i][0], output_freqs[i][1]));
#endif
#if ATK_USE_ACCELERATE == 1
      angle[i] = std::arg(std::complex<DataType_>(splitData.realp[j], splitData.imagp[j]));
#endif
    }
  }

  template class FFT<double>;
}
<|endoftext|>


<commit_before>//=======================================================================
// Copyright (c) 2014-2017 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include "test_light.hpp"

#include <cmath>

TEMPLATE_TEST_CASE_2("saxpy/0", "[saxpy][fast]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> x = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> y = {1.0, 3.0, 0.5, 1.2};
    etl::fast_matrix<Z, 2, 2> yy;

    yy = Z(2.0) * x + y;

    REQUIRE_EQUALS(yy[0], Z(-1.0));
    REQUIRE_EQUALS(yy[1], Z(7.0));
    REQUIRE_EQUALS(yy[2], Z(0.5));
    REQUIRE_EQUALS(yy[3], Z(3.2));
}

TEMPLATE_TEST_CASE_2("fast_matrix/max", "fast_matrix::max", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};

    etl::fast_matrix<Z, 2, 2> d;
    d = max(a, 1.0);

    REQUIRE_EQUALS(d[0], 1.0);
    REQUIRE_EQUALS(d[1], 2.0);
    REQUIRE_EQUALS(d[2], 1.0);
    REQUIRE_EQUALS(d[3], 1.0);
}

TEMPLATE_TEST_CASE_2("fast_matrix/min", "fast_matrix::min", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};

    etl::fast_matrix<Z, 2, 2> d;
    d = min(a, 1.0);

    REQUIRE_EQUALS(d[0], -1.0);
    REQUIRE_EQUALS(d[1], 1.0);
    REQUIRE_EQUALS(d[2], 0.0);
    REQUIRE_EQUALS(d[3], 1.0);
}

TEMPLATE_TEST_CASE_2("pow_precise/0", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {-1.0, 2.0, 1.0, 1.0, 2.0, 4.0, 5.0, -6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow_precise(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow/0", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {0.1, 2.0, 1.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(0.01));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow/1", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> d;

    d = pow((a >> a) + 1.0, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(4.0));
}

TEMPLATE_TEST_CASE_2("pow/2", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {0.1, 2.0, 0.5, 1.0};
    etl::fast_matrix<Z, 2, 2> d;

    d = pow(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(0.01));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(0.25));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
}

TEMPLATE_TEST_CASE_2("pow/3", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {0.01, 2.0, 9.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow(a, -2.0);

    REQUIRE_EQUALS_APPROX(d[0], Z(10000.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(1.0) / Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0) / Z(81.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(1.0) / Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(1.0) / Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(1.0) / Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(1.0) / Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow_int/0", "[fast][pow_int]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {-1.0, 2.0, 0.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow_int(a, 2);

    REQUIRE_EQUALS(d[0], 1.0);
    REQUIRE_EQUALS(d[1], 4.0);
    REQUIRE_EQUALS(d[2], 0.0);
    REQUIRE_EQUALS(d[3], 1.0);
    REQUIRE_EQUALS(d[4], 4.0);
    REQUIRE_EQUALS(d[5], 16.0);
    REQUIRE_EQUALS(d[6], 25.0);
    REQUIRE_EQUALS(d[7], 36.0);
}

TEMPLATE_TEST_CASE_2("pow_int/1", "[fast][pow_int]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> d;
    d = pow_int((a >> a) + 1.0, 2);

    REQUIRE_EQUALS(d[0], 4.0);
    REQUIRE_EQUALS(d[1], 25.0);
    REQUIRE_EQUALS(d[2], 1.0);
    REQUIRE_EQUALS(d[3], 4.0);
}
<commit_msg>Complete the tests<commit_after>//=======================================================================
// Copyright (c) 2014-2017 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include "test_light.hpp"

#include <cmath>

TEMPLATE_TEST_CASE_2("saxpy/0", "[saxpy][fast]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> x = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> y = {1.0, 3.0, 0.5, 1.2};
    etl::fast_matrix<Z, 2, 2> yy;

    yy = Z(2.0) * x + y;

    REQUIRE_EQUALS(yy[0], Z(-1.0));
    REQUIRE_EQUALS(yy[1], Z(7.0));
    REQUIRE_EQUALS(yy[2], Z(0.5));
    REQUIRE_EQUALS(yy[3], Z(3.2));
}

TEMPLATE_TEST_CASE_2("saxpy/1", "[saxpy][fast]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> x = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> y = {1.0, 3.0, 0.5, 1.2};
    etl::fast_matrix<Z, 2, 2> yy;

    yy = x + Z(2.0) * y;

    REQUIRE_EQUALS(yy[0], Z(1.0));
    REQUIRE_EQUALS(yy[1], Z(8.0));
    REQUIRE_EQUALS(yy[2], Z(1.0));
    REQUIRE_EQUALS(yy[3], Z(3.4));
}

TEMPLATE_TEST_CASE_2("saxpy/2", "[saxpy][fast]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> x = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> y = {1.0, 3.0, 0.5, 1.2};
    etl::fast_matrix<Z, 2, 2> yy;

    yy = x * Z(2) + y;

    REQUIRE_EQUALS(yy[0], Z(-1.0));
    REQUIRE_EQUALS(yy[1], Z(7.0));
    REQUIRE_EQUALS(yy[2], Z(0.5));
    REQUIRE_EQUALS(yy[3], Z(3.2));
}

TEMPLATE_TEST_CASE_2("saxpy/3", "[saxpy][fast]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> x = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> y = {1.0, 3.0, 0.5, 1.2};
    etl::fast_matrix<Z, 2, 2> yy;

    yy = x + y * Z(2);

    REQUIRE_EQUALS(yy[0], Z(1.0));
    REQUIRE_EQUALS(yy[1], Z(8.0));
    REQUIRE_EQUALS(yy[2], Z(1.0));
    REQUIRE_EQUALS(yy[3], Z(3.4));
}

TEMPLATE_TEST_CASE_2("fast_matrix/max", "fast_matrix::max", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};

    etl::fast_matrix<Z, 2, 2> d;
    d = max(a, 1.0);

    REQUIRE_EQUALS(d[0], 1.0);
    REQUIRE_EQUALS(d[1], 2.0);
    REQUIRE_EQUALS(d[2], 1.0);
    REQUIRE_EQUALS(d[3], 1.0);
}

TEMPLATE_TEST_CASE_2("fast_matrix/min", "fast_matrix::min", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};

    etl::fast_matrix<Z, 2, 2> d;
    d = min(a, 1.0);

    REQUIRE_EQUALS(d[0], -1.0);
    REQUIRE_EQUALS(d[1], 1.0);
    REQUIRE_EQUALS(d[2], 0.0);
    REQUIRE_EQUALS(d[3], 1.0);
}

TEMPLATE_TEST_CASE_2("pow_precise/0", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {-1.0, 2.0, 1.0, 1.0, 2.0, 4.0, 5.0, -6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow_precise(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow/0", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {0.1, 2.0, 1.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(0.01));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow/1", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> d;

    d = pow((a >> a) + 1.0, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(25.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(4.0));
}

TEMPLATE_TEST_CASE_2("pow/2", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {0.1, 2.0, 0.5, 1.0};
    etl::fast_matrix<Z, 2, 2> d;

    d = pow(a, Z(2));

    REQUIRE_EQUALS_APPROX(d[0], Z(0.01));
    REQUIRE_EQUALS_APPROX(d[1], Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(0.25));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
}

TEMPLATE_TEST_CASE_2("pow/3", "[fast][pow]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {0.01, 2.0, 9.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow(a, -2.0);

    REQUIRE_EQUALS_APPROX(d[0], Z(10000.0));
    REQUIRE_EQUALS_APPROX(d[1], Z(1.0) / Z(4.0));
    REQUIRE_EQUALS_APPROX(d[2], Z(1.0) / Z(81.0));
    REQUIRE_EQUALS_APPROX(d[3], Z(1.0));
    REQUIRE_EQUALS_APPROX(d[4], Z(1.0) / Z(4.0));
    REQUIRE_EQUALS_APPROX(d[5], Z(1.0) / Z(16.0));
    REQUIRE_EQUALS_APPROX(d[6], Z(1.0) / Z(25.0));
    REQUIRE_EQUALS_APPROX(d[7], Z(1.0) / Z(36.0));
}

TEMPLATE_TEST_CASE_2("pow_int/0", "[fast][pow_int]", Z, float, double) {
    etl::fast_matrix<Z, 2, 4> a = {-1.0, 2.0, 0.0, 1.0, 2.0, 4.0, 5.0, 6.0};
    etl::fast_matrix<Z, 2, 4> d;

    d = pow_int(a, 2);

    REQUIRE_EQUALS(d[0], 1.0);
    REQUIRE_EQUALS(d[1], 4.0);
    REQUIRE_EQUALS(d[2], 0.0);
    REQUIRE_EQUALS(d[3], 1.0);
    REQUIRE_EQUALS(d[4], 4.0);
    REQUIRE_EQUALS(d[5], 16.0);
    REQUIRE_EQUALS(d[6], 25.0);
    REQUIRE_EQUALS(d[7], 36.0);
}

TEMPLATE_TEST_CASE_2("pow_int/1", "[fast][pow_int]", Z, float, double) {
    etl::fast_matrix<Z, 2, 2> a = {-1.0, 2.0, 0.0, 1.0};
    etl::fast_matrix<Z, 2, 2> d;
    d = pow_int((a >> a) + 1.0, 2);

    REQUIRE_EQUALS(d[0], 4.0);
    REQUIRE_EQUALS(d[1], 25.0);
    REQUIRE_EQUALS(d[2], 1.0);
    REQUIRE_EQUALS(d[3], 4.0);
}
<|endoftext|>


<commit_before>// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <mutex>
#include <string>
#include <vector>

#include <mesos/hook.hpp>

#include <mesos/module/hook.hpp>

#include <stout/check.hpp>
#include <stout/foreach.hpp>
#include <stout/nothing.hpp>
#include <stout/strings.hpp>
#include <stout/try.hpp>

#include "hook/manager.hpp"
#include "module/manager.hpp"

using std::map;
using std::string;
using std::vector;

using mesos::modules::ModuleManager;

namespace mesos {
namespace internal {

static std::mutex mutex;
static hashmap<string, Hook*> availableHooks;

Try<Nothing> HookManager::initialize(const string& hookList)
{
  synchronized (mutex) {
    const vector<string> hooks = strings::split(hookList, ",");
    foreach (const string& hook, hooks) {
      if (availableHooks.contains(hook)) {
        return Error("Hook module '" + hook + "' already loaded");
      }

      if (!ModuleManager::contains<Hook>(hook)) {
        return Error("No hook module named '" + hook + "' available");
      }

      // Let's create an instance of the hook module.
      Try<Hook*> module = ModuleManager::create<Hook>(hook);
      if (module.isError()) {
        return Error(
            "Failed to instantiate hook module '" + hook + "': " +
            module.error());
      }

      // Add the hook module to the list of available hooks.
      availableHooks[hook] = module.get();
    }
  }

  return Nothing();
}

Try<Nothing> HookManager::unload(const std::string& hookName)
{
  synchronized (mutex) {
    if (!availableHooks.contains(hookName)) {
      return Error(
          "Error unloading hook module '" + hookName + "': module not loaded");
    }

    // Now remove the hook from the list of available hooks.
    availableHooks.erase(hookName);
  }

  return Nothing();
}

bool HookManager::hooksAvailable()
{
  synchronized (mutex) {
    return !availableHooks.empty();
  }
}

Labels HookManager::masterLaunchTaskLabelDecorator(
    const TaskInfo& taskInfo,
    const FrameworkInfo& frameworkInfo,
    const SlaveInfo& slaveInfo)
{
  synchronized (mutex) {
    // We need a mutable copy of the task info and set the new
    // labels after each hook invocation. Otherwise, the last hook
    // will be the only effective hook setting the labels.
    TaskInfo taskInfo_ = taskInfo;

    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<Labels> result =
        hook->masterLaunchTaskLabelDecorator(
            taskInfo_,
            frameworkInfo,
            slaveInfo);

      // NOTE: If the hook returns None(), the task labels won't be
      // changed.
      if (result.isSome()) {
        taskInfo_.mutable_labels()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Master label decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return taskInfo_.labels();
  }
}

void HookManager::masterSlaveLostHook(const SlaveInfo& slaveInfo)
{
  foreachpair (const string& name, Hook* hook, availableHooks) {
    Try<Nothing> result = hook->masterSlaveLostHook(slaveInfo);
    if (result.isError()) {
      LOG(WARNING) << "Master slave-lost hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

Labels HookManager::slaveRunTaskLabelDecorator(
    const TaskInfo& taskInfo,
    const ExecutorInfo& executorInfo,
    const FrameworkInfo& frameworkInfo,
    const SlaveInfo& slaveInfo)
{
  synchronized (mutex) {
    TaskInfo taskInfo_ = taskInfo;

    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<Labels> result = hook->slaveRunTaskLabelDecorator(
          taskInfo_, executorInfo, frameworkInfo, slaveInfo);

      // NOTE: If the hook returns None(), the task labels won't be
      // changed.
      if (result.isSome()) {
        taskInfo_.mutable_labels()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave label decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return taskInfo_.labels();
  }
}

Environment HookManager::slaveExecutorEnvironmentDecorator(
    ExecutorInfo executorInfo)
{
  synchronized (mutex) {
    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<Environment> result =
        hook->slaveExecutorEnvironmentDecorator(executorInfo);

      // NOTE: If the hook returns None(), the environment won't be
      // changed.
      if (result.isSome()) {
        executorInfo.mutable_command()->mutable_environment()->CopyFrom(
            result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave environment decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return executorInfo.command().environment();
  }
}

void HookManager::slavePreLaunchDockerHook(
    const ContainerInfo& containerInfo,
    const CommandInfo& commandInfo,
    const Option<TaskInfo>& taskInfo,
    const ExecutorInfo& executorInfo,
    const string& name,
    const string& sandboxDirectory,
    const string& mappedDirectory,
    const Option<Resources>& resources,
    const Option<map<string, string>>& env)
{
  foreachpair (const string& name, Hook* hook, availableHooks) {
    Try<Nothing> result =
      hook->slavePreLaunchDockerHook(
          containerInfo,
          commandInfo,
          taskInfo,
          executorInfo,
          name,
          sandboxDirectory,
          mappedDirectory,
          resources,
          env);
    if (result.isError()) {
      LOG(WARNING) << "Slave pre launch docker hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

void HookManager::slaveRemoveExecutorHook(
    const FrameworkInfo& frameworkInfo,
    const ExecutorInfo& executorInfo)
{
  foreachpair (const string& name, Hook* hook, availableHooks) {
    const Try<Nothing> result =
      hook->slaveRemoveExecutorHook(frameworkInfo, executorInfo);
    if (result.isError()) {
      LOG(WARNING) << "Slave remove executor hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

TaskStatus HookManager::slaveTaskStatusDecorator(
    const FrameworkID& frameworkId,
    TaskStatus status)
{
  synchronized (mutex) {
    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<TaskStatus> result =
        hook->slaveTaskStatusDecorator(frameworkId, status);

      // NOTE: Labels/ContainerStatus remain unchanged if the hook returns
      // None().
      if (result.isSome()) {
        if (result.get().has_labels()) {
          status.mutable_labels()->CopyFrom(result.get().labels());
        }

        if (result.get().has_container_status()) {
          status.mutable_container_status()->CopyFrom(
              result.get().container_status());
        }
      } else if (result.isError()) {
        LOG(WARNING) << "Slave TaskStatus decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return status;
  }
}

Resources HookManager::slaveResourcesDecorator(
    const SlaveInfo& slaveInfo)
{
  // We need a mutable copy of the Resources object. Each hook will see the
  // changes made by previous hooks, so the order of execution matters. The
  // execution order is currently unspecified since availableHooks uses a
  // hashmap.
  SlaveInfo slaveInfo_ = slaveInfo;

  synchronized (mutex) {
    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<Resources> result =
        hook->slaveResourcesDecorator(slaveInfo_);

      // NOTE: Resources remain unchanged if the hook returns None().
      if (result.isSome()) {
        slaveInfo_.mutable_resources()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave Resources decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return slaveInfo_.resources();
  }
}

Attributes HookManager::slaveAttributesDecorator(
    const SlaveInfo& slaveInfo)
{
  // We need a mutable copy of the Attributes object. Each hook will see the
  // changes made by previous hooks, so the order of execution matters. The
  // execution order is currently unspecified since availableHooks uses a
  // hashmap.
  SlaveInfo slaveInfo_ = slaveInfo;

  synchronized (mutex) {
    foreachpair (const string& name, Hook* hook, availableHooks) {
      const Result<Attributes> result =
        hook->slaveAttributesDecorator(slaveInfo_);

      // NOTE: Attributes remain unchanged if the hook returns None().
      if (result.isSome()) {
        slaveInfo_.mutable_attributes()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave Attributes decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return slaveInfo_.attributes();
  }
}

} // namespace internal {
} // namespace mesos {
<commit_msg>Made hook execution order deterministic.<commit_after>// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <mutex>
#include <string>
#include <vector>

#include <mesos/hook.hpp>

#include <mesos/module/hook.hpp>

#include <stout/check.hpp>
#include <stout/foreach.hpp>
#include <stout/linkedhashmap.hpp>
#include <stout/nothing.hpp>
#include <stout/strings.hpp>
#include <stout/try.hpp>

#include "hook/manager.hpp"
#include "module/manager.hpp"

using std::map;
using std::string;
using std::vector;

using mesos::modules::ModuleManager;

namespace mesos {
namespace internal {

static std::mutex mutex;
static LinkedHashMap<string, Hook*> availableHooks;

Try<Nothing> HookManager::initialize(const string& hookList)
{
  synchronized (mutex) {
    const vector<string> hooks = strings::split(hookList, ",");
    foreach (const string& hook, hooks) {
      if (availableHooks.contains(hook)) {
        return Error("Hook module '" + hook + "' already loaded");
      }

      if (!ModuleManager::contains<Hook>(hook)) {
        return Error("No hook module named '" + hook + "' available");
      }

      // Let's create an instance of the hook module.
      Try<Hook*> module = ModuleManager::create<Hook>(hook);
      if (module.isError()) {
        return Error(
            "Failed to instantiate hook module '" + hook + "': " +
            module.error());
      }

      // Add the hook module to the list of available hooks.
      availableHooks[hook] = module.get();
    }
  }

  return Nothing();
}

Try<Nothing> HookManager::unload(const std::string& hookName)
{
  synchronized (mutex) {
    if (!availableHooks.contains(hookName)) {
      return Error(
          "Error unloading hook module '" + hookName + "': module not loaded");
    }

    // Now remove the hook from the list of available hooks.
    availableHooks.erase(hookName);
  }

  return Nothing();
}

bool HookManager::hooksAvailable()
{
  synchronized (mutex) {
    return !availableHooks.empty();
  }
}

Labels HookManager::masterLaunchTaskLabelDecorator(
    const TaskInfo& taskInfo,
    const FrameworkInfo& frameworkInfo,
    const SlaveInfo& slaveInfo)
{
  synchronized (mutex) {
    // We need a mutable copy of the task info and set the new
    // labels after each hook invocation. Otherwise, the last hook
    // will be the only effective hook setting the labels.
    TaskInfo taskInfo_ = taskInfo;

    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<Labels> result =
        hook->masterLaunchTaskLabelDecorator(
            taskInfo_,
            frameworkInfo,
            slaveInfo);

      // NOTE: If the hook returns None(), the task labels won't be
      // changed.
      if (result.isSome()) {
        taskInfo_.mutable_labels()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Master label decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return taskInfo_.labels();
  }
}

void HookManager::masterSlaveLostHook(const SlaveInfo& slaveInfo)
{
  foreach (const string& name, availableHooks.keys()) {
    Hook* hook = availableHooks[name];
    Try<Nothing> result = hook->masterSlaveLostHook(slaveInfo);
    if (result.isError()) {
      LOG(WARNING) << "Master slave-lost hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

Labels HookManager::slaveRunTaskLabelDecorator(
    const TaskInfo& taskInfo,
    const ExecutorInfo& executorInfo,
    const FrameworkInfo& frameworkInfo,
    const SlaveInfo& slaveInfo)
{
  synchronized (mutex) {
    TaskInfo taskInfo_ = taskInfo;

    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<Labels> result = hook->slaveRunTaskLabelDecorator(
          taskInfo_, executorInfo, frameworkInfo, slaveInfo);

      // NOTE: If the hook returns None(), the task labels won't be
      // changed.
      if (result.isSome()) {
        taskInfo_.mutable_labels()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave label decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return taskInfo_.labels();
  }
}

Environment HookManager::slaveExecutorEnvironmentDecorator(
    ExecutorInfo executorInfo)
{
  synchronized (mutex) {
    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<Environment> result =
        hook->slaveExecutorEnvironmentDecorator(executorInfo);

      // NOTE: If the hook returns None(), the environment won't be
      // changed.
      if (result.isSome()) {
        executorInfo.mutable_command()->mutable_environment()->CopyFrom(
            result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave environment decorator hook failed for module '"
                    << name << "': " << result.error();
      }
    }

    return executorInfo.command().environment();
  }
}

void HookManager::slavePreLaunchDockerHook(
    const ContainerInfo& containerInfo,
    const CommandInfo& commandInfo,
    const Option<TaskInfo>& taskInfo,
    const ExecutorInfo& executorInfo,
    const string& name,
    const string& sandboxDirectory,
    const string& mappedDirectory,
    const Option<Resources>& resources,
    const Option<map<string, string>>& env)
{
  foreach (const string& name, availableHooks.keys()) {
    Hook* hook = availableHooks[name];
    Try<Nothing> result =
      hook->slavePreLaunchDockerHook(
          containerInfo,
          commandInfo,
          taskInfo,
          executorInfo,
          name,
          sandboxDirectory,
          mappedDirectory,
          resources,
          env);
    if (result.isError()) {
      LOG(WARNING) << "Slave pre launch docker hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

void HookManager::slaveRemoveExecutorHook(
    const FrameworkInfo& frameworkInfo,
    const ExecutorInfo& executorInfo)
{
  foreach (const string& name, availableHooks.keys()) {
    Hook* hook = availableHooks[name];
    const Try<Nothing> result =
      hook->slaveRemoveExecutorHook(frameworkInfo, executorInfo);
    if (result.isError()) {
      LOG(WARNING) << "Slave remove executor hook failed for module '"
                   << name << "': " << result.error();
    }
  }
}

TaskStatus HookManager::slaveTaskStatusDecorator(
    const FrameworkID& frameworkId,
    TaskStatus status)
{
  synchronized (mutex) {
    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<TaskStatus> result =
        hook->slaveTaskStatusDecorator(frameworkId, status);

      // NOTE: Labels/ContainerStatus remain unchanged if the hook returns
      // None().
      if (result.isSome()) {
        if (result.get().has_labels()) {
          status.mutable_labels()->CopyFrom(result.get().labels());
        }

        if (result.get().has_container_status()) {
          status.mutable_container_status()->CopyFrom(
              result.get().container_status());
        }
      } else if (result.isError()) {
        LOG(WARNING) << "Slave TaskStatus decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return status;
  }
}

Resources HookManager::slaveResourcesDecorator(
    const SlaveInfo& slaveInfo)
{
  // We need a mutable copy of the Resources object. Each hook will see the
  // changes made by previous hooks, so the order of execution matters. The
  // execution order is currently unspecified since availableHooks uses a
  // hashmap.
  SlaveInfo slaveInfo_ = slaveInfo;

  synchronized (mutex) {
    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<Resources> result =
        hook->slaveResourcesDecorator(slaveInfo_);

      // NOTE: Resources remain unchanged if the hook returns None().
      if (result.isSome()) {
        slaveInfo_.mutable_resources()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave Resources decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return slaveInfo_.resources();
  }
}

Attributes HookManager::slaveAttributesDecorator(
    const SlaveInfo& slaveInfo)
{
  // We need a mutable copy of the Attributes object. Each hook will see the
  // changes made by previous hooks, so the order of execution matters. The
  // execution order is currently unspecified since availableHooks uses a
  // hashmap.
  SlaveInfo slaveInfo_ = slaveInfo;

  synchronized (mutex) {
    foreach (const string& name, availableHooks.keys()) {
      Hook* hook = availableHooks[name];
      const Result<Attributes> result =
        hook->slaveAttributesDecorator(slaveInfo_);

      // NOTE: Attributes remain unchanged if the hook returns None().
      if (result.isSome()) {
        slaveInfo_.mutable_attributes()->CopyFrom(result.get());
      } else if (result.isError()) {
        LOG(WARNING) << "Slave Attributes decorator hook failed for "
                     << "module '" << name << "': " << result.error();
      }
    }

    return slaveInfo_.attributes();
  }
}

} // namespace internal {
} // namespace mesos {
<|endoftext|>


<commit_before>#ifdef __linux__

#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <map>
#include <list>
#include <uv.h>
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
#include <node_version.h>
#include <nan.h>
#include "epoll.h"

// TODO - strerror isn't threadsafe, use strerror_r instead
// TODO - use uv_strerror rather than strerror_r for libuv errors?

static int epfd_g;

static uv_sem_t sem_g;
static uv_async_t async_g;

static struct epoll_event event_g;
static int errno_g;

/*
 * Watcher thread
 */
static void *watcher(void *arg) {
  int count;

  while (true) {
    // Wait till the event loop says it's ok to poll. The semaphore serves more
    // than one purpose.
    // - It synchronizing access to '1 element queue' in variables
    //   event_g and errno_g.
    // - When level-triggered epoll is used, the default when EPOLLET isn't
    //   specified, the event triggered by the last call to epoll_wait may be
    //   trigged again and again if the condition that triggered it hasn't been
    //   cleared yet. Waiting prevents multiple triggers for the same event.
    // - It forces a context switch from the watcher thread to the event loop
    //   thread.
    uv_sem_wait(&sem_g);

    do {
      count = epoll_wait(epfd_g, &event_g, 1, -1);
    } while (count == -1 && errno == EINTR);

    errno_g = count == -1 ? errno : 0;

    // Errors returned from uv_async_send are silently ignored.
    uv_async_send(&async_g);
  }

  return 0;
}

static int start_watcher() {
  static bool watcher_started = false;
  pthread_t theread_id;

  if (watcher_started)
    return 0;

  epfd_g = epoll_create1(0);
  if (epfd_g == -1)
    return errno;

  int err = uv_sem_init(&sem_g, 1);
  if (err < 0) {
    // node v0.11.5 uses uv v0.11.7. The uv v0.11.7 version of uv_sem_init
    // returns -errno on error. uv_sem_init in lower versions of node returns
    // -1 on error. So, if the node version is less than v0.11.5, set err to
    // -errno.
#if ! NODE_VERSION_AT_LEAST(0, 11, 5)
    err = -errno;
#endif
    close(epfd_g);
    return -err;
  }

  err = uv_async_init(uv_default_loop(), &async_g, Epoll::HandleEvent);
  if (err < 0) {
    // node v0.11.5 uses uv v0.11.7. The uv v0.11.7 version of uv_async_init
    // returns -errno on error. uv_async_init in lower versions of node returns
    // -1 on error. So, if the node version is less than v0.11.5, set err to
    // -errno.
#if ! NODE_VERSION_AT_LEAST(0, 11, 5)
    err = -errno;
#endif
    close(epfd_g);
    uv_sem_destroy(&sem_g);
    return -err;
  }

  // Prevent async_g from keeping event loop alive, for the time being.
  uv_unref((uv_handle_t *) &async_g);

  err = pthread_create(&theread_id, 0, watcher, 0);
  if (err != 0) {
    close(epfd_g);
    uv_sem_destroy(&sem_g);
    uv_close((uv_handle_t *) &async_g, 0);
    return err;
  }

  watcher_started = true;

  return 0;
}

/*
 * Epoll
 */
Nan::Persistent<v8::Function> Epoll::constructor;
std::map<int, Epoll*> Epoll::fd2epoll;

Epoll::Epoll(Nan::Callback *callback)
  : callback_(callback), closed_(false) {
};

Epoll::~Epoll() {
  // v8 decides when and if destructors are called. In particular, if the
  // process is about to terminate, it's highly likely that destructors will
  // not be called. This is therefore not the place for calling the likes of
  // uv_unref, which, in general, must be called to terminate a process
  // gracefully!
  Nan::HandleScope scope;
  if (callback_) delete callback_;
};

NAN_MODULE_INIT(Epoll::Init) {
  // Constructor
  v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Epoll::New);
  ctor->SetClassName(Nan::New("Epoll").ToLocalChecked());
  ctor->InstanceTemplate()->SetInternalFieldCount(1);

  // Prototype
  Nan::SetPrototypeMethod(ctor, "add", Add);
  Nan::SetPrototypeMethod(ctor, "modify", Modify);
  Nan::SetPrototypeMethod(ctor, "remove", Remove);
  Nan::SetPrototypeMethod(ctor, "close", Close);

  v8::Local<v8::ObjectTemplate> proto = ctor->PrototypeTemplate();
  Nan::SetAccessor(proto, Nan::New<v8::String>("closed").ToLocalChecked(), GetClosed);

  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLIN").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLIN), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLOUT").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLOUT), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLRDHUP").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLRDHUP), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLPRI").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLPRI), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLERR").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLERR), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLHUP").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLHUP), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLET").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLET), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLONESHOT").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLONESHOT), v8::ReadOnly);

  constructor.Reset(ctor->GetFunction());
  Nan::Set(target, Nan::New<v8::String>("Epoll").ToLocalChecked(), ctor->GetFunction());

  // TODO - Is it a good idea to throw an exception here?
  if (int err = start_watcher())
    Nan::ThrowError(strerror(err)); // TODO - use err also
}

NAN_METHOD(Epoll::New) {
  if (info.Length() < 1 || !info[0]->IsFunction())
    return Nan::ThrowError("First argument to construtor must be a callback");

  Nan::Callback *callback = new Nan::Callback(info[0].As<v8::Function>());

  Epoll *epoll = new Epoll(callback);
  epoll->Wrap(info.This());

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Add) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("add can't be called after calling close");

  // Epoll.EPOLLET is -0x8000000 on ARM and an IsUint32 check fails so
  // check for IsNumber instead.
  if (info.Length() < 2 || !info[0]->IsInt32() || !info[1]->IsNumber())
    return Nan::ThrowError("incorrect arguments passed to add"
      "(int fd, int events)");

  int err = epoll->Add(info[0]->Int32Value(), info[1]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Modify) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("modify can't be called after calling close");

  // Epoll.EPOLLET is -0x8000000 on ARM and an IsUint32 check fails so
  // check for IsNumber instead.
  if (info.Length() < 2 || !info[0]->IsInt32() || !info[1]->IsNumber())
    return Nan::ThrowError("incorrect arguments passed to modify"
      "(int fd, int events)");

  int err = epoll->Modify(info[0]->Int32Value(), info[1]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Remove) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("remove can't be called after calling close");

  if (info.Length() < 1 || !info[0]->IsInt32())
    return Nan::ThrowError("incorrect arguments passed to remove(int fd)");

  int err = epoll->Remove(info[0]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Close) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("close can't be called more than once");

  int err = epoll->Close();
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().SetNull();
}

NAN_GETTER(Epoll::GetClosed) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  info.GetReturnValue().Set(Nan::New<v8::Boolean>(epoll->closed_));
}

int Epoll::Add(int fd, uint32_t events) {
  struct epoll_event event;
  event.events = events;
  event.data.fd = fd;

  if (epoll_ctl(epfd_g, EPOLL_CTL_ADD, fd, &event) == -1)
    return errno;

  fd2epoll.insert(std::pair<int, Epoll*>(fd, this));
  fds_.push_back(fd);

  // Keep event loop alive. uv_unref called in Remove.
  uv_ref((uv_handle_t *) &async_g);

  // Prevent GC for this instance. Unref called in Remove.
  Ref();

  return 0;
}

int Epoll::Modify(int fd, uint32_t events) {
  struct epoll_event event;
  event.events = events;
  event.data.fd = fd;

  if (epoll_ctl(epfd_g, EPOLL_CTL_MOD, fd, &event) == -1)
    return errno;

  return 0;
}

int Epoll::Remove(int fd) {
  if (epoll_ctl(epfd_g, EPOLL_CTL_DEL, fd, 0) == -1)
    return errno;

  fd2epoll.erase(fd);
  fds_.remove(fd);

  if (fd2epoll.empty())
    uv_unref((uv_handle_t *) &async_g);
  Unref();

  return 0;
}

int Epoll::Close() {
  closed_ = true;

  delete callback_;
  callback_ = 0;

  std::list<int>::iterator it = fds_.begin();
  for (; it != fds_.end(); it = fds_.begin()) {
    int err = Remove(*it);
    if (err != 0)
      return err; // TODO - Returning here may leave things messed up.
  }

  return 0;
}

#if NODE_VERSION_AT_LEAST(0, 11, 13)
void Epoll::HandleEvent(uv_async_t* handle) {
#else
void Epoll::HandleEvent(uv_async_t* handle, int status) {
#endif
  // This method is executed in the event loop thread.
  // By the time flow of control arrives here the original Epoll instance that
  // registered interest in the event may no longer have this interest. If
  // this is the case, the event will be silently ignored.

  std::map<int, Epoll*>::iterator it = fd2epoll.find(event_g.data.fd);
  if (it != fd2epoll.end()) {
    it->second->DispatchEvent(errno_g, &event_g);
  }

  uv_sem_post(&sem_g);
}

void Epoll::DispatchEvent(int err, struct epoll_event *event) {
  Nan::HandleScope scope;

  if (err) {
    v8::Local<v8::Value> args[1] = {
      v8::Exception::Error(Nan::New<v8::String>(strerror(err)).ToLocalChecked())
    };
    callback_->Call(1, args);
  } else {
    v8::Local<v8::Value> args[3] = {
      Nan::Null(),
      Nan::New<v8::Integer>(event->data.fd),
      Nan::New<v8::Integer>(event->events)
    };
    callback_->Call(3, args);
  }
}

NODE_MODULE(epoll, Epoll::Init)

#endif

<commit_msg>add accessor to instance template<commit_after>#ifdef __linux__

#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <map>
#include <list>
#include <uv.h>
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
#include <node_version.h>
#include <nan.h>
#include "epoll.h"

// TODO - strerror isn't threadsafe, use strerror_r instead
// TODO - use uv_strerror rather than strerror_r for libuv errors?

static int epfd_g;

static uv_sem_t sem_g;
static uv_async_t async_g;

static struct epoll_event event_g;
static int errno_g;

/*
 * Watcher thread
 */
static void *watcher(void *arg) {
  int count;

  while (true) {
    // Wait till the event loop says it's ok to poll. The semaphore serves more
    // than one purpose.
    // - It synchronizing access to '1 element queue' in variables
    //   event_g and errno_g.
    // - When level-triggered epoll is used, the default when EPOLLET isn't
    //   specified, the event triggered by the last call to epoll_wait may be
    //   trigged again and again if the condition that triggered it hasn't been
    //   cleared yet. Waiting prevents multiple triggers for the same event.
    // - It forces a context switch from the watcher thread to the event loop
    //   thread.
    uv_sem_wait(&sem_g);

    do {
      count = epoll_wait(epfd_g, &event_g, 1, -1);
    } while (count == -1 && errno == EINTR);

    errno_g = count == -1 ? errno : 0;

    // Errors returned from uv_async_send are silently ignored.
    uv_async_send(&async_g);
  }

  return 0;
}

static int start_watcher() {
  static bool watcher_started = false;
  pthread_t theread_id;

  if (watcher_started)
    return 0;

  epfd_g = epoll_create1(0);
  if (epfd_g == -1)
    return errno;

  int err = uv_sem_init(&sem_g, 1);
  if (err < 0) {
    // node v0.11.5 uses uv v0.11.7. The uv v0.11.7 version of uv_sem_init
    // returns -errno on error. uv_sem_init in lower versions of node returns
    // -1 on error. So, if the node version is less than v0.11.5, set err to
    // -errno.
#if ! NODE_VERSION_AT_LEAST(0, 11, 5)
    err = -errno;
#endif
    close(epfd_g);
    return -err;
  }

  err = uv_async_init(uv_default_loop(), &async_g, Epoll::HandleEvent);
  if (err < 0) {
    // node v0.11.5 uses uv v0.11.7. The uv v0.11.7 version of uv_async_init
    // returns -errno on error. uv_async_init in lower versions of node returns
    // -1 on error. So, if the node version is less than v0.11.5, set err to
    // -errno.
#if ! NODE_VERSION_AT_LEAST(0, 11, 5)
    err = -errno;
#endif
    close(epfd_g);
    uv_sem_destroy(&sem_g);
    return -err;
  }

  // Prevent async_g from keeping event loop alive, for the time being.
  uv_unref((uv_handle_t *) &async_g);

  err = pthread_create(&theread_id, 0, watcher, 0);
  if (err != 0) {
    close(epfd_g);
    uv_sem_destroy(&sem_g);
    uv_close((uv_handle_t *) &async_g, 0);
    return err;
  }

  watcher_started = true;

  return 0;
}

/*
 * Epoll
 */
Nan::Persistent<v8::Function> Epoll::constructor;
std::map<int, Epoll*> Epoll::fd2epoll;

Epoll::Epoll(Nan::Callback *callback)
  : callback_(callback), closed_(false) {
};

Epoll::~Epoll() {
  // v8 decides when and if destructors are called. In particular, if the
  // process is about to terminate, it's highly likely that destructors will
  // not be called. This is therefore not the place for calling the likes of
  // uv_unref, which, in general, must be called to terminate a process
  // gracefully!
  Nan::HandleScope scope;
  if (callback_) delete callback_;
};

NAN_MODULE_INIT(Epoll::Init) {
  // Constructor
  v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Epoll::New);
  ctor->SetClassName(Nan::New("Epoll").ToLocalChecked());
  ctor->InstanceTemplate()->SetInternalFieldCount(1);

  // Prototype
  Nan::SetPrototypeMethod(ctor, "add", Add);
  Nan::SetPrototypeMethod(ctor, "modify", Modify);
  Nan::SetPrototypeMethod(ctor, "remove", Remove);
  Nan::SetPrototypeMethod(ctor, "close", Close);

  v8::Local<v8::ObjectTemplate> itpl = ctor->InstanceTemplate();
  Nan::SetAccessor(itpl, Nan::New<v8::String>("closed").ToLocalChecked(), GetClosed);

  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLIN").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLIN), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLOUT").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLOUT), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLRDHUP").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLRDHUP), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLPRI").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLPRI), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLERR").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLERR), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLHUP").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLHUP), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLET").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLET), v8::ReadOnly);
  Nan::SetTemplate(ctor, Nan::New<v8::String>("EPOLLONESHOT").ToLocalChecked(), Nan::New<v8::Integer>(EPOLLONESHOT), v8::ReadOnly);

  constructor.Reset(ctor->GetFunction());
  Nan::Set(target, Nan::New<v8::String>("Epoll").ToLocalChecked(), ctor->GetFunction());

  // TODO - Is it a good idea to throw an exception here?
  if (int err = start_watcher())
    Nan::ThrowError(strerror(err)); // TODO - use err also
}

NAN_METHOD(Epoll::New) {
  if (info.Length() < 1 || !info[0]->IsFunction())
    return Nan::ThrowError("First argument to construtor must be a callback");

  Nan::Callback *callback = new Nan::Callback(info[0].As<v8::Function>());

  Epoll *epoll = new Epoll(callback);
  epoll->Wrap(info.This());

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Add) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("add can't be called after calling close");

  // Epoll.EPOLLET is -0x8000000 on ARM and an IsUint32 check fails so
  // check for IsNumber instead.
  if (info.Length() < 2 || !info[0]->IsInt32() || !info[1]->IsNumber())
    return Nan::ThrowError("incorrect arguments passed to add"
      "(int fd, int events)");

  int err = epoll->Add(info[0]->Int32Value(), info[1]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Modify) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("modify can't be called after calling close");

  // Epoll.EPOLLET is -0x8000000 on ARM and an IsUint32 check fails so
  // check for IsNumber instead.
  if (info.Length() < 2 || !info[0]->IsInt32() || !info[1]->IsNumber())
    return Nan::ThrowError("incorrect arguments passed to modify"
      "(int fd, int events)");

  int err = epoll->Modify(info[0]->Int32Value(), info[1]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Remove) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("remove can't be called after calling close");

  if (info.Length() < 1 || !info[0]->IsInt32())
    return Nan::ThrowError("incorrect arguments passed to remove(int fd)");

  int err = epoll->Remove(info[0]->Int32Value());
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Epoll::Close) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  if (epoll->closed_)
    return Nan::ThrowError("close can't be called more than once");

  int err = epoll->Close();
  if (err != 0)
    return Nan::ThrowError(strerror(err)); // TODO - use err also

  info.GetReturnValue().SetNull();
}

NAN_GETTER(Epoll::GetClosed) {
  Epoll *epoll = ObjectWrap::Unwrap<Epoll>(info.This());

  info.GetReturnValue().Set(Nan::New<v8::Boolean>(epoll->closed_));
}

int Epoll::Add(int fd, uint32_t events) {
  struct epoll_event event;
  event.events = events;
  event.data.fd = fd;

  if (epoll_ctl(epfd_g, EPOLL_CTL_ADD, fd, &event) == -1)
    return errno;

  fd2epoll.insert(std::pair<int, Epoll*>(fd, this));
  fds_.push_back(fd);

  // Keep event loop alive. uv_unref called in Remove.
  uv_ref((uv_handle_t *) &async_g);

  // Prevent GC for this instance. Unref called in Remove.
  Ref();

  return 0;
}

int Epoll::Modify(int fd, uint32_t events) {
  struct epoll_event event;
  event.events = events;
  event.data.fd = fd;

  if (epoll_ctl(epfd_g, EPOLL_CTL_MOD, fd, &event) == -1)
    return errno;

  return 0;
}

int Epoll::Remove(int fd) {
  if (epoll_ctl(epfd_g, EPOLL_CTL_DEL, fd, 0) == -1)
    return errno;

  fd2epoll.erase(fd);
  fds_.remove(fd);

  if (fd2epoll.empty())
    uv_unref((uv_handle_t *) &async_g);
  Unref();

  return 0;
}

int Epoll::Close() {
  closed_ = true;

  delete callback_;
  callback_ = 0;

  std::list<int>::iterator it = fds_.begin();
  for (; it != fds_.end(); it = fds_.begin()) {
    int err = Remove(*it);
    if (err != 0)
      return err; // TODO - Returning here may leave things messed up.
  }

  return 0;
}

#if NODE_VERSION_AT_LEAST(0, 11, 13)
void Epoll::HandleEvent(uv_async_t* handle) {
#else
void Epoll::HandleEvent(uv_async_t* handle, int status) {
#endif
  // This method is executed in the event loop thread.
  // By the time flow of control arrives here the original Epoll instance that
  // registered interest in the event may no longer have this interest. If
  // this is the case, the event will be silently ignored.

  std::map<int, Epoll*>::iterator it = fd2epoll.find(event_g.data.fd);
  if (it != fd2epoll.end()) {
    it->second->DispatchEvent(errno_g, &event_g);
  }

  uv_sem_post(&sem_g);
}

void Epoll::DispatchEvent(int err, struct epoll_event *event) {
  Nan::HandleScope scope;

  if (err) {
    v8::Local<v8::Value> args[1] = {
      v8::Exception::Error(Nan::New<v8::String>(strerror(err)).ToLocalChecked())
    };
    callback_->Call(1, args);
  } else {
    v8::Local<v8::Value> args[3] = {
      Nan::Null(),
      Nan::New<v8::Integer>(event->data.fd),
      Nan::New<v8::Integer>(event->events)
    };
    callback_->Call(3, args);
  }
}

NODE_MODULE(epoll, Epoll::Init)

#endif

<|endoftext|>


<commit_before>/*
	Copyright (c) 2018, Will Pearce

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	Original source: http://roar11.com/2016/01/a-platform-independent-thread-pool-using-c14/

	Modified by Martin Klemsa
*/

#ifndef _ctoolhu_thread_pool_included_
#define _ctoolhu_thread_pool_included_

#include "future.hpp"
#include "queue.hpp"
#include "../singleton/holder.hpp"
#include <algorithm>
#include <atomic>
#include <functional>
#include <future>
#include <memory>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>

namespace Ctoolhu::Thread {

	namespace Private {

		class IThreadTask {

		  public:

			IThreadTask() = default;
			virtual ~IThreadTask() = default;

			IThreadTask(const IThreadTask &) = delete;
			IThreadTask &operator=(const IThreadTask &) = delete;

			IThreadTask(IThreadTask &&) = default;
			IThreadTask &operator=(IThreadTask &&) = default;

			//run the task
			virtual void execute() = 0;
		};

		template <typename Func>
		class ThreadTask : public IThreadTask {

		  public:

			ThreadTask(Func &&func)
				: _func{std::move(func)}
			{
			}

			~ThreadTask() override = default;

			ThreadTask(const ThreadTask &) = delete;
			ThreadTask &operator=(const ThreadTask &) = delete;

			ThreadTask(ThreadTask &&) = default;
			ThreadTask &operator=(ThreadTask &&) = default;

			/**
				* Run the task.
				*/
			void execute() override
			{
				_func();
			}

		  private:

			Func _func;
		};

	} //ns Private

	//keeps a set of threads constantly waiting to execute incoming jobs
	class Pool {

	  public:

		explicit Pool(unsigned int numThreads)
		{
			try {
				for (unsigned int i{0u}; i < numThreads; ++i)
					_threads.emplace_back(&Pool::worker, this);
			}
			catch(...) {
				destroy();
				throw;
			}
		}

		Pool()
			: Pool{std::max(std::thread::hardware_concurrency(), 1u)} {} //always create at least one thread by default(hardware_concurrency can return 0)

		~Pool()
		{
			destroy();
		}

		Pool(const Pool &) = delete;
		Pool &operator=(const Pool &) = delete;

		//submit a job to be run by the thread pool
		template <typename Func, typename... Args>
		auto submit(Func &&func, Args &&... args)
		{
			auto boundTask = std::bind(std::forward<Func>(func), std::forward<Args>(args)...); //using a lambda instead would lose the perfect forwarding
			using job_result_t = std::invoke_result_t<decltype(boundTask)>;
			using packaged_task_t = std::packaged_task<job_result_t()>;
			using task_t = Private::ThreadTask<packaged_task_t>;

			packaged_task_t task{std::move(boundTask)};
			auto result = task.get_future();
			_workQueue.push(std::make_unique<task_t>(std::move(task)));
			return result;
		}

	  private:

		//constantly running function each thread uses to acquire work items from the queue
		void worker()
		{
			while (!_done) {
				std::unique_ptr<Private::IThreadTask> task;
				if (_workQueue.waitPop(task))
					task->execute();
			}
		}

		//invalidates the queue and joins all running threads
		void destroy()
		{
			_done = true;
			_workQueue.invalidate();
			for (auto &thread : _threads) {
				if (thread.joinable())
					thread.join();
			}
		}

		Queue<std::unique_ptr<Private::IThreadTask>> _workQueue;
		std::vector<std::thread> _threads;
		std::atomic_bool _done{false};
	};

	using SinglePool = Singleton::Holder<Pool>;

} //ns Ctoolhu::Thread

#endif //file guard
<commit_msg>Removed redundant code<commit_after>/*
	Copyright (c) 2018, Will Pearce

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	Original source: http://roar11.com/2016/01/a-platform-independent-thread-pool-using-c14/

	Modified by Martin Klemsa
*/

#ifndef _ctoolhu_thread_pool_included_
#define _ctoolhu_thread_pool_included_

#include "future.hpp"
#include "queue.hpp"
#include "../singleton/holder.hpp"
#include <algorithm>
#include <atomic>
#include <functional>
#include <future>
#include <memory>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>

namespace Ctoolhu::Thread {

	namespace Private {

		class IThreadTask {

		  public:

			IThreadTask() = default;
			virtual ~IThreadTask() = default;

			IThreadTask(const IThreadTask &) = delete;
			IThreadTask &operator=(const IThreadTask &) = delete;

			IThreadTask(IThreadTask &&) = default;
			IThreadTask &operator=(IThreadTask &&) = default;

			//run the task
			virtual void execute() = 0;
		};

		template <typename Func>
		class ThreadTask : public IThreadTask {

		  public:

			ThreadTask(Func &&func)
				: _func{std::move(func)}
			{
			}

			ThreadTask(const ThreadTask &) = delete;
			ThreadTask &operator=(const ThreadTask &) = delete;

			ThreadTask(ThreadTask &&) = default;
			ThreadTask &operator=(ThreadTask &&) = default;

			/**
				* Run the task.
				*/
			void execute() override
			{
				_func();
			}

		  private:

			Func _func;
		};

	} //ns Private

	//keeps a set of threads constantly waiting to execute incoming jobs
	class Pool {

	  public:

		explicit Pool(unsigned int numThreads)
		{
			try {
				for (unsigned int i{0u}; i < numThreads; ++i)
					_threads.emplace_back(&Pool::worker, this);
			}
			catch(...) {
				destroy();
				throw;
			}
		}

		Pool()
			: Pool{std::max(std::thread::hardware_concurrency(), 1u)} {} //always create at least one thread by default(hardware_concurrency can return 0)

		~Pool()
		{
			destroy();
		}

		Pool(const Pool &) = delete;
		Pool &operator=(const Pool &) = delete;

		//submit a job to be run by the thread pool
		template <typename Func, typename... Args>
		auto submit(Func &&func, Args &&... args)
		{
			auto boundTask = std::bind(std::forward<Func>(func), std::forward<Args>(args)...); //using a lambda instead would lose the perfect forwarding
			using job_result_t = std::invoke_result_t<decltype(boundTask)>;
			using packaged_task_t = std::packaged_task<job_result_t()>;
			using task_t = Private::ThreadTask<packaged_task_t>;

			packaged_task_t task{std::move(boundTask)};
			auto result = task.get_future();
			_workQueue.push(std::make_unique<task_t>(std::move(task)));
			return result;
		}

	  private:

		//constantly running function each thread uses to acquire work items from the queue
		void worker()
		{
			while (!_done) {
				std::unique_ptr<Private::IThreadTask> task;
				if (_workQueue.waitPop(task))
					task->execute();
			}
		}

		//invalidates the queue and joins all running threads
		void destroy()
		{
			_done = true;
			_workQueue.invalidate();
			for (auto &thread : _threads) {
				if (thread.joinable())
					thread.join();
			}
		}

		Queue<std::unique_ptr<Private::IThreadTask>> _workQueue;
		std::vector<std::thread> _threads;
		std::atomic_bool _done{false};
	};

	using SinglePool = Singleton::Holder<Pool>;

} //ns Ctoolhu::Thread

#endif //file guard
<|endoftext|>