sbTestMediacoreStressThreads.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://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 
29 
30 #include <nsAutoLock.h>
31 #include <nsComponentManagerUtils.h>
32 #include <nsCOMPtr.h>
33 #include <nsServiceManagerUtils.h>
34 #include <nsThreadUtils.h>
35 #include <nsIGenericFactory.h>
37 
38 #include <sbIMediacoreEvent.h>
39 #include <sbIMediacoreEventTarget.h>
41 
43  nsIRunnable,
47 
49  : mCounter(-999),
50  mMonitor(nsnull)
51 {
52  mBaseEventTarget = new sbBaseMediacoreEventTarget(this);
53  /* member initializers and constructor code */
54 }
55 
56 sbTestMediacoreStressThreads::~sbTestMediacoreStressThreads()
57 {
58  if(mMonitor) {
59  nsAutoMonitor::DestroyMonitor(mMonitor);
60  }
61  /* destructor code */
62 }
63 
64 /* void run (); */
65 NS_IMETHODIMP sbTestMediacoreStressThreads::Run()
66 {
67  NS_ENSURE_FALSE(mMonitor, NS_ERROR_ALREADY_INITIALIZED);
68 
69  mMonitor = nsAutoMonitor::NewMonitor(__FILE__);
70  NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
71 
72  nsresult rv;
73 
75 
76  rv = mBaseEventTarget->AddListener(static_cast<sbIMediacoreEventListener*>(this));
77  NS_ENSURE_SUCCESS(rv, rv);
78 
79  // spin up a *ton* of threads...
80  mCounter = 0;
81  for (int i = 0; i < 100; ++i) {
82  nsAutoMonitor mon(mMonitor);
83  nsCOMPtr<nsIRunnable> event =
84  NS_NEW_RUNNABLE_METHOD(sbTestMediacoreStressThreads, this, OnEvent);
85  NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
86 
87  nsCOMPtr<nsIThread> thread;
88  ++mCounter;
89  rv = NS_NewThread(getter_AddRefs(thread), event);
90  NS_ENSURE_SUCCESS(rv, rv);
91 
92  mThreads.AppendObject(thread);
93  }
94 
95  // and wait for them to receive their messages... spin event loop until that
96  // happens. We need to wait explicitly as the OnEvent method does not directly
97  // queue all the events needed to complete the test - some get queued later,
98  // so calling thread->Shutdown() at this point will not allow the test to
99  // complete correctly.
100  nsCOMPtr<nsIThread> target;
101  rv = NS_GetMainThread(getter_AddRefs(target));
102  NS_ENSURE_SUCCESS(rv, rv);
103 
104  PRBool processed = PR_FALSE;
105  while(mCounter > 0) {
106  rv = target->ProcessNextEvent(PR_FALSE, &processed);
107  NS_ENSURE_SUCCESS(rv, rv);
108  }
109 
110  // Now shutdown all the threads
111  while (mThreads.Count()) {
112  nsCOMPtr<nsIThread> thread = mThreads[0];
113  PRBool succeeded = mThreads.RemoveObjectAt(0);
114  NS_ENSURE_TRUE(succeeded, NS_ERROR_FAILURE);
115  rv = thread->Shutdown();
116  NS_ENSURE_SUCCESS(rv, rv);
117  }
118 
119  rv = mBaseEventTarget->RemoveListener(static_cast<sbIMediacoreEventListener*>(this));
120  NS_ENSURE_SUCCESS(rv, rv);
121 
122  NS_ENSURE_TRUE(mCounter == 0, NS_ERROR_FAILURE);
123 
124  mBaseEventTarget = nsnull;
125 
126  return NS_OK;
127 }
128 
129 /* void onMediacoreEvent (in sbMediacoreEvent aEvent); */
130 NS_IMETHODIMP sbTestMediacoreStressThreads::OnMediacoreEvent(sbIMediacoreEvent *aEvent)
131 {
132  nsAutoMonitor mon(mMonitor);
133  --mCounter;
134  if (!NS_IsMainThread()) {
135  NS_WARNING("Not on main thread!");
136  mCounter = -2000; // some random error value
137  return NS_ERROR_UNEXPECTED;
138  }
139 
140  return NS_OK;
141 }
142 
143 void sbTestMediacoreStressThreads::OnEvent()
144 {
145  nsresult rv;
146 
147  nsCOMPtr<sbIMediacore> core = do_QueryInterface(NS_ISUPPORTS_CAST(sbIMediacore*, this), &rv);
148  NS_ENSURE_SUCCESS(rv, /* void */);
149 
150  nsCOMPtr<sbIMediacoreEvent> event;
152  nsnull,
153  nsnull,
154  core,
155  getter_AddRefs(event));
156  NS_ENSURE_SUCCESS(rv, /* void */);
157 
158  rv = mBaseEventTarget->DispatchEvent(event, PR_FALSE, nsnull);
159  NS_ENSURE_SUCCESS(rv, /* void */);
160 }
161 
162 /* readonly attribute AString instanceName; */
163 NS_IMETHODIMP sbTestMediacoreStressThreads::GetInstanceName(nsAString & aInstanceName)
164 {
165  return NS_ERROR_NOT_IMPLEMENTED;
166 }
167 
168 /* readonly attribute sbIMediacoreCapabilities capabilities; */
169 NS_IMETHODIMP sbTestMediacoreStressThreads::GetCapabilities(sbIMediacoreCapabilities * *aCapabilities)
170 {
171  return NS_ERROR_NOT_IMPLEMENTED;
172 }
173 
174 /* readonly attribute sbIMediacoreStatus status; */
175 NS_IMETHODIMP sbTestMediacoreStressThreads::GetStatus(sbIMediacoreStatus * *aStatus)
176 {
177  return NS_ERROR_NOT_IMPLEMENTED;
178 }
179 
180 /* attribute sbIMediacoreSequencer sequencer; */
181 NS_IMETHODIMP sbTestMediacoreStressThreads::GetSequencer(sbIMediacoreSequencer* *aSequencer)
182 {
183  return NS_ERROR_NOT_IMPLEMENTED;
184 }
185 
186 NS_IMETHODIMP sbTestMediacoreStressThreads::SetSequencer(sbIMediacoreSequencer *aSequencer)
187 {
188  return NS_ERROR_NOT_IMPLEMENTED;
189 }
190 
191 /* void shutdown (); */
192 NS_IMETHODIMP sbTestMediacoreStressThreads::Shutdown()
193 {
194  return NS_ERROR_NOT_IMPLEMENTED;
195 }
196 
197 NS_IMETHODIMP
198 sbTestMediacoreStressThreads::DispatchEvent(sbIMediacoreEvent *aEvent,
199  PRBool aAsync,
200  PRBool* _retval)
201 {
202  return mBaseEventTarget ? mBaseEventTarget->DispatchEvent(aEvent, aAsync, _retval) : NS_ERROR_NULL_POINTER;
203 }
204 
205 NS_IMETHODIMP
206 sbTestMediacoreStressThreads::AddListener(sbIMediacoreEventListener *aListener)
207 {
208  return mBaseEventTarget ? mBaseEventTarget->AddListener(aListener) : NS_ERROR_NULL_POINTER;
209 }
210 
211 NS_IMETHODIMP
212 sbTestMediacoreStressThreads::RemoveListener(sbIMediacoreEventListener *aListener)
213 {
214  return mBaseEventTarget ? mBaseEventTarget->RemoveListener(aListener) : NS_ERROR_NULL_POINTER;
215 
216 }
return NS_OK
function succeeded(ch, cx, status, data)
var event
Definition of the sbIMediacoreEvent interface.
static nsresult CreateEvent(PRUint32 aType, sbIMediacoreError *aError, nsIVariant *aData, sbIMediacore *aOrigin, sbIMediacoreEvent **retval)
nsAutoPtr< sbBaseMediacoreEventTarget > mBaseEventTarget
const unsigned long URI_CHANGE
URI used for operation has changed.
_getSelectedPageStyle s i
NS_IMPL_THREADSAFE_ISUPPORTS4(sbTestMediacoreStressThreads, nsIRunnable, sbIMediacore, sbIMediacoreEventListener, sbIMediacoreEventTarget) sbTestMediacoreStressThreads