1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
4 //---------------------------------------------------------------------
8 #include "PriorityMessageQueue.h"
11 class PriorityMessageQueue::MessageInfo
{
13 MessageInfo(BMessage
*message
, int32 priority
)
19 BMessage
*Message() const { return fMessage
; }
20 int32
Priority() const { return fPriority
; }
29 PriorityMessageQueue::PriorityMessageQueue()
36 PriorityMessageQueue::~PriorityMessageQueue()
38 // delete the messages
39 for (int32 i
= 0; MessageInfo
*info
= fMessages
.ItemAt(i
); i
++)
40 delete info
->Message();
41 // the infos are deleted automatically
46 PriorityMessageQueue::Lock()
53 PriorityMessageQueue::Unlock()
60 PriorityMessageQueue::PushMessage(BMessage
*message
, int32 priority
)
62 bool result
= (message
);
66 if (MessageInfo
*info
= new MessageInfo(message
, priority
)) {
67 // find the insertion index
68 int32 index
= _FindInsertionIndex(priority
);
69 if (!fMessages
.AddItem(info
, index
)) {
82 PriorityMessageQueue::PopMessage()
84 BMessage
*result
= NULL
;
86 if (MessageInfo
*info
= fMessages
.RemoveItemAt(0)) {
87 result
= info
->Message();
97 PriorityMessageQueue::CountMessages() const
101 result
= fMessages
.CountItems();
109 PriorityMessageQueue::IsEmpty() const
111 return (CountMessages() == 0);
114 // _FindInsertionIndex
116 PriorityMessageQueue::_FindInsertionIndex(int32 priority
)
119 int32 upper
= fMessages
.CountItems();
120 while (lower
< upper
) {
121 int32 mid
= (lower
+ upper
) / 2;
122 MessageInfo
*info
= fMessages
.ItemAt(mid
);
123 if (info
->Priority() >= priority
)