Files
sdk/runtime/bin/eventhandler.h
T
sgjesse@google.com 0365cf42d0 Refactored thread pool
The thread pool has been refactored to hold all implementation state
in the ThreadPool class and have all internal state protected by a
single monitor.

Added support for starting and stopping the same pool several
times. When stopping the queue can be drained or not.

Added two tests which handles messages.

This change is based on https://chromiumcodereview.appspot.com/9141005/.

R=iposva@google.com, ager@google.com

BUG=
TEST=

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

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@4181 260f80e4-7a28-3924-810f-c04153c831b5
2012-02-13 12:05:08 +00:00

80 lines
2.0 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_EVENTHANDLER_H_
#define BIN_EVENTHANDLER_H_
#include "bin/builtin.h"
#include "bin/thread_pool.h"
// Flags used to provide information and actions to the eventhandler
// when sending a message about a file descriptor. These flags should
// be kept in sync with the constants in socket_impl.dart. For more
// information see the comments in socket_impl.dart
enum MessageFlags {
kInEvent = 0,
kOutEvent = 1,
kErrorEvent = 2,
kCloseEvent = 3,
kCloseCommand = 8,
kShutdownReadCommand = 9,
kShutdownWriteCommand = 10,
kListeningSocket = 16,
kPipe = 17,
};
// The event handler delegation class is OS specific.
#if defined(TARGET_OS_LINUX)
#include "bin/eventhandler_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "bin/eventhandler_macos.h"
#elif defined(TARGET_OS_WINDOWS)
#include "bin/eventhandler_win.h"
#else
#error Unknown target os.
#endif
class EventHandler {
public:
void SendData(intptr_t id, Dart_Port dart_port, intptr_t data) {
delegate_.SendData(id, dart_port, data);
}
static void AsyncTaskHandler(ThreadPool::Task args) {
if (Dart_IsVMFlagSet("trace_thread_pool")) {
printf("Got async task\n");
}
}
static void Initialize() {
if (Dart_IsVMFlagSet("enable_thread_pool")) {
ASSERT(thread_pool_ == NULL);
thread_pool_ = new ThreadPool(&EventHandler::AsyncTaskHandler);
thread_pool_->Start();
}
}
static void Terminate() {
if (Dart_IsVMFlagSet("enable_thread_pool")) {
if (thread_pool_ != NULL) {
thread_pool_->Shutdown();
}
}
}
static EventHandler* StartEventHandler() {
EventHandler* handler = new EventHandler();
handler->delegate_.StartEventHandler();
return handler;
}
private:
EventHandlerImplementation delegate_;
static ThreadPool* thread_pool_;
};
#endif // BIN_EVENTHANDLER_H_