From cdff0031fee48862bb121303b293ee973b86da90 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 26 Apr 2011 23:24:44 +0000 Subject: [PATCH] Same pattern here: minimize the weirding (by having createMarshallers look like a non-weird function that just returns a useful value), and comment the weirding (in this case, only in CustomClassStreamer.{read|write}Object, since that's the only place where a race condition may occur. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6634 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/main/java/com/threerings/io/Streamer.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/threerings/io/Streamer.java b/src/main/java/com/threerings/io/Streamer.java index 07609598e..2c7276a42 100644 --- a/src/main/java/com/threerings/io/Streamer.java +++ b/src/main/java/com/threerings/io/Streamer.java @@ -325,7 +325,7 @@ public abstract class Streamer { _target = target; initConstructor(); - initMarshallers(); + _marshallers = createMarshallers(); } @Override @@ -445,9 +445,9 @@ public abstract class Streamer } /** - * Initialize the reading and writing marshallers. + * Creates and returns the reading and writing marshallers. */ - protected void initMarshallers () + protected FieldMarshaller[] createMarshallers () { // reflect on all the object's fields List fields = Lists.newArrayList(); @@ -483,7 +483,7 @@ public abstract class Streamer _fields[ii].getName() + "."); } } - _marshallers = marshallers; + return marshallers; } @Override @@ -551,7 +551,9 @@ public abstract class Streamer // otherwise, ensure the marshallers are initialized and call super if (_marshallers == null) { - super.initMarshallers(); + // this can race with other threads, but the worst that can happen is that the work + // done in createMarshallers() is duplicated + _marshallers = super.createMarshallers(); } super.writeObject(object, out, useWriter); } @@ -589,18 +591,21 @@ public abstract class Streamer // otherwise, ensure the marshallers are iniitalized and call super if (_marshallers == null) { - super.initMarshallers(); + // this can race with other threads, but the worst that can happen is that the work + // done in createMarshallers() is duplicated + _marshallers = super.createMarshallers(); } super.readObject(object, in, useReader); } @Override - protected void initMarshallers () + protected FieldMarshaller[] createMarshallers () { // we will lazy-initialize the marshallers only if needed, so we don't call super // (It's possible there is only a writer method, but the object is never read from // clients, so don't get cute and set up the marshallers at construct time if one of // the methods is null). + return null; } @Override