Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: runtime/bin/thread_pool_macos.cc

Issue 8983017: Start of thread pool implementation (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix compilation on Windows Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include <pthread.h>
6
7 #include "bin/thread_pool.h"
8
9 TaskQueue::TaskQueue() : head_(NULL), tail_(NULL) {
10 int result;
11
12 result = pthread_mutex_init(data_.mutex(), NULL);
13 if (result != 0) {
14 FATAL("pthread_mutex_init failed");
15 }
16
17 result = pthread_cond_init(data_.cond(), NULL);
18 if (result != 0) {
19 FATAL("pthread_cond_init failed");
20 }
21 }
22
23
24 void TaskQueue::Insert(TaskQueueEntry* entry) {
25 pthread_mutex_lock(data_.mutex());
26 if (head_ == NULL) {
27 ASSERT(tail_ == NULL);
28 head_ = entry;
29 tail_ = entry;
30 pthread_cond_signal(data_.cond());
31 } else {
32 ASSERT(tail_ != NULL);
33 tail_->set_next(entry);
34 tail_ = entry;
35 }
36 pthread_mutex_unlock(data_.mutex());
37 }
38
39
40 TaskQueueEntry* TaskQueue::Remove() {
41 pthread_mutex_lock(data_.mutex());
42 TaskQueueEntry* result = head_;
43 while (result == NULL) {
44 pthread_cond_wait(data_.cond(), data_.mutex());
45 result = head_;
46 }
47 head_ = result->next();
48 #ifdef DEBUG
49 if (head_ == NULL) {
50 ASSERT(tail_ == result);
51 tail_ = NULL;
52 }
53 #endif
54 pthread_mutex_unlock(data_.mutex());
55 return result;
56 }
57
58
59 void ThreadPool::Start() {
60 for (int i = 0; i < size_; i++) {
61 pthread_t handler_thread;
62 int result = pthread_create(&handler_thread,
63 NULL,
64 &ThreadPool::Main,
65 this);
66 if (result != 0) {
67 FATAL("Create and start thread pool thread");
68 }
69 data_.threads()[i] = handler_thread;
70 }
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698