2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 /* Concurrent Priority Vector
26 * ==========================
28 * Concurrent priority vector holds non-NULL pointers to objects in an
29 * increasing priority order and allows readers to traverse the vector without
30 * being concerned about writers modifying the vector as they are traversing
33 * The priority order is maintained as a linear vector of elements to allow
34 * for efficient memory prefetching.
36 * Concurrency is implemented with OVS RCU so that the readers can assume
37 * that once they have taken a pointer to the vector with
38 * pvector_cursor_init(), the 'size' member will not decrease, so that
39 * they can safely read 'size' entries from 'vector', and find that each
40 * entry has a valid, non-NULL 'ptr', and the vector is in order from highest
41 * to lowest 'priority'. The 'priority' values can change any time, but only
42 * so that the order of the entries does not change, so readers can use
43 * 'priority' values read at any time after acquisition of the vector pointer.
45 * Writers can concurrently add entries to the end of the vector, incrementing
46 * 'size', or update the 'priority' value of an entry, but only if that does
47 * not change the ordering of the entries. Writers will never change the 'ptr'
48 * values, or decrement the 'size' on a copy that readers have access to.
51 struct pvector_entry {
52 unsigned int priority;
56 /* Writers will preallocate space for some entries at the end to avoid future
58 enum { PVECTOR_EXTRA_ALLOC = 4 };
61 size_t size; /* Number of entries in the vector. */
62 size_t allocated; /* Number of allocated entries. */
63 struct pvector_entry vector[];
66 /* Concurrent priority vector. */
68 OVSRCU_TYPE(struct pvector_impl *) impl;
72 void pvector_init(struct pvector *);
73 void pvector_destroy(struct pvector *);
76 static inline size_t pvector_count(const struct pvector *);
77 static inline bool pvector_is_empty(const struct pvector *);
79 /* Insertion and deletion. */
80 void pvector_insert(struct pvector *, void *, unsigned int);
81 void pvector_change_priority(struct pvector *, void *, unsigned int);
82 void pvector_remove(struct pvector *, void *);
90 * Iteration is safe even in a pvector that is changing concurrently.
91 * Multiple writers must exclude each other via e.g., a mutex.
100 * struct my_node elem1, elem2, *iter;
101 * struct pvector my_pvector;
103 * pvector_init(&my_pvector);
105 * pvector_insert(&my_pvector, &elem1, 1);
106 * pvector_insert(&my_pvector, &elem2, 2);
108 * PVECTOR_FOR_EACH (iter, &my_pvector) {
109 * ...operate on '*iter'...
110 * ...elem2 to be seen before elem1...
113 * pvector_destroy(&my_pvector);
115 * There is no PVECTOR_FOR_EACH_SAFE variant as iteration is performed on RCU
116 * protected instance of the priority vector. Any concurrent modifications
117 * that would be disruptive for readers (such as deletions), will be performed
118 * on a new instance. To see any of the modifications, a new iteration loop
121 * The PVECTOR_FOR_EACH_PRIORITY limits the iteration to entries with higher
122 * than given priority and allows for object lookahead.
124 * The iteration loop must be completed without entering the OVS RCU quiescent
125 * period. That is, an old iteration loop must not be continued after any
126 * blocking IO (VLOG is non-blocking, so that is OK).
128 struct pvector_cursor {
129 size_t size; /* Number of entries in the vector. */
130 size_t entry_idx; /* Current index. */
131 const struct pvector_entry *vector;
134 static inline struct pvector_cursor pvector_cursor_init(const struct pvector *,
137 static inline void *pvector_cursor_next(struct pvector_cursor *,
138 int64_t stop_at_priority,
139 size_t n_ahead, size_t obj_size);
140 static inline void pvector_cursor_lookahead(const struct pvector_cursor *,
143 #define PVECTOR_FOR_EACH(PTR, PVECTOR) \
144 for (struct pvector_cursor cursor__ = pvector_cursor_init(PVECTOR, 0, 0); \
145 ((PTR) = pvector_cursor_next(&cursor__, -1, 0, 0)) != NULL; )
147 /* Loop while priority is higher than 'PRIORITY' and prefetch objects
148 * of size 'SZ' 'N' objects ahead from the current object. */
149 #define PVECTOR_FOR_EACH_PRIORITY(PTR, PRIORITY, N, SZ, PVECTOR) \
150 for (struct pvector_cursor cursor__ = pvector_cursor_init(PVECTOR, N, SZ); \
151 ((PTR) = pvector_cursor_next(&cursor__, PRIORITY, N, SZ)) != NULL; )
154 /* Inline implementations. */
156 static inline struct pvector_cursor
157 pvector_cursor_init(const struct pvector *pvec,
158 size_t n_ahead, size_t obj_size)
160 const struct pvector_impl *impl;
161 struct pvector_cursor cursor;
163 impl = ovsrcu_get(struct pvector_impl *, &pvec->impl);
165 ovs_prefetch_range(impl->vector, impl->size * sizeof impl->vector[0]);
167 cursor.size = impl->size;
168 cursor.vector = impl->vector;
169 cursor.entry_idx = -1;
171 for (size_t i = 0; i < n_ahead; i++) {
172 /* Prefetch the first objects. */
173 pvector_cursor_lookahead(&cursor, i, obj_size);
178 static inline void *pvector_cursor_next(struct pvector_cursor *cursor,
179 int64_t stop_at_priority,
180 size_t n_ahead, size_t obj_size)
182 if (++cursor->entry_idx < cursor->size &&
183 cursor->vector[cursor->entry_idx].priority > stop_at_priority) {
185 pvector_cursor_lookahead(cursor, n_ahead, obj_size);
187 return cursor->vector[cursor->entry_idx].ptr;
192 static inline void pvector_cursor_lookahead(const struct pvector_cursor *cursor,
195 if (cursor->entry_idx + n < cursor->size) {
196 ovs_prefetch_range(cursor->vector[cursor->entry_idx + n].ptr, size);
200 static inline size_t pvector_count(const struct pvector *pvec)
202 return ovsrcu_get(struct pvector_impl *, &pvec->impl)->size;
205 static inline bool pvector_is_empty(const struct pvector *pvec)
207 return pvector_count(pvec) == 0;
210 #endif /* pvector.h */