Avoid wp<>::unsafe_get() with a few exceptions
Avoid using wp<>::unsafe_get() except in a log, and other specific cases
when it's known to be safe.
Use more specific subclass types for parameters to avoid down-casts.
When a constructor or method parameter is "this" of an object that is
currently being constructed, it's better to use a raw pointer rather
than either sp<> or wp<>.
Using the raw pointer is safe, provided either:
- it is "this" of an object being constructed (which has sp<> refcount of 0),
- or the caller already holds an sp<>
The raw pointer is simpler and faster, and it avoids the problem of the
sp<> reference count being incremented and then decremented to zero on
scope exit, which would cause the object's destructor to run while the
object is still being constructed.
Also removed some dead code per a review comment.
Change-Id: I7375f64da3aec11b928c33cb01faff186252ef5e
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 2e2834c..5af163a 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -3231,7 +3231,7 @@
{
// FIXME explain this formula
int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
- OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread,
+ OutputTrack *outputTrack = new OutputTrack(thread,
this,
mSampleRate,
mFormat,
@@ -3300,7 +3300,7 @@
// TrackBase constructor must be called with AudioFlinger::mLock held
AudioFlinger::ThreadBase::TrackBase::TrackBase(
- const wp<ThreadBase>& thread,
+ ThreadBase *thread,
const sp<Client>& client,
uint32_t sampleRate,
audio_format_t format,
@@ -3456,7 +3456,7 @@
// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
AudioFlinger::PlaybackThread::Track::Track(
- const wp<ThreadBase>& thread,
+ PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -3470,11 +3470,9 @@
mAuxEffectId(0), mHasVolumeController(false)
{
if (mCblk != NULL) {
- sp<ThreadBase> baseThread = thread.promote();
- if (baseThread != 0) {
- PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get();
- mName = playbackThread->getTrackName_l();
- mMainBuffer = playbackThread->mixBuffer();
+ if (thread != NULL) {
+ mName = thread->getTrackName_l();
+ mMainBuffer = thread->mixBuffer();
}
ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
if (mName < 0) {
@@ -3760,7 +3758,7 @@
sp<AudioFlinger::PlaybackThread::TimedTrack>
AudioFlinger::PlaybackThread::TimedTrack::create(
- const wp<ThreadBase>& thread,
+ PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -3785,7 +3783,7 @@
}
AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
- const wp<ThreadBase>& thread,
+ PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -3926,15 +3924,6 @@
return INVALID_OPERATION;
}
- // get ahold of the output stream that these samples will be written to
- sp<ThreadBase> thread = mThread.promote();
- if (thread == NULL) {
- buffer->raw = 0;
- buffer->frameCount = 0;
- return INVALID_OPERATION;
- }
- PlaybackThread* playbackThread = static_cast<PlaybackThread*>(thread.get());
-
Mutex::Autolock _l(mTimedBufferQueueLock);
while (true) {
@@ -4160,7 +4149,7 @@
// RecordTrack constructor must be called with AudioFlinger::mLock held
AudioFlinger::RecordThread::RecordTrack::RecordTrack(
- const wp<ThreadBase>& thread,
+ RecordThread *thread,
const sp<Client>& client,
uint32_t sampleRate,
audio_format_t format,
@@ -4273,17 +4262,16 @@
// ----------------------------------------------------------------------------
AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
- const wp<ThreadBase>& thread,
+ PlaybackThread *playbackThread,
DuplicatingThread *sourceThread,
uint32_t sampleRate,
audio_format_t format,
uint32_t channelMask,
int frameCount)
- : Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
+ : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
mActive(false), mSourceThread(sourceThread)
{
- PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get();
if (mCblk != NULL) {
mCblk->flags |= CBLK_DIRECTION_OUT;
mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
@@ -6595,18 +6583,17 @@
#undef LOG_TAG
#define LOG_TAG "AudioFlinger::EffectModule"
-AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread,
+AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
const wp<AudioFlinger::EffectChain>& chain,
effect_descriptor_t *desc,
int id,
int sessionId)
- : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
+ : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
mStatus(NO_INIT), mState(IDLE), mSuspended(false)
{
ALOGV("Constructor %p", this);
int lStatus;
- sp<ThreadBase> thread = mThread.promote();
- if (thread == 0) {
+ if (thread == NULL) {
return;
}
@@ -7576,15 +7563,14 @@
#undef LOG_TAG
#define LOG_TAG "AudioFlinger::EffectChain"
-AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread,
+AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
int sessionId)
- : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
+ : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
{
mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
- sp<ThreadBase> thread = mThread.promote();
- if (thread == 0) {
+ if (thread == NULL) {
return;
}
mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index 50712cf..1a52de5 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -318,7 +318,7 @@
// The upper 16 bits are used for track-specific flags.
};
- TrackBase(const wp<ThreadBase>& thread,
+ TrackBase(ThreadBase *thread,
const sp<Client>& client,
uint32_t sampleRate,
audio_format_t format,
@@ -591,7 +591,7 @@
// playback track
class Track : public TrackBase {
public:
- Track( const wp<ThreadBase>& thread,
+ Track( PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -674,7 +674,7 @@
class TimedTrack : public Track {
public:
- static sp<TimedTrack> create(const wp<ThreadBase>& thread,
+ static sp<TimedTrack> create(PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -719,7 +719,7 @@
void trimTimedBufferQueue_l();
private:
- TimedTrack(const wp<ThreadBase>& thread,
+ TimedTrack(PlaybackThread *thread,
const sp<Client>& client,
audio_stream_type_t streamType,
uint32_t sampleRate,
@@ -755,7 +755,7 @@
int16_t *mBuffer;
};
- OutputTrack( const wp<ThreadBase>& thread,
+ OutputTrack(PlaybackThread *thread,
DuplicatingThread *sourceThread,
uint32_t sampleRate,
audio_format_t format,
@@ -1042,7 +1042,7 @@
// record track
class RecordTrack : public TrackBase {
public:
- RecordTrack(const wp<ThreadBase>& thread,
+ RecordTrack(RecordThread *thread,
const sp<Client>& client,
uint32_t sampleRate,
audio_format_t format,
@@ -1168,7 +1168,7 @@
// the attached track(s) to accumulate their auxiliary channel.
class EffectModule: public RefBase {
public:
- EffectModule(const wp<ThreadBase>& wThread,
+ EffectModule(ThreadBase *thread,
const wp<AudioFlinger::EffectChain>& chain,
effect_descriptor_t *desc,
int id,
@@ -1353,6 +1353,7 @@
class EffectChain: public RefBase {
public:
EffectChain(const wp<ThreadBase>& wThread, int sessionId);
+ EffectChain(ThreadBase *thread, int sessionId);
virtual ~EffectChain();
// special key used for an entry in mSuspendedEffects keyed vector