Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #ifndef __WORK_PROCESSOR_INL_H__
7 : #define __WORK_PROCESSOR_INL_H__
8 :
9 : #include <vector>
10 : #include <boost/function.hpp>
11 : #include <boost/bind/bind.hpp>
12 : #include <boost/shared_ptr.hpp>
13 : #include <boost/scoped_ptr.hpp>
14 : #include <boost/assign/list_of.hpp>
15 : #include <boost/type_traits.hpp>
16 : #include <boost/mpl/assert.hpp>
17 : #include <boost/tuple/tuple.hpp>
18 : #include <limits>
19 : #include <sstream>
20 : #include "base/task.h"
21 :
22 : using namespace boost::placeholders;
23 :
24 : struct PipelineWorker : public Task {
25 224276 : PipelineWorker(int tid, int tinst, boost::function<bool(void)> runner) :
26 224276 : Task(tid,tinst), runner_(runner) {}
27 :
28 223848 : bool Run() {
29 223848 : if (!(runner_)()) return false;
30 222605 : return true;
31 : }
32 0 : std::string Description() const { return "PipelineWorker"; }
33 : private:
34 : const boost::function<bool(void)> runner_;
35 : };
36 :
37 : template<typename InputT, typename SubResultT, typename ExternalT>
38 : class WorkProcessor : public ExternalProcIf<ExternalT> {
39 : public:
40 : typedef boost::function<ExternalBase::Efn(
41 : uint32_t inst,
42 : const std::vector<ExternalT*> & exts, // Info for previous steps of this stage
43 : const InputT & inp, // Info from previous stage
44 : SubResultT & subRes // Access to final result of this instance
45 : )> ExecuteFn;
46 :
47 110981 : WorkProcessor(uint32_t stage, ExecuteFn efn, FinFn finFn, const InputT & inp,
48 : uint32_t inst, int tid, int tinst) :
49 110967 : stage_(stage),
50 110967 : finFn_(finFn),
51 110889 : efn_(efn),
52 110864 : inp_(inp),
53 110841 : finished_(false),
54 110841 : running_(false),
55 110841 : inst_(inst),
56 110841 : tid_(tid),
57 110841 : tinst_(tinst),
58 110841 : w_(new PipelineWorker(tid_, tinst_,
59 : boost::bind(&WorkProcessor<InputT,SubResultT,ExternalT>::Runner,
60 221845 : this))) {
61 110801 : res_.reset(new SubResultT);
62 110765 : }
63 :
64 111127 : void Start() {
65 111127 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
66 111123 : scheduler->Enqueue(w_);
67 111128 : }
68 :
69 110739 : boost::shared_ptr<SubResultT> Result() const {
70 110739 : if (!finished_) return boost::shared_ptr<SubResultT>();
71 110739 : return res_;
72 : }
73 :
74 110229 : void Release() {
75 110229 : assert(finished_);
76 110229 : res_.reset();
77 110913 : }
78 :
79 254 : std::string Key() const {
80 254 : std::stringstream keystr;
81 254 : keystr << "PROC-STAGE:" << stage_ << "-INST:" << inst_ <<
82 254 : "-STEP:" << externals_.size();
83 508 : return keystr.str();
84 254 : }
85 :
86 : private:
87 : uint32_t stage_;
88 : FinFn finFn_;
89 : const ExecuteFn efn_;
90 : const InputT & inp_;
91 : boost::shared_ptr<SubResultT> res_;
92 : std::vector<ExternalT*> externals_;
93 : bool finished_;
94 : bool running_;
95 : const uint32_t inst_;
96 : const int tid_;
97 : const int tinst_;
98 : PipelineWorker * const w_;
99 :
100 110647 : void WorkDone(bool ret_code) {
101 110647 : for (typename std::vector<ExternalT*>::iterator it = externals_.begin();
102 221299 : it!=externals_.end(); it++) {
103 110801 : delete (*it);
104 : }
105 110267 : finished_ = true;
106 110267 : finFn_(ret_code);
107 111022 : }
108 :
109 221376 : bool Runner(void) {
110 221376 : running_ = true;
111 221376 : ExternalBase::Efn fn = (efn_)(inst_, externals_, inp_, *res_);
112 221906 : running_ = false;
113 221906 : if (fn.empty()) {
114 110696 : WorkDone(true);
115 : } else {
116 111339 : if (fn == &ExternalBase::Incomplete) return false;
117 :
118 111352 : if (!fn(this)) {
119 0 : WorkDone(false);
120 : }
121 : }
122 222396 : return true;
123 222396 : }
124 :
125 111508 : void Response(std::unique_ptr<ExternalT> resp) {
126 111508 : ExternalT * msg = resp.get();
127 111510 : resp.release();
128 111507 : externals_.push_back(msg);
129 111498 : assert(!running_);
130 111498 : PipelineWorker *w = new PipelineWorker(tid_, tinst_,
131 : boost::bind(&WorkProcessor<InputT,SubResultT,ExternalT>::Runner,
132 : this));
133 111495 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
134 111495 : scheduler->Enqueue(w);
135 111532 : }
136 : };
137 :
138 : template <typename InputT, typename ResultT>
139 : struct WorkStageIf {
140 : virtual void Start(uint32_t stage, FinFn finFn, const boost::shared_ptr<InputT> & inp) = 0;
141 : virtual boost::shared_ptr<ResultT> Result() const = 0;
142 : virtual void Release() = 0;
143 2104 : virtual ~WorkStageIf() {}
144 : };
145 : #endif
|