]>
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); | |
417e7e66 | 25 | ovs_list_init(&list->list); |
05067881 BP |
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, | |
ca6ba700 | 54 | struct ovs_list *node, size_t max) |
05067881 BP |
55 | { |
56 | size_t retval = 0; | |
57 | ||
58 | ovs_mutex_lock(&list->mutex); | |
59 | if (list->n < max) { | |
417e7e66 | 60 | ovs_list_push_back(&list->list, node); |
05067881 BP |
61 | retval = ++list->n; |
62 | } | |
63 | ovs_mutex_unlock(&list->mutex); | |
64 | ||
65 | return retval; | |
66 | } | |
67 | ||
ca6ba700 | 68 | struct ovs_list * |
05067881 BP |
69 | guarded_list_pop_front(struct guarded_list *list) |
70 | { | |
ca6ba700 | 71 | struct ovs_list *node = NULL; |
05067881 BP |
72 | |
73 | ovs_mutex_lock(&list->mutex); | |
74 | if (list->n) { | |
417e7e66 | 75 | node = ovs_list_pop_front(&list->list); |
05067881 BP |
76 | list->n--; |
77 | } | |
78 | ovs_mutex_unlock(&list->mutex); | |
79 | ||
80 | return node; | |
81 | } | |
82 | ||
83 | size_t | |
ca6ba700 | 84 | guarded_list_pop_all(struct guarded_list *list, struct ovs_list *elements) |
05067881 BP |
85 | { |
86 | size_t n; | |
87 | ||
88 | ovs_mutex_lock(&list->mutex); | |
417e7e66 | 89 | ovs_list_move(elements, &list->list); |
05067881 BP |
90 | n = list->n; |
91 | ||
417e7e66 | 92 | ovs_list_init(&list->list); |
05067881 BP |
93 | list->n = 0; |
94 | ovs_mutex_unlock(&list->mutex); | |
95 | ||
96 | return n; | |
97 | } |