worker.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include <functional>
  2. #include <list>
  3. #include <vector>
  4. #include <mutex>
  5. #include <atomic>
  6. #include <memory>
  7. #include <thread>
  8. #include <ev++.h>
  9. #include "log.h"
  10. #include "worker.h"
  11. #include "message.h"
  12. #include "card_base.h"
  13. #include "card.h"
  14. #include "zloop.h"
  15. #include "area.h"
  16. #include "clock.h"
  17. #include "mine_business.h"
  18. loop_thread::loop_thread ()
  19. {
  20. m_thread.reset(new std::thread(std::bind(&loop_thread::run,this)));
  21. }
  22. void loop_thread::run()
  23. {
  24. loop_init();
  25. log_info("worker_thread start....");
  26. while(!check_stop_flag())
  27. {
  28. try
  29. {
  30. ev::dynamic_loop::run(0);
  31. }
  32. catch(const std::exception&e)
  33. {
  34. log_error("捕获到异常:%s",e.what());
  35. }
  36. catch(...)
  37. {
  38. log_error("捕获到未知异常");
  39. }
  40. }
  41. log_info("worker_thread exit....");
  42. }
  43. void loop_thread::on_async(const std::list<task*>&task_list)
  44. {
  45. for(task*t:task_list)
  46. {
  47. do_task(*t);
  48. }
  49. }
  50. void loop_thread::loop_init()
  51. {
  52. }
  53. void loop_thread::join()
  54. {
  55. m_thread->join();
  56. }
  57. void loop_thread::stop()
  58. {
  59. async_stop();
  60. }
  61. loop_thread::~loop_thread()
  62. {
  63. }
  64. struct hash_thread
  65. {
  66. int m_num_thread=4;
  67. void set_num_thread(int num_thread)
  68. {
  69. m_num_thread=num_thread;
  70. }
  71. int hash_code(uint32_t card_id)
  72. {
  73. return card_id*2003%m_num_thread;
  74. }
  75. };
  76. struct timer_worker_thread: loop_thread
  77. {
  78. void do_task_0001()
  79. {
  80. static int version = -1;
  81. zclock clock;
  82. visit_site_status vss;
  83. sit_list::instance()->accept(vss);
  84. int v = card_list::instance()->version();
  85. card_list_visit clv;
  86. if(v != version)
  87. {
  88. version=v;
  89. clv._flag=true;
  90. mine_business::inst()->clear_vehicle();
  91. }
  92. card_list::instance()->accept(clv);
  93. mine_business::inst()->run_business();
  94. log_info("timer_worker_thread use time:%ldus", clock.count_us());
  95. }
  96. void on_timeout()
  97. {
  98. do_task_0001();
  99. }
  100. std::unique_ptr<ev::timer> card_timer_1s;
  101. void loop_init()
  102. {
  103. card_timer_1s.reset(new ev::timer(*this));
  104. card_timer_1s->set<timer_worker_thread,&timer_worker_thread::on_timeout>(this);
  105. card_timer_1s->start(1,1);
  106. }
  107. void do_task(task&t)
  108. {
  109. t.destroy();
  110. }
  111. };
  112. std::unique_ptr<timer_worker_thread> m_timer_worker;
  113. hash_thread g_hash;
  114. struct worker_thread: loop_thread ,visitor<std::shared_ptr<card_location_base>>
  115. {
  116. int m_thread_id=0;
  117. int m_card_list_version=-1;
  118. std::vector<std::shared_ptr<card_location_base>> m_local_card_list;
  119. worker_thread ()
  120. {
  121. m_local_card_list.reserve(128);
  122. }
  123. void set_thread_id(int id)
  124. {
  125. m_thread_id=id;
  126. }
  127. std::unique_ptr<ev::timer> card_timer_1s;
  128. void loop_init()
  129. {
  130. card_timer_1s.reset(new ev::timer(*this));
  131. card_timer_1s->set<worker_thread,&worker_thread::on_timeout>(this);
  132. card_timer_1s->start(1,1);
  133. }
  134. void update_local_cards()
  135. {
  136. int version=card_list::instance()->version();
  137. if(m_card_list_version!=version)
  138. {
  139. m_card_list_version=version;
  140. init_local_card_list();
  141. }
  142. }
  143. void on_timeout()
  144. {
  145. update_local_cards();
  146. for(auto&c:m_local_card_list)
  147. {
  148. c->on_timer();
  149. }
  150. }
  151. bool visit(std::shared_ptr<card_location_base> c)
  152. {
  153. if(g_hash.hash_code(c->m_id)==m_thread_id) //32bit id也可以
  154. {
  155. m_local_card_list.push_back(c);
  156. }
  157. return true;
  158. }
  159. void init_local_card_list()
  160. {
  161. m_local_card_list.clear();
  162. card_list::instance()->accept(*this);
  163. log_info("update local cards,count=%d",m_local_card_list.size());
  164. }
  165. void do_task(task&t)
  166. {
  167. switch(t.m_cmd_code)
  168. {
  169. case 0x843b://tof
  170. case 0x863b://tdoa
  171. log_info("card loc message%04X",t.m_cmd_code);
  172. card_list::instance()->on_message(this,t.body<message_locinfo>(),false);
  173. t.destroy();
  174. //card_message::on_loc_message(this,t.m_param1);
  175. break;
  176. case 0x853b://tof his
  177. case 0x873b://tdoa his
  178. log_info("site history message%04X",t.m_cmd_code);
  179. card_list::instance()->on_message(this,t.body<message_locinfo>(),true);
  180. //site_message::on_sync(this,t.m_param1);
  181. t.destroy();
  182. break;
  183. case 0x804c://ctrl site message
  184. log_info("ctrl site message%04X",t.m_cmd_code);
  185. t.destroy();
  186. break;
  187. case 0x10001://区域业务类型修改
  188. {
  189. update_local_cards();
  190. for(auto&c:m_local_card_list)
  191. {
  192. c->get_area_tool()->on_change_business(c,t);
  193. }
  194. auto&mcb=t.body<message_change_business>();
  195. std::shared_ptr<area> a=area_list::instance()->get(mcb.area_id);
  196. if(a && a->sub_frozen_count()==2)
  197. {
  198. a->set_business_list(std::move(mcb.new_list));
  199. a->sub_frozen_count();
  200. }
  201. if(mcb.ref_count.fetch_sub(1)==1)
  202. {
  203. t.destroy();
  204. }
  205. break;
  206. }
  207. case 0x10002://区域业务类型修改
  208. {
  209. auto&mcb=t.body<message_change_card_display>();
  210. auto c = card_list::instance()->get(mcb.m_card_id);
  211. c->m_display=mcb.m_display;
  212. t.destroy();
  213. break;
  214. }
  215. }
  216. }
  217. };
  218. struct worker_impl:worker
  219. {
  220. std::vector<std::shared_ptr<worker_thread>> m_threads;
  221. std::atomic<int> m_init_flag{-2};
  222. virtual void stop()
  223. {
  224. int exp=0;
  225. if(!m_init_flag.compare_exchange_strong (exp,1))
  226. return;
  227. for(auto&thr:m_threads)
  228. thr->stop();
  229. for(auto&thr:m_threads)
  230. thr->join();
  231. }
  232. worker_thread& hash(uint32_t i)
  233. {
  234. return *m_threads[g_hash.hash_code(i)];
  235. }
  236. virtual int num_thread()
  237. {
  238. return std::thread::hardware_concurrency();
  239. }
  240. bool running()
  241. {
  242. return m_init_flag.load()==0;
  243. }
  244. virtual void request(task*t)
  245. {
  246. hash(t->m_hash_id).async_request(t);
  247. }
  248. virtual void broadcast(task*tk)
  249. {
  250. for(auto&thr:m_threads)
  251. thr->async_request(tk);
  252. }
  253. void init(int num_thread)
  254. {
  255. int exp=-2;
  256. if(m_init_flag.compare_exchange_strong (exp,-1))
  257. {
  258. m_threads.resize(num_thread);
  259. for(int i=0;i<num_thread;i++)
  260. {
  261. m_threads[i].reset(new worker_thread());
  262. m_threads[i]->set_thread_id(i);
  263. }
  264. m_init_flag.store(0);
  265. }
  266. while(0!=m_init_flag.load())
  267. std::this_thread::yield();
  268. }
  269. };
  270. worker_impl _worker_impl;
  271. worker*worker::instance()
  272. {
  273. int num_thread=_worker_impl.num_thread();
  274. if(!_worker_impl.running())
  275. {
  276. log_info("worker thread count=%d",num_thread);
  277. g_hash.set_num_thread(num_thread);
  278. _worker_impl.init(num_thread);
  279. m_timer_worker.reset(new timer_worker_thread);
  280. }
  281. return &_worker_impl;
  282. }