Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6909

@CompileStatic in combo @WithXXXLock transform is broken

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.3.4, 2.4.0-beta-2
    • None
    • None

    Description

      This code:

      import groovy.transform.*
      
      @CompileStatic
      class Counters {
          public final Map<String,Integer> map = [:].withDefault { 0 }
      
          @WithReadLock
          int get(String id) {
              map.get(id)
          }
      
          @WithWriteLock
          void add(String id, int num) {
              Thread.sleep(100) // emulate long computation
              map.put(id, map.get(id)+num)
          }
      }
      
      def counters = new Counters()
      assert counters.get('a') == 0
      assert counters.get('b') == 0
      
      10.times { cpt ->
          Thread.start { counters.add('a', 1) }
          def t = Thread.start {
              Thread.sleep(20)
              assert counters.get('a') == cpt+1
          }
          t.join(250)
      }
      

      gives errors such as:

      assert counters.get('a') == cpt+1
             |        |        |  |  |
             |        2        |  5  6
             Counters@3b871f   false
      

      Attachments

        Activity

          People

            paulk Paul King
            paulk Paul King
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: