1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #ifndef _WORKER_HPP_
- #define _WORKER_HPP_
- #include <ev++.h>
- #include <atomic>
- #include <thread>
- #include <vector>
- #include <zloop.h>
- struct task
- {
- uint32_t m_cmd_code;
- uint32_t m_hash_id;
- task(uint32_t hash_id,uint32_t cmd_code)
- :m_cmd_code(cmd_code)
- ,m_hash_id(hash_id)
- {
- }
- task()
- {}
- void destroy()
- {
- delete this;
- }
- template<typename T> const T&body()const
- {
- return *(T*)this;
- }
- template<typename T> T&body()
- {
- return *(T*)this;
- }
- template<typename T> static task*alloc()
- {
- return new T();
- }
- virtual ~task(){}
- };
- struct area_business;
- struct message_change_business:task
- {
- int area_id;
- std::atomic<int> ref_count;
- std::vector<area_business*> del_list,add_list,new_list;
- };
- struct message_change_card_display:task
- {
- uint64_t m_card_id;
- uint16_t m_display;
- };
- struct worker
- {
- virtual void stop()=0;
- virtual void request(task*tk)=0;
- virtual void broadcast(task*tk)
- {
- }
- virtual int num_thread()
- {
- return 1;
- }
- static worker*instance();
- };
- struct loop_thread:zloop<task*>
- {
- std::unique_ptr<std::thread> m_thread;
- loop_thread ();
- ~loop_thread();
- void run();
- void stop();
- void join();
- void on_async(const std::list<task*>&task_list);
- virtual void loop_init();
- virtual void do_task(task&t)=0;
- };
- #endif
|