• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef MessageThread_h
27 #define MessageThread_h
28 
29 #include <list>
30 
31 #include "MessageTypes.h"
32 
33 #include <utils/threads.h>
34 
35 using std::list;
36 
37 namespace android {
38 
39 class MessageQueue {
40 public:
MessageQueue()41     MessageQueue() {}
42 
43     // Return true if the queue has messages with the given object and member
44     // function.  If member is null, return true if the message has the same
45     // object.
46     template <class T>
47     bool hasMessages(T* object, void (T::*member)(void));
48 
49     // Remove all messages with the given object and member function.  If
50     // member is null, remove all messages with the given object.
51     template <class T>
52     void remove(T* object, void (T::*member)(void));
53 
54     // Post a new message to the queue.
55     void post(Message* closure);
56 
57     // Post a new message at the front of the queue.
58     void postAtFront(Message* closure);
59 
60     // Obtain the next message.  Blocks until either a new message arrives or
61     // we reach the time of the next message.
62     Message* next();
63 
64 private:
65     bool hasMessages(const Message& message);
66     void remove(const Message& message);
67 
68     list<Message*> m_messages;
69     Mutex          m_mutex;
70     Condition      m_condition;
71 };
72 
73 template <class T>
hasMessages(T * object,void (T::* member)(void))74 bool MessageQueue::hasMessages(T* object, void (T::*member)(void)) {
75     MemberFunctionMessage<T, void> message(object, member);
76     return hasMessages(message);
77 }
78 
79 template <class T>
remove(T * object,void (T::* member)(void))80 void MessageQueue::remove(T* object, void (T::*member)(void)) {
81     MemberFunctionMessage<T, void> message(object, member);
82     remove(message);
83 }
84 
85 class MessageThread : public Thread {
86 public:
queue()87     MessageQueue& queue() { return m_queue; }
88 
89 private:
MessageThread()90     MessageThread() : Thread(true /* canCallJava */) {}
91 
92     virtual bool threadLoop();
93 
94     MessageQueue m_queue;
95     // Used for thread initialization
96     Mutex m_mutex;
97     Condition m_condition;
98 
99     friend MessageThread* messageThread();
100 };
101 
102 // Get (possibly creating) the global MessageThread object used to pass
103 // messages to WebCore.
104 MessageThread* messageThread();
105 
106 }  // namespace android
107 
108 #endif  // MessageThread_h
109