digitalmars.D.announce - I'm porting some go code to D
- Rory McGuire (109/109) Aug 23 2013 So I'm porting so #golang code to #dlang and there is all these blasted
- Jesse Phillips (5/11) Aug 23 2013 I'd suggest posting long snippets of code to
- Rory McGuire (4/12) Aug 24 2013 color friendly solution.
- David (2/2) Aug 24 2013 Daemonic Threads often end with a segfault, so if your main thread
- Rory McGuire (2/4) Aug 24 2013 Thanks, I wonder what they're accessing that they shouldn't.
- Sean Kelly (4/6) Sep 20 2013 By default, sure. But with daemon threads you really want to have some ...
- Moritz Maxeiner (5/21) Aug 24 2013 I'm probably missunderstanding somehting about the TLS model, but
- Rory McGuire (13/30) Aug 24 2013 what I know, for something like this, shouldn't you make the class insta...
- David Nadlinger (8/12) Aug 24 2013 It's a cute idea, but not really practical, I'm afraid –
- bearophile (5/8) Aug 24 2013 Can't Rory McGuire add a scheduler to his code? How much code
- Jonas Drewsen (4/10) Aug 24 2013 It would be very nice to have a builtin scheduler in the runtime.
- Rory McGuire (14/22) Aug 24 2013 nes are
- Paulo Pinto (12/33) Aug 24 2013 Funny, I always thought otherwise, because Windows only has
- Rory McGuire (9/15) Aug 25 2013 across the whole Processes -> LinuxThreads -> NPTL -> Posix Threads
- Rory McGuire (38/46) Aug 25 2013 nes are
- Timon Gehr (2/6) Aug 25 2013 You want 'shared static this' instead to avoid a race condition.
- Rory McGuire (9/17) Aug 25 2013 Awesome thanks, thought what I did there was dodgy. It was really weird
- Rory McGuire (17/38) Sep 18 2013 I've made the scheduler a bit more inteligent and the channel
So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think? Fire away :). import std.stdio; import core.thread : Thread; import core.sync.mutex : Mutex; import core.time; void main() { auto ch = chan!int(1); go!({ foreach (i; 22..44) { ch._ = i; } }); foreach (i; 0..10) { writeln("pop: ", ch._); } } // Implementation void go(alias F)() { auto t = new Thread(F); t.isDaemon(true); // we don't care if this thread dies. t.start(); } /** * chan allows messaging between threads without having to deal with locks, similar to how chan works in golang */ class chan_(T) { shared Mutex lock; struct Container(T) { T value; Container!T* next; } shared Container!T* buf; shared Container!T* last; shared size_t length; shared void insert(shared T v) { shared Container!T* newItem = new shared Container!T(); newItem.value = v; if (buf is null) { buf = newItem; last = newItem; } else { last.next = newItem; last = newItem; } length++; } shared T popFront() { T ret; synchronized (lock) { ret = buf.value; buf = buf.next; length--; } return ret; } size_t maxItems; bool blockOnFull = false; this(int maxItems = 1024, bool blockOnFull = true) { lock = cast(shared)new Mutex; length = 0; this.maxItems = maxItems; this.blockOnFull = blockOnFull; } property shared void _(T value) { bool done; while(true) { synchronized(lock) { if (!done && length < maxItems) { insert(value); done = true; } else if (!blockOnFull) { throw new ChannelFull("Channel Full"); } if (length <= maxItems-1) { break; } } Thread.sleep(dur!"msecs"(5)); } } property shared T _() { while(true) { size_t len; synchronized(lock) { len = length; } if (len > 0) { break; } Thread.sleep(dur!"msecs"(5)); }; auto r = popFront(); return r; } } auto chan(T)(int n, bool blockOnFull = true) { return cast(shared)new chan_!T(n, blockOnFull); } class ChannelFull : Exception { this(string msg, string file = __FILE__, ulong line = cast(ulong)__LINE__, Throwable next = null) { super(msg,file,line,next); } }
Aug 23 2013
On Friday, 23 August 2013 at 23:54:55 UTC, Rory McGuire wrote:So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think? Fire away :).I'd suggest posting long snippets of code to https://gist.github.com/ or http://dpaste.dzfl.pl/ A couple lines is fine but whole implementations, definitely want a more color friendly solution.
Aug 23 2013
On 24 Aug 2013 03:15, "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> wrote:On Friday, 23 August 2013 at 23:54:55 UTC, Rory McGuire wrote:http://dpaste.dzfl.pl/So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think? Fire away :).I'd suggest posting long snippets of code to https://gist.github.com/ orA couple lines is fine but whole implementations, definitely want a morecolor friendly solution. Good point.
Aug 24 2013
Daemonic Threads often end with a segfault, so if your main thread exists, the other threads will probably segfault.
Aug 24 2013
On 24 Aug 2013 11:00, "David" <d dav1d.de> wrote:Daemonic Threads often end with a segfault, so if your main thread exists, the other threads will probably segfault.Thanks, I wonder what they're accessing that they shouldn't.
Aug 24 2013
On Aug 24, 2013, at 1:59 AM, David <d dav1d.de> wrote:Daemonic Threads often end with a segfault, so if your main thread exists, the other threads will probably segfault.By default, sure. But with daemon threads you really want to have some = kind of shutdown mechanism inside a static dtor somewhere. The goal is = more to have threads that don't implicitly block app shutdown.=
Sep 20 2013
On Friday, 23 August 2013 at 23:54:55 UTC, Rory McGuire wrote:So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think? Fire away :). /** * chan allows messaging between threads without having to deal with locks, similar to how chan works in golang */ class chan_(T) { shared Mutex lock; struct Container(T) { T value; Container!T* next;I'm probably missunderstanding somehting about the TLS model, but from what I know, for something like this, shouldn't you make the class instance itself go into shared storage instead instead of all the members?
Aug 24 2013
On 24 Aug 2013 11:25, "Moritz Maxeiner" <moritz ucworks.org> wrote:On Friday, 23 August 2013 at 23:54:55 UTC, Rory McGuire wrote:locks,So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think? Fire away :). /** * chan allows messaging between threads without having to deal withwhat I know, for something like this, shouldn't you make the class instance itself go into shared storage instead instead of all the members?similar to how chan works in golang */ class chan_(T) { shared Mutex lock; struct Container(T) { T value; Container!T* next;I'm probably missunderstanding somehting about the TLS model, but fromI have no idea if shared on the class makes all it's parts shared. It was a struct but I had problems with passing it to the spawn func when I was using std.concurrent . I'm trying to port a cassandra cql library from go so it's really just to help with that. I'm having a hard time imagining how to implement select from go. Could probably use std.concurrent.receive because that can handle multiple types at once. Sorry I'm rambling...
Aug 24 2013
On Friday, 23 August 2013 at 23:54:55 UTC, Rory McGuire wrote:So I'm porting so #golang code to #dlang and there is all these blasted "go" statements.So I thought I'd give implmenting it in D a shot. What do you guys think?It's a cute idea, but not really practical, I'm afraid – Goroutines are managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads. Thus, if the Go application you are porting uses many Goroutines (and the Go code I've seen usually does so very liberally), the performance of the D equivalent is going to be horrible. David
Aug 24 2013
David Nadlinger:It's a cute idea, but not really practical, I'm afraid – Goroutines are managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads.Can't Rory McGuire add a scheduler to his code? How much code does it take? Bye, bearophile
Aug 24 2013
On Saturday, 24 August 2013 at 13:26:32 UTC, bearophile wrote:David Nadlinger:It would be very nice to have a builtin scheduler in the runtime. It would make async non-threaded programming so much nicer in D. /JonasIt's a cute idea, but not really practical, I'm afraid – Goroutines are managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads.Can't Rory McGuire add a scheduler to his code? How much code does it take?
Aug 24 2013
On Sat, Aug 24, 2013 at 3:26 PM, bearophile <bearophileHUGS lycos.com>wrote= :David Nadlinger: It's a cute idea, but not really practical, I'm afraid =E2=80=93 Gorouti=nes aree?managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads.Can't Rory McGuire add a scheduler to his code? How much code does it tak=Bye, bearophileI imagine that it will be fine on Linux because threads truly are lightweight on Linux, but its not going to be great on windows. Go's scheduler is basically the same as the way vibe.d works. Fibers. I haven't tried it but I could use fibers to do the same sort of thing, though it would be easiest to use in Vibe.d because even in go you have to do something that will wait/sleep in order to have your program advance unless you set GOPROCS to a value greater than 1. Go's scheduler is not preemptive so there are the equivalent of Fiber.yield() spread throughout the standard library, I think. I'm not sure how threads + fibers would have to interact in such a system.
Aug 24 2013
On Saturday, 24 August 2013 at 20:03:58 UTC, Rory McGuire wrote:On Sat, Aug 24, 2013 at 3:26 PM, bearophile <bearophileHUGS lycos.com>wrote:Funny, I always thought otherwise, because Windows only has threads. Processes are just a means of grouping threads on Windows, as there isn't the distinction between threads and processes that UNIX systems used to make. Then again, I lost track how the performance on Linux systems changed across the whole Processes -> LinuxThreads -> NPTL -> Posix Threads evolution. -- PauloDavid Nadlinger: It's a cute idea, but not really practical, I'm afraid – Goroutines areI imagine that it will be fine on Linux because threads truly are lightweight on Linux, but its not going to be great on windows.managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads.Can't Rory McGuire add a scheduler to his code? How much code does it take? Bye, bearophile
Aug 24 2013
On 24 Aug 2013 23:55, "Paulo Pinto" <pjmp progtools.org> wrote:Funny, I always thought otherwise, because Windows only has threads. Processes are just a means of grouping threads on Windows, as there isn't the distinction between threads and processes that UNIX systemsused to make.Then again, I lost track how the performance on Linux systems changedacross the whole Processes -> LinuxThreads -> NPTL -> Posix Threads evolution.-- PauloJust did a quick search to see if I was right. I'm not sure if it's still the case but it appears that is actually the scheduler that causes windows to be much slower than Linux. Ideally one would want a hybrid of threads and fibres anyway. I wonder how much of the standard library would need to change.
Aug 25 2013
On Sat, Aug 24, 2013 at 3:26 PM, bearophile <bearophileHUGS lycos.com>wrote= :David Nadlinger: It's a cute idea, but not really practical, I'm afraid =E2=80=93 Gorouti=nes aree?managed by a scheduler in the Go runtime library, whereas D threads directly map to OS threads.Can't Rory McGuire add a scheduler to his code? How much code does it tak=Bye, bearophileHere is a basic co-operative scheduler based version of go!(F) that uses a channel to keep things simple, its co-op so Fiber.yield has to be called at some point. If the channel detects its in a Fiber it calls yield if there is nothing available in it: void go(alias F)() { scheduler._ =3D new Fiber(F); } shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice static this () { if (scheduler is null) { scheduler =3D makeChan!Fiber(100); // create the workers auto goprocs =3D environment.get("GOPROCS"); int num_threads =3D 1; if (goprocs !=3D null) { num_threads =3D to!int(goprocs); } foreach (i; 0..num_threads) { // create threads that process the live fibers auto t =3D new Thread(() { for (;;) { auto fiber =3D scheduler._; fiber.call(); if (fiber.state !=3D Fiber.State.TERM) { scheduler._ (fiber); } } }); t.start(); } } } Just need to figure out a way to detect when main() has exited.
Aug 25 2013
On 08/26/2013 12:55 AM, Rory McGuire wrote:shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice static this () { if (scheduler is null) {You want 'shared static this' instead to avoid a race condition.
Aug 25 2013
Awesome thanks, thought what I did there was dodgy. It was really weird when this() started multiple schedulers at least now I see the obvious reason. BTW: gist is at: https://gist.github.com/rjmcguire/6336931 Could someone point me at the correct linked list to use inside the channel. I'd prefer to use a range container of some kind if it exists in the std lib. I tried SList and had a bad experience hence the custom implementation. On Mon, Aug 26, 2013 at 1:21 AM, Timon Gehr <timon.gehr gmx.ch> wrote:On 08/26/2013 12:55 AM, Rory McGuire wrote:shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice static this () { if (scheduler is null) {You want 'shared static this' instead to avoid a race condition.
Aug 25 2013
I've made the scheduler a bit more inteligent and the channel implementation is now backed by a lock free queue. https://github.com/rjmcguire/goport/blob/tip/wrapper2.d for a example using libev https://github.com/rjmcguire/goport/blob/tip/goroutine.d for the "go" routines https://github.com/rjmcguire/goport/blob/tip/channel.d channel implementation, the channel still has a read lock because I didn't like looping wasting cpu cycles/battery, I may change this to use the same loop as the select(...) expression with a timeout that backs off. https://github.com/rjmcguire/goport/blob/tip/concurrentlinkedqueue.d queue implementation there is a problem with the file EV handler in that it can get spurios READ events and I can't seem to set it to non-blocking. The select(...) implementation and usage is quite interesting because it works very much like Go's select statement. On Mon, Aug 26, 2013 at 1:28 AM, Rory McGuire <rjmcguire gmail.com> wrote:Awesome thanks, thought what I did there was dodgy. It was really weird when this() started multiple schedulers at least now I see the obvious reason. BTW: gist is at: https://gist.github.com/rjmcguire/6336931 Could someone point me at the correct linked list to use inside the channel. I'd prefer to use a range container of some kind if it exists in the std lib. I tried SList and had a bad experience hence the custom implementation. On Mon, Aug 26, 2013 at 1:21 AM, Timon Gehr <timon.gehr gmx.ch> wrote:On 08/26/2013 12:55 AM, Rory McGuire wrote:shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice static this () { if (scheduler is null) {You want 'shared static this' instead to avoid a race condition.
Sep 18 2013