/src/openvswitch/lib/guarded-list.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2013 Nicira, Inc. |
3 | | * |
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: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
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. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | |
19 | | #include "guarded-list.h" |
20 | | |
21 | | void |
22 | | guarded_list_init(struct guarded_list *list) |
23 | 0 | { |
24 | 0 | ovs_mutex_init(&list->mutex); |
25 | 0 | ovs_list_init(&list->list); |
26 | 0 | list->n = 0; |
27 | 0 | } |
28 | | |
29 | | void |
30 | | guarded_list_destroy(struct guarded_list *list) |
31 | 0 | { |
32 | 0 | ovs_mutex_destroy(&list->mutex); |
33 | 0 | } |
34 | | |
35 | | bool |
36 | | guarded_list_is_empty(const struct guarded_list *list) |
37 | 0 | { |
38 | 0 | bool empty; |
39 | |
|
40 | 0 | ovs_mutex_lock(&list->mutex); |
41 | 0 | empty = list->n == 0; |
42 | 0 | ovs_mutex_unlock(&list->mutex); |
43 | |
|
44 | 0 | return empty; |
45 | 0 | } |
46 | | |
47 | | /* If 'list' has fewer than 'max' elements, adds 'node' at the end of the list |
48 | | * and returns the number of elements now on the list. |
49 | | * |
50 | | * If 'list' already has at least 'max' elements, returns 0 without modifying |
51 | | * the list. */ |
52 | | size_t |
53 | | guarded_list_push_back(struct guarded_list *list, |
54 | | struct ovs_list *node, size_t max) |
55 | 0 | { |
56 | 0 | size_t retval = 0; |
57 | |
|
58 | 0 | ovs_mutex_lock(&list->mutex); |
59 | 0 | if (list->n < max) { |
60 | 0 | ovs_list_push_back(&list->list, node); |
61 | 0 | retval = ++list->n; |
62 | 0 | } |
63 | 0 | ovs_mutex_unlock(&list->mutex); |
64 | |
|
65 | 0 | return retval; |
66 | 0 | } |
67 | | |
68 | | struct ovs_list * |
69 | | guarded_list_pop_front(struct guarded_list *list) |
70 | 0 | { |
71 | 0 | struct ovs_list *node = NULL; |
72 | |
|
73 | 0 | ovs_mutex_lock(&list->mutex); |
74 | 0 | if (list->n) { |
75 | 0 | node = ovs_list_pop_front(&list->list); |
76 | 0 | list->n--; |
77 | 0 | } |
78 | 0 | ovs_mutex_unlock(&list->mutex); |
79 | |
|
80 | 0 | return node; |
81 | 0 | } |
82 | | |
83 | | size_t |
84 | | guarded_list_pop_all(struct guarded_list *list, struct ovs_list *elements) |
85 | 0 | { |
86 | 0 | size_t n; |
87 | |
|
88 | 0 | ovs_mutex_lock(&list->mutex); |
89 | 0 | ovs_list_move(elements, &list->list); |
90 | 0 | n = list->n; |
91 | |
|
92 | 0 | ovs_list_init(&list->list); |
93 | 0 | list->n = 0; |
94 | 0 | ovs_mutex_unlock(&list->mutex); |
95 | |
|
96 | 0 | return n; |
97 | 0 | } |