worker.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. }
  89. void do_task(task&t)
  90. {
  91. switch(t.m_cmd_code)
  92. {
  93. case 0x843b://tof
  94. case 0x863b://tdoa
  95. log_info("card loc message%04X",t.m_cmd_code);
  96. card_list::instance()->on_message(this,t.body<message_locinfo>(),false);
  97. free(&t);
  98. //card_message::on_loc_message(this,t.m_param1);
  99. break;
  100. case 0x853b://tof his
  101. case 0x873b://tdoa his
  102. log_info("site history message%04X",t.m_cmd_code);
  103. card_list::instance()->on_message(this,t.body<message_locinfo>(),true);
  104. //site_message::on_sync(this,t.m_param1);
  105. free(&t);
  106. break;
  107. case 0x804c://ctrl site message
  108. log_info("ctrl site message%04X",t.m_cmd_code);
  109. free(&t);
  110. break;
  111. case 0x10001://区域业务类型修改
  112. {
  113. update_local_cards();
  114. for(auto&c:m_local_card_list)
  115. {
  116. c->get_area_tool()->on_change_business(c,t);
  117. }
  118. auto&mcb=t.body<message_change_business>();
  119. std::shared_ptr<area> a=area_list::instance()->get(mcb.area_id);
  120. if(a && a->sub_frozen_count()==2)
  121. {
  122. a->set_business_list(std::move(mcb.new_list));
  123. a->sub_frozen_count();
  124. }
  125. if(mcb.ref_count.fetch_sub(1)==1)
  126. {
  127. free(&t);
  128. }
  129. }
  130. }
  131. }
  132. void join()
  133. {
  134. m_thread->join();
  135. }
  136. void stop()
  137. {
  138. async_stop();
  139. }
  140. };
  141. struct worker_impl:worker
  142. {
  143. std::vector<std::shared_ptr<worker_thread>> m_threads;
  144. std::atomic<int> m_init_flag{-2};
  145. virtual void stop()
  146. {
  147. int exp=0;
  148. if(!m_init_flag.compare_exchange_strong (exp,1))
  149. return;
  150. for(auto&thr:m_threads)
  151. thr->stop();
  152. for(auto&thr:m_threads)
  153. thr->join();
  154. }
  155. worker_thread& hash(uint32_t i)
  156. {
  157. return *m_threads[g_hash.hash_code(i)];
  158. }
  159. virtual int num_thread()
  160. {
  161. return std::thread::hardware_concurrency();
  162. }
  163. bool running()
  164. {
  165. return m_init_flag.load()==0;
  166. }
  167. virtual void request(task*t)
  168. {
  169. hash(t->m_hash_id).async_request(t);
  170. }
  171. virtual void broadcast(task*tk)
  172. {
  173. for(auto&thr:m_threads)
  174. thr->async_request(tk);
  175. }
  176. void init(int num_thread)
  177. {
  178. int exp=-2;
  179. if(m_init_flag.compare_exchange_strong (exp,-1))
  180. {
  181. m_threads.resize(num_thread);
  182. for(int i=0;i<num_thread;i++)
  183. {
  184. m_threads[i].reset(new worker_thread());
  185. m_threads[i]->set_thread_id(i);
  186. }
  187. m_init_flag.store(0);
  188. }
  189. while(0!=m_init_flag.load())
  190. std::this_thread::yield();
  191. }
  192. };
  193. worker_impl _worker_impl;
  194. worker*worker::instance()
  195. {
  196. int num_thread=_worker_impl.num_thread();
  197. if(!_worker_impl.running())
  198. {
  199. log_info("worker thread count=%d",num_thread);
  200. g_hash.set_num_thread(num_thread);
  201. _worker_impl.init(num_thread);
  202. }
  203. return &_worker_impl;
  204. }