Files
sdk/runtime/bin/thread_pool.h
T
sgjesse@google.com 409a5cc49a Use platform implementation of Monitor in thread pool
This removes the direct use of pthread API for the thread pool monitor now that that code is shared. It also gets the Windows implementation for free.

Next step will be to get rid of using the pthread API for handling the threads as well, but that requires additional operations on the Thread class in platform/thread.h.

R=ager@google.com

BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com//9231020

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@3362 260f80e4-7a28-3924-810f-c04153c831b5
2012-01-17 15:36:35 +00:00

94 lines
2.1 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef BIN_THREAD_POOL_H_
#define BIN_THREAD_POOL_H_
#include "bin/builtin.h"
#include "platform/globals.h"
#include "platform/thread.h"
// Declare the OS-specific types ahead of defining the generic classes.
#if defined(TARGET_OS_LINUX)
#include "bin/thread_pool_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "bin/thread_pool_macos.h"
#elif defined(TARGET_OS_WINDOWS)
#include "bin/thread_pool_win.h"
#else
#error Unknown target os.
#endif
typedef void* Task;
class TaskQueueEntry {
public:
explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {}
Task task() { return task_; }
TaskQueueEntry* next() { return next_; }
void set_next(TaskQueueEntry* value) { next_ = value; }
private:
Task task_;
TaskQueueEntry* next_;
};
// The task queue is a single linked list. Link direction is from tail
// to head. New entried are inserted at the tail and entries are
// removed from the head.
class TaskQueue {
public:
TaskQueue() : terminate_(false), head_(NULL), tail_(NULL) {}
void Insert(TaskQueueEntry* task);
TaskQueueEntry* Remove();
void Shutdown();
private:
bool terminate_;
TaskQueueEntry* head_;
TaskQueueEntry* tail_;
dart::Monitor monitor_;
DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};
class ThreadPool {
public:
typedef void* (*TaskHandler)(void* args);
ThreadPool(TaskHandler task_handler, int initial_size = 4)
: terminate_(false),
size_(initial_size),
task_handler_(task_handler) {}
void Start();
void Shutdown();
void InsertTask(Task task);
private:
Task WaitForTask();
static void* Main(void* args);
TaskQueue queue_;
// TODO(sgjesse): Move the monitor in TaskQueue to ThreadPool and
// obtain it for updating terminate_.
bool terminate_;
int size_; // Number of threads.
TaskHandler task_handler_;
ThreadPoolData data_;
DISALLOW_COPY_AND_ASSIGN(ThreadPool);
};
#endif // BIN_THREAD_POOL_H_