73c2a13913
This the initial commit for adding a thread pool to the Dart server. For now it is hidden behind a flag (--enable_thread_pool) and when enabled just starts up and processes 100 void tasks. Mac OS implementation is not tested and Windows implementation is still pending. As we are currently not sharing anything between bin/ and vm/ I have just used pthread calls directly in the Linux and Mac OS implementation code. However with Monitor and Thread classes from vm/ the thread pool could be written platform independently. This can always be changed later. R=ager@google.com BUG= TEST= Review URL: http://codereview.chromium.org//8983017 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@2946 260f80e4-7a28-3924-810f-c04153c831b5
68 lines
1.7 KiB
C++
68 lines
1.7 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.
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "bin/thread_pool.h"
|
|
|
|
TaskQueue::TaskQueue() : head_(NULL), tail_(NULL) {
|
|
int result;
|
|
|
|
result = pthread_mutex_init(data_.mutex(), NULL);
|
|
if (result != 0) {
|
|
FATAL("pthread_mutex_init failed");
|
|
}
|
|
|
|
result = pthread_cond_init(data_.cond(), NULL);
|
|
if (result != 0) {
|
|
FATAL("pthread_cond_init failed");
|
|
}
|
|
}
|
|
|
|
|
|
void TaskQueue::Insert(TaskQueueEntry* entry) {
|
|
pthread_mutex_lock(data_.mutex());
|
|
if (head_ == NULL) {
|
|
head_ = entry;
|
|
tail_ = entry;
|
|
pthread_cond_signal(data_.cond());
|
|
} else {
|
|
tail_->set_next(entry);
|
|
tail_ = entry;
|
|
}
|
|
pthread_mutex_unlock(data_.mutex());
|
|
}
|
|
|
|
|
|
TaskQueueEntry* TaskQueue::Remove() {
|
|
pthread_mutex_lock(data_.mutex());
|
|
TaskQueueEntry* result = head_;
|
|
while (result == NULL) {
|
|
pthread_cond_wait(data_.cond(), data_.mutex());
|
|
result = head_;
|
|
}
|
|
head_ = result->next();
|
|
ASSERT(head_ != NULL || tail_ == result);
|
|
pthread_mutex_unlock(data_.mutex());
|
|
return result;
|
|
}
|
|
|
|
|
|
void ThreadPool::Start() {
|
|
pthread_t* threads
|
|
= reinterpret_cast<pthread_t*>(calloc(size_, sizeof(pthread_t*)));
|
|
data_.set_threads(threads);
|
|
for (int i = 0; i < size_; i++) {
|
|
pthread_t handler_thread;
|
|
int result = pthread_create(&handler_thread,
|
|
NULL,
|
|
&ThreadPool::Main,
|
|
this);
|
|
if (result != 0) {
|
|
FATAL("Create and start thread pool thread");
|
|
}
|
|
data_.threads()[i] = handler_thread;
|
|
}
|
|
}
|