2 * Copyright 2001-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 * Axel Dörfler, axeld@pinc-software.de
11 // Queue for holding BMessages
14 #include <MessageQueue.h>
19 BMessageQueue::BMessageQueue()
24 fLock("BMessageQueue Lock")
29 BMessageQueue::~BMessageQueue()
34 BMessage
* message
= fHead
;
35 while (message
!= NULL
) {
36 BMessage
* next
= message
->fQueueLink
;
45 BMessageQueue::AddMessage(BMessage
* message
)
54 // The message passed in will be the last message on the queue so its
55 // link member should be set to null.
56 message
->fQueueLink
= NULL
;
61 // there are no messages in the queue yet
62 fHead
= fTail
= message
;
64 // just add it after the tail
65 fTail
->fQueueLink
= message
;
72 BMessageQueue::RemoveMessage(BMessage
* message
)
81 BMessage
* last
= NULL
;
82 for (BMessage
* entry
= fHead
; entry
!= NULL
; entry
= entry
->fQueueLink
) {
83 if (entry
== message
) {
86 fHead
= entry
->fQueueLink
;
88 last
->fQueueLink
= entry
->fQueueLink
;
102 BMessageQueue::CountMessages() const
104 return fMessageCount
;
109 BMessageQueue::IsEmpty() const
111 return fMessageCount
== 0;
116 BMessageQueue::FindMessage(int32 index
) const
122 if (index
< 0 || index
>= fMessageCount
)
125 for (BMessage
* message
= fHead
; message
!= NULL
; message
= message
->fQueueLink
) {
126 // If the index reaches zero, then we have found a match.
138 BMessageQueue::FindMessage(uint32 what
, int32 index
) const
144 if (index
< 0 || index
>= fMessageCount
)
147 for (BMessage
* message
= fHead
; message
!= NULL
; message
= message
->fQueueLink
) {
148 if (message
->what
== what
) {
149 // If the index reaches zero, then we have found a match.
162 BMessageQueue::Lock()
169 BMessageQueue::Unlock()
176 BMessageQueue::IsLocked() const
178 return fLock
.IsLocked();
183 BMessageQueue::NextMessage()
189 // remove the head of the queue, if any, and return it
191 BMessage
* head
= fHead
;
196 fHead
= head
->fQueueLink
;
199 // If the queue is empty after removing the front element,
200 // we need to set the tail of the queue to NULL since the queue
210 BMessageQueue::IsNextMessage(const BMessage
* message
) const
213 return fHead
== message
;
217 // This method is only here for R5 binary compatibility!
218 // It should be dropped as soon as possible (it misses the const qualifier).
220 BMessageQueue::IsLocked()
222 return fLock
.IsLocked();
226 void BMessageQueue::_ReservedMessageQueue1() {}
227 void BMessageQueue::_ReservedMessageQueue2() {}
228 void BMessageQueue::_ReservedMessageQueue3() {}