Changeset 125:84e29d5ff27b in rrlib_finroc_core_utils-java


Ignore:
Timestamp:
08.06.2017 02:57:28 (22 months ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Phase:
public
Message:

Cleans up code of concurrent list and array wrapper classes

Location:
jc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • jc/ArrayWrapper.java

    r116 r125  
    2828 * The wrapper keeps track of the array's 'capacity' and the number 
    2929 * of elements it currently contains ('size'). 
    30  * Usually, 'size' and 'capacity' are identical. 
    31  * In C++, bounds checking is done via asserts (=> can be (de)activated). 
    3230 * 
    3331 * To efficiently iterate over the array, this code should be used: 
     
    3735 *       for (int i = 0, n = iterable.size(); i < n; i++) { 
    3836 *          ... iterable.get(i) 
    39  *       } 
    40  * 
    41  *      C++: 
    42  *       ArrayWrapper<T> iterable = ...; 
    43  *       for (size_t i = 0, n = iterable->size(); i < n; i++) { 
    44  *          ... iterable.get(i)  OR  ... iterable[i] 
    4537 *       } 
    4638 */ 
     
    122114 
    123115    /** 
    124      * @return Backend 
    125      */ 
    126     public T[] getBackend() { 
    127         return backend; 
    128     } 
    129  
    130     /** 
    131116     * @return Is there free capacity in the array? 
    132117     */ 
     
    154139        } 
    155140        size = 0; 
    156     } 
    157  
    158     /** 
    159      * Clear array contents and delete objects in Array 
    160      * (should only be used when using array with raw pointers) 
    161      */ 
    162     public void clearAndDelete() { 
    163         clear(); 
    164141    } 
    165142 
     
    192169     * Sets (somewhat virtual) size of Array (not capacity) 
    193170     * - if size is reduced, elements are not deleted or set to NULL 
    194      * (Attention! Potential Java and C++ memory leaks...) 
    195171     */ 
    196172    public void setSize(int newSize) { 
  • jc/container/SafeConcurrentlyIterableList.java

    r116 r125  
    2828 * @author Max Reichardt 
    2929 * 
    30  * Implementation of class below. 
     30 * This list is thread-safe. It can be iterated over (concurrently to modifications) 
     31 * by many threads without blocking. Iterations are very efficient. 
     32 * The list may contain null entries after deleting (so check!). 
     33 * 
     34 * To efficiently iterate over the list, this code should be used: 
     35 * 
     36 *       ArrayWrapper<T> iterable = xyz.getIterable(); 
     37 *       for (int i = 0, n = iterable.size(); i < n; i++) { 
     38 *          T x = iterable.get(i); 
     39 *          if (x != null) { 
     40 *              // do something 
     41 *          } 
     42 *       } 
     43 * 
     44 * Idea: Unlike ArrayList (Java) or std::vector (C++), old Array backends are deleted deferred so that 
     45 * threads still iterating over this area can always complete this. 
    3146 */ 
    32 abstract class SafeConcurrentlyIterableListBase<T> { 
     47public class SafeConcurrentlyIterableList<T> { 
    3348 
    3449    /** Mutex for list - Since we call garbage collector lock for list needs to be before in order */ 
     
    4156    private int firstFreeFromHere = 0; 
    4257 
     58    /** Empty Dummy List */ 
     59    @SuppressWarnings("rawtypes") 
     60    private static final SafeConcurrentlyIterableList EMPTY = new SafeConcurrentlyIterableList(0, 0); 
     61 
     62    /** Factor by which list is enlarged, when backend is too small */ 
     63    private final int resizeFactor; 
     64 
     65    /** 
     66     * @return Empty Dummy List 
     67     */ 
     68    @SuppressWarnings("rawtypes") 
     69    public static SafeConcurrentlyIterableList getEmptyInstance() { 
     70        return EMPTY; 
     71    } 
     72 
    4373    /** 
    4474     * @param initialSize Initial size of backend 
    4575     * @param resizeFactor Factor by which list is enlarged, when backend is too small (dummy in C++, template parameter specifies it here) 
    46      * @param deleteElemsWithList Delete elements when List is deleted? (relevant for C++ only) 
    4776     */ 
    4877    @SuppressWarnings("unchecked") 
    49     public SafeConcurrentlyIterableListBase(int initialSize) { 
     78    public SafeConcurrentlyIterableList(int initialSize, int resizeFactor_) { 
    5079        objMutex = new MutexLockOrder(Integer.MAX_VALUE - 20); 
    5180        currentBackend = initialSize > 0 ? new ArrayWrapper<T>(0, initialSize) : ArrayWrapper.getEmpty(); 
     81        resizeFactor = resizeFactor_; 
    5282    } 
    5383 
     
    74104            backend.add(element); 
    75105        } else { 
    76             ArrayWrapper<T> old = currentBackend; 
    77106            ArrayWrapper<T> newBackend = new ArrayWrapper<T>(0, Math.max(1, backend.getCapacity()) * getResizeFactor()); 
    78107            newBackend.copyAllFrom(backend); 
    79108            newBackend.add(element); 
    80109            currentBackend = newBackend; 
    81  
    82             if (old.size() > 0) { // we don't want to delete empty backend from ArrayWrapper class 
    83                 deleteBackend(old); 
    84             } 
    85110        } 
    86111        return currentBackend.size() - 1; 
    87     } 
    88  
    89     private void deleteBackend(ArrayWrapper<T> b) { 
    90112    } 
    91113 
     
    100122     * @return Factor by which list is enlarged, when backend is too small 
    101123     */ 
    102     protected abstract int getResizeFactor(); 
    103  
    104     /** 
    105      * @return Delete elements when List is deleted? (relevant for C++ only) 
    106      */ 
    107     protected abstract boolean deleteElemsWithList(); 
     124    protected int getResizeFactor() { 
     125        return resizeFactor; 
     126    } 
    108127 
    109128    /** 
     
    115134        ArrayWrapper<T> backend = currentBackend; // acquire non-volatile pointer 
    116135        if (backend.getCapacity() < cap) { 
    117             ArrayWrapper<T> old = currentBackend; 
    118136            ArrayWrapper<T> newBackend = new ArrayWrapper<T>(0, cap * getResizeFactor()); 
    119137            newBackend.copyAllFrom(backend); 
    120138            currentBackend = newBackend; 
    121  
    122             deleteBackend(old); 
    123139        } 
    124140    } 
     
    201217    } 
    202218} 
    203  
    204 /** 
    205  * @author Max Reichardt 
    206  * 
    207  * This list is thread-safe. It can be iterated over (concurrently to modifications) 
    208  * by many threads without blocking. Iterations are very efficient (both C++ and Java). 
    209  * The list may contain null entries after deleting (so check!). 
    210  * 
    211  * To efficiently iterate over the list, this code should be used: 
    212  * 
    213  *      Java: 
    214  *       ArrayWrapper<T> iterable = xyz.getIterable(); 
    215  *       for (int i = 0, n = iterable.size(); i < n; i++) { 
    216  *          T x = iterable.get(i); 
    217  *          if (x != null) { 
    218  *              // do something 
    219  *          } 
    220  *       } 
    221  * 
    222  *      C++: 
    223  *       ArrayWrapper<T>* iterable = xyz.getIterable(); 
    224  *       for (int i = 0, n = iterable->size(); i < n; i++) { 
    225  *          T* x = iterable->get(i); 
    226  *          if (x != NULL { 
    227  *              // do something 
    228  *          } 
    229  *       } 
    230  * 
    231  * Idea: Unlike ArrayList (Java) or std::vector (C++), old Array backends are deleted deferred so that 
    232  * threads still iterating over this area can always complete this. 
    233  * TODO: If iterations can be particularly delayed, use a delay-iterator in C++. 
    234  * 
    235  * 
    236  * (C++) This list may only contain pointers (no shared pointers etc. because of thread-safety)... 
    237  *       because only this allows atomic deletion by overwriting with null 
    238  */ 
    239 public class SafeConcurrentlyIterableList<T> extends SafeConcurrentlyIterableListBase<T> { 
    240  
    241     /** Empty Dummy List */ 
    242     @SuppressWarnings("rawtypes") 
    243     private static final SafeConcurrentlyIterableList EMPTY = new SafeConcurrentlyIterableList(0, 0); 
    244  
    245     /** Factor by which list is enlarged, when backend is too small */ 
    246     private final int resizeFactor; 
    247  
    248     /** 
    249      * @return Empty Dummy List 
    250      */ 
    251     @SuppressWarnings("rawtypes") 
    252     public static SafeConcurrentlyIterableList getEmptyInstance() { 
    253         return EMPTY; 
    254     } 
    255  
    256     /** 
    257      * @param initialSize Initial size of backend 
    258      * @param resizeFactor Factor by which list is enlarged, when backend is too small (dummy in C++, template parameter specifies it here) 
    259      */ 
    260     public SafeConcurrentlyIterableList(int initialSize, int resizeFactor_) { 
    261         super(initialSize); 
    262         resizeFactor = resizeFactor_; 
    263     } 
    264  
    265     @Override 
    266     protected boolean deleteElemsWithList() { 
    267         return false; 
    268     } 
    269  
    270     @Override 
    271     protected int getResizeFactor() { 
    272         return resizeFactor; 
    273     } 
    274 } 
  • jc/stream/ChunkedBuffer.java

    r120 r125  
    2929import org.rrlib.serialization.BinarySerializable; 
    3030import org.rrlib.serialization.BufferInfo; 
    31 import org.rrlib.serialization.ConstSource; 
    3231import org.rrlib.serialization.FixedBuffer; 
    3332import org.rrlib.serialization.Sink; 
     
    6261 * and only commit complete chunks. 
    6362 */ 
    64 public class ChunkedBuffer implements BinarySerializable, ConstSource, Sink, HasDestructor, Clearable { 
     63public class ChunkedBuffer implements BinarySerializable, Source, Sink, HasDestructor, Clearable { 
    6564 
    6665    /** First chunk in buffer - only changed by reader - next ones can be determined following links through "next"-attributes*/ 
Note: See TracChangeset for help on using the changeset viewer.