From dd9b31ccaef70915c7a2b18b05d9f699fbb4ae5f Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Wed, 23 Jan 2013 00:42:23 +0100 Subject: [PATCH 1/2] Fixes Issue 6224 - Add ownerTid() property. --- std/concurrency.d | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/std/concurrency.d b/std/concurrency.d index 740c24b6a..1ace12275 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -303,6 +303,18 @@ class MailboxFull : Exception } +/** + * Thrown when $(D ownerTid) doesn't find an owner thread. + */ +class OwnerTidMissing : Exception +{ + this(string msg, string file = __FILE__, size_t line = __LINE__) + { + super(msg, file, line); + } +} + + ////////////////////////////////////////////////////////////////////////////// // Thread ID ////////////////////////////////////////////////////////////////////////////// @@ -343,6 +355,34 @@ private: return Tid( mbox ); } +/** + * Return the Tid of the thread which + * spawned the caller's thread. + * + * Throws: A $(D OwnerTidMissing) exception if + * there is no owner thread. + */ +@property Tid ownerTid() +{ + enforceEx!OwnerTidMissing(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!OwnerTidMissing(ownerTid); + auto child = spawn(&fun); + child.send("Main calling"); + string res = receiveOnly!string(); + assert(res == "Child responding"); +} ////////////////////////////////////////////////////////////////////////////// // Thread Creation From 59e894698f48453f9b8bdc430884f99ed35f9eec Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Wed, 23 Jan 2013 00:56:08 +0100 Subject: [PATCH 2/2] Remove Tid.send() duplicated function, as UFCS function can take its place. --- std/concurrency.d | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/std/concurrency.d b/std/concurrency.d index 1ace12275..7e16872ff 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -304,9 +304,10 @@ class MailboxFull : Exception /** - * Thrown when $(D ownerTid) doesn't find an owner thread. + * Thrown when a Tid is missing, e.g. when $(D ownerTid) doesn't + * find an owner thread. */ -class OwnerTidMissing : Exception +class TidMissingException : Exception { this(string msg, string file = __FILE__, size_t line = __LINE__) { @@ -325,14 +326,6 @@ class OwnerTidMissing : 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 ) { @@ -359,12 +352,12 @@ private: * Return the Tid of the thread which * spawned the caller's thread. * - * Throws: A $(D OwnerTidMissing) exception if + * Throws: A $(D TidMissingException) exception if * there is no owner thread. */ @property Tid ownerTid() { - enforceEx!OwnerTidMissing(owner.mbox !is null, "Error: Thread has no owner thread."); + enforceEx!TidMissingException(owner.mbox !is null, "Error: Thread has no owner thread."); return owner; } @@ -377,7 +370,7 @@ unittest ownerTid.send("Child responding"); } - assertThrown!OwnerTidMissing(ownerTid); + assertThrown!TidMissingException(ownerTid); auto child = spawn(&fun); child.send("Main calling"); string res = receiveOnly!string();