1 module hunt.net.EventLoopPool;
2 
3 import hunt.logging.ConsoleLogger;
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 = 64;
24     EventLoopPool objPool = new EventLoopPool(new EventLoopObjectFactory(), options);
25     return objPool;
26 }
27 
28 void shutdownEventLoopPool() {
29     if(_pool !is null) {
30         _pool.close();
31     }
32 }
33 
34 /**
35  * 
36  */
37 class EventLoopObjectFactory : ObjectFactory!(EventLoop) {
38 
39     override EventLoop makeObject() {
40         EventLoop r = new EventLoop();
41         r.runAsync();
42 
43         while(!r.isReady()) {
44             version(HUNT_IO_DEBUG) warning("Waiting for the eventloop got ready...");
45         }
46 
47         return r;
48     }    
49 
50     override void destroyObject(EventLoop p) {
51         p.stop();
52     }
53 
54     override bool isValid(EventLoop p) {
55         return p.isRuning();
56     }
57 }