Giter Club home page Giter Club logo

Comments (17)

ncannasse avatar ncannasse commented on August 24, 2024 7

Threads support has been merged.

from hashlink.

ncannasse avatar ncannasse commented on August 24, 2024 3

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.

ncannasse avatar ncannasse commented on August 24, 2024 3

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.

ncannasse avatar ncannasse commented on August 24, 2024

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.

ncannasse avatar ncannasse commented on August 24, 2024

Progress in #123

from hashlink.

sonygod avatar sonygod commented on August 24, 2024

is possible use hashlink replace Neko as socket server now?
@ncannasse

from hashlink.

ncannasse avatar ncannasse commented on August 24, 2024

@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.

sonygod avatar sonygod commented on August 24, 2024

could you build the last hashlink 1.6? binary for download?
it's seem haxe last build do not support hashlink1.5,

from hashlink.

ncannasse avatar ncannasse commented on August 24, 2024

Yes I am planning to release 1.6 soon

from hashlink.

sonygod avatar sonygod commented on August 24, 2024

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.

ncannasse avatar ncannasse commented on August 24, 2024

@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.

flashultra avatar flashultra commented on August 24, 2024

@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.

ncannasse avatar ncannasse commented on August 24, 2024

@flashultra use hl.vm.Thread.create(function() { .... })

from hashlink.

flashultra avatar flashultra commented on August 24, 2024

@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.

ncannasse avatar ncannasse commented on August 24, 2024

from hashlink.

flashultra avatar flashultra commented on August 24, 2024

@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.

ncannasse avatar ncannasse commented on August 24, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.