Revert of Update V8 to version 4.5.70. (patchset #1 id:1 of https://codereview.chromi...
[chromium-blink-merge.git] / jingle / glue / thread_wrapper.h
blob7969d2bc6af88ce7a59eff020c80b112263b872f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef JINGLE_GLUE_THREAD_WRAPPER_H_
6 #define JINGLE_GLUE_THREAD_WRAPPER_H_
8 #include <list>
9 #include <map>
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "third_party/webrtc/base/thread.h"
17 namespace jingle_glue {
19 // JingleThreadWrapper implements rtc::Thread interface on top of
20 // Chromium's SingleThreadTaskRunner interface. Currently only the bare minimum
21 // that is used by P2P part of libjingle is implemented. There are two ways to
22 // create this object:
24 // - Call EnsureForCurrentMessageLoop(). This approach works only on threads
25 // that have MessageLoop In this case JingleThreadWrapper deletes itself
26 // automatically when MessageLoop is destroyed.
27 // - Using JingleThreadWrapper() constructor. In this case the creating code
28 // must pass a valid task runner for the current thread and also delete the
29 // wrapper later.
30 class JingleThreadWrapper : public base::MessageLoop::DestructionObserver,
31 public rtc::Thread {
32 public:
33 // Create JingleThreadWrapper for the current thread if it hasn't been created
34 // yet. The thread wrapper is destroyed automatically when the current
35 // MessageLoop is destroyed.
36 static void EnsureForCurrentMessageLoop();
38 // Creates JingleThreadWrapper for |task_runner| that runs tasks on the
39 // current thread.
40 static scoped_ptr<JingleThreadWrapper> WrapTaskRunner(
41 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
43 // Returns thread wrapper for the current thread or nullptr if it doesn't
44 // exist.
45 static JingleThreadWrapper* current();
47 ~JingleThreadWrapper() override;
49 // Sets whether the thread can be used to send messages
50 // synchronously to another thread using Send() method. Set to false
51 // by default to avoid potential jankiness when Send() used on
52 // renderer thread. It should be set explicitly for threads that
53 // need to call Send() for other threads.
54 void set_send_allowed(bool allowed) { send_allowed_ = allowed; }
56 // MessageLoop::DestructionObserver implementation.
57 void WillDestroyCurrentMessageLoop() override;
59 // rtc::MessageQueue overrides.
60 void Post(rtc::MessageHandler* phandler,
61 uint32 id,
62 rtc::MessageData* pdata,
63 bool time_sensitive) override;
64 void PostDelayed(int delay_ms,
65 rtc::MessageHandler* handler,
66 uint32 id,
67 rtc::MessageData* data) override;
68 void Clear(rtc::MessageHandler* handler,
69 uint32 id,
70 rtc::MessageList* removed) override;
71 void Send(rtc::MessageHandler* handler,
72 uint32 id,
73 rtc::MessageData* data) override;
75 // Following methods are not supported.They are overriden just to
76 // ensure that they are not called (each of them contain NOTREACHED
77 // in the body). Some of this methods can be implemented if it
78 // becomes neccessary to use libjingle code that calls them.
79 void Quit() override;
80 bool IsQuitting() override;
81 void Restart() override;
82 bool Get(rtc::Message* message, int delay_ms, bool process_io) override;
83 bool Peek(rtc::Message* message, int delay_ms) override;
84 void PostAt(uint32 timestamp,
85 rtc::MessageHandler* handler,
86 uint32 id,
87 rtc::MessageData* data) override;
88 void Dispatch(rtc::Message* message) override;
89 void ReceiveSends() override;
90 int GetDelay() override;
92 // rtc::Thread overrides.
93 void Stop() override;
94 void Run() override;
96 private:
97 typedef std::map<int, rtc::Message> MessagesQueue;
98 struct PendingSend;
100 explicit JingleThreadWrapper(
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
103 void PostTaskInternal(
104 int delay_ms, rtc::MessageHandler* handler,
105 uint32 message_id, rtc::MessageData* data);
106 void RunTask(int task_id);
107 void ProcessPendingSends();
109 // Task runner used to execute messages posted on this thread.
110 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
112 bool send_allowed_;
114 // |lock_| must be locked when accessing |messages_|.
115 base::Lock lock_;
116 int last_task_id_;
117 MessagesQueue messages_;
118 std::list<PendingSend*> pending_send_messages_;
119 base::WaitableEvent pending_send_event_;
121 base::WeakPtr<JingleThreadWrapper> weak_ptr_;
122 base::WeakPtrFactory<JingleThreadWrapper> weak_ptr_factory_;
124 DISALLOW_COPY_AND_ASSIGN(JingleThreadWrapper);
127 } // namespace jingle_glue
129 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_