~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/libpr0n/src/imgThread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
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/
 
7
 * 
 
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.
 
12
 * 
 
13
 * The Original Code is mozilla.org code.
 
14
 * 
 
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.
 
19
 * 
 
20
 * Contributor(s):
 
21
 *   Stuart Parmenter <pavlov@netscape.com>
 
22
 */
 
23
 
 
24
#include "imgThread.h"
 
25
 
 
26
#include "nsIEventQueueService.h"
 
27
 
 
28
#include "nsIComponentManager.h"
 
29
#include "nsIServiceManager.h"
 
30
 
 
31
#include "prlock.h"
 
32
 
 
33
#include "nsAutoLock.h"
 
34
 
 
35
NS_IMPL_THREADSAFE_ISUPPORTS1(imgThread, nsIRunnable)
 
36
 
 
37
imgThread::imgThread()
 
38
{
 
39
  /* member initializers and constructor code */
 
40
  mLock = PR_NewLock();
 
41
  mMonitor = PR_NewMonitor(); 
 
42
}
 
43
 
 
44
imgThread::~imgThread()
 
45
{
 
46
  /* destructor code */
 
47
  PR_DestroyLock(mLock);
 
48
  PR_DestroyMonitor(mMonitor);
 
49
}
 
50
 
 
51
nsresult imgThread::Init()
 
52
{
 
53
  if (!mThread) {
 
54
    nsresult rv = NS_NewThread(getter_AddRefs(mThread),
 
55
                               NS_STATIC_CAST(nsIRunnable*, this),
 
56
                               0,
 
57
                               PR_UNJOINABLE_THREAD,
 
58
                               PR_PRIORITY_NORMAL,
 
59
                               PR_GLOBAL_THREAD);
 
60
    if (NS_FAILED(rv))
 
61
      return rv;
 
62
  }
 
63
 
 
64
  return NS_OK;
 
65
}
 
66
 
 
67
nsresult imgThread::GetEventQueue(nsIEventQueue **aEventQueue)
 
68
{
 
69
  PR_EnterMonitor(mMonitor);
 
70
 
 
71
  if (!mEventQueue)
 
72
    PR_Wait(mMonitor, PR_INTERVAL_NO_TIMEOUT);
 
73
 
 
74
  *aEventQueue = mEventQueue;
 
75
  NS_ADDREF(*aEventQueue);
 
76
 
 
77
  PR_ExitMonitor(mMonitor);
 
78
  return NS_OK;
 
79
}
 
80
 
 
81
 
 
82
/** nsIRunnable methods **/
 
83
 
 
84
/* void Run(); */
 
85
NS_IMETHODIMP imgThread::Run()
 
86
{
 
87
  nsresult rv;
 
88
 
 
89
  nsCOMPtr<nsIEventQueue> currentThreadQ;
 
90
 
 
91
  PR_EnterMonitor(mMonitor);
 
92
 
 
93
  nsCOMPtr<nsIEventQueueService> eventService(do_GetService("@mozilla.org/event-queue-service;1", &rv));
 
94
  if (NS_FAILED(rv)) return rv;
 
95
 
 
96
  rv = eventService->CreateMonitoredThreadEventQueue();
 
97
  if (NS_FAILED(rv)) return rv;
 
98
 
 
99
  rv = eventService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(mEventQueue));
 
100
 
 
101
  NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create event queue");
 
102
  if (NS_FAILED(rv)) return rv;
 
103
 
 
104
  PR_Notify(mMonitor);
 
105
 
 
106
  PR_ExitMonitor(mMonitor);
 
107
 
 
108
  PLEvent *event;
 
109
  while (1) {
 
110
     rv = mEventQueue->WaitForEvent(&event);
 
111
     if (NS_FAILED(rv)) return rv;
 
112
 
 
113
     rv = mEventQueue->HandleEvent(event);
 
114
     if (NS_FAILED(rv)) return rv;
 
115
  }
 
116
 
 
117
  rv = eventService->DestroyThreadEventQueue();
 
118
  if (NS_FAILED(rv)) return rv;
 
119
 
 
120
  mEventQueue = nsnull;
 
121
 
 
122
  return NS_OK;
 
123
}