15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef REMOTING_BASE_AUTO_THREAD_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define REMOTING_BASE_AUTO_THREAD_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/platform_thread.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "remoting/base/auto_thread_task_runner.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace remoting {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Thread implementation that runs a MessageLoop on a new thread, and manages
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the lifetime of the MessageLoop and thread by tracking references to the
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// thread's TaskRunner.  The caller passes the thread's TaskRunner to each
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object that needs to run code on the thread, and when no references to the
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TaskRunner remain, the thread will exit.  When the caller destroys this
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object they will be blocked until the thread exits.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All pending tasks queued on the thread's message loop will run to completion
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// before the thread is terminated.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// After the thread is stopped, the destruction sequence is:
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  (1) Thread::CleanUp()
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  (2) MessageLoop::~MessageLoop
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  (3.b)    MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AutoThread : base::PlatformThread::Delegate {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create an AutoThread with the specified message-loop |type| and |name|.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The supplied AutoThreadTaskRunner will be used to join and delete the
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // new thread when no references to it remain.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static scoped_refptr<AutoThreadTaskRunner> CreateWithType(
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const char* name,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<AutoThreadTaskRunner> joiner,
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::MessageLoop::Type type);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static scoped_refptr<AutoThreadTaskRunner> Create(
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const char* name,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<AutoThreadTaskRunner> joiner);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Create an AutoThread initialized for COM.  |com_init_type| specifies the
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // type of COM apartment to initialize.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum ComInitType { COM_INIT_NONE, COM_INIT_STA, COM_INIT_MTA };
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static scoped_refptr<AutoThreadTaskRunner> CreateWithLoopAndComInitTypes(
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const char* name,
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_refptr<AutoThreadTaskRunner> joiner,
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::MessageLoop::Type loop_type,
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ComInitType com_init_type);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Construct the AutoThread.  |name| identifies the thread for debugging.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit AutoThread(const char* name);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Waits for the thread to exit, and then destroys it.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AutoThread();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Starts the thread, running the specified type of MessageLoop.  Returns
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an AutoThreadTaskRunner through which tasks may be posted to the thread
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if successful, or NULL on failure.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: This function can't be called on Windows with the loader lock held;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // i.e. during a DllMain, global object construction or destruction, atexit()
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: You must not call this MessageLoop's Quit method directly.  The
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thread will exit when no references to the TaskRunner remain.
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_refptr<AutoThreadTaskRunner> StartWithType(
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::MessageLoop::Type type);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Configures the thread to initialize the specified COM apartment type.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // SetComInitType() must be called before Start().
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetComInitType(ComInitType com_init_type);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutoThread(const char* name, AutoThreadTaskRunner* joiner);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void QuitThread(scoped_refptr<base::SingleThreadTaskRunner> task_runner);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void JoinAndDeleteThread();
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // base::PlatformThread::Delegate methods:
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ThreadMain() OVERRIDE;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to pass data to ThreadMain.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct StartupData;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartupData* startup_data_;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Specifies which kind of COM apartment to initialize, if any.
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ComInitType com_init_type_;
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The thread's handle.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformThreadHandle thread_;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The name of the thread.  Used for debugging purposes.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string name_;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flag used to indicate whether MessageLoop was quit properly.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This allows us to detect premature exit via MessageLoop::Quit().
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool was_quit_properly_;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AutoThreadTaskRunner to post a task to to join & delete this thread.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<AutoThreadTaskRunner> joiner_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AutoThread);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace remoting
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // REMOTING_AUTO_THREAD_H_
116