Skip to content

Commit

Permalink
enforce thisTid was called before calling receive
Browse files Browse the repository at this point in the history
- Simply calling receive in main as stated by the comment
  makes no sense, as nobody could send a message.
  • Loading branch information
MartinNowak committed Sep 30, 2013
1 parent d9073b3 commit a0c7d83
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions std/concurrency.d
Expand Up @@ -190,21 +190,6 @@ private
Tid owner;
}

private shared bool firstInitialization = true;

static this()
{
// NOTE: Normally, mbox is initialized by spawn() or thisTid(). This
// doesn't support the simple case of calling only receive() in main
// however. To ensure that this works, initialize the main thread's
// mbox field here only the first time this is run.
if (firstInitialization)
{
mbox = new MessageBox;
firstInitialization = false;
}
}


static ~this()
{
Expand Down Expand Up @@ -639,6 +624,12 @@ private void _send(T...)( MsgType type, Tid tid, T vals )
* ---
*/
void receive(T...)( T ops )
in
{
assert(mbox !is null, "Cannot receive a message until a thread was spawned "
"or thisTid was passed to a running thread.");
}
body
{
checkops( ops );
mbox.get( ops );
Expand Down Expand Up @@ -715,6 +706,12 @@ private template receiveOnlyRet(T...)
* ---
*/
receiveOnlyRet!(T) receiveOnly(T...)()
in
{
assert(mbox !is null, "Cannot receive a message until a thread was spawned "
"or thisTid was passed to a running thread.");
}
body
{
Tuple!(T) ret;

Expand Down Expand Up @@ -776,6 +773,12 @@ unittest
message and $(D false) if it timed out waiting for one.
+/
bool receiveTimeout(T...)( Duration duration, T ops )
in
{
assert(mbox !is null, "Cannot receive a message until a thread was spawned "
"or thisTid was passed to a running thread.");
}
body
{
checkops( ops );
return mbox.get( duration, ops );
Expand Down

0 comments on commit a0c7d83

Please sign in to comment.