Concurrency Definitions Implemented

I just finished implementing concurrency definitions. This is an alternative view on external XML files, as explained previously: Instead of listing every class and method, and then saying what it may or may not do, concurrency definitions define a set of invariants, and then list methods and classes that must maintain those invariants.

I basically rotated the whole picture by 90 degrees, or to quote the example I used to explain the concept to my girlfriend:

Previously, I had a list of countries, and for each country, I’d say what someone does or does not do there. For example:

  • United States:
    • People drive on the right side of the road
    • People may not drink in public
    • People speak English natively
  • England
    • People don’t drive on the right side of the road
    • People may drink in public
    • People speak English natively
  • Germany
    • People drive on the right side of the road
    • People may drink in public
    • People don’t speak English natively

Note that these are all either-or properties; if someone drives on the right side of the road, then he implicitly doesn’t drive on the left side. That’s why I’ve listed some properties in italics; stating them isn’t necessary, because the opposite wasn’t said.

This is a very natural representation if you’re in a certain country and want to find out what somebody does or doesn’t do. However, if you’re compiling lists about these things, chances are it’s a whole lot easier to compile a list about driving, a list about drinking, and a list about speaking English, and then listing the countries in each category:

  • People drive on the right side of the road:
    • United States
    • Germany
  • People may drink in public:
    • England
    • Germany
  • People speak English natively:
    • United States
    • England

I claim this list is easier to compile, because you have to think about just one thing, and then go through the list countries. In the other, original approach you would enumerate the countries, and then try to list every property you can come up with.

The original approach (I describe it as class-based or <threadcheck> approach) was well-suited for instrumenting class files, because the instrumentor was basically a traveler through all the classes and methods, and what the instrumentor wanted to know was “what can I do here?” I therefore don’t regret writing it that way, and in fact, it’s still written that way. It’s just a much more efficient format.

However, when I look at the current Java API annotations, then I don’t get a clear picture of what may be done and what may not be done. I just don’t see the categories, the invariants. All I see again are classes and methods, and that brings us back to the original problem: The boundaries between classes are not the same as the boundaries between threads.

I just converted the same annotations in the XML file shown in the other poast from the old format to the new format (I call it as concurrency definition-based or <threadcheckLdef> approach), and I must say I find it significantly easier to understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?xml version="1.0" encoding="UTF-8"?>
<concutest>
<!-- invariant: only event thread after component has been realized -->
<?xml version="1.0" encoding="UTF-8"?>
<concutest>
<threadcheck:def>
    <invariant>
      <eventThread type="only\_after\_realized"/>
    </invariant>
    <class name="javax.swing.JComponent"/>
    <class name="javax.swing.JTable"/>
    <class name="javax.swing.table.JTableHeader"/>
  </threadcheck:def>

