|
1 | 1 | package hxcoro.run; |
2 | 2 |
|
3 | | -import haxe.coro.schedulers.IScheduler; |
4 | | -import hxcoro.dispatchers.IShutDown; |
5 | 3 | import haxe.coro.BaseContinuation; |
6 | 4 | import haxe.coro.context.Context; |
7 | 5 | import haxe.coro.dispatchers.Dispatcher; |
| 6 | +import haxe.coro.schedulers.IScheduler; |
8 | 7 | import hxcoro.schedulers.ILoop; |
9 | 8 |
|
10 | 9 | class Setup { |
@@ -59,23 +58,32 @@ class Setup { |
59 | 58 |
|
60 | 59 | #if (cpp && hxcpp_luv_io) |
61 | 60 |
|
62 | | - static public function createLuvGen(createDispatcher:(cpp.luv.Luv.LuvLoop, IScheduler) -> Dispatcher) { |
| 61 | + static public function createLuvGen<T:Dispatcher>(createDispatcher:(cpp.luv.Luv.LuvLoop, IScheduler) -> T, shutDownDispatcher:T -> Void) { |
63 | 62 | final loop = cpp.luv.Luv.allocLoop(); |
64 | 63 | final scheduler = new hxcoro.schedulers.LuvScheduler(loop); |
65 | 64 | final dispatcher = createDispatcher(loop, scheduler); |
66 | 65 | function finalize() { |
67 | 66 | scheduler.shutDown(); |
68 | | - if (dispatcher is IShutDown) { |
69 | | - (cast dispatcher : IShutDown).shutDown(); |
70 | | - } |
| 67 | + shutDownDispatcher(dispatcher); |
71 | 68 | cpp.luv.Luv.stopLoop(loop); |
72 | 69 | cpp.luv.Luv.freeLoop(loop); |
73 | 70 | } |
74 | 71 | return new LoopSetup(scheduler, dispatcher, finalize); |
75 | 72 | } |
76 | 73 |
|
77 | 74 | static public function createLuv() { |
78 | | - return createLuvGen((uvLoop, loop) -> new hxcoro.dispatchers.LuvDispatcher(uvLoop, loop)); |
| 75 | + return createLuvGen( |
| 76 | + (uvLoop, loop) -> new hxcoro.dispatchers.LuvDispatcher(uvLoop, loop), |
| 77 | + dispatcher -> dispatcher.shutDown() |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + static public function createLuvThreadPool() { |
| 82 | + final pool = new hxcoro.thread.FixedThreadPool(10); |
| 83 | + return createLuvGen( |
| 84 | + (uvLoop, loop) -> new hxcoro.dispatchers.LuvDispatcher(uvLoop, loop), |
| 85 | + dispatcher -> pool.shutDown() |
| 86 | + ); |
79 | 87 | } |
80 | 88 |
|
81 | 89 | #elseif interp |
@@ -111,8 +119,10 @@ class Setup { |
111 | 119 | #end |
112 | 120 |
|
113 | 121 | static public function createDefault() { |
114 | | - #if (cpp && hxcpp_luv_io || interp) |
| 122 | + #if interp |
115 | 123 | return createLuv(); |
| 124 | + #elseif (cpp && hxcpp_luv_io) |
| 125 | + return createLuvThreadPool(); |
116 | 126 | #elseif (jvm || cpp || hl) |
117 | 127 | return createThreadPool(10); |
118 | 128 | #else |
|
0 commit comments