Skip to content

Commit 56c4c44

Browse files
bradfitzAndroid Code Review
authored andcommitted
Merge "Lazy initialization must be synchronized to avoid parallel instances cretation."
2 parents b98c8fc + 17eb45f commit 56c4c44

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

telephony/java/com/android/internal/telephony/cat/CatService.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public class CatService extends Handler implements AppInterface {
118118
private static IccRecords mIccRecords;
119119

120120
// Service members.
121+
// Protects singleton instance lazy initialization.
122+
private static final Object sInstanceLock = new Object();
121123
private static CatService sInstance;
122124
private CommandsInterface mCmdIf;
123125
private Context mContext;
@@ -515,26 +517,28 @@ private void eventDownload(int event, int sourceId, int destinationId,
515517
*/
516518
public static CatService getInstance(CommandsInterface ci, IccRecords ir,
517519
Context context, IccFileHandler fh, IccCard ic) {
518-
if (sInstance == null) {
519-
if (ci == null || ir == null || context == null || fh == null
520-
|| ic == null) {
521-
return null;
520+
synchronized (sInstanceLock) {
521+
if (sInstance == null) {
522+
if (ci == null || ir == null || context == null || fh == null
523+
|| ic == null) {
524+
return null;
525+
}
526+
HandlerThread thread = new HandlerThread("Cat Telephony service");
527+
thread.start();
528+
sInstance = new CatService(ci, ir, context, fh, ic);
529+
CatLog.d(sInstance, "NEW sInstance");
530+
} else if ((ir != null) && (mIccRecords != ir)) {
531+
CatLog.d(sInstance, "Reinitialize the Service with SIMRecords");
532+
mIccRecords = ir;
533+
534+
// re-Register for SIM ready event.
535+
mIccRecords.registerForRecordsLoaded(sInstance, MSG_ID_ICC_RECORDS_LOADED, null);
536+
CatLog.d(sInstance, "sr changed reinitialize and return current sInstance");
537+
} else {
538+
CatLog.d(sInstance, "Return current sInstance");
522539
}
523-
HandlerThread thread = new HandlerThread("Cat Telephony service");
524-
thread.start();
525-
sInstance = new CatService(ci, ir, context, fh, ic);
526-
CatLog.d(sInstance, "NEW sInstance");
527-
} else if ((ir != null) && (mIccRecords != ir)) {
528-
CatLog.d(sInstance, "Reinitialize the Service with SIMRecords");
529-
mIccRecords = ir;
530-
531-
// re-Register for SIM ready event.
532-
mIccRecords.registerForRecordsLoaded(sInstance, MSG_ID_ICC_RECORDS_LOADED, null);
533-
CatLog.d(sInstance, "sr changed reinitialize and return current sInstance");
534-
} else {
535-
CatLog.d(sInstance, "Return current sInstance");
540+
return sInstance;
536541
}
537-
return sInstance;
538542
}
539543

540544
/**

0 commit comments

Comments
 (0)