worker.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. struct hash_thread
  17. {
  18. int m_num_thread=4;
  19. void set_num_thread(int num_thread)
  20. {
  21. m_num_thread=num_thread;
  22. }
  23. int hash_code(uint32_t card_id)
  24. {
  25. return card_id*2003%m_num_thread;
  26. }
  27. };
  28. hash_thread g_hash;
  29. struct worker_thread: zloop<task*> ,visitor<std::shared_ptr<card_location_base>>
  30. {
  31. std::unique_ptr<std::thread> m_thread;
  32. int m_thread_id=0;
  33. int m_card_list_version=-1;
  34. std::vector<std::shared_ptr<card_location_base>> m_local_card_list;
  35. worker_thread ()
  36. {
  37. m_thread.reset(new std::thread(std::bind(&worker_thread::run,this)));
  38. m_local_card_list.reserve(128);
  39. }
  40. void set_thread_id(int id)
  41. {
  42. m_thread_id=id;
  43. }
  44. void run()
  45. {
  46. ev::timer card_timer_1s(*this);
  47. card_timer_1s.set<worker_thread,&worker_thread::on_timeout>(this);
  48. card_timer_1s.start(1,1);
  49. ev::dynamic_loop::run(0);
  50. log_info("worker_thread exit....");
  51. }
  52. virtual void on_async(const std::list<task*>&task_list)
  53. {
  54. for(task*t:task_list)
  55. {
  56. do_task(*t);
  57. }
  58. }
  59. void update_local_cards()
  60. {
  61. int version=card_list::instance()->version();
  62. if(m_card_list_version!=version)
  63. {
  64. m_card_list_version=version;
  65. init_local_card_list();
  66. }
  67. }
  68. void on_timeout()
  69. {
  70. update_local_cards();
  71. for(auto&c:m_local_card_list)
  72. {
  73. c->on_timer();
  74. }
  75. }
  76. bool visit(std::shared_ptr<card_location_base> c)
  77. {
  78. if(g_hash.hash_code(c->m_id)==m_thread_id) //32bit id也可以
  79. {
  80. m_local_card_list.push_back(c);
  81. }
  82. return true;
  83. }
  84. void init_local_card_list()
  85. {
  86. m_local_card_list.clear();
  87. card_list::instance()->accept(*this);
  88. log_info("update local cards,count=%d",m_local_card_list.size());
  89. }
  90. void do_task(task&t)
  91. {
  92. switch(t.m_cmd_code)
  93. {
  94. case 0x843b://tof
  95. case 0x863b://tdoa
  96. log_info("card loc message%04X",t.m_cmd_code);
  97. card_list::instance()->on_message(this,t.body<message_locinfo>(),false);
  98. t.destroy();
  99. //card_message::on_loc_message(this,t.m_param1);
  100. break;
  101. case 0x853b://tof his
  102. case 0x873b://tdoa his
  103. log_info("site history message%04X",t.m_cmd_code);
  104. card_list::instance()->on_message(this,t.body<message_locinfo>(),true);
  105. //site_message::on_sync(this,t.m_param1);
  106. t.destroy();
  107. break;
  108. case 0x804c://ctrl site message
  109. log_info("ctrl site message%04X",t.m_cmd_code);
  110. t.destroy();
  111. break;
  112. case 0x10001://区域业务类型修改
  113. {
  114. update_local_cards();
  115. for(auto&c:m_local_card_list)
  116. {
  117. c->get_area_tool()->on_change_business(c,t);
  118. }
  119. auto&mcb=t.body<message_change_business>();
  120. std::shared_ptr<area> a=area_list::instance()->get(mcb.area_id);
  121. if(a && a->sub_frozen_count()==2)
  122. {
  123. a->set_business_list(std::move(mcb.new_list));
  124. a->sub_frozen_count();
  125. }
  126. if(mcb.ref_count.fetch_sub(1)==1)
  127. {
  128. t.destroy();
  129. }
  130. }
  131. }
  132. }
  133. void join()
  134. {
  135. m_thread->join();
  136. }
  137. void stop()
  138. {
  139. async_stop();
  140. }
  141. };
  142. struct worker_impl:worker
  143. {
  144. std::vector<std::shared_ptr<worker_thread>> m_threads;
  145. std::atomic<int> m_init_flag{-2};
  146. virtual void stop()
  147. {
  148. int exp=0;
  149. if(!m_init_flag.compare_exchange_strong (exp,1))
  150. return;
  151. for(auto&thr:m_threads)
  152. thr->stop();
  153. for(auto&thr:m_threads)
  154. thr->join();
  155. }
  156. worker_thread& hash(uint32_t i)
  157. {
  158. return *m_threads[g_hash.hash_code(i)];
  159. }
  160. virtual int num_thread()
  161. {
  162. return std::thread::hardware_concurrency();
  163. }
  164. bool running()
  165. {
  166. return m_init_flag.load()==0;
  167. }
  168. virtual void request(task*t)
  169. {
  170. hash(t->m_hash_id).async_request(t);
  171. }
  172. virtual void broadcast(task*tk)
  173. {
  174. for(auto&thr:m_threads)
  175. thr->async_request(tk);
  176. }
  177. void init(int num_thread)
  178. {
  179. int exp=-2;
  180. if(m_init_flag.compare_exchange_strong (exp,-1))
  181. {
  182. m_threads.resize(num_thread);
  183. for(int i=0;i<num_thread;i++)
  184. {
  185. m_threads[i].reset(new worker_thread());
  186. m_threads[i]->set_thread_id(i);
  187. }
  188. m_init_flag.store(0);
  189. }
  190. while(0!=m_init_flag.load())
  191. std::this_thread::yield();
  192. }
  193. };
  194. worker_impl _worker_impl;
  195. worker*worker::instance()
  196. {
  197. int num_thread=_worker_impl.num_thread();
  198. if(!_worker_impl.running())
  199. {
  200. log_info("worker thread count=%d",num_thread);
  201. g_hash.set_num_thread(num_thread);
  202. _worker_impl.init(num_thread);
  203. }
  204. return &_worker_impl;
  205. }