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