Bug 757511 - Make it possible to raise the maximum number of threads. r=biesi
authorJan Varga <jan.varga@gmail.com>
Wed, 23 May 2012 06:24:18 +0200
changeset 94562 36e938e51481e9633e326a61b2ada0a7686191c6
parent 94561 a301e1c41b7d1515d98b569ec81369686c6933bc
child 94660 aa2b52bd037474469dfa43e4ebcbd16d4fc6c094
push id22731
push userJan.Varga@gmail.com
push dateWed, 23 May 2012 04:25:50 +0000
treeherdermozilla-central@36e938e51481 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbiesi
bugs757511
milestone15.0a1
first release with
nightly linux32
36e938e51481 / 15.0a1 / 20120523030525 / files
nightly linux64
36e938e51481 / 15.0a1 / 20120523030525 / files
nightly mac
36e938e51481 / 15.0a1 / 20120523030525 / files
nightly win32
36e938e51481 / 15.0a1 / 20120523030525 / files
nightly win64
36e938e51481 / 15.0a1 / 20120523030525 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 757511 - Make it possible to raise the maximum number of threads. r=biesi
netwerk/base/public/nsIStreamTransportService.idl
netwerk/base/src/nsStreamTransportService.cpp
--- a/netwerk/base/public/nsIStreamTransportService.idl
+++ b/netwerk/base/public/nsIStreamTransportService.idl
@@ -10,17 +10,17 @@ interface nsIOutputStream;
 
 /**
  * This service read/writes a stream on a background thread.
  *
  * Use this service to transform any blocking stream (e.g., file stream)
  * into a fully asynchronous stream that can be read/written without 
  * blocking the main thread.
  */
-[scriptable, uuid(8268D474-EFBF-494f-A152-E8A8616F4E52)]
+[scriptable, uuid(51CAC889-ABC6-4948-97A3-4F135A6E7630)]
 interface nsIStreamTransportService : nsISupports
 {
     /**
      * CreateInputTransport
      *
      * @param aStream
      *        The input stream that will be read on a background thread.
      *        This stream must implement "blocking" stream semantics.
@@ -60,9 +60,38 @@ interface nsIStreamTransportService : ns
      *        contents have been completely written.
      *
      * @return nsITransport instance.
      */
     nsITransport createOutputTransport(in nsIOutputStream aStream,
                                        in long long aStartOffset,
                                        in long long aWriteLimit,
                                        in boolean aCloseWhenDone);
+
+    /**
+     * Raise the maximum number of active threads by one.
+     *
+     * Calling this method won't create any additional thread synchronously.
+     * It will be only created when it's needed (lazily).
+     *
+     * Used by mozilla::dom::file::FileService to increase the maximum number
+     * of active threads in the thread pool for asynchronous file IO.
+     */
+    void raiseThreadLimit();
+
+    /**
+     * Lower the maximum number of active threads by one.
+     * lowerThreadLimit() should be always paired with raiseThreadLimit().
+     *
+     * Calling this method won't destroy any already running thread
+     * synchronously. It will be only destroyed when it's done with
+     * currently running event.
+     *
+     * This will never lower the maximum number of active threads beyond
+     * the internal limit.
+     *
+     * @throws NS_ERROR_UNEXPECTED
+     *         When trying to lower the maximum number of active threads
+     *         beyond the internal limit (for example in the case of badly
+     *         nested calls)
+     */
+    void lowerThreadLimit();
 };
--- a/netwerk/base/src/nsStreamTransportService.cpp
+++ b/netwerk/base/src/nsStreamTransportService.cpp
@@ -496,16 +496,45 @@ nsStreamTransportService::CreateOutputTr
         new nsOutputStreamTransport(stream, offset, limit, closeWhenDone);
     if (!trans)
         return NS_ERROR_OUT_OF_MEMORY;
     NS_ADDREF(*result = trans);
     return NS_OK;
 }
 
 NS_IMETHODIMP
+nsStreamTransportService::RaiseThreadLimit()
+{
+    NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED);
+
+    PRUint32 threadLimit;
+    nsresult rv = mPool->GetThreadLimit(&threadLimit);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    return mPool->SetThreadLimit(threadLimit + 1);
+}
+
+NS_IMETHODIMP
+nsStreamTransportService::LowerThreadLimit()
+{
+    NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED);
+
+    PRUint32 threadLimit;
+    nsresult rv = mPool->GetThreadLimit(&threadLimit);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    if (threadLimit == 4) {
+      NS_WARNING("Badly nested raise/lower thread limit!");
+      return NS_ERROR_UNEXPECTED;
+    }
+
+    return mPool->SetThreadLimit(threadLimit - 1);
+}
+
+NS_IMETHODIMP
 nsStreamTransportService::Observe(nsISupports *subject, const char *topic,
                                   const PRUnichar *data)
 {
   NS_ASSERTION(strcmp(topic, "xpcom-shutdown-threads") == 0, "oops");
 
   if (mPool) {
     mPool->Shutdown();
     mPool = nsnull;