Branch data Line data Source code
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 : 16548 : guarded_list_init(struct guarded_list *list)
23 : : {
24 : 16548 : ovs_mutex_init(&list->mutex);
25 : 16548 : ovs_list_init(&list->list);
26 : 16548 : list->n = 0;
27 : 16548 : }
28 : :
29 : : void
30 : 749 : guarded_list_destroy(struct guarded_list *list)
31 : : {
32 : 749 : ovs_mutex_destroy(&list->mutex);
33 : 749 : }
34 : :
35 : : bool
36 : 18417 : guarded_list_is_empty(const struct guarded_list *list)
37 : : {
38 : : bool empty;
39 : :
40 : 18417 : ovs_mutex_lock(&list->mutex);
41 : 18417 : empty = list->n == 0;
42 : 18417 : ovs_mutex_unlock(&list->mutex);
43 : :
44 : 18417 : return empty;
45 : : }
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 : 338571 : guarded_list_push_back(struct guarded_list *list,
54 : : struct ovs_list *node, size_t max)
55 : : {
56 : 338571 : size_t retval = 0;
57 : :
58 : 338571 : ovs_mutex_lock(&list->mutex);
59 [ + - ]: 338571 : if (list->n < max) {
60 : 338571 : ovs_list_push_back(&list->list, node);
61 : 338571 : retval = ++list->n;
62 : : }
63 : 338571 : ovs_mutex_unlock(&list->mutex);
64 : :
65 : 338571 : return retval;
66 : : }
67 : :
68 : : struct ovs_list *
69 : 449 : guarded_list_pop_front(struct guarded_list *list)
70 : : {
71 : 449 : struct ovs_list *node = NULL;
72 : :
73 : 449 : ovs_mutex_lock(&list->mutex);
74 [ + - ]: 449 : if (list->n) {
75 : 449 : node = ovs_list_pop_front(&list->list);
76 : 449 : list->n--;
77 : : }
78 : 449 : ovs_mutex_unlock(&list->mutex);
79 : :
80 : 449 : return node;
81 : : }
82 : :
83 : : size_t
84 : 435560 : guarded_list_pop_all(struct guarded_list *list, struct ovs_list *elements)
85 : : {
86 : : size_t n;
87 : :
88 : 435560 : ovs_mutex_lock(&list->mutex);
89 : 435560 : ovs_list_move(elements, &list->list);
90 : 435560 : n = list->n;
91 : :
92 : 435560 : ovs_list_init(&list->list);
93 : 435560 : list->n = 0;
94 : 435560 : ovs_mutex_unlock(&list->mutex);
95 : :
96 : 435560 : return n;
97 : : }
|