1
0

worker.cpp 7.3 KB

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