Java Continuations and GreenThreads

Whoa, this is seriously cool. I will be checking this out in-depth.

Thanks a ton! :slight_smile:
Bach

These warnings should now also be fixed (copy & paste bug).

While debugging these a potential issue with the JavaAgent was discovered:
It is possible that multiple threads load classes at the same time, which also means that these thread execute instrumentClass in parallel.
I did not add a synchronization to that method (to prevent potential dead locks with ClassLoader locks) as MethodDatabase might need to load additional classes (eg the base class) - so instead I protected the shared hash maps individually.
This might cause redundant work (if multiple threads analyze the same class) - but each thread should produce the same results or a warning is generated.

So I suggest to use the JavaAgent only while developing - and keep an eye on the console for warning :slight_smile:

It took a while to squash that bug, but together we persevered 8)

Version 0.8.5 has been released.

Download files

Iā€™m writing to express my thoughts on how awesome this really is. This library opens up a whole new dimension for game logic writing. Iā€™ll be adding this library into my engine very soon! (with credits of course!) ;D

Confirmed warnings are gone and it is still working as it should. :slight_smile:

Just wanted to say that this works extremely well. Been implementing it into a little ā€œprototypingā€ framework Iā€™m building and Iā€™m having a blast working with it.

Thanks heaps!
Bach

With two users (and counting) this is without a doubt my most successful library (wrapper)! 8)

Itā€™s justā€¦ Too high for meā€¦ need to learn more scale / scheme (especially) or haskell firstā€¦ Continuations? I just canā€™t deal with something like that right nao :smiley:

Imagine that ā€œreturnā€ is a function you call instead of some hardwired stack behavior. Thatā€™s all a continuation is, the ā€œreturnā€ function. The convention in languages that use a continuation passing style is that the compiler has the caller pass in the continuation. In CPS, you never pop the stack, you garbage collect it. Java doesnā€™t do any of this, so so a continuation library is essentially faking continuations as best it can.

You donā€™t need to know any of that to gain the benefits of green threads though.

Add me to the user list. It looks very interesting. Always was looking for something like this. Havenā€™t gotten around to playing with it, but will do so in the weekends.

Version 0.8.6 has been released.

Bug fixes
I found an issue with the callsite of certain constructors and Matthias solved it immediately by letting the instrumentation code emit an exception. Matthias is looking into a permanent solution, but for now itā€™s good to have the instrumentator notify you of the edge case where it cannot produce valid continuationsā€¦ yet. More info on this later, until then, download the latest version and pay attention to any warnings printed on the console.

Features


@@		final VirtualExchanger<String> ex = new VirtualExchanger<>();

		new VirtualThread(new VirtualRunnable() {
			@Override
			public void run() throws SuspendExecution {
@@				String got = ex.exchange("object1a");
				System.out.println("thread1 got: " + got);
				
@@				got = ex.exchange("object1b");
				System.out.println("thread1 got: " + got);
			}
		}).start();

		new VirtualThread(new VirtualRunnable() {
			@Override
			public void run() throws SuspendExecution {
@@				String got = ex.exchange("object2a");
				System.out.println("thread2 got: " + got);
				
@@				got = ex.exchange("object2b");
				System.out.println("thread2 got: " + got);
			}
		}).start();


// exchange #1
thread2 got: object1a
thread1 got: object2a

// exchange #2
thread1 got: object2b
thread2 got: object1b

Download files

I am currently using the Matthias continuations library and have found that it has greatly simplified my AI and network code. Perhaps i am a bit slow, but what have you added from that original lib? Is it just a green threads implementation?

Canā€™t you already run many many native threads in linux for example anyway (aka the old thread per socket web server or a Selector).

You can run many threads on any OS; the problem is that you are then dealing with synchronization and non-deterministic behaviour. And on ARM you even have significant memory barrier overhead as the ARM architecture doesnā€™t enforce memory consistency between cores.

Cas :slight_smile:

Itā€™s ā€˜justā€™ that, yes. If you browse through the code youā€™ll see that ā€˜justā€™ is relative :slight_smile:

I wrote this highlevel library on top of the continuations library, so that the concepts (processor, thread, sleep, lock, etc) are familiar for anybody having minimal experience with threading. Writing a proper scheduler (the VirtualProcessor) is not quite trivial, and it took me a few iterations to get it to where it is now. I think itā€™s good to share the code, so others donā€™t have to reinvent the wheel, lowering the bar for usage of Matthias Mannā€™s ingenious library.

What Cas said, and your native threads need at least a 256K stack size to do anything non trivial. When youā€™d run the AI of every unit ingame on a native thread youā€™d quickly run out of memory.

I added another update. Besides fixing methods which return ā€˜longā€™ - this version adds support to call suspendable methods as arguments for constructors (constructors itself are not suspendable).

This now allows constructs like this:

System.out.println(new StringBuilder(someSuspendableMethodWhichReturnString()).append(" Bla").toString());
// Eclipse generates the above code for the following Java statement:
System.out.println(someSuspendableMethodWhichReturnString()+" Bla");

Version 0.8.7 has been released.

Bug fixes

  • See Matthiasā€™s post.

Download files

Anyone tried making a Server Client model with green thread clients?

Where does it lie in scalability between old Client/Thread and nio channels?

I started a HTTP client back in 2008 when I first wrote the lib, it is basically working - but I didnā€™t do much testing on it.

Most scalable servers use a model that isnā€™t thread-per-client, but they tend to use nio, which is probably not what you meant. JRockit uses a hybrid green/native model, and that runs some pretty heavy duty servers.

Well for that matter Ericsson has been since the mid-80s. Highly reliable and heavy workload.