1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla code.
17 : *
18 : * The Initial Developer of the Original Code is Google Inc.
19 : * Portions created by the Initial Developer are Copyright (C) 2006
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Darin Fisher <darin@meer.net>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsEventQueue.h"
40 : #include "nsAutoPtr.h"
41 : #include "prlog.h"
42 : #include "nsThreadUtils.h"
43 :
44 : using namespace mozilla;
45 :
46 : #ifdef PR_LOGGING
47 1464 : static PRLogModuleInfo *sLog = PR_NewLogModule("nsEventQueue");
48 : #endif
49 : #define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
50 :
51 10504 : nsEventQueue::nsEventQueue()
52 : : mReentrantMonitor("nsEventQueue.mReentrantMonitor")
53 : , mHead(nsnull)
54 : , mTail(nsnull)
55 : , mOffsetHead(0)
56 10504 : , mOffsetTail(0)
57 : {
58 10504 : }
59 :
60 20978 : nsEventQueue::~nsEventQueue()
61 : {
62 : // It'd be nice to be able to assert that no one else is holding the monitor,
63 : // but NSPR doesn't really expose APIs for it.
64 10489 : NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked.");
65 :
66 10489 : if (mHead)
67 7652 : FreePage(mHead);
68 10489 : }
69 :
70 : bool
71 814915 : nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result)
72 : {
73 : {
74 1629834 : ReentrantMonitorAutoEnter mon(mReentrantMonitor);
75 :
76 1689492 : while (IsEmpty()) {
77 212004 : if (!mayWait) {
78 152322 : if (result)
79 22903 : *result = nsnull;
80 152322 : return false;
81 : }
82 59682 : LOG(("EVENTQ(%p): wait begin\n", this));
83 59682 : mon.Wait();
84 59682 : LOG(("EVENTQ(%p): wait end\n", this));
85 : }
86 :
87 662597 : if (result) {
88 293881 : *result = mHead->mEvents[mOffsetHead++];
89 :
90 : // Check if mHead points to empty Page
91 293881 : if (mOffsetHead == EVENTS_PER_PAGE) {
92 621 : Page *dead = mHead;
93 621 : mHead = mHead->mNext;
94 621 : FreePage(dead);
95 621 : mOffsetHead = 0;
96 : }
97 : }
98 : }
99 :
100 662601 : return true;
101 : }
102 :
103 : bool
104 293883 : nsEventQueue::PutEvent(nsIRunnable *runnable)
105 : {
106 : // Avoid calling AddRef+Release while holding our monitor.
107 587765 : nsRefPtr<nsIRunnable> event(runnable);
108 293883 : bool rv = true;
109 : {
110 587766 : ReentrantMonitorAutoEnter mon(mReentrantMonitor);
111 :
112 293883 : if (!mHead) {
113 7965 : mHead = NewPage();
114 7965 : if (!mHead) {
115 0 : rv = false;
116 : } else {
117 7965 : mTail = mHead;
118 7965 : mOffsetHead = 0;
119 7965 : mOffsetTail = 0;
120 : }
121 285918 : } else if (mOffsetTail == EVENTS_PER_PAGE) {
122 319 : Page *page = NewPage();
123 319 : if (!page) {
124 0 : rv = false;
125 : } else {
126 319 : mTail->mNext = page;
127 319 : mTail = page;
128 319 : mOffsetTail = 0;
129 : }
130 : }
131 293883 : if (rv) {
132 293883 : event.swap(mTail->mEvents[mOffsetTail]);
133 293883 : ++mOffsetTail;
134 293883 : LOG(("EVENTQ(%p): notify\n", this));
135 293883 : mon.NotifyAll();
136 : }
137 : }
138 293882 : return rv;
139 4392 : }
|