@@ -58,32 +58,32 @@ public abstract class Codelet implements Runnable, MemoryObserver {
5858 /**
5959 * Activation level of the Codelet. Ranges from 0.0 to 1.0d.
6060 */
61- protected double activation = 0.0d ;
61+ protected volatile double activation = 0.0d ;
6262
6363 /**
6464 * Threshold of the codelet, which is used to decide if it runs or not. If
6565 * activation is equal or greater than activation, codelet runs
6666 * proc().Ranges from 0.0 to 1.0d.
6767 */
68- protected double threshold = 0.0d ;
68+ protected volatile double threshold = 0.0d ;
6969 /**
7070 * Input memories, the ones that are read.
7171 */
72- protected List <Memory > inputs = new ArrayList <Memory >();
72+ protected volatile List <Memory > inputs = new ArrayList <Memory >();
7373 /**
7474 * Output memories, the ones that are written.
7575 */
76- protected List <Memory > outputs = new ArrayList <Memory >();
76+ protected volatile List <Memory > outputs = new ArrayList <Memory >();
7777 /**
7878 * Input memories, the ones that were broadcasted.
7979 */
80- protected List <Memory > broadcast = new ArrayList <Memory >();
80+ protected volatile List <Memory > broadcast = new ArrayList <Memory >();
8181
8282 /** defines if proc() should be automatically called in a loop */
83- protected boolean loop = true ; //
83+ protected volatile boolean loop = true ; //
8484
8585 /** defines if codelet is a memory observer (runs when memory input changes) */
86- protected boolean isMemoryObserver = false ; //
86+ protected volatile boolean isMemoryObserver = false ; //
8787
8888 /**
8989 * If the proc() method is set to be called automatically in a loop, this
@@ -97,7 +97,7 @@ public abstract class Codelet implements Runnable, MemoryObserver {
9797 * A codelet is a priori enabled to run its proc(). However, if it tries to
9898 * read from a given output and fails, it becomes not able to do so.
9999 */
100- private boolean enabled = true ;
100+ private volatile boolean enabled = true ;
101101
102102 /** Must be zero for this codelet to be enabled */
103103 private int enable_count = 0 ;
@@ -106,10 +106,10 @@ public abstract class Codelet implements Runnable, MemoryObserver {
106106 protected String name = Thread .currentThread ().getName ();
107107
108108 /** The time for the last proc() execution for profiling purposes */
109- long laststarttime = 0l ;
109+ volatile long laststarttime = 0l ;
110110
111111 /** This variable is a safe lock for multithread access */
112- public Lock lock = new ReentrantLock ();
112+ public volatile Lock lock = new ReentrantLock ();
113113
114114 /**
115115 * This method is used in every Codelet to capture input, broadcast and
@@ -131,7 +131,7 @@ public abstract class Codelet implements Runnable, MemoryObserver {
131131 */
132132 public abstract void proc ();
133133
134- private Timer timer = new Timer ();
134+ private volatile Timer timer = new Timer ();
135135
136136 /**
137137 * Option for profiling execution times
@@ -172,8 +172,10 @@ public synchronized void run() {
172172 * Starts this codelet execution.
173173 */
174174 public synchronized void start () {
175+ if (isMemoryObserver == false ) {
175176 Thread t = new Thread (this );
176177 t .start ();
178+ }
177179 }
178180
179181 /**
@@ -801,17 +803,22 @@ public synchronized void setIsMemoryObserver(boolean isMemoryObserver) {
801803 this .isMemoryObserver = isMemoryObserver ;
802804 }
803805
806+ @ SuppressWarnings ("empty-statement" )
804807 public synchronized void setPublishSubscribe (boolean enable ) {
805808 if (enable ) {
806809 setIsMemoryObserver (true );
807810 for (Memory m : inputs ) {
808811 m .addMemoryObserver (this );
809812 }
810813 } else {
811- setIsMemoryObserver (false );
812814 for (Memory m : inputs ) {
813815 m .removeMemoryObserver (this );
814816 }
817+ setIsMemoryObserver (false );
818+ try { this .wait (300L ); }
819+ catch (InterruptedException e ) {
820+ // just ignore exception
821+ };
815822 run ();
816823 }
817824 }
0 commit comments