sbThreadPoolService.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
33 #include "sbThreadPoolService.h"
34 
35 #include <nsIAppStartupNotifier.h>
36 #include <nsIObserverService.h>
37 
38 #include <nsComponentManagerUtils.h>
39 #include <nsCOMPtr.h>
40 #include <nsServiceManagerUtils.h>
41 #include <nsXPCOM.h>
42 
43 #include <prlog.h>
44 
49 #ifdef PR_LOGGING
50 static PRLogModuleInfo* gThreadPoolServiceLog = nsnull;
51 #define TRACE(args) PR_LOG(gThreadPoolServiceLog, PR_LOG_DEBUG, args)
52 #define LOG(args) PR_LOG(gThreadPoolServiceLog, PR_LOG_WARN, args)
53 #else
54 #define TRACE(args) /* nothing */
55 #define LOG(args) /* nothing */
56 #endif
57 
58 #define DEFAULT_THREAD_LIMIT (8)
59 #define DEFAULT_IDLE_LIMIT (1)
60 #define DEFAULT_IDLE_TIMEOUT (10000) /* in milliseconds */
61 
62 #define NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID "xpcom-shutdown-threads"
63 
65  nsIEventTarget,
66  nsIThreadPool,
68 
70 {
71 #ifdef PR_LOGGING
72  if (!gThreadPoolServiceLog) {
73  gThreadPoolServiceLog = PR_NewLogModule("sbThreadPoolService");
74  }
75 
76  TRACE(("sbThreadPoolService[0x%x] - ctor", this));
77 #endif
78 }
79 
81 {
82  TRACE(("sbThreadPoolService[0x%x] - dtor", this));
83 }
84 
85 nsresult
87 {
88  nsresult rv = NS_ERROR_UNEXPECTED;
89  mThreadPool = do_CreateInstance("@mozilla.org/thread-pool;1", &rv);
90  NS_ENSURE_SUCCESS(rv, rv);
91 
92  rv = mThreadPool->SetThreadLimit(DEFAULT_THREAD_LIMIT);
93  NS_ENSURE_SUCCESS(rv, rv);
94 
95  rv = mThreadPool->SetIdleThreadLimit(DEFAULT_IDLE_LIMIT);
96  NS_ENSURE_SUCCESS(rv, rv);
97 
98  rv = mThreadPool->SetIdleThreadTimeout(DEFAULT_IDLE_TIMEOUT);
99  NS_ENSURE_SUCCESS(rv, rv);
100 
101  return NS_OK;
102 }
103 
104 
105 NS_IMETHODIMP
106 sbThreadPoolService::Observe(nsISupports *aSubject,
107  const char *aTopic,
108  const PRUnichar *aData)
109 {
110  LOG(("sbThreadPoolService[0x%x] - Observe (%s)", this, aTopic));
111 
112  nsresult rv = NS_ERROR_UNEXPECTED;
113 
114  if (!strcmp(aTopic, NS_XPCOM_STARTUP_OBSERVER_ID)) {
115  nsresult rv = NS_ERROR_UNEXPECTED;
116 
117  nsCOMPtr<nsIObserverService> obsSvc =
118  do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
119  NS_ENSURE_SUCCESS(rv, rv);
120 
121  nsCOMPtr<nsIObserver> observer =
122  do_QueryInterface(NS_ISUPPORTS_CAST(nsIObserver*, this), &rv);
123  NS_ENSURE_SUCCESS(rv, rv);
124 
125  rv = obsSvc->AddObserver(observer, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, PR_FALSE);
126  NS_ENSURE_SUCCESS(rv, rv);
127  }
128  else if(!strcmp(aTopic, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID)) {
129  nsCOMPtr<nsIObserverService> obsSvc =
130  do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
131  NS_ENSURE_SUCCESS(rv, rv);
132 
133  nsCOMPtr<nsIObserver> observer =
134  do_QueryInterface(NS_ISUPPORTS_CAST(nsIObserver*, this), &rv);
135  NS_ENSURE_SUCCESS(rv, rv);
136 
137  rv = obsSvc->RemoveObserver(observer, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID);
138  NS_ENSURE_SUCCESS(rv, rv);
139 
140  // All further attempts to dispatch runnables will fail gracefully
141  // after Shutdown is called on the underlying threadpool.
142  nsresult rv = mThreadPool->Shutdown();
143  NS_ENSURE_SUCCESS(rv, rv);
144  }
145 
146  return NS_OK;
147 }
#define DEFAULT_IDLE_LIMIT
return NS_OK
NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER nsresult Init()
#define DEFAULT_THREAD_LIMIT
NS_IMPL_THREADSAFE_ISUPPORTS3(sbThreadPoolService, nsIEventTarget, nsIThreadPool, nsIObserver) sbThreadPoolService
#define TRACE(args)
#define LOG(args)
Songbird Thread Pool Service.
#define NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID
#define DEFAULT_IDLE_TIMEOUT
let observer
_updateTextAndScrollDataForFrame aData