Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 82719 invoked from network); 3 Sep 2009 23:55:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Sep 2009 23:55:28 -0000 Received: (qmail 18033 invoked by uid 500); 3 Sep 2009 23:55:28 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 17977 invoked by uid 500); 3 Sep 2009 23:55:27 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 17968 invoked by uid 99); 3 Sep 2009 23:55:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Sep 2009 23:55:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Sep 2009 23:55:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2803323888E7; Thu, 3 Sep 2009 23:55:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r811174 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/main: Makefile.am decaf/lang/Thread.cpp decaf/lang/Thread.h decaf/lang/ThreadGroup.cpp decaf/lang/ThreadGroup.h Date: Thu, 03 Sep 2009 23:55:02 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090903235505.2803323888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Thu Sep 3 23:54:57 2009 New Revision: 811174 URL: http://svn.apache.org/viewvc?rev=811174&view=rev Log: More Thread Refactorings and cleanups, added skeleton ThreadGroup class. Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp (with props) activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h (with props) Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?rev=811174&r1=811173&r2=811174&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Thu Sep 3 23:54:57 2009 @@ -1113,6 +1113,7 @@ decaf/io/OutputStream.h \ decaf/io/Reader.h \ decaf/io/UTFDataFormatException.h \ + decaf/io/UnsupportedEncodingException.h \ decaf/io/Writer.h \ decaf/lang/Appendable.h \ decaf/lang/Boolean.h \ Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp?rev=811174&r1=811173&r2=811174&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp (original) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp Thu Sep 3 23:54:57 2009 @@ -27,6 +27,8 @@ #include #include +#include +#include #include #include #include @@ -65,13 +67,33 @@ */ bool joined; - public: + /** + * The Assigned name of this thread. + */ + std::string name; - ThreadProperties() : task( NULL ), threadHandle( NULL ), started( false ), joined( false ) { - } + /** + * The currently assigned priority + */ + int priority; - ThreadProperties( Runnable* task ) : task( task ), threadHandle( NULL ), started( false ), joined( false ) { - } + /** + * static value that holds the incrementing Thread ID for unnamed threads. + */ + static unsigned int id; + + /** + * The handler to invoke if the thread terminates due to an exception that + * was not caught in the user's run method. + */ + Thread::UncaughtExceptionHandler* exHandler; + + /** + * The Thread that created this Object. + */ + Thread* parent; + + public: static void* APR_THREAD_FUNC runCallback( apr_thread_t* self, void* param ) { @@ -81,11 +103,24 @@ // Invoke run on the task. try{ properties->task->run(); - } catch( ... ){ - RuntimeException ex( + } catch( decaf::lang::Throwable& error ){ + + if( properties->exHandler != NULL ) { + properties->exHandler->uncaughtException( properties->parent, error ); + } + + error.printStackTrace(); + + } catch( ... ) { + RuntimeException error( __FILE__, __LINE__, "unhandled exception bubbled up to Thread::run"); - ex.printStackTrace(); + + if( properties->exHandler != NULL ) { + properties->exHandler->uncaughtException( properties->parent, error ); + } + + error.printStackTrace(); } // Indicate we are done. @@ -98,27 +133,46 @@ }} //////////////////////////////////////////////////////////////////////////////// -Thread::Thread() { +unsigned int ThreadProperties::id = 0; - this->initialize( this ); +//////////////////////////////////////////////////////////////////////////////// +Thread::Thread() { + this->initialize( this, "" ); } //////////////////////////////////////////////////////////////////////////////// Thread::Thread( Runnable* task ) { + this->initialize( task, "" ); +} - if( task == NULL ) { - throw NullPointerException( - __FILE__, __LINE__, - "Thread::Thread( Runnable* ) Runnable instance passed was NULL" ); - } +//////////////////////////////////////////////////////////////////////////////// +Thread::Thread( const std::string& name ) { + this->initialize( this, name ); +} - this->initialize( task ); +//////////////////////////////////////////////////////////////////////////////// +Thread::Thread( Runnable* task, const std::string& name ) { + this->initialize( task, name ); } //////////////////////////////////////////////////////////////////////////////// -void Thread::initialize( Runnable* task ) { +void Thread::initialize( Runnable* task, const std::string& name ) { + + this->properties.reset( new ThreadProperties() ); + + if( name == "" ) { + this->properties->name = std::string( "Thread-" ) + Integer::toString( ++ThreadProperties::id ); + } else { + this->properties->name = name; + } - this->properties.reset( new ThreadProperties( task ) ); + this->properties->joined = false; + this->properties->started = false; + this->properties->priority = Thread::NORM_PRIORITY; + this->properties->task = task; + this->properties->threadHandle = NULL; + this->properties->exHandler = NULL; + this->properties->parent = this; } //////////////////////////////////////////////////////////////////////////////// @@ -173,7 +227,18 @@ } //////////////////////////////////////////////////////////////////////////////// -void Thread::sleep( int millisecs ) { +void Thread::sleep( long long millisecs ) + throw( lang::exceptions::InterruptedException, + lang::exceptions::IllegalArgumentException ) { + + Thread::sleep( millisecs, 0 ); +} + +//////////////////////////////////////////////////////////////////////////////// +void Thread::sleep( long long millisecs, unsigned int nanos ) + throw( lang::exceptions::InterruptedException, + lang::exceptions::IllegalArgumentException ) { + apr_sleep( (apr_interval_time_t)(millisecs * 1000) ); } @@ -186,3 +251,50 @@ unsigned long Thread::getId() { return (unsigned long)( apr_os_thread_current() ); } + +//////////////////////////////////////////////////////////////////////////////// +void Thread::setName( const std::string& name ) { + this->properties->name = name; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string Thread::getName() const { + return this->properties->name; +} + +//////////////////////////////////////////////////////////////////////////////// +void Thread::setPriority( int value ) throw( decaf::lang::exceptions::IllegalArgumentException ) { + + if( value < Thread::MIN_PRIORITY || value > Thread::MAX_PRIORITY ) { + throw IllegalArgumentException( + __FILE__, __LINE__, + "Thread::setPriority - Specified value {%d} is out of range", value ); + } + + this->properties->priority = value; + + // TODO - Alter Threads actual priority. +} + +//////////////////////////////////////////////////////////////////////////////// +int Thread::getPriority() const { + return this->properties->priority; +} + +//////////////////////////////////////////////////////////////////////////////// +void Thread::setUncaughtExceptionHandler( UncaughtExceptionHandler* handler ) { + this->properties->exHandler = handler; +} + +//////////////////////////////////////////////////////////////////////////////// +const Thread::UncaughtExceptionHandler* Thread::getUncaughtExceptionHandler() const { + return this->properties->exHandler; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string Thread::toString() const { + + return this->properties->name + ": Priority=" + + Integer::toString( this->properties->priority ) + ", ThreadID=" + + Long::toString( Thread::getId() ); +} Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h?rev=811174&r1=811173&r2=811174&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h (original) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h Thu Sep 3 23:54:57 2009 @@ -17,6 +17,8 @@ #ifndef _DECAF_LANG_THREAD_H_ #define _DECAF_LANG_THREAD_H_ +#include +#include #include #include #include @@ -25,6 +27,7 @@ namespace decaf{ namespace lang{ + class ThreadGroup; class ThreadProperties; /** @@ -63,17 +66,77 @@ public: + /** The minimum priority that a thread can have. */ + static const int MIN_PRIORITY = 1; + + /** The default priority that a thread is given at create time. */ + static const int NORM_PRIORITY = 5; + + /** The maximum priority that a thread can have. */ + static const int MAX_PRIORITY = 10; + + public: + /** - * default Constructor + * Interface for handlers invoked when a Thread abruptly terminates due to an + * uncaught exception. + */ + class UncaughtExceptionHandler { + public: + + /** + * Method invoked when the given thread terminates due to the given uncaught exception. + * + * This method is defined to indicate that it will not throw an exception, throwing + * and exception from this method will on most systems result in a segmentation fault. + */ + virtual void uncaughtException( const Thread* thread, const Throwable& error ) throw() = 0; + + }; + + public: + + /** + * Constructs a new Thread. This constructor has the same effect as + * Thread( NULL, NULL, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. + * When no name is given the name is automatically generated and are of the form + * "Thread-"+n, where n is an integer. */ Thread(); /** - * Constructor - * @param task the Runnable that this thread manages + * Constructs a new Thread with the given target Runnable task. This constructor + * has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a + * newly generated name. When no name is given the name is automatically generated + * and are of the form "Thread-"+n, where n is an integer. + * + * @param task the Runnable that this thread manages, if the task is NULL the Thread's + * run method is used instead. */ Thread( Runnable* task ); + /** + * Constructs a new Thread with the given name. This constructor has the same effect + * as Thread( NULL, NULL, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. + * When no name is given the name is automatically generated and are of the form + * "Thread-"+n, where n is an integer. + * + * @param name the name to assign to this Thread. + */ + Thread( const std::string& name ); + + /** + * Constructs a new Thread with the given target Runnable task and name. This constructor + * has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a + * newly generated name. When no name is given the name is automatically generated + * and are of the form "Thread-"+n, where n is an integer. + * + * @param task the Runnable that this thread manages, if the task is NULL the Thread's + * run method is used instead. + * @param name the name to assign to this Thread. + */ + Thread( Runnable* task, const std::string& name ); + virtual ~Thread(); /** @@ -97,16 +160,94 @@ */ virtual void run(){}; + /** + * Returns the Thread's assigned name. + * @returns the Name of the Thread. + */ + std::string getName() const; + + /** + * Sets the name of the Thread to the new Name given by the argument name + * + * @paran name the new name of the Thread. + */ + void setName( const std::string& name ); + + /** + * Gets the currently set priority for this Thread. + * + * @return an int value representing the Thread's current priority. + */ + int getPriority() const; + + /** + * Sets the current Thread's priority to the newly specified value. The given value + * must be within the rane Thread::MIN_PRIORITY and Thread::MAX_PRIORITY. + * + * @param value the new priority value to assign to this Thread. + * + * @throws IllegalArgumentException if the value is out of range. + */ + void setPriority( int value ) throw( decaf::lang::exceptions::IllegalArgumentException ); + + /** + * Set the handler invoked when this thread abruptly terminates due to an uncaught exception. + * + * @returns a pointer to the set UncaughtExceptionHandler. + */ + const UncaughtExceptionHandler* getUncaughtExceptionHandler() const; + + /** + * Set the handler invoked when this thread abruptly terminates due to an uncaught exception. + * + * @param handler the UncaightExceptionHandler to invoke when the Thread terminates due + * to an uncaught exception. + */ + void setUncaughtExceptionHandler( UncaughtExceptionHandler* handler ); + + /** + * Returns a string that describes the Thread. + * + * @return string describing the Thread. + */ + std::string toString() const; + public: /** - * Halts execution of the calling thread for a specified no of millisec. + * Causes the currently executing thread to halt execution for the specified number of + * milliseconds, subject to the precision and accuracy of system timers and schedulers. + * + * Note that this method is a static method that applies to the + * calling thread and not to the thread object. + * + * @param millisecs time in milliseconds to halt execution. + * + * @throws IllegalArgumentException if the milliseconds parameter is negative. + * @throws InterruptedException if the Thread was interrupted while sleeping. + */ + static void sleep( long long millisecs ) + throw( lang::exceptions::InterruptedException, + lang::exceptions::IllegalArgumentException ); + + /** + * Causes the currently executing thread to halt execution for the specified number of + * milliseconds plus any additionally specified nanoseconds given, subject to the precision + * and accuracy of system timers and schedulers. * * Note that this method is a static method that applies to the * calling thread and not to the thread object. - * @param millisecs time in milliseconds to sleep + * + * @param millisecs time in milliseconds to halt execution. + * @param nanos 0-999999 extra nanoseconds to sleep. + * + * @throws IllegalArgumentException if the nanoseconds parameter is out of range + * or the milliseconds paramter is negative. + * @throws InterruptedException if the Thread was interrupted while sleeping. */ - static void sleep( int millisecs ); + static void sleep( long long millisecs, unsigned int nanos ) + throw( lang::exceptions::InterruptedException, + lang::exceptions::IllegalArgumentException ); /** * Causes the currently executing thread object to temporarily pause @@ -123,7 +264,7 @@ private: // Initialize the Threads internal state - void initialize( Runnable* task ); + void initialize( Runnable* task, const std::string& name ); }; Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp?rev=811174&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp Thu Sep 3 23:54:57 2009 @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ThreadGroup.h" + +using namespace decaf; +using namespace decaf::lang; + +//////////////////////////////////////////////////////////////////////////////// +ThreadGroup::ThreadGroup() { +} + +//////////////////////////////////////////////////////////////////////////////// +ThreadGroup::~ThreadGroup() { +} Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.cpp ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h?rev=811174&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h Thu Sep 3 23:54:57 2009 @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _DECAF_LANG_THREADGROUP_H_ +#define _DECAF_LANG_THREADGROUP_H_ + +#include + +namespace decaf { +namespace lang { + + /** + * + * @since 1.0 + */ + class DECAF_API ThreadGroup { + public: + + ThreadGroup(); + virtual ~ThreadGroup(); + + }; + +}} + +#endif /* _DECAF_LANG_THREADGROUP_H_ */ Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/ThreadGroup.h ------------------------------------------------------------------------------ svn:eol-style = native