worker.cpp 7.8 KB

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