1 module geario.event.EventLoopThreadPool; 2 3 import geario.event.EventLoop; 4 import geario.util.ThreadPool; 5 6 import core.sync.mutex; 7 import core.thread; 8 9 import geario.logging; 10 11 class EventLoopThreadPool 12 { 13 public: 14 15 this(size_t capacity) 16 { 17 _cur_index = 0; 18 _capacity = capacity; 19 _mutex_cur_idx = new Mutex; 20 21 for (int i = 0; i < capacity; i++) 22 { 23 _loops[i] = new EventLoop; 24 } 25 26 Init(); 27 } 28 29 ~this() 30 { 31 // 32 } 33 34 EventLoop GetNextLoop() 35 { 36 // _mutex_cur_idx.lock(); 37 scope (exit) 38 { 39 // _mutex_cur_idx.unlock(); 40 _cur_index++; 41 } 42 43 if (_cur_index == _capacity) { 44 _cur_index = 0; 45 } 46 47 return _loops[_cur_index]; 48 } 49 50 private: 51 void Init() 52 { 53 foreach (EventLoop loop; _loops) 54 { 55 _threads ~= new Thread(&loop.StartLoop).start(); 56 } 57 } 58 59 size_t _capacity; 60 EventLoop[size_t] _loops; 61 Thread[] _threads; 62 size_t _cur_index; 63 Mutex _mutex_cur_idx; 64 }