blob: 93aa10e240c80a70fcb04a65d87613286222644f [file] [log] [blame]
Ian Rogers693ff612013-02-01 10:56:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef ART_SRC_THREAD_INL_H_
18#define ART_SRC_THREAD_INL_H_
19
20#include "thread.h"
21
22#include "base/mutex-inl.h"
23#include "cutils/atomic-inline.h"
24
25namespace art {
26
27inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const {
28#ifdef NDEBUG
29 UNUSED(check_locks); // Keep GCC happy about unused parameters.
30#else
31 CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_;
32 if (check_locks) {
33 bool bad_mutexes_held = false;
34 for (int i = kMaxMutexLevel; i >= 0; --i) {
35 // We expect no locks except the mutator_lock_.
36 if (i != kMutatorLock) {
37 BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i));
38 if (held_mutex != NULL) {
39 LOG(ERROR) << "holding \"" << held_mutex->GetName()
40 << "\" at point where thread suspension is expected";
41 bad_mutexes_held = true;
42 }
43 }
44 }
45 CHECK(!bad_mutexes_held);
46 }
47#endif
48}
49
50inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) {
51 AssertThreadSuspensionIsAllowable();
52 DCHECK_NE(new_state, kRunnable);
53 DCHECK_EQ(this, Thread::Current());
54 // Change to non-runnable state, thereby appearing suspended to the system.
55 DCHECK_EQ(GetState(), kRunnable);
56 union StateAndFlags old_state_and_flags;
57 union StateAndFlags new_state_and_flags;
58 do {
59 old_state_and_flags = state_and_flags_;
60 // Copy over flags and try to clear the checkpoint bit if it is set.
61 new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest;
62 new_state_and_flags.as_struct.state = new_state;
63 // CAS the value without a memory barrier, that will occur in the unlock below.
64 } while (UNLIKELY(android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
65 &state_and_flags_.as_int) != 0));
66 // If we toggled the checkpoint flag we must have cleared it.
67 uint16_t flag_change = new_state_and_flags.as_struct.flags ^ old_state_and_flags.as_struct.flags;
68 if (UNLIKELY((flag_change & kCheckpointRequest) != 0)) {
69 RunCheckpointFunction();
70 }
71 // Release share on mutator_lock_.
72 Locks::mutator_lock_->SharedUnlock(this);
73}
74
75inline ThreadState Thread::TransitionFromSuspendedToRunnable() {
76 bool done = false;
77 union StateAndFlags old_state_and_flags = state_and_flags_;
78 int16_t old_state = old_state_and_flags.as_struct.state;
79 DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable);
80 do {
81 Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC..
82 old_state_and_flags = state_and_flags_;
83 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
84 if (UNLIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0)) {
85 // Wait while our suspend count is non-zero.
86 MutexLock mu(this, *Locks::thread_suspend_count_lock_);
87 old_state_and_flags = state_and_flags_;
88 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
89 while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) {
90 // Re-check when Thread::resume_cond_ is notified.
91 Thread::resume_cond_->Wait(this);
92 old_state_and_flags = state_and_flags_;
93 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
94 }
95 DCHECK_EQ(GetSuspendCount(), 0);
96 }
97 // Re-acquire shared mutator_lock_ access.
98 Locks::mutator_lock_->SharedLock(this);
99 // Atomically change from suspended to runnable if no suspend request pending.
100 old_state_and_flags = state_and_flags_;
101 DCHECK_EQ(old_state_and_flags.as_struct.state, old_state);
102 if (LIKELY((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0)) {
103 union StateAndFlags new_state_and_flags = old_state_and_flags;
104 new_state_and_flags.as_struct.state = kRunnable;
105 // CAS the value without a memory barrier, that occurred in the lock above.
106 done = android_atomic_cas(old_state_and_flags.as_int, new_state_and_flags.as_int,
107 &state_and_flags_.as_int) == 0;
108 }
109 if (UNLIKELY(!done)) {
110 // Failed to transition to Runnable. Release shared mutator_lock_ access and try again.
111 Locks::mutator_lock_->SharedUnlock(this);
112 }
113 } while (UNLIKELY(!done));
114 return static_cast<ThreadState>(old_state);
115}
116
117} // namespace art
118
119#endif // ART_SRC_THREAD_INL_H_