blob: e95f798241887cf9caf94e12ea85439e1d09a2f3 [file] [log] [blame]
/*
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// WARNING: You should *NOT* be using this class directly. PlatformThread is
// the low-level platform-specific abstraction to the OS's threading interface.
// You should instead be using a message-loop driven Thread, see thread.h.
#ifndef IMP_GOOPY_BASE_THREADING_PLATFORM_THREAD_H_
#define IMP_GOOPY_BASE_THREADING_PLATFORM_THREAD_H_
#pragma once
#include "base/basictypes.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#elif defined(OS_POSIX)
#include <pthread.h>
#if defined(OS_MACOSX)
#include <mach/mach.h>
#else // OS_POSIX && !OS_MACOSX
#include <unistd.h>
#endif
#endif
namespace base {
// PlatformThreadHandle should not be assumed to be a numeric type, since the
// standard intends to allow pthread_t to be a structure. This means you
// should not initialize it to a value, like 0. If it's a member variable, the
// constructor can safely "value initialize" using () in the initializer list.
#if defined(OS_WIN)
typedef DWORD PlatformThreadId;
typedef void* PlatformThreadHandle; // HANDLE
const PlatformThreadHandle kNullThreadHandle = NULL;
#elif defined(OS_POSIX)
typedef pthread_t PlatformThreadHandle;
const PlatformThreadHandle kNullThreadHandle = 0;
#if defined(OS_MACOSX)
typedef mach_port_t PlatformThreadId;
#else // OS_POSIX && !OS_MACOSX
typedef pid_t PlatformThreadId;
#endif
#endif
const PlatformThreadId kInvalidThreadId = 0;
// A namespace for low-level thread functions.
class PlatformThread {
public:
// Implement this interface to run code on a background thread. Your
// ThreadMain method will be called on the newly created thread.
class Delegate {
public:
virtual ~Delegate() {}
virtual void ThreadMain() = 0;
};
// Gets the current thread id, which may be useful for logging purposes.
static PlatformThreadId CurrentId();
// Yield the current thread so another thread can be scheduled.
static void YieldCurrentThread();
// Sleeps for the specified duration (units are milliseconds).
static void Sleep(int duration_ms);
// Sets the thread name visible to a debugger. This has no effect otherwise.
static void SetName(const char* name);
// Creates a new thread. The |stack_size| parameter can be 0 to indicate
// that the default stack size should be used. Upon success,
// |*thread_handle| will be assigned a handle to the newly created thread,
// and |delegate|'s ThreadMain method will be executed on the newly created
// thread.
// NOTE: When you are done with the thread handle, you must call Join to
// release system resources associated with the thread. You must ensure that
// the Delegate object outlives the thread.
static bool Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle);
// CreateNonJoinable() does the same thing as Create() except the thread
// cannot be Join()'d. Therefore, it also does not output a
// PlatformThreadHandle.
static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
// Joins with a thread created via the Create function. This function blocks
// the caller until the designated thread exits. This will invalidate
// |thread_handle|.
static void Join(PlatformThreadHandle thread_handle);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
};
} // namespace base
#endif // IMP_GOOPY_BASE_THREADING_PLATFORM_THREAD_H_