2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
31 void* ThreadRunner(void* data) {
32 Thread* thread = reinterpret_cast<Thread*>(data);
34 thread->WaitForPending();
35 Action* action = thread->GetAction();
36 action->Execute(thread->pointers());
37 bool end_thread = action->EndThread();
38 thread->ClearPending();
46 Threads::Threads(Pointers* pointers, size_t max_threads)
47 : pointers_(pointers), max_threads_(max_threads) {
48 size_t pagesize = getpagesize();
49 data_size_ = (max_threads_ * sizeof(Thread) + pagesize - 1) & ~(pagesize - 1);
50 max_threads_ = data_size_ / sizeof(Thread);
52 void* memory = mmap(nullptr, data_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
53 if (memory == MAP_FAILED) {
54 err(1, "Failed to map in memory for Threads: map size %zu, max threads %zu\n",
55 data_size_, max_threads_);
58 if (Thread::ACTION_SIZE < Action::MaxActionSize()) {
59 err(1, "Thread action size is too small: ACTION_SIZE %zu, max size %zu\n",
60 Thread::ACTION_SIZE, Action::MaxActionSize());
63 threads_ = new (memory) Thread[max_threads_];
68 munmap(threads_, data_size_);
74 Thread* Threads::CreateThread(pid_t tid) {
75 if (num_threads_ == max_threads_) {
76 err(1, "Too many threads created, current max %zu.\n", num_threads_);
78 Thread* thread = FindEmptyEntry(tid);
79 if (thread == nullptr) {
80 err(1, "No empty entries found, current max %zu, num threads %zu\n",
81 max_threads_, num_threads_);
84 thread->pointers_ = pointers_;
85 if (pthread_create(&thread->thread_id_, nullptr, ThreadRunner, thread) == -1) {
86 err(1, "Failed to create thread %d: %s\n", tid, strerror(errno));
93 Thread* Threads::FindThread(pid_t tid) {
94 size_t index = GetHashEntry(tid);
95 for (size_t entries = num_threads_; entries != 0; ) {
96 pid_t cur_tid = threads_[index].tid_;
98 return threads_ + index;
103 if (++index == max_threads_) {
110 void Threads::WaitForAllToQuiesce() {
111 for (size_t i = 0, threads = 0; threads < num_threads_; i++) {
112 pid_t cur_tid = threads_[i].tid_;
115 threads_[i].WaitForReady();
120 size_t Threads::GetHashEntry(pid_t tid) {
121 return tid % max_threads_;
124 Thread* Threads::FindEmptyEntry(pid_t tid) {
125 size_t index = GetHashEntry(tid);
126 for (size_t entries = 0; entries < max_threads_; entries++) {
127 if (threads_[index].tid_ == 0) {
128 return threads_ + index;
130 if (++index == max_threads_) {
137 void Threads::Finish(Thread* thread) {
138 pthread_join(thread->thread_id_, nullptr);
143 void Threads::FinishAll() {
144 for (size_t i = 0; i < max_threads_; i++) {
145 if (threads_[i].tid_ != 0) {
146 threads_[i].CreateAction(0, "thread_done", nullptr);
147 threads_[i].SetPending();
148 Finish(threads_ + i);