Modified facebook::thrift::concurrency::Monitor.wait:
        Throw TimedOutException on wait timeout so caller can distinguish between timeout and event.

Modified facebook::thrift::concurrency::PthreadThread.start:
        Throw SystemrResourceException on any pthread_* function call failure rather than asserting 0.

Added facebook::thrift::concurrency::Thread.id() and  facebook::thrift::concurrency::ThreadFactory.currentThreadId():
        Return thread-id of thread and current thread respectively.  Needed for reentrancy tests in ThreadManager

Added facebook::thrift::concurrency::ThreadManager.pendingTaskCountMaxN
Modified facebook::thrift::concurrency::ThreadManager.add():
        Now support a maximum pending task count and block if the current pending task count is max.
        If timeout is specified for add, TimedOutException is thrown if pending task count doesn't decrease
        in the timeout interval.  If add() is called by a ThreadManager worker thread and the task cannot
        be added, a TooManyPendingTasksException is thrown rather than blocking, since deadlocks can ensue
        if worker threads block waiting for works threads to complete tasks.

Reviewed By: mcslee, aditya

Revert Plan: revertible

Test Plan: concurrency/test/ThreadManagerTests.h
           run concurrency-test thread-manager


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665120 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/ThreadManager.h b/lib/cpp/src/concurrency/ThreadManager.h
index f0c745f..19f77cc 100644
--- a/lib/cpp/src/concurrency/ThreadManager.h
+++ b/lib/cpp/src/concurrency/ThreadManager.h
@@ -11,7 +11,7 @@
 #include <sys/types.h>
 #include "Thread.h"
 
-namespace facebook { namespace thrift { namespace concurrency { 
+namespace facebook { namespace thrift { namespace concurrency {
 
 /**
  * Thread Pool Manager and related classes
@@ -56,7 +56,7 @@
    * Stops the thread manager. Aborts all remaining unprocessed task, shuts
    * down all created worker threads, and realeases all allocated resources.
    * This method blocks for all worker threads to complete, thus it can
-   * potentially block forever if a worker thread is running a task that 
+   * potentially block forever if a worker thread is running a task that
    * won't terminate.
    */
   virtual void stop() = 0;
@@ -76,7 +76,7 @@
     STOPPING,
     STOPPED
   };
-  
+
   virtual const STATE state() const = 0;
 
   virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0;
@@ -108,11 +108,26 @@
   virtual size_t totalTaskCount() const = 0;
 
   /**
-   * Adds a task to be execued at some time in the future by a worker thread.
-   *
-   * @param value The task to run
+   * Gets the maximum pending task count.  0 indicates no maximum
    */
-  virtual void add(boost::shared_ptr<Runnable>value) = 0;
+  virtual size_t pendingTaskCountMax() const = 0;
+
+  /**
+   * Adds a task to be executed at some time in the future by a worker thread.
+   *
+   * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount()
+   * is greater than or equalt to pendingTaskCountMax().  If this method is called in the
+   * context of a ThreadManager worker thread it will throw a
+   * TooManyPendingTasksException
+   *
+   * @param task  The task to queue for execution
+   *
+   * @param timeout Time to wait in milliseconds to add a task when a pending-task-count
+   * is specified
+   *
+   * @throws TooManyPendingTasksException Pending task count exceeds max pending task count
+   */
+  virtual void add(boost::shared_ptr<Runnable>task, long long timeout=0LL) = 0;
 
   /**
    * Removes a pending task
@@ -122,12 +137,14 @@
   static boost::shared_ptr<ThreadManager> newThreadManager();
 
   /**
-   * Creates a simple thread manager the uses count number of worker threads
+   * Creates a simple thread manager the uses count number of worker threads and has
+   * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit
+   * on pending tasks
    */
-  static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4);
+  static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4, size_t pendingTaskCountMax=0);
 
   class Task;
-  
+
   class Worker;
 
   class Impl;