• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PipeReader"
18 //#define LOG_NDEBUG 0
19 
20 #include <cutils/compiler.h>
21 #include <utils/Log.h>
22 #include "PipeReader.h"
23 
24 namespace android {
25 
PipeReader(Pipe & pipe)26 PipeReader::PipeReader(Pipe& pipe) :
27         NBAIO_Source(pipe.mFormat),
28         mPipe(pipe),
29         // any data already in the pipe is not visible to this PipeReader
30         mFront(android_atomic_acquire_load(&pipe.mRear)),
31         mFramesOverrun(0),
32         mOverruns(0)
33 {
34     android_atomic_inc(&pipe.mReaders);
35 }
36 
~PipeReader()37 PipeReader::~PipeReader()
38 {
39     int32_t readers = android_atomic_dec(&mPipe.mReaders);
40     ALOG_ASSERT(readers > 0);
41 }
42 
availableToRead()43 ssize_t PipeReader::availableToRead()
44 {
45     if (CC_UNLIKELY(!mNegotiated)) {
46         return NEGOTIATE;
47     }
48     int32_t rear = android_atomic_acquire_load(&mPipe.mRear);
49     // read() is not multi-thread safe w.r.t. itself, so no mutex or atomic op needed to read mFront
50     size_t avail = rear - mFront;
51     if (CC_UNLIKELY(avail > mPipe.mMaxFrames)) {
52         // Discard 1/16 of the most recent data in pipe to avoid another overrun immediately
53         int32_t oldFront = mFront;
54         mFront = rear - mPipe.mMaxFrames + (mPipe.mMaxFrames >> 4);
55         mFramesOverrun += (size_t) (mFront - oldFront);
56         ++mOverruns;
57         return OVERRUN;
58     }
59     return avail;
60 }
61 
read(void * buffer,size_t count)62 ssize_t PipeReader::read(void *buffer, size_t count)
63 {
64     ssize_t avail = availableToRead();
65     if (CC_UNLIKELY(avail <= 0)) {
66         return avail;
67     }
68     // An overrun can occur from here on and be silently ignored,
69     // but it will be caught at next read()
70     if (CC_LIKELY(count > (size_t) avail)) {
71         count = avail;
72     }
73     size_t front = mFront & (mPipe.mMaxFrames - 1);
74     size_t red = mPipe.mMaxFrames - front;
75     if (CC_LIKELY(red > count)) {
76         red = count;
77     }
78     // In particular, an overrun during the memcpy will result in reading corrupt data
79     memcpy(buffer, (char *) mPipe.mBuffer + (front << mBitShift), red << mBitShift);
80     // We could re-read the rear pointer here to detect the corruption, but why bother?
81     if (CC_UNLIKELY(front + red == mPipe.mMaxFrames)) {
82         if (CC_UNLIKELY((count -= red) > front)) {
83             count = front;
84         }
85         if (CC_LIKELY(count > 0)) {
86             memcpy((char *) buffer + (red << mBitShift), mPipe.mBuffer, count << mBitShift);
87             red += count;
88         }
89     }
90     mFront += red;
91     mFramesRead += red;
92     return red;
93 }
94 
95 }   // namespace android
96