Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TMPWorker.h
Go to the documentation of this file.
1 /* @(#)root/multiproc:$Id$ */
2 // Author: Enrico Guiraud July 2015
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TMPWorker
13 #define ROOT_TMPWorker
14 
15 #include "MPCode.h"
16 #include "MPSendRecv.h" //MPCodeBufPair
17 #include "PoolUtils.h"
18 #include "TSysEvtHandler.h" //TFileHandler
19 
20 #include <memory> //unique_ptr
21 #include <string>
22 #include <sstream>
23 #include <type_traits> //std::result_of
24 #include <unistd.h> //pid_t
25 
26 class TMPWorker {
27 public:
28  TMPWorker() : fNWorkers(0), fMaxNEntries(0),
29  fProcessedEntries(0), fS(), fPid(0), fNWorker(0) { }
30  TMPWorker(unsigned nWorkers, ULong64_t maxEntries)
31  : fNWorkers(nWorkers), fMaxNEntries(maxEntries),
32  fProcessedEntries(0), fS(), fPid(0), fNWorker(0) { }
33  virtual ~TMPWorker() { }
34  //it doesn't make sense to copy a TMPWorker (each one has a uniq_ptr to its socket)
35  TMPWorker(const TMPWorker &) = delete;
36  TMPWorker &operator=(const TMPWorker &) = delete;
37 
38  virtual void Init(int fd, unsigned workerN);
39  void Run();
40  TSocket *GetSocket() { return fS.get(); }
41  pid_t GetPid() { return fPid; }
42  unsigned GetNWorker() const { return fNWorker; }
43 
44 protected:
45  std::string fId; ///< identifier string in the form W<nwrk>|P<proc id>
46  unsigned fNWorkers; ///< the number of workers spawned
47  ULong64_t fMaxNEntries; ///< the maximum number of entries to be processed by this worker
48  ULong64_t fProcessedEntries; ///< the number of entries processed by this worker so far
49 
50  void SendError(const std::string& errmsg, unsigned int code = MPCode::kError);
51 
52 private:
53  virtual void HandleInput(MPCodeBufPair &msg);
54 
55  std::unique_ptr<TSocket> fS; ///< This worker's socket. The unique_ptr makes sure resources are released.
56  pid_t fPid; ///< the PID of the process in which this worker is running
57  unsigned fNWorker; ///< the ordinal number of this worker (0 to nWorkers-1)
58 };
59 
60 #endif