1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is mozilla.org code.
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 2001 Netscape Communications Corporation.
18
* All Rights Reserved.
21
* Stuart Parmenter <pavlov@netscape.com>
24
#include "imgThread.h"
26
#include "nsIEventQueueService.h"
28
#include "nsIComponentManager.h"
29
#include "nsIServiceManager.h"
33
#include "nsAutoLock.h"
35
NS_IMPL_THREADSAFE_ISUPPORTS1(imgThread, nsIRunnable)
37
imgThread::imgThread()
39
/* member initializers and constructor code */
41
mMonitor = PR_NewMonitor();
44
imgThread::~imgThread()
47
PR_DestroyLock(mLock);
48
PR_DestroyMonitor(mMonitor);
51
nsresult imgThread::Init()
54
nsresult rv = NS_NewThread(getter_AddRefs(mThread),
55
NS_STATIC_CAST(nsIRunnable*, this),
67
nsresult imgThread::GetEventQueue(nsIEventQueue **aEventQueue)
69
PR_EnterMonitor(mMonitor);
72
PR_Wait(mMonitor, PR_INTERVAL_NO_TIMEOUT);
74
*aEventQueue = mEventQueue;
75
NS_ADDREF(*aEventQueue);
77
PR_ExitMonitor(mMonitor);
82
/** nsIRunnable methods **/
85
NS_IMETHODIMP imgThread::Run()
89
nsCOMPtr<nsIEventQueue> currentThreadQ;
91
PR_EnterMonitor(mMonitor);
93
nsCOMPtr<nsIEventQueueService> eventService(do_GetService("@mozilla.org/event-queue-service;1", &rv));
94
if (NS_FAILED(rv)) return rv;
96
rv = eventService->CreateMonitoredThreadEventQueue();
97
if (NS_FAILED(rv)) return rv;
99
rv = eventService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(mEventQueue));
101
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create event queue");
102
if (NS_FAILED(rv)) return rv;
106
PR_ExitMonitor(mMonitor);
110
rv = mEventQueue->WaitForEvent(&event);
111
if (NS_FAILED(rv)) return rv;
113
rv = mEventQueue->HandleEvent(event);
114
if (NS_FAILED(rv)) return rv;
117
rv = eventService->DestroyThreadEventQueue();
118
if (NS_FAILED(rv)) return rv;
120
mEventQueue = nsnull;