|
@@ -10,20 +10,52 @@
|
|
|
#include "log.h"
|
|
|
#include "worker.h"
|
|
|
#include "message.h"
|
|
|
+#include "card_base.h"
|
|
|
#include "card.h"
|
|
|
#include "zloop.h"
|
|
|
|
|
|
-struct worker_thread: zloop<task*>
|
|
|
+struct hash_thread
|
|
|
{
|
|
|
- std::unique_ptr<std::thread> m_thread;
|
|
|
+ int m_num_thread=4;
|
|
|
+
|
|
|
+ void set_num_thread(int num_thread)
|
|
|
+ {
|
|
|
+ m_num_thread=num_thread;
|
|
|
+ }
|
|
|
+
|
|
|
+ int hash_code(uint32_t card_id)
|
|
|
+ {
|
|
|
+ return card_id*2003%m_num_thread;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+hash_thread g_hash;
|
|
|
|
|
|
+struct worker_thread: zloop<task*> ,visitor<std::shared_ptr<card_location_base>>
|
|
|
+{
|
|
|
+ std::unique_ptr<std::thread> m_thread;
|
|
|
+ int m_thread_id=0;
|
|
|
+ int m_card_list_version=-1;
|
|
|
+ std::vector<std::shared_ptr<card_location_base>> m_local_card_list;
|
|
|
worker_thread ()
|
|
|
{
|
|
|
m_thread.reset(new std::thread(std::bind(&worker_thread::run,this)));
|
|
|
+ m_local_card_list.reserve(128);
|
|
|
+ }
|
|
|
+
|
|
|
+ void set_thread_id(int id)
|
|
|
+ {
|
|
|
+ m_thread_id=id;
|
|
|
}
|
|
|
|
|
|
void run()
|
|
|
{
|
|
|
+ ev::timer card_timer_1s(*this);
|
|
|
+
|
|
|
+ card_timer_1s.set(1,1);
|
|
|
+ card_timer_1s.set<worker_thread,&worker_thread::on_timeout>(this);
|
|
|
+ card_timer_1s.start();
|
|
|
+
|
|
|
ev::dynamic_loop::run(0);
|
|
|
log_info("worker_thread exit....");
|
|
|
}
|
|
@@ -37,6 +69,37 @@ struct worker_thread: zloop<task*>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ void on_timeout()
|
|
|
+ {
|
|
|
+ int version=card_list::instance()->version();
|
|
|
+ if(m_card_list_version!=version)
|
|
|
+ {
|
|
|
+ m_card_list_version=version;
|
|
|
+ init_local_card_list();
|
|
|
+ }
|
|
|
+
|
|
|
+ for(auto&c:m_local_card_list)
|
|
|
+ {
|
|
|
+ c->on_timer();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bool visit(std::shared_ptr<card_location_base> c)
|
|
|
+ {
|
|
|
+ if(g_hash.hash_code(c->m_id)==m_thread_id) //32bit id也可以
|
|
|
+ {
|
|
|
+ m_local_card_list.push_back(c);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ void init_local_card_list()
|
|
|
+ {
|
|
|
+ m_local_card_list.clear();
|
|
|
+ card_list::instance()->accept(*this);
|
|
|
+ }
|
|
|
+
|
|
|
void do_task(const task&t)
|
|
|
{
|
|
|
switch(t.m_cmd_code)
|
|
@@ -89,9 +152,9 @@ struct worker_impl:worker
|
|
|
thr->join();
|
|
|
}
|
|
|
|
|
|
- worker_thread& hash(uint64_t i)
|
|
|
+ worker_thread& hash(uint32_t i)
|
|
|
{
|
|
|
- return *m_threads[i*2003%m_threads.size()];
|
|
|
+ return *m_threads[g_hash.hash_code(i)];
|
|
|
}
|
|
|
|
|
|
virtual void request(task*t)
|
|
@@ -108,6 +171,7 @@ struct worker_impl:worker
|
|
|
for(int i=0;i<num_thread;i++)
|
|
|
{
|
|
|
m_threads[i].reset(new worker_thread());
|
|
|
+ m_threads[i]->set_thread_id(i);
|
|
|
}
|
|
|
m_init_flag.store(0);
|
|
|
}
|
|
@@ -120,10 +184,11 @@ struct worker_impl:worker
|
|
|
worker_impl _worker_impl;
|
|
|
worker*worker::instance()
|
|
|
{
|
|
|
- int num_thread=std::thread::hardware_concurrency();
|
|
|
+ int num_thread=std::thread::hardware_concurrency()*2;
|
|
|
|
|
|
- log_info("worker thread count=%d",num_thread*2);
|
|
|
- _worker_impl.init(num_thread<<1);
|
|
|
+ log_info("worker thread count=%d",num_thread);
|
|
|
+ g_hash.set_num_thread(num_thread);
|
|
|
+ _worker_impl.init(num_thread);
|
|
|
return &_worker_impl;
|
|
|
}
|
|
|
|