1
0

worker.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. loop_thread::loop_thread ()
  20. {
  21. m_thread.reset(new std::thread(std::bind(&loop_thread::run,this)));
  22. }
  23. void loop_thread::run()
  24. {
  25. loop_init();
  26. log_info("worker_thread start....");
  27. while(!check_stop_flag())
  28. {
  29. try
  30. {
  31. ev::dynamic_loop::run(0);
  32. }
  33. catch(const std::exception&e)
  34. {
  35. log_error("捕获到异常:%s",e.what());
  36. }
  37. catch(...)
  38. {
  39. log_error("捕获到未知异常");
  40. }
  41. }
  42. log_info("worker_thread exit....");
  43. }
  44. void loop_thread::on_async(const std::list<task*>&task_list)
  45. {
  46. for(task*t:task_list)
  47. {
  48. do_task(*t);
  49. }
  50. }
  51. void loop_thread::loop_init()
  52. {
  53. }
  54. void loop_thread::join()
  55. {
  56. m_thread->join();
  57. }
  58. void loop_thread::stop()
  59. {
  60. async_stop();
  61. }
  62. loop_thread::~loop_thread()
  63. {
  64. }
  65. struct hash_thread
  66. {
  67. int m_num_thread=4;
  68. void set_num_thread(int num_thread)
  69. {
  70. m_num_thread=num_thread;
  71. }
  72. int hash_code(uint32_t card_id)
  73. {
  74. return card_id*2003%m_num_thread;
  75. }
  76. };
  77. struct timer_worker_thread: loop_thread
  78. {
  79. void do_task_0001()
  80. {
  81. static int version = -1;
  82. zclock clock;
  83. int v = card_list::instance()->version();
  84. card_list_visit clv;
  85. if(v != version)
  86. {
  87. version=v;
  88. clv._flag=true;
  89. mine_business::inst()->clear_vehicle();
  90. }
  91. card_list::instance()->accept(clv);
  92. bulletin_broad_show::inst()->run_bulletin_board();
  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. if(m_timer_worker)
  225. {
  226. m_timer_worker->async_stop();
  227. m_timer_worker->join();
  228. m_timer_worker.reset(nullptr);
  229. }
  230. int exp=0;
  231. if(!m_init_flag.compare_exchange_strong (exp,1))
  232. return;
  233. for(auto&thr:m_threads)
  234. thr->stop();
  235. for(auto&thr:m_threads)
  236. thr->join();
  237. }
  238. worker_thread& hash(uint32_t i)
  239. {
  240. return *m_threads[g_hash.hash_code(i)];
  241. }
  242. virtual int num_thread()
  243. {
  244. return std::thread::hardware_concurrency();
  245. }
  246. bool running()
  247. {
  248. return m_init_flag.load()==0;
  249. }
  250. virtual void request(task*t)
  251. {
  252. hash(t->m_hash_id).async_request(t);
  253. }
  254. virtual void broadcast(task*tk)
  255. {
  256. for(auto&thr:m_threads)
  257. thr->async_request(tk);
  258. }
  259. void init(int num_thread)
  260. {
  261. int exp=-2;
  262. if(m_init_flag.compare_exchange_strong (exp,-1))
  263. {
  264. m_threads.resize(num_thread);
  265. for(int i=0;i<num_thread;i++)
  266. {
  267. m_threads[i].reset(new worker_thread());
  268. m_threads[i]->set_thread_id(i);
  269. }
  270. m_init_flag.store(0);
  271. }
  272. while(0!=m_init_flag.load())
  273. std::this_thread::yield();
  274. }
  275. };
  276. worker_impl _worker_impl;
  277. worker*worker::instance()
  278. {
  279. int num_thread=_worker_impl.num_thread();
  280. if(!_worker_impl.running())
  281. {
  282. log_info("worker thread count=%d",num_thread);
  283. g_hash.set_num_thread(num_thread);
  284. _worker_impl.init(num_thread);
  285. m_timer_worker.reset(new timer_worker_thread);
  286. }
  287. return &_worker_impl;
  288. }