1 module hunt.net.EventLoopPool; 2 3 import hunt.logging; 4 import hunt.event.EventLoop; 5 import hunt.util.pool; 6 7 import std.concurrency : initOnce; 8 9 alias EventLoopPool = ObjectPool!EventLoop; 10 11 private __gshared EventLoopPool _pool; 12 13 EventLoopPool eventLoopPool() { 14 return initOnce!_pool(buildEventLoopPool()); 15 } 16 17 void buildEventLoopPool(PoolOptions options) { 18 initOnce!_pool(new EventLoopPool(new EventLoopObjectFactory(), options)); 19 } 20 21 private EventLoopPool buildEventLoopPool() { 22 PoolOptions options = new PoolOptions(); 23 options.size = 128; 24 options.name = "EventLoopPool"; 25 EventLoopPool objPool = new EventLoopPool(new EventLoopObjectFactory(), options); 26 return objPool; 27 } 28 29 void shutdownEventLoopPool() { 30 if(_pool !is null) { 31 _pool.close(); 32 } 33 } 34 35 /** 36 * 37 */ 38 class EventLoopObjectFactory : ObjectFactory!(EventLoop) { 39 40 override EventLoop makeObject() { 41 // EventLoop r = new EventLoop(); 42 // r.runAsync(); 43 44 // while(!r.isReady()) { 45 // version(HUNT_IO_DEBUG) warning("Waiting for the eventloop got ready..."); 46 // } 47 48 // return r; 49 return buildEventLoop(); 50 } 51 52 static EventLoop buildEventLoop() { 53 EventLoop el = new EventLoop(); 54 version(HUNT_NET_DEBUG) warningf("Waiting for the eventloop[%d] got ready...", el.getId()); 55 56 el.runAsync(-1); 57 import core.thread; 58 import core.time; 59 60 while(!el.isReady()) { 61 version(HUNT_IO_DEBUG_MORE) warning("Waiting for the eventloop got ready..."); 62 } 63 version(HUNT_NET_DEBUG) warningf("eventloop[%d] is ready", el.getId()); 64 return el; 65 } 66 67 override void destroyObject(EventLoop p) { 68 p.stop(); 69 } 70 71 override bool isValid(EventLoop p) { 72 return p.isRuning(); 73 } 74 }