worker.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_TOF_INS:
  181. log_info("card loc message: 0x%04X", t.m_cmd_code);
  182. card_list::instance()->on_message(this, t.body<message_ins_locinfo>(), false);
  183. t.destroy();
  184. break;
  185. case CHAR_LOCATEDATA_PDOA: // pdoa实时定位数据
  186. card_list::instance()->on_message(this, t.body<message_pdoa_locinfo>(), false);
  187. t.destroy();
  188. break;
  189. case CHAR_LOCATEDATA_TDOA: // tdoa实时定位数据
  190. case CHAR_LOCATEDATA_TDOA_EXTEND: // 扩展tdoa实时定位数据
  191. case CHAR_LOCATEDATA_TDOA_EXTEND_INS: // 扩展tdoa实时定位数据,带惯导数据
  192. log_info("card loc message: 0x%04X", t.m_cmd_code);
  193. card_list::instance()->on_message(this, t.body<message_tdoa_locinfo>(), false);
  194. break;
  195. //card_message::on_loc_message(this,t.m_param1);
  196. break;
  197. case 0x853b://tof his
  198. case 0x873b://tdoa his
  199. log_info("site history message: 0x%04X",t.m_cmd_code);
  200. card_list::instance()->on_message(this,t.body<message_locinfo>(),true);
  201. //site_message::on_sync(this,t.m_param1);
  202. t.destroy();
  203. break;
  204. case CHAR_NET_CALI_TIME:
  205. case CHAR_CAN_CALI_TIME:
  206. log_info("site_cali_time: 0x%04X", t.m_cmd_code);
  207. break;
  208. case 0x804c://ctrl site message
  209. log_info("ctrl site message%04X",t.m_cmd_code);
  210. t.destroy();
  211. break;
  212. case CHAR_LIGHT_STATUS:
  213. log_info("light message: 0x%04X", t.m_cmd_code);
  214. break;
  215. case CHAR_LIGHT_HEART:
  216. log_info("light message: 0x%04X", t.m_cmd_code);
  217. break;
  218. case 0x10001://区域业务类型修改
  219. {
  220. update_local_cards();
  221. for(auto&c:m_local_card_list)
  222. {
  223. c->get_area_tool()->on_change_business(c,t);
  224. }
  225. auto&mcb=t.body<message_change_business>();
  226. std::shared_ptr<area> a=area_list::instance()->get(mcb.area_id);
  227. if(a && a->sub_frozen_count()==2)
  228. {
  229. a->set_business_list(std::move(mcb.new_list));
  230. a->sub_frozen_count();
  231. }
  232. if(mcb.ref_count.fetch_sub(1)==1)
  233. {
  234. t.destroy();
  235. }
  236. break;
  237. }
  238. case 0x10002://区域业务类型修改
  239. {
  240. auto&mcb=t.body<message_change_card_display>();
  241. auto c = card_list::instance()->get(mcb.m_card_id);
  242. c->m_display=mcb.m_display;
  243. t.destroy();
  244. break;
  245. }
  246. }
  247. }
  248. };
  249. struct worker_impl:worker
  250. {
  251. std::vector<std::shared_ptr<worker_thread>> m_threads;
  252. std::atomic<int> m_init_flag{-2};
  253. virtual void stop()
  254. {
  255. if(m_timer_worker)
  256. {
  257. m_timer_worker->async_stop();
  258. m_timer_worker->join();
  259. m_timer_worker.reset(nullptr);
  260. }
  261. int exp=0;
  262. if(!m_init_flag.compare_exchange_strong (exp,1))
  263. return;
  264. for(auto&thr:m_threads)
  265. thr->stop();
  266. for(auto&thr:m_threads)
  267. thr->join();
  268. }
  269. worker_thread& hash(uint32_t i)
  270. {
  271. return *m_threads[g_hash.hash_code(i)];
  272. }
  273. virtual int num_thread()
  274. {
  275. return std::thread::hardware_concurrency();
  276. }
  277. bool running()
  278. {
  279. return m_init_flag.load()==0;
  280. }
  281. virtual void request(task*t)
  282. {
  283. hash(t->m_hash_id).async_request(t);
  284. }
  285. virtual void broadcast(task*tk)
  286. {
  287. for(auto&thr:m_threads)
  288. thr->async_request(tk);
  289. }
  290. void init(int num_thread)
  291. {
  292. int exp=-2;
  293. if(m_init_flag.compare_exchange_strong (exp,-1))
  294. {
  295. m_threads.resize(num_thread);
  296. for(int i=0;i<num_thread;i++)
  297. {
  298. m_threads[i].reset(new worker_thread());
  299. m_threads[i]->set_thread_id(i);
  300. }
  301. m_init_flag.store(0);
  302. }
  303. while(0!=m_init_flag.load())
  304. std::this_thread::yield();
  305. }
  306. };
  307. worker_impl _worker_impl;
  308. worker*worker::instance()
  309. {
  310. int num_thread=_worker_impl.num_thread();
  311. if(!_worker_impl.running())
  312. {
  313. log_info("worker thread count=%d",num_thread);
  314. g_hash.set_num_thread(num_thread);
  315. _worker_impl.init(num_thread);
  316. m_timer_worker.reset(new timer_worker_thread);
  317. }
  318. return &_worker_impl;
  319. }