1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Introduces thread-safe concurrent collections
19 # This module offers new thread-safe collections. If you want to
20 # render basic collections thread-safe and don't mind the overhead cost,
21 # you can use `threads::redef_collections`.
23 # Concurrent collections:
25 # - [x] `ConcurrentArray`
26 # - [x] `ConcurrentList`
27 # - [ ] `ConcurrentHashMap`
28 # - [ ] `ConcurrentHashSet`
29 # - [ ] `ConcurrentContainer`
30 # - [ ] `ConcurrentQueue`
32 # Introduced collections specialize their critical methods according to the
33 # current implementation in the standard library. If additional services
34 # are added to the underlying collections by refinement or evolution, they
35 # might need to be covered in the concurrent version.
36 module concurrent_collections
40 redef class Collection[E
]
41 # Type of the concurrent variant of this collection
42 type CONCURRENT: ConcurrentCollection[E
]
44 # Wraps `self` in a thread-safe collection
45 fun to_concurrent
: CONCURRENT is abstract
48 redef class SequenceRead[E
]
49 redef type CONCURRENT: ConcurrentSequenceRead[E
]
52 redef class Sequence[E
]
53 redef type CONCURRENT: ConcurrentSequence[E
]
57 redef type CONCURRENT: ConcurrentArray[E
]
59 redef fun to_concurrent
do return new ConcurrentArray[E
].wrap
(self)
63 redef type CONCURRENT: ConcurrentList[E
]
65 redef fun to_concurrent
do return new ConcurrentList[E
].wrap
(self)
68 # A concurrent variant to the standard `Collection`
69 abstract class ConcurrentCollection[E
]
72 # Type of the equivalent non thread-safe collection
73 type REAL: Collection[E
]
75 # Collection wrapped by `self`
76 var real_collection
: REAL is noinit
78 # `Mutex` used to synchronize access to `self`
80 # It is used by the implementation on each protected methods. It can also
81 # be used externally to ensure that no other `Thread` modify this object.
87 var r
= real_collection
.count
(e
)
95 var r
= real_collection
.first
103 var r
= real_collection
.has
(e
)
111 var r
= real_collection
.has_all
(e
)
116 redef fun has_only
(e
)
119 var r
= real_collection
.has_only
(e
)
127 var r
= real_collection
.is_empty
135 var r
= real_collection
.iterator
143 var r
= real_collection
.length
151 var r
= real_collection
.to_a
159 var r
= real_collection
.rand
167 var r
= real_collection
.join
(sep
)
175 var r
= real_collection
.to_s
181 # A concurrent variant to the standard `SequenceRead`
182 abstract class ConcurrentSequenceRead[E
]
183 super ConcurrentCollection[E
]
184 super SequenceRead[E
]
186 redef type REAL: SequenceRead[E
]
191 var r
= real_collection
== o
199 var r
= real_collection
[index
]
207 var r
= real_collection
.hash
212 redef fun index_of
(index
)
215 var r
= real_collection
.index_of
(index
)
220 redef fun index_of_from
(index
, from
)
223 var r
= real_collection
.index_of_from
(index
, from
)
228 redef fun iterator_from
(index
)
231 var r
= real_collection
.iterator_from
(index
)
239 var r
= real_collection
.last
244 redef fun last_index_of
(e
)
247 var r
= real_collection
.last_index_of
(e
)
252 redef fun last_index_of_from
(e
, from
)
255 var r
= real_collection
.last_index_of_from
(e
, from
)
260 redef fun reverse_iterator
263 var r
= real_collection
.reverse_iterator
268 redef fun reverse_iterator_from
(from
)
271 var r
= real_collection
.reverse_iterator_from
(from
)
277 # A concurrent variant to the standard `Sequence`
278 abstract class ConcurrentSequence[E
]
279 super ConcurrentSequenceRead[E
]
282 redef type REAL: Sequence[E
]
284 redef fun []=(index
, e
)
287 real_collection
[index
] = e
294 real_collection
.add e
301 real_collection
.append e
308 real_collection
.first
= e
312 redef fun insert
(e
, i
)
315 real_collection
.insert
(e
, i
)
319 redef fun insert_all
(from
, pos
)
329 real_collection
.last
= e
336 var r
= real_collection
.pop
344 real_collection
.prepend e
351 real_collection
.push e
355 redef fun remove_at
(index
)
358 real_collection
.remove_at
(index
)
365 var r
= real_collection
.shift
373 real_collection
.unshift
(e
)
377 redef fun subarray
(start
, len
)
380 var r
= real_collection
.subarray
(start
, len
)
386 # A concurrent variant to the standard `Array`
387 class ConcurrentArray[E
]
388 super ConcurrentSequence[E
]
391 redef type REAL: Array[E
]
393 init wrap
(real_collection
: REAL) do self.real_collection
= real_collection
394 init do self.real_collection
= new Array[E
]
399 real_collection
.clear
403 redef fun enlarge
(cap
)
406 real_collection
.enlarge
(cap
)
410 redef fun remove_all
(e
)
413 real_collection
.remove_all
(e
)
417 redef fun swap_at
(a
, b
)
420 real_collection
.swap_at
(a
, b
)
425 ## The following method defs are conflict resolutions
431 real_collection
.add e
438 var r
= real_collection
.length
444 # A concurrent variant to the standard `List`
445 class ConcurrentList[E
]
446 super ConcurrentSequence[E
]
449 redef type REAL: List[E
]
451 init wrap
(real_collection
: REAL) do self.real_collection
= real_collection
452 init do self.real_collection
= new List[E
]
457 real_collection
.link
(l
)
461 redef fun slice
(from
, to
)
464 var r
= real_collection
.slice
(from
, to
)
470 ## The following method defs are conflict resolutions
476 var r
= real_collection
.pop
484 var r
= real_collection
.is_empty
492 real_collection
.unshift
(e
)