blob: d10c8599654faf8f68d11c3a93746628bf3f7d80 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#ifdef WIN32
#include <windows.h>
// MemoryBarrier is a Win32 macro that clashes with MemoryBarrier in
// base/atomicops.h.
#undef MemoryBarrier
#include <pthread.h>
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/message_loop.h"
namespace pp {
// This class is a simple wrapper around a pthread/Windows thread that creates
// and runs a PPAPI message loop on that thread.
class SimpleThread {
#ifdef WIN32
typedef HANDLE ThreadHandle;
typedef pthread_t ThreadHandle;
typedef void (*ThreadFunc)(MessageLoop&, void* user_data);
explicit SimpleThread(const InstanceHandle& instance);
explicit SimpleThread(const InstanceHandle& instance, size_t stacksize);
// Starts a thread and runs a message loop in it. If you need control over
// how the message loop is run, use StartWithFunction. Returns true on
// success, false if the thread is already running or couldn't be started.
bool Start();
// Posts a quit message to the message loop and blocks until the thread
// exits. Returns true on success. If the thread is not running, returns
// false.
bool Join();
// Normally you can just use Start() to start a thread, and then post work to
// it. In some cases you will want control over the message. If ThreadFunc
// is NULL, this acts the same as Start().
bool StartWithFunction(ThreadFunc func, void* user_data);
MessageLoop& message_loop() { return message_loop_; }
ThreadHandle thread() const { return thread_; }
InstanceHandle instance_;
MessageLoop message_loop_;
const size_t stacksize_;
ThreadHandle thread_;
// Disallow (not implemented).
SimpleThread(const SimpleThread&);
SimpleThread(const SimpleThread&, size_t stacksize);
SimpleThread& operator=(const SimpleThread&);
} // namespace pp