-
-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 12090 - Make std.concurrency compatible with fibers as threads #1910
Issue 12090 - Make std.concurrency compatible with fibers as threads #1910
Conversation
@complexmath Needs rebase |
ping. Still needs rebasing. |
test failures, rebase |
rebased On May 19, 2014, at 5:15 PM, Andrei Alexandrescu [email protected] wrote:
|
Seems to include some unrelated commits right now. |
Well how the heck did that happen? Should I submit a new pull request, or does someone know how to remove those? |
Try this: #1153 (comment) |
That worked. Thanks! |
Now it has two commits with identical title (first one being trivial changes) ;) You probably want to squash it all together later. Anyway, going to review this soon! Also pinging @s-ludwig |
|
||
interface Scheduler | ||
{ | ||
void start( void delegate() op ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't appear to be used anywhere but in the implementations of spawn
, maybe it should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what it's for. The Scheduler allows different concurrency mechanisms to back the threading in std.concurrency. The start function is for creating these threads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the implementations of ThreadScheduler.spawn
and FiberScheduler.spawn
. If it was used in std.concurrency.spawn
then that might serve as an example of its utility, but as it is, it looks like Scheduler.spawn
is the only required primitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, this definitely needs to be documented because it isn't evident from the code. start() isn't strictly required in many cases. That's intended to be called in Dmain so everything, even the main thread, is run by the scheduler. This is necessary for vibe.d, for example, but many other types of schedulers will work without it.
This adds public but undocumented types along with the apparently user-facing |
Scheduler should really be documented. Though the two implemented examples were meant to serve as documentation of their own. |
I've added some documentation. Let me know how it looks. |
Can you please add some examples of using import std.concurrency;
import core.thread;
import std.stdio;
void spawned(Tid tid)
{
for (;;)
{
writeln("waiting for numbers");
scheduler.yield();
receive(
(int i) { writefln("received number %s", i); }
);
}
}
void main()
{
scheduler = new FiberScheduler();
auto tid = spawn(&spawned, thisTid);
writefln("main after spawn");
foreach (i; 0..5)
{
writefln("sending %s", i);
tid.send(i);
scheduler.yield();
}
} It works as I expected it to work when Another thing is that I am worried about Sorry for "soon" being so long :) |
Try import std.concurrency;
import core.thread;
import std.stdio;
void spawned(Tid tid)
{
for (;;)
{
writeln("waiting for numbers");
scheduler.yield(); // explicit yield is unnecessary as receive will yield if it blocks, but can't hurt
receive(
(int i) { writefln("received number %s", i); }
);
}
}
void main()
{
scheduler = new FiberScheduler();
scheduler.start({
auto tid = spawn(&spawned, thisTid);
writefln("main after spawn");
foreach (i; 0..5)
{
writefln("sending %s", i);
tid.send(i);
scheduler.yield();
}
});
} scheduler.start() starts the dispatcher, which is necessary for FiberScheduler. yield() will start it as well, but that could lead to weird behavior as execution won't (currently) return to the yielding thread until all fibers terminate. I'll see about adding an example. |
Ah this is what has confused me. is this because of implementation difficulties or intentional? Anyway, should be mentioned in This works and should be probably added as documented unittest to module description as it makes obvious difference between fiber and thread schedulers: import core.thread;
import std.stdio;
void spawned(Tid tid)
{
bool finished = false;
for (;;)
{
receive(
(int i) { writefln("received number %s", i); },
(Tid t) { finished = true; }
);
if (finished)
return;
}
}
void main()
{
// scheduler = new FiberScheduler();
scheduler = new ThreadScheduler();
scheduler.start({
auto tid = spawn(&spawned, thisTid);
foreach (i; 0..5)
{
writefln("sending %s", i);
tid.send(i);
scheduler.yield();
}
tid.send(thisTid);
});
} |
Also, @s-ludwig - your feedback is still necessary :P |
Your comment about having a standalone yield got me thinking. I have another pull request blocked by this one that was going to add a standalone yield for another purpose, and I've realized that for it to work correctly, the user should never call scheduler.yield() explicitly. So I think it's a good idea to get the standalone yield in right now, and I may just merge the two pull requests for this reason even though they aren't strictly otherwise related. I'll see about improving the documentation for Scheduler as well. I think scheduler.start() should always be called in main even if some schedulers don't require it just for the sake of simplicity. |
I'll try to find some time to build a custom DMD/Phobos and make a demo integration for vibe.d over the next days, as that's the only definitive way to find out the last possible issues, I guess. Just a little technical documentation comment: There are some doc comments without a short summary paragraph (see "Sections" at http://dlang.org/ddoc.html), which will result in some crowded overview tables in the DDOX documentation layout. |
Thanks @s-ludwig. From previous discussion, I've also merged in my other pull request with this one. This seemed appropriate since it affects the yield() implementation. I've also added a unittest that tests both the FiberScheduler and this new Generator type. |
Test failure because of "statement is not reachable" warning |
Random Win64 breakage? That errorlevel seems weird. |
I guess this means that fibers are broken on win64? Is there anyone that can look into this? |
They definitely were broken until recently: http://issues.dlang.org/show_bug.cgi?id=12800 |
Hrm. Then I'm at a loss to explain two successive test failures on Win64 only. It doesn't appear to be displaying any output at all from the test run on that platform. Could the failure be elsewhere? |
Getting vibe.d to compile on DMD master was a bit more involved than I had hoped for due to some regressions, but now I've got something that seems to work ( But If I see it right, tasks currently have to be started using *:
BTW, I'll try to reproduce the Win64 issue tomorrow. |
For Win64, I'm getting a different error code ( |
Got it to run finally. The crash is in |
@complexmath I have rebased your PR against current master and squashed everything in two commits : https://github.com/Dicebot/phobos/tree/complexmath-concurrency-pr You can reset your PR to that branch by doing this:
|
48f4c2b
to
1b73a4e
Compare
Done. Thanks for the help! |
Hm, it can't merge again. Needs yet another |
Oh FFS. So I did this: git fetch upstream master And this created a million changes. Instead of the "git merge master" step I guess I should have done something else? |
@complexmath Yeah.. instead just this would have worked:
No need to re-fetch or anything.
That should put your branch at the right commit as it was. |
59d3678
to
1b73a4e
Compare
https://d.puremagic.com/issues/show_bug.cgi?id=12090 With this commit fibers can have own message boxes and act similar to threads in that regard.
Generator is an extension to a Fiber that yields return values of specific type
1b73a4e
to
8fbad99
Compare
Thanks. For some reason I thought "git pull" would always result in the huge merge we were trying to avoid. |
@complexmath You are welcome. Going to auto-merge it unless something spooky happens. |
So |
I don't see any truly compelling reason to not merge it. If there are any issues with it will be easier to create separate PR's for those before release and figure those out on case by case basis. This has taken wa too long already, some sense of accomplishment is necessary :) |
Auto-merge toggled on |
Issue 12090 - Make std.concurrency compatible with fibers as threads
Couldn't resists to press the button myself :P |
It is other way around :) |
Yay! |
Awesome. Oh, the strange pleasure of having 6+ month old PR merged :) |
@DmitryOlshansky May be my new AA implementation will be merged somewhen=) |
@IgorStepanov other common durations are 8 month, and a year ;) |
Ok, I have to be patient:)
|
Write smaller pull requests and discuss design decisions upfront. Please never start a complex feature by implementing a pull request. So for the next time, please start with a DIP or a newsgroup discussion telling people what you want to achieve and how you intend to implement it. |
auto t = new Thread( &exec ); t.start(); | ||
links[spawnTid] = linked; | ||
if( scheduler !is null ) | ||
scheduler.spawn( &exec ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you simply initialize scheduler with a default 1:1 threading scheduler?
It's now possible to create and store classes in globals at CT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but this would add unnecessary overhead for the default case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a vtable right? More a matter of taste.
@MartinNowak I disagree that this specific feature is complex enough to keep bashing it again and again. Most of the linked issues are not really related to adding fiber support to std.concurrency but to possible system built on top of this. This specific addition was small and was working as intended, I see no point in keeping it stalling for 1 more year while discussing more abstract things. |
https://d.puremagic.com/issues/show_bug.cgi?id=12090