Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <memory>
  4. #include <functional>
  5. #include <future>
  6. #include <vector>
  7. #include <deque>
  8.  
  9. using namespace std;
  10.  
  11. enum class ioctl_code
  12. {
  13. OK,
  14. FAILED
  15. };
  16.  
  17. struct dev_io_cmd
  18. {
  19. ioctl_code code;
  20. };
  21.  
  22. struct action_result
  23. {
  24. bool overall_result_;
  25. int system_error_;
  26. };
  27.  
  28. class action_if
  29. {
  30. public:
  31. virtual action_result operator() () = 0;
  32. };
  33.  
  34. class failed_action : public action_if
  35. {
  36. public:
  37. action_result operator() () override
  38. {
  39. action_result result_{ false, -1 };
  40.  
  41. printf("failed action coming through\n");
  42.  
  43. return result_;
  44. }
  45. };
  46.  
  47. class successful_action : public action_if
  48. {
  49. public:
  50. action_result operator() () override
  51. {
  52. action_result result_{ true, 0 };
  53.  
  54. printf("success is achieved\n");
  55.  
  56. return result_;
  57. }
  58. };
  59.  
  60. using action_vector = vector<unique_ptr<action_if>>;
  61.  
  62. class actions_factory
  63. {
  64. public:
  65. action_vector create_actions(ioctl_code code)
  66. {
  67. action_vector vec;
  68.  
  69. switch(code)
  70. {
  71. case ioctl_code::OK:
  72. vec.emplace_back( make_unique<successful_action>() );
  73. break;
  74. case ioctl_code::FAILED:
  75. vec.emplace_back( make_unique<failed_action>() );
  76. }
  77.  
  78. return vec;
  79. }
  80. };
  81.  
  82.  
  83. class executor
  84. {
  85. public:
  86. action_result execute(action_vector& vec)
  87. {
  88. action_result result;
  89.  
  90. for (auto &x : vec)
  91. {
  92. result = (*x)();
  93.  
  94. if (!result.overall_result_)
  95. {
  96. break;
  97. }
  98. }
  99.  
  100. return result;
  101. }
  102. };
  103.  
  104. using action_task = packaged_task<action_result()>;
  105. using action_tasks = deque<action_task>;
  106.  
  107. class dispatcher;
  108. extern dispatcher* global_disp;
  109.  
  110. class dispatcher
  111. {
  112. public:
  113. dispatcher()
  114. : lock_{}
  115. , tasks_{}
  116. {
  117.  
  118. }
  119.  
  120. dispatcher(dispatcher&& disp)
  121. : lock_{ }
  122. , tasks_{ std::move(disp.tasks_) }
  123. {
  124.  
  125. }
  126.  
  127. void operator() ()
  128. {
  129. global_disp = this;
  130. cout << "im in" << endl;
  131. while (true)
  132. {
  133. lock_.lock();
  134. for (auto &x: tasks_)
  135. {
  136. lock_.unlock();
  137. x();
  138. lock_.lock();
  139. }
  140. tasks_.clear();
  141. lock_.unlock();
  142. }
  143. }
  144.  
  145. void add_task(action_task&& task)
  146. {
  147. std::lock_guard<std::mutex> lock{lock_};
  148. tasks_.emplace_back(std::move(task));
  149. }
  150.  
  151. private:
  152. mutex lock_;
  153. action_tasks tasks_;
  154. };
  155.  
  156. dispatcher* global_disp;
  157.  
  158. int main()
  159. {
  160. dispatcher disp;
  161.  
  162. actions_factory factory;
  163. action_vector vec = factory.create_actions(ioctl_code::OK);
  164.  
  165. executor exec;
  166.  
  167. auto func = std::bind(executor::execute, exec, std::ref(vec));
  168. packaged_task<action_result()> task{ func };
  169. future<action_result> f = task.get_future();
  170. disp.add_task(std::move(task));
  171. std::thread thr{ std::move(disp) };
  172. f.wait();
  173. action_result result = f.get();
  174. cout << "overall result" << result.overall_result_ << endl;
  175. thr.join();
  176. return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement