sbThreadUtils.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2009 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird thread utilities services.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird thread utilities imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbThreadUtils.h"
48 
49 // Mozilla imports.
50 #include <nsIThreadManager.h>
51 #include <nsServiceManagerUtils.h>
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Songbird thread utilities services.
57 //
58 //------------------------------------------------------------------------------
59 
70 PRBool
71 SB_IsMainThread(nsIThreadManager* aThreadManager)
72 {
73  nsresult rv;
74 
75  // Get the thread manager if not provided.
76  nsCOMPtr<nsIThreadManager> threadManager = aThreadManager;
77  if (!threadManager) {
78  threadManager = do_GetService("@mozilla.org/thread-manager;1", &rv);
79  NS_ENSURE_SUCCESS(rv, PR_FALSE);
80  }
81 
82  // Check if the current thread is the main thread.
83  PRBool isMainThread;
84  rv = threadManager->GetIsMainThread(&isMainThread);
85  NS_ENSURE_SUCCESS(rv, PR_FALSE);
86 
87  return isMainThread;
88 }
89 
90 
91 
92 NS_IMETHODIMP
94 {
95  // Enter the monitor to set the done flag:
96  mozilla::MonitorAutoEnter lock(mMonitor);
97 
98  // Set the done flag and notify all waiters:
99  mDone = true;
100  mMonitor.NotifyAll();
101 
102  return NS_OK;
103 }
104 
105 
106 
107 PRBool
108 sbRunnable::Wait(PRIntervalTime aTimeout)
109 {
110  // Compute a fixed expiration time that won't drift:
111  const PRIntervalTime expiry = PR_IntervalNow() + aTimeout;
112 
113  // Enter the monitor to check the done flag:
114  mozilla::MonitorAutoEnter lock(mMonitor);
115 
116  // Only wait for Run() to complete if asked:
117  if (aTimeout != PR_INTERVAL_NO_WAIT) {
118  // Loop every time the monitor is signaled, until done
119  // or timed out:
120  while (!mDone) {
121  // Compute how long to wait: indefinitely if PR_INTERVAL_NO_TIMEOUT
122  // was given, or the time remaining until the fixed expiration time,
123  // otherwise.
124  PRIntervalTime timeout;
125  if (aTimeout == PR_INTERVAL_NO_TIMEOUT) {
126  timeout = PR_INTERVAL_NO_TIMEOUT;
127  }
128  else {
129  timeout = expiry - PR_IntervalNow();
130  if (PRInt32(timeout) <= 0) {
131  break;
132  }
133  }
134  // Wait for a signal from Run(), or until timed out
135  mMonitor.Wait(timeout);
136  }
137  }
138 
139  // Return the done flag:
140  return mDone;
141 }
return NS_OK
PRBool Wait(PRIntervalTime aTimeout)
PRBool SB_IsMainThread(nsIThreadManager *aThreadManager)
Songbird Thread Utilities Definitions.
NS_IMETHOD Run()