diff --git a/std/concurrency.d b/std/concurrency.d index 43b5b629b..cc658cebe 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -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 ////////////////////////////////////////////////////////////////////////////// @@ -313,14 +326,6 @@ class MailboxFull : Exception */ struct Tid { - void send(T...)( T vals ) - { - static assert( !hasLocalAliasing!(T), - "Aliases to mutable thread-local data not allowed." ); - _send( this, vals ); - } - - private: this( MessageBox m ) { @@ -343,6 +348,34 @@ private: 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