y987425112 opened a new pull request #145: Adding volatile keywords to member 
variables
URL: https://github.com/apache/tomcat/pull/145
 
 
   The semantics of the Java programming language allow compilers and 
microprocessors to
   perform optimizations that can interact with incorrectly synchronized code 
in ways that can
   produce behaviors that seem 
paradoxical.[https://edelivery.oracle.com/otn-pub/jcp/memory_model-1.0-pfd-spec-oth-JSpec/memory_model-1_0-pfd-spec.pdf](url)
 page6
   
   For example 
   ```
   org.apache.catalina.core.StandardContext.java
   
   private String applicationListeners[] = new String[0];
   .........
    @Override
   
     public void addApplicationListener(String listener) {
   ```
   
           synchronized (applicationListenersLock) {
               String results[] = new String[applicationListeners.length + 1];
               for (int i = 0; i < applicationListeners.length; i++) {
                   if (listener.equals(applicationListeners[i])) {
                       
log.info(sm.getString("standardContext.duplicateListener",listener));
                       return;
                   }
                   results[i] = applicationListeners[i];
               }
               results[applicationListeners.length] = listener;
               applicationListeners = results;
           }
           fireContainerEvent("addApplicationListener", listener);
       }
   
   We expect this line of code(`applicationListeners = results;`) to be 
executed eventually
   But because of instruction reordering,The following operating procedures are 
also allowed to exis
   ```
   @Override
       public void addApplicationListener(String listener) {
   ```
   
           synchronized (applicationListenersLock) {
               String results[] = new String[applicationListeners.length + 1];
               applicationListeners = results;
               for (int i = 0; i < applicationListeners.length; i++) {
                   if (listener.equals(applicationListeners[i])) {
                       
log.info(sm.getString("standardContext.duplicateListener",listener));
                       return;
                   }
                   results[i] = applicationListeners[i];
               }
               results[applicationListeners.length] = listener;
               
           }
           fireContainerEvent("addApplicationListener", listener);
   
       }
   If this happens, it will be different from our expectations.
   
   If volatile modifiers are added to variables, instruction reordering is 
prohibited
   `private String applicationListeners[] = new String[0]; `-->
   ` private volatile String applicationListeners[] = new String[0];`
   Satisfy the purpose of the last execution of this line of 
code(`applicationListeners = results;`).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to