| 1 | struct Xyz { |
| 2 | mut: |
| 3 | n int |
| 4 | } |
| 5 | |
| 6 | struct Abc { |
| 7 | a shared Xyz |
| 8 | i int |
| 9 | } |
| 10 | |
| 11 | fn test_shared_struct_auto_struct() { |
| 12 | e := Abc{} |
| 13 | shared a := e.a |
| 14 | lock a { |
| 15 | a.n = 17 |
| 16 | } |
| 17 | r := rlock a { |
| 18 | a.n |
| 19 | } |
| 20 | assert r == 17 |
| 21 | } |
| 22 | |
| 23 | fn test_shared_struct_in_struct() { |
| 24 | shared y := Xyz{ |
| 25 | n: 7 |
| 26 | } |
| 27 | z := Abc{ |
| 28 | a: y |
| 29 | } |
| 30 | u := Xyz{ |
| 31 | n: 17 |
| 32 | } |
| 33 | x := Abc{ |
| 34 | a: u |
| 35 | } |
| 36 | v := Abc{ |
| 37 | a: Xyz{ |
| 38 | n: 5 |
| 39 | } |
| 40 | i: 3 |
| 41 | } |
| 42 | shared f := x.a |
| 43 | shared g := v.a |
| 44 | shared h := z.a |
| 45 | a := rlock f { |
| 46 | f.n |
| 47 | } |
| 48 | b := rlock g { |
| 49 | g.n |
| 50 | } |
| 51 | c := rlock h { |
| 52 | h.n |
| 53 | } |
| 54 | assert a == 17 |
| 55 | assert b == 5 |
| 56 | assert c == 7 |
| 57 | assert v.i == 3 |
| 58 | } |
| 59 | |
| 60 | struct Efg { |
| 61 | a shared []f64 |
| 62 | i int |
| 63 | } |
| 64 | |
| 65 | fn test_shared_auto_init_array() { |
| 66 | e := Efg{} |
| 67 | shared a := unsafe { e.a } |
| 68 | lock a { |
| 69 | a << 23.0625 |
| 70 | a << -133.25 |
| 71 | a << 0.125 |
| 72 | } |
| 73 | r := rlock a { |
| 74 | a[1] |
| 75 | } |
| 76 | assert r == -133.25 |
| 77 | } |
| 78 | |
| 79 | fn test_shared_array_in_struct() { |
| 80 | x := Efg{ |
| 81 | a: [1.25, 2.75, 7, 13.0625] |
| 82 | i: 12 |
| 83 | } |
| 84 | shared t := unsafe { x.a } |
| 85 | lock t { |
| 86 | t[2] = -1.125 |
| 87 | } |
| 88 | shared tt := unsafe { x.a } |
| 89 | v := rlock tt { |
| 90 | tt[3] |
| 91 | } |
| 92 | w := rlock tt { |
| 93 | tt[2] |
| 94 | } |
| 95 | assert v == 13.0625 |
| 96 | assert w == -1.125 |
| 97 | assert x.i == 12 |
| 98 | } |
| 99 | |
| 100 | struct Hjk { |
| 101 | m shared map[string]f64 |
| 102 | i int |
| 103 | } |
| 104 | |
| 105 | fn test_shared_auto_init_map() { |
| 106 | a := Hjk{} |
| 107 | shared m := a.m |
| 108 | lock m { |
| 109 | m['xcv'] = -31.125 |
| 110 | } |
| 111 | r := rlock m { |
| 112 | m['xcv'] |
| 113 | } |
| 114 | assert r == -31.125 |
| 115 | } |
| 116 | |
| 117 | fn test_shared_map_in_struct() { |
| 118 | x := Hjk{ |
| 119 | m: { |
| 120 | 'st': -6.0625 |
| 121 | 'xy': 12.125 |
| 122 | 'rz': 2.25 |
| 123 | } |
| 124 | i: 23 |
| 125 | } |
| 126 | shared k := x.m |
| 127 | lock k { |
| 128 | k['yxc'] = -23.5 |
| 129 | } |
| 130 | shared p := x.m |
| 131 | a := rlock p { |
| 132 | p['xy'] |
| 133 | } |
| 134 | b := rlock p { |
| 135 | p['yxc'] |
| 136 | } |
| 137 | assert a == 12.125 |
| 138 | assert b == -23.5 |
| 139 | assert x.i == 23 |
| 140 | } |
| 141 | |
| 142 | fn test_array_of_shared() { |
| 143 | mut a := []shared Xyz{cap: 3} |
| 144 | a0 := Xyz{ |
| 145 | n: 3 |
| 146 | } |
| 147 | a << a0 |
| 148 | a1 := Xyz{ |
| 149 | n: 7 |
| 150 | } |
| 151 | a << a1 |
| 152 | a2 := Xyz{ |
| 153 | n: 13 |
| 154 | } |
| 155 | a << a2 |
| 156 | shared p := a[0] |
| 157 | shared q := a[2] |
| 158 | lock q { |
| 159 | q.n = -17 |
| 160 | } |
| 161 | shared r := a[2] |
| 162 | e := rlock p { |
| 163 | p.n |
| 164 | } |
| 165 | f := rlock r { |
| 166 | r.n |
| 167 | } |
| 168 | assert e == 3 |
| 169 | assert f == -17 |
| 170 | } |
| 171 | |
| 172 | struct FooWithSharedArray { |
| 173 | mut: |
| 174 | bars []shared Xyz |
| 175 | } |
| 176 | |
| 177 | fn test_lock_element_in_array_of_shared_structs() { |
| 178 | mut foo := FooWithSharedArray{} |
| 179 | foo.bars << Xyz{ |
| 180 | n: 3 |
| 181 | } |
| 182 | foo.bars << Xyz{ |
| 183 | n: 7 |
| 184 | } |
| 185 | lock foo.bars[0] { |
| 186 | foo.bars[0].n = 11 |
| 187 | } |
| 188 | first := rlock foo.bars[0] { |
| 189 | foo.bars[0].n |
| 190 | } |
| 191 | second := rlock foo.bars[1] { |
| 192 | foo.bars[1].n |
| 193 | } |
| 194 | assert first == 11 |
| 195 | assert second == 7 |
| 196 | } |
| 197 | |
| 198 | // Test for issue #16234: empty shared struct initialized as null causing segmentation fault |
| 199 | struct EmptyStruct {} |
| 200 | |
| 201 | struct OwnerWithEmptyShared { |
| 202 | empty EmptyStruct |
| 203 | lck shared EmptyStruct |
| 204 | } |
| 205 | |
| 206 | fn test_shared_empty_struct() { |
| 207 | mut i := OwnerWithEmptyShared{} |
| 208 | dump(i) |
| 209 | // Test that we can lock the empty shared struct without segfault |
| 210 | lock i.lck { |
| 211 | // Empty struct, nothing to do |
| 212 | } |
| 213 | assert true |
| 214 | } |
| 215 | |