Merge pull request #1092 from AndrejMitrovic/Fix6224

Issue 6224 - Add ownerTid() property.
This commit is contained in:
Alex Rønne Petersen 2013-03-08 09:53:48 -08:00
commit 9cb2065fe6

View file

@ -303,6 +303,19 @@ class MailboxFull : Exception
} }
/**
* Thrown when a Tid is missing, e.g. when $(D ownerTid) doesn't
* find an owner thread.
*/
class TidMissingException : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__)
{
super(msg, file, line);
}
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Thread ID // Thread ID
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -313,14 +326,6 @@ class MailboxFull : Exception
*/ */
struct Tid struct Tid
{ {
void send(T...)( T vals )
{
static assert( !hasLocalAliasing!(T),
"Aliases to mutable thread-local data not allowed." );
_send( this, vals );
}
private: private:
this( MessageBox m ) this( MessageBox m )
{ {
@ -343,6 +348,34 @@ private:
return Tid( mbox ); return Tid( mbox );
} }
/**
* Return the Tid of the thread which
* spawned the caller's thread.
*
* Throws: A $(D TidMissingException) exception if
* there is no owner thread.
*/
@property Tid ownerTid()
{
enforceEx!TidMissingException(owner.mbox !is null, "Error: Thread has no owner thread.");
return owner;
}
unittest
{
static void fun()
{
string res = receiveOnly!string();
assert(res == "Main calling");
ownerTid.send("Child responding");
}
assertThrown!TidMissingException(ownerTid);
auto child = spawn(&fun);
child.send("Main calling");
string res = receiveOnly!string();
assert(res == "Child responding");
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Thread Creation // Thread Creation