THRIFT-5295 makeThread and ThreadFactory extensible
Client: cpp
Patch: Riccardo Ghetta

This closes #2260

Signed-off-by: rglarix <rglarix@users.noreply.github.com>
diff --git a/lib/cpp/src/thrift/concurrency/Thread.h b/lib/cpp/src/thrift/concurrency/Thread.h
index ed43754..344d2ca 100644
--- a/lib/cpp/src/thrift/concurrency/Thread.h
+++ b/lib/cpp/src/thrift/concurrency/Thread.h
@@ -67,10 +67,11 @@
  *
  * @see apache::thrift::concurrency::ThreadFactory)
  */
-class Thread final : public std::enable_shared_from_this<Thread> {
+class Thread : public std::enable_shared_from_this<Thread> {
 
 public:
   typedef std::thread::id id_t;
+  typedef void (*thread_funct_t)(std::shared_ptr<Thread> );
 
   enum STATE { uninitialized, starting, started, stopping, stopped };
 
@@ -84,7 +85,7 @@
     this->_runnable = runnable;
   }
 
-  ~Thread() {
+  virtual ~Thread() {
     if (!detached_ && thread_->joinable()) {
       try {
         join();
@@ -117,7 +118,7 @@
    * configuration then invokes the run method of the Runnable object bound
    * to this thread.
    */
-  void start() {
+  virtual void start() {
     if (getState() != uninitialized) {
       return;
     }
@@ -126,7 +127,7 @@
     setState(starting);
 
     Synchronized sync(monitor_);
-    thread_ = std::unique_ptr<std::thread>(new std::thread(threadMain, selfRef));
+    thread_ = std::unique_ptr<std::thread>(new std::thread(getThreadFunc(), selfRef));
 
     if (detached_)
       thread_->detach();
@@ -142,7 +143,7 @@
    * until this thread completes.  If the target thread is not joinable, then
    * nothing happens.
    */
-  void join() {
+  virtual void join() {
     if (!detached_ && state_ != uninitialized) {
       thread_->join();
     }
@@ -158,6 +159,12 @@
    */
   std::shared_ptr<Runnable> runnable() const { return _runnable; }
 
+protected:
+
+  virtual thread_funct_t getThreadFunc() const {
+      return threadMain;
+  } 
+
 private:
   std::shared_ptr<Runnable> _runnable;
   std::unique_ptr<std::thread> thread_;
diff --git a/lib/cpp/src/thrift/concurrency/ThreadFactory.h b/lib/cpp/src/thrift/concurrency/ThreadFactory.h
index a1547a6..9db832e 100644
--- a/lib/cpp/src/thrift/concurrency/ThreadFactory.h
+++ b/lib/cpp/src/thrift/concurrency/ThreadFactory.h
@@ -31,7 +31,7 @@
  * Factory to create thread object and bind them to Runnable
  * object for execution
  */
-class ThreadFactory final {
+class ThreadFactory {
 public:
   /**
    * All threads created by a factory are reference-counted
@@ -43,7 +43,7 @@
    */
   ThreadFactory(bool detached = true) : detached_(detached) { }
 
-  ~ThreadFactory() = default;
+  virtual ~ThreadFactory() = default;
 
   /**
    * Gets current detached mode
@@ -58,7 +58,7 @@
   /**
    * Create a new thread.
    */
-  std::shared_ptr<Thread> newThread(std::shared_ptr<Runnable> runnable) const;
+  virtual std::shared_ptr<Thread> newThread(std::shared_ptr<Runnable> runnable) const;
 
   /**
    * Gets the current thread id or unknown_thread_id if the current thread is not a thrift thread