<!-- invariant: any thread may run! -->
  <threadcheck:def>
    <invariant>
      <name type="only" value=".*"/>
    </invariant>
    <method name="javax.swing.text.DefaultCaret" sig="repaint()V"/>
    <method name="javax.swing.text.DefaultStyledDocument"
           sig="setParagraphAttributes(IILjavax/swing/text/AttributeSet;Z)V"/>
    <method name="javax.swing.undo.UndoManager" sig="canUndoOrRedo()Z"/>
    <method name="javax.swing.undo.UndoManager" sig="redo()V"/>
    <method name="java.awt.Component" sig="repaint()V"/>
    <method name="javax.swing.JTextPane"
           sig="setParagraphAttributes(Ljavax/swing/text/AttributeSet;Z)V"/>
    <method name="javax.swing.text.StyleContext"
           sig="reclaim(Ljavax/swing/text/AttributeSet;)V"/>
    <method name="javax.swing.text.StyleContext"
           sig="removeAttribute(Ljavax/swing/text/AttributeSet;Ljava/lang/Object;)
                Ljavax/swing/text/AttributeSet;"/>

    <method name="javax.swing.undo.UndoManager" sig="end()V"/>
    <method name="javax.swing.JTextPane"
           sig="setLogicalStyle(Ljavax/swing/text/Style;)V"/>
    <method name="javax.swing.undo.UndoManager" sig="undoOrRedo()V"/>
    <method name="javax.swing.text.DefaultStyledDocument"
           sig="insert(I[Ljavax/swing/text/DefaultStyledDocument$ElementSpec;)V"/>
    <method name="javax.swing.text.StyleContext"
           sig="removeAttributes(Ljavax/swing/text/AttributeSet;
                Ljavax/swing/text/AttributeSet;)Ljavax/swing/text/AttributeSet;"/>

    <method name="javax.swing.JTextPane"
           sig="setCharacterAttributes(Ljavax/swing/text/AttributeSet;Z)V"/>
    <method name="javax.swing.undo.UndoManager" sig="canUndo()Z"/>
    <method name="javax.swing.JTextPane"
           sig="insertComponent(Ljava/awt/Component;)V"/>
    <method name="javax.swing.JComponent" sig="revalidate()V"/>
    <method name="javax.swing.undo.UndoManager"
           sig="addEdit(Ljavax/swing/undo/UndoableEdit;)Z"/>
    <method name="javax.swing.text.DefaultStyledDocument"
           sig="setLogicalStyle(ILjavax/swing/text/Style;)V"/>
    <method name="javax.swing.text.PlainDocument"
           sig="insertString(ILjava/lang/String;Ljavax/swing/text/AttributeSet;)V"/>
    <method name="javax.swing.text.StyleContext"
           sig="addAttribute(Ljavax/swing/text/AttributeSet;
                Ljava/lang/Object;Ljava/langObject;)Ljavax/swing/text/AttributeSet;"/>

    <method name="javax.swing.text.DefaultStyledDocument"
           sig="setCharacterAttributes(IILjavax/swing/text/AttributeSet;Z)V"/>
    <method name="javax.swing.undo.UndoManager"
           sig="getUndoPresentationName()Ljava/lang/String;"/>
    <method name="javax.swing.text.Document" sig="render(Ljava/lang/Runnable;)V"/>
    <method name="javax.swing.undo.UndoManager" sig="canRedo()Z"/>
    <method name="javax.swing.undo.UndoManager" sig="undo()V"/>
    <method name="javax.swing.text.StyleContext"
           sig="removeAttributes(Ljavax/swing/text/AttributeSet;
                Ljava/util/Enumeration;)Ljavax/swing/text/AttributeSet;"/>

    <method name="javax.swing.undo.UndoManager" sig="discardAllEdits()V"/>
    <method name="javax.swing.JTextArea" sig="replaceRange(Ljava/lang/String;II)V"/>
    <method name="javax.swing.text.html.HTMLEditorKit$Parser"
           sig="parse(Ljava/io/Reader;
                Ljavax/swing/text/html/HTMLEditorKit$ParserCallback;Z)V"/>

    <method name="javax.swing.undo.UndoManager" sig="getLimit()I"/>
    <method name="javax.swing.text.JTextComponent" sig="setText(Ljava/lang/String;)V"/>
    <method name="javax.swing.undo.UndoManager"
           sig="getRedoPresentationName()Ljava/lang/String;"/>
    <method name="javax.swing.text.JTextComponent"
           sig="replaceSelection(Ljava/lang/String;)V"/>
    <method name="javax.swing.JTextArea" sig="append(Ljava/lang/String;)V"/>
    <method name="javax.swing.undo.UndoManager"
           sig="getUndoOrRedoPresentationName()Ljava/lang/String;"/>
    <method name="java.awt.Component" sig="invalidate()V"/>
    <method name="javax.swing.text.StyleContext"
           sig="addAttributes(Ljavax/swing/text/AttributeSet;
                Ljavax/swing/text/AttributeSet;)Ljavax/swing/text/AttributeSet;"/>

    <method name="javax.swing.JTextArea" sig="insert(Ljava/lang/String;I)V"/>
    <method name="javax.swing.undo.UndoManager" sig="setLimit(I)V"/>
    <method name="javax.swing.JTextPane" sig="insertIcon(Ljavax/swing/Icon;)V"/>
  </threadcheck:def>

<!-- invariant: only event thread may run! -->
  <threadcheck:def>
    <invariant>
      <eventThread type="only"/>
    </invariant>
    <class name="javax.swing.table.TableColumn"/>
    <class name="javax.swing.tree.TreeCellRenderer"/>
    <class name="javax.swing.tree.AbstractLayoutCache"/>
    <class name="javax.swing.table.AbstractTableModel"/>
    <class name="javax.swing.tree.RowMapper"/>
    <class name="javax.swing.tree.DefaultTreeCellEditor"/>
    <class name="javax.swing.tree.TreeCellEditor"/>
    <class name="javax.swing.tree.DefaultTreeCellRenderer"/>
    <class name="javax.swing.table.DefaultTableCellRenderer"/>
    <class name="javax.swing.tree.VariableHeightLayoutCache"/>
    <class name="javax.swing.table.TableCellEditor"/>
    <class name="javax.swing.table.TableModel"/>
    <class name="javax.swing.tree.TreeSelectionModel"/>
    <class name="javax.swing.table.DefaultTableModel"/>
    <class name="javax.swing.tree.TreePath"/>
    <class name="javax.swing.table.DefaultTableCellRenderer$UIResource"/>
    <class name="javax.swing.table.TableColumnModel"/>
    <class name="javax.swing.tree.MutableTreeNode"/>
    <class name="javax.swing.table.DefaultTableColumnModel"/>
    <class name="javax.swing.tree.FixedHeightLayoutCache"/>
    <class name="javax.swing.tree.TreeModel"/>
    <class name="javax.swing.table.TableCellRenderer"/>
    <class name="javax.swing.tree.DefaultTreeModel"/>
    <class name="javax.swing.tree.DefaultMutableTreeNode"/>
    <class name="javax.swing.tree.TreeNode"/>
    <class name="javax.swing.tree.DefaultTreeSelectionModel"/>
    <class name="javax.swing.tree.AbstractLayoutCache$NodeDimensions"/>
    <method name="javax.swing.plaf.TextUI"
           sig="modelToView(Ljavax/swing/text/JTextComponent;
                ILjavax/swing/text/Position$Bias;)Ljava/awt/Rectangle;"/>

    <method name="javax.swing.plaf.TextUI"
           sig="modelToView(Ljavax/swing/text/JTextComponent;I)Ljava/awt/Rectangle;"/>
    <method name="javax.swing.text.DefaultCaret" sig="repaintNewCaret()V"/>
  </threadcheck:def>
</concutest>

than the old XML file (reproduced here for your enjoyment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0" encoding="UTF-8"?>
<concutest>
  <threadcheck>
    <javax>
      <swing>
        <plaf>
          <TextUI>
            <class>
              <method sig="modelToView(Ljavax/swing/text/JTextComponent;I)
                Ljava/awt/Rectangle;"
>
                <eventThread type="only" value="true"/>
              </method>
              <method sig="modelToView(Ljavax/swing/text/JTextComponent;I
                Ljavax/swing/text/Position$Bias;)Ljava/awt/Rectangle;"
>
                <eventThread type="only" value="true"/>
              </method>
            </class>
          </TextUI>
        </plaf>
        <text>
          <DefaultCaret>
            <class>
              <method sig="repaintNewCaret()V">
                <eventThread type="only" value="true"/>
              </method>
              <method sig="repaint()V">
                <eventThread type="only" value=".*"/>
              </method>
            </class>
          </DefaultCaret>
          <JTextComponent>
            <class>
              <method sig="replaceSelection(Ljava/lang/String;)V">
                <name type="only" value=".*"/>
              </method>
              <method sig="setText(Ljava/lang/String;)V">
                <name type="only" value=".*"/>
              </method>
            </class>
          </JTextComponent>
          <Document>
            <class>
              <method sig="render(Ljava/lang/Runnable;)V">
                <name type="only" value=".*"/>
              </method>
            </class>
          </Document>
          <PlainDocument>
            <class>
              <method sig="insertString(ILjava/lang/String;
                Ljavax/swing/text/AttributeSet;)V"
>
                <name type="only" value=".*"/>
              </method>
            </class>
          </PlainDocument>
          <DefaultStyledDocument>
            <class>
              <method sig="insert(I[Ljavax/swing/text/DefaultStyledDocument
                $ElementSpec;)V"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="setLogicalStyle(ILjavax/swing/text/Style;)V">
                <name type="only" value=".*"/>
              </method>
              <method sig="setCharacterAttributes(IILjavax/swing/text/AttributeSet;Z)V">
                <name type="only" value=".*"/>
              </method>
              <method sig="setParagraphAttributes(IILjavax/swing/text/AttributeSet;Z)V">
                <name type="only" value=".*"/>
              </method>
            </class>
          </DefaultStyledDocument>
          <StyleContext>
            <class>
              <method sig="addAttribute(Ljavax/swing/text/AttributeSet;Ljava/lang/Object;
                Ljava/langObject;)Ljavax/swing/text/AttributeSet;"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="addAttributes(Ljavax/swing/text/AttributeSet;
                Ljavax/swing/text/AttributeSet;)Ljavax/swing/text/AttributeSet;"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="removeAttribute(Ljavax/swing/text/AttributeSet;
                Ljava/lang/Object;)Ljavax/swing/text/AttributeSet;"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="removeAttributes(Ljavax/swing/text/AttributeSet;
                Ljava/util/Enumeration;)Ljavax/swing/text/AttributeSet;"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="removeAttributes(Ljavax/swing/text/AttributeSet;
                Ljavax/swing/text/AttributeSet;)Ljavax/swing/text/AttributeSet;"
>
                <name type="only" value=".*"/>
              </method>
              <method sig="reclaim(Ljavax/swing/text/AttributeSet;)V">
                <name type="only" value=".*"/>
              </method>
            </class>
          </StyleContext>
          <html>
            <HTMLEditorKit-Parser>
              <class>
                <method sig="parse(Ljava/io/Reader;Ljavax/swing/text/html/
                  HTMLEditorKit$ParserCallback;Z)V"
>
                  <name type="only" value=".*"/>
                </method>
              </class>
            </HTMLEditorKit-Parser>
          </html>
        </text>
        <undo>
          <UndoManager>
            <class>
              <method sig="addEdit(Ljavax/swing/undo/UndoableEdit;)Z">
                <name type="only" value=".*"/>
              </method>
              <method sig="canRedo()Z">
                <name type="only" value=".*"/>
              </method>
              <method sig="canUndo()Z">
                <name type="only" value=".*"/>
              </method>
              <method sig="canUndoOrRedo()Z">
                <name type="only" value=".*"/>
              </method>
              <method sig="discardAllEdits()V">
                <name type="only" value=".*"/>
              </method>
              <method sig="end()V">
                <name type="only" value=".*"/>
              </method>
              <method sig="getLimit()I">
                <name type="only" value=".*"/>
              </method>
              <method sig="getRedoPresentationName()Ljava/lang/String;">
                <name type="only" value=".*"/>
              </method>
              <method sig="getUndoPresentationName()Ljava/lang/String;">
                <name type="only" value=".*"/>
              </method>
              <method sig="getUndoOrRedoPresentationName()Ljava/lang/String;">
                <name type="only" value=".*"/>
              </method>
              <method sig="redo()V">
                <name type="only" value=".*"/>
              </method>
              <method sig="setLimit(I)V">
                <name type="only" value=".*"/>
              </method>
              <method sig="undo()V">
                <name type="only" value=".*"/>
              </method>
              <method sig="undoOrRedo()V">
                <name type="only" value=".*"/>
              </method>
            </class>
          </UndoManager>
        </undo>
        <JComponent>
          <class>
            <method sig="revalidate()V">
              <name type="only" value=".*"/>
            </method>
          </class>
        </JComponent>
        <JTextArea>
          <class>
            <method sig="append(Ljava/lang/String;)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="insert(Ljava/lang/String;I)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="replaceRange(Ljava/lang/String;II)V">
              <name type="only" value=".*"/>
            </method>
          </class>
        </JTextArea>
        <JTextPane>
          <class>
            <method sig="insertComponent(Ljava/awt/Component;)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="insertIcon(Ljavax/swing/Icon;)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="setLogicalStyle(Ljavax/swing/text/Style;)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="setCharacterAttributes(Ljavax/swing/text/AttributeSet;Z)V">
              <name type="only" value=".*"/>
            </method>
            <method sig="setParagraphAttributes(Ljavax/swing/text/AttributeSet;Z)V">
              <name type="only" value=".*"/>
            </method>
          </class>
        </JTextPane>
      </swing>
    </javax>
    <java>
      <awt>
        <Component>
          <class>
            <method sig="repaint()V">
              <name type="only" value=".*"/>
            </method>
            <method sig="invalidate()V">
              <name type="only" value=".*"/>
            </method>
          </class>
        </Component>
      </awt>
    </java>
  </threadcheck>
</concutest>

In the newer version, I can clearly see the three different invariants I’m using. I had no idea what I’d been using in the old one, without actually going through it line by line.

Making this happen caused a major refactoring that touched the XML configuration library and loading external XML annotations. I factored out most common code, and now it is even possible to convert one XML representation into the other and back, and to merge them while eliminating overlap. Annotations can now also be printed out just like Java code, which helps a lot in generating human-readable output.

My MacBook has been silently working on a remote backup to my server’s backup drive for, oh, 8 hours now, and it’s only half done. I should have plugged in the Ethernet cable first.

I’m cautiously optimistic about my health. Yesterday and today I haven’t had to cough very much anymore, and there was hardly any mucus. Both times when I got up I had a horrible muscle cramp in my lower right leg, though, one that made me scream out loud (“sol”, like “lol”?). After stretching a bit, I limped into the bathroom and took a hot bath.

I also made a bunch of backups in the past days and finished a small home-improvement project: I finally connected the line-out of my media server to the stereo in my bedroom. The sound quality has improved remarkably when not only my two small computer speakers are playing. I ran the speaker wire up in the corner of the walls, along the edge of the ceiling, and then through the air conditioning duct. Most of it is hidden.

Of course, it’s not as fancy or functional as a Squeezebox, but I did this for about a tenth of the price.

It’s 9 AM now, I need to go to bed. I think I’ll let my MacBook finish its backup. Good night/morning.

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Concurrent Unit Testing, Uncategorized. Bookmark the permalink.

Leave a Reply