| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #define LOG_TAG "StateQueue" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 20 | #include "Configuration.h" |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 21 | #include <time.h> |
| 22 | #include <cutils/atomic.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include "StateQueue.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 28 | #ifdef STATE_QUEUE_DUMP |
| 29 | void StateQueueObserverDump::dump(int fd) |
| 30 | { |
| Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 31 | dprintf(fd, "State queue observer: stateChanges=%u\n", mStateChanges); |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void StateQueueMutatorDump::dump(int fd) |
| 35 | { |
| Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 36 | dprintf(fd, "State queue mutator: pushDirty=%u pushAck=%u blockedSequence=%u\n", |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 37 | mPushDirty, mPushAck, mBlockedSequence); |
| 38 | } |
| 39 | #endif |
| 40 | |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 41 | // Observer APIs |
| 42 | |
| 43 | template<typename T> const T* StateQueue<T>::poll() |
| 44 | { |
| Hans Boehm | f39b560 | 2014-07-16 12:13:16 -0700 | [diff] [blame] | 45 | const T *next = (const T *) atomic_load_explicit(&mNext, memory_order_acquire); |
| 46 | |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 47 | if (next != mCurrent) { |
| 48 | mAck = next; // no additional barrier needed |
| 49 | mCurrent = next; |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 50 | #ifdef STATE_QUEUE_DUMP |
| 51 | mObserverDump->mStateChanges++; |
| 52 | #endif |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 53 | } |
| 54 | return next; |
| 55 | } |
| 56 | |
| 57 | // Mutator APIs |
| 58 | |
| 59 | template<typename T> T* StateQueue<T>::begin() |
| 60 | { |
| 61 | ALOG_ASSERT(!mInMutation, "begin() called when in a mutation"); |
| 62 | mInMutation = true; |
| 63 | return mMutating; |
| 64 | } |
| 65 | |
| 66 | template<typename T> void StateQueue<T>::end(bool didModify) |
| 67 | { |
| 68 | ALOG_ASSERT(mInMutation, "end() called when not in a mutation"); |
| 69 | ALOG_ASSERT(mIsInitialized || didModify, "first end() must modify for initialization"); |
| 70 | if (didModify) { |
| 71 | mIsDirty = true; |
| 72 | mIsInitialized = true; |
| 73 | } |
| 74 | mInMutation = false; |
| 75 | } |
| 76 | |
| 77 | template<typename T> bool StateQueue<T>::push(StateQueue<T>::block_t block) |
| 78 | { |
| 79 | #define PUSH_BLOCK_ACK_NS 3000000L // 3 ms: time between checks for ack in push() |
| 80 | // FIXME should be configurable |
| 81 | static const struct timespec req = {0, PUSH_BLOCK_ACK_NS}; |
| 82 | |
| 83 | ALOG_ASSERT(!mInMutation, "push() called when in a mutation"); |
| 84 | |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 85 | #ifdef STATE_QUEUE_DUMP |
| 86 | if (block == BLOCK_UNTIL_ACKED) { |
| 87 | mMutatorDump->mPushAck++; |
| 88 | } |
| 89 | #endif |
| 90 | |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 91 | if (mIsDirty) { |
| 92 | |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 93 | #ifdef STATE_QUEUE_DUMP |
| 94 | mMutatorDump->mPushDirty++; |
| 95 | #endif |
| 96 | |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 97 | // wait for prior push to be acknowledged |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 98 | if (mExpecting != nullptr) { |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 99 | #ifdef STATE_QUEUE_DUMP |
| 100 | unsigned count = 0; |
| 101 | #endif |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 102 | for (;;) { |
| 103 | const T *ack = (const T *) mAck; // no additional barrier needed |
| 104 | if (ack == mExpecting) { |
| 105 | // unnecessary as we're about to rewrite |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 106 | //mExpecting = nullptr; |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | if (block == BLOCK_NEVER) { |
| 110 | return false; |
| 111 | } |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 112 | #ifdef STATE_QUEUE_DUMP |
| 113 | if (count == 1) { |
| 114 | mMutatorDump->mBlockedSequence++; |
| 115 | } |
| 116 | ++count; |
| 117 | #endif |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 118 | nanosleep(&req, nullptr); |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 119 | } |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 120 | #ifdef STATE_QUEUE_DUMP |
| 121 | if (count > 1) { |
| 122 | mMutatorDump->mBlockedSequence++; |
| 123 | } |
| 124 | #endif |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // publish |
| Hans Boehm | f39b560 | 2014-07-16 12:13:16 -0700 | [diff] [blame] | 128 | atomic_store_explicit(&mNext, (uintptr_t)mMutating, memory_order_release); |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 129 | mExpecting = mMutating; |
| 130 | |
| 131 | // copy with circular wraparound |
| 132 | if (++mMutating >= &mStates[kN]) { |
| 133 | mMutating = &mStates[0]; |
| 134 | } |
| 135 | *mMutating = *mExpecting; |
| 136 | mIsDirty = false; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | // optionally wait for this push or a prior push to be acknowledged |
| 141 | if (block == BLOCK_UNTIL_ACKED) { |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 142 | if (mExpecting != nullptr) { |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 143 | #ifdef STATE_QUEUE_DUMP |
| 144 | unsigned count = 0; |
| 145 | #endif |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 146 | for (;;) { |
| 147 | const T *ack = (const T *) mAck; // no additional barrier needed |
| 148 | if (ack == mExpecting) { |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 149 | mExpecting = nullptr; |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 150 | break; |
| 151 | } |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 152 | #ifdef STATE_QUEUE_DUMP |
| 153 | if (count == 1) { |
| 154 | mMutatorDump->mBlockedSequence++; |
| 155 | } |
| 156 | ++count; |
| 157 | #endif |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 158 | nanosleep(&req, nullptr); |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 159 | } |
| Glenn Kasten | 3999308 | 2012-05-31 13:40:27 -0700 | [diff] [blame] | 160 | #ifdef STATE_QUEUE_DUMP |
| 161 | if (count > 1) { |
| 162 | mMutatorDump->mBlockedSequence++; |
| 163 | } |
| 164 | #endif |
| Glenn Kasten | dc998c8 | 2012-03-23 18:53:59 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | } // namespace android |
| 172 | |
| Andy Hung | 528b050 | 2023-05-24 18:30:17 -0700 | [diff] [blame] | 173 | // Instantiate StateQueue template for the types we need. |
| 174 | // This needs to be done in the same translation unit as the template |
| 175 | // method definitions above. |
| 176 | |
| 177 | #include "FastCaptureState.h" |
| 178 | #include "FastMixerState.h" |
| 179 | |
| 180 | namespace android { |
| 181 | template class StateQueue<FastCaptureState>; |
| 182 | template class StateQueue<FastMixerState>; |
| 183 | } // namespace android |