Comments (17)
Threads support has been merged.
from hashlink.
This requires the following steps:
- add a global lock for gc_alloc_gen, to prevent two threads from allocating
- keep a list of threads running, each with their own stack top
- when we want to start a collection, we need to wait until all threads are stopped. a thread can be stopped when trying to do an allocation or when leaving a blocking section
- blocking sections can be added in some native system primitives which does not perform allocation but might lock the thread for some time. This allow the GC not to wait for such a blocked thread. sys_sleep, socket and file i/o are good candidates.
- once all threads are stopped, we need to scan all their stacks + gc roots. A good optimization here is to use as many threads for that as we have CPU cores
- there is some additional work to have stack traces per-thread since currently stack top is a static in JIT/HLC
This is done as part of 99de533
from hashlink.
I haven't merged the branch yet because of the following benchmark.
class Main {
public static function main() {
var t0 = Sys.time();
for( i in 0...40000000 )
new hl.Bytes(1);
trace(Sys.time() - t0);
}
}
(results in both 32 and 64 bits)
- without HL_THREADS: 0.67s / 0.55s
- with HL_THREADS: 1.52s / 1.33s
Time is used by:
- +0.59s / +0.56s in global_lock mutex acquire/release: finding a faster soft lock seems necessary, especially since it's most likely low concurrency -- https://www.codeproject.com/Articles/184046/Spin-Lock-in-C
- + ~0.20s in gc_save_context in global_lock: this could be avoided by waking up the locked threads if a stop the world is necessary.
-
+0.15s in TLS thread get : use faster TLS for Windows ?+0.04 using HL_THREAD_VAR (acceptable)
from hashlink.
In progress here https://github.com/HaxeFoundation/hashlink/tree/threads
Note for threading implementation (things to deal with)
- error.c: stack_trace/stack_count/exc_rethrow (TLS) : requires moving all error handling in per thread data (make gc_thread* public in hl.h?)
- JIT changes (HL_THREADS disable)
- JIT changes (HL_THREADS enable)
- obj.c: hl_cache (lock)
- gc.c: lock in gc_alloc_gen
- gc.c: is_blocking (TLS) + hl_is_blocking should get hl_thread param ?
- check libs for statics
- handle ssl globals (entropy etc.) to confirm with @pperidont
- Haxe Thread APIs wrappers
-
hlc_main.c
should use hl_dyn_call_safe - update debugger protocol with multiple threads
- linux spin lock implementation and benchmarks
- optimization for single threaded app
from hashlink.
Progress in #123
from hashlink.
is possible use hashlink replace Neko as socket server now?
@ncannasse
from hashlink.
@sonygod for a generic socket server yes it should be doable. Maybe even more efficient if you use hl.uv.Tcp
given it uses libuv (no need for threads in that case)
from hashlink.
could you build the last hashlink 1.6? binary for download?
it's seem haxe last build do not support hashlink1.5,
from hashlink.
Yes I am planning to release 1.6 soon
from hashlink.
could you give some small example how to use hl.uv.tcp libuv?
BTW,what's the mean uv in hl.uv?
current I want to use hashlink1.6 repalce neko 2.1,
from hashlink.
@sonygod you can update to Haxe 4.0-preview 4 and HL 1.6 now. "uv" is about "libuv" (https://github.com/libuv/libuv) you can read their manual and use hl.uv package as part of Haxe std library
from hashlink.
@ncannasse I'm working on generic socket server (using Hashlink and sys.net.* ) and looking to create three separate threads for acceptor, socket reader and socket writer from the main class.
In java I just can implements Runnable to create thread from class.
How I can create / start thread in Hashlink ?
from hashlink.
@flashultra use hl.vm.Thread.create(function() { .... })
from hashlink.
@ncannasse Something goes wrong with Thread.create . I did this for test :
Thread.create(function() {
while(true) {
trace("test");
}
});
and after 6 iteration thread stops . The final result was :
src/Server.hx:52: test
src/Server.hx:52: test
src/Server.hx:52: test
src/Server.hx:52: test
src/Server.hx:52: test
src/Server.hx:52: test
src/Serve
PS
I'm using HL/JIT 1.6.0 and Haxe 4.0.0-preview.4+1e3e5e016
from hashlink.
from hashlink.
@ncannasse Thank you for your help. Would I ask what is the best way to share Array between two threads ? At the moment I've got strange error , but when not used threads everything works:
Uncaught exception: Error while waiting on socket
Called from sys.net.$Socket.select(C:\HaxeToolkit\haxe\std/hl/_std/sys/net/Socket.hx:245)
In details:
I have two classes SocketAcceptor and SocketReader running in separate threads . In SocketAcceptor I bind to server ip and port . Here is part of the code , where connections : Array<Socket>
var select = Socket.select([server], null, null, 0);
for (socket in select.read)
{
var client : Socket = socket.accept();
client.setBlocking(false);
connections.push(client);
}
In SocketReader i have:
if ( socketAcceptor.connections.length > 0 )
{
var select = Socket.select(socketAcceptor.connections, null, null,0);
for (socket in select.read)
{
try
{
bytesReceived = socket.input.readBytes(buffer, 0, buffer.length);
trace(bytesReceived+" , "+buffer);
} catch (e:Eof) {
socket.close();
socketAcceptor.connections.remove(socket);
}
}
}
but everything stops after Socket.select(socketAcceptor.connections, ..... ) and return the error above
Same code when it is in one thread works. In java I can use synchronize(...) , but I'm not sure is it necessary to use some synchronization here ?
Thank you again for your time.
from hashlink.
@flashultra you can use hl.vm.Mutex to ensure two threads don't modify the array at the same time
from hashlink.
Related Issues (20)
- Linking to libopenal.a causes duplicated symbol error
- Recent change breaks hashlink builds. HOT 3
- macOS libhl.dylib path causes crash HOT 1
- The Hashlink profiler causes an exception in Process.stdout.readLine
- The Hashlink GC can block the main thread if sys.thread.Thread is used HOT 1
- Get live objects from a specific type
- JIT ERROR 0 mixing hl.UI8 and Float HOT 2
- Explicit constant Float to Int type conversion HOT 1
- Game controller not recognized HOT 10
- --debug is faster to run than non debug. HOT 2
- HL thread hangs when using trace() and semaphores HOT 2
- Can't run any .hl file on Mac M1 - Segmentation Fault HOT 2
- Unexpected value conversion when using an abstract class with type param
- Any way to get hl.Bytes from an hl.NativeArray? HOT 1
- Single and hl.F32 function args for callMethod stack issue HOT 1
- With SDL backend on Windows, adding an element to a SpriteBatch can cause a large lag spike HOT 2
- src/module.c(567) : FATAL ERROR : Failed to load function sdl@gl_polygon_offset HOT 3
- Macbook M1 Support HOT 2
- JIT/VM calls incorrect method HOT 3
- Unable to build C output on Windows gcc HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashlink.