a59e5536 |
1 | /* |
2 | * Copyright (C) 2020 Paul Cercueil <paul@crapouillou.net> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | */ |
14 | |
15 | #include "blockcache.h" |
16 | #include "debug.h" |
17 | #include "lightrec-private.h" |
18 | #include "memmanager.h" |
19 | #include "slist.h" |
20 | #include "reaper.h" |
21 | |
22 | #include <errno.h> |
23 | #include <pthread.h> |
24 | #include <stdbool.h> |
25 | |
26 | struct reaper_elm { |
27 | reap_func_t func; |
28 | void *data; |
29 | struct slist_elm slist; |
30 | }; |
31 | |
32 | struct reaper { |
33 | struct lightrec_state *state; |
34 | pthread_mutex_t mutex; |
35 | struct slist_elm reap_list; |
36 | }; |
37 | |
38 | struct reaper *lightrec_reaper_init(struct lightrec_state *state) |
39 | { |
40 | struct reaper *reaper; |
41 | int ret; |
42 | |
43 | reaper = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*reaper)); |
44 | if (!reaper) { |
45 | pr_err("Cannot create reaper: Out of memory\n"); |
46 | return NULL; |
47 | } |
48 | |
49 | reaper->state = state; |
50 | slist_init(&reaper->reap_list); |
51 | |
52 | ret = pthread_mutex_init(&reaper->mutex, NULL); |
53 | if (ret) { |
54 | pr_err("Cannot init mutex variable: %d\n", ret); |
55 | lightrec_free(reaper->state, MEM_FOR_LIGHTREC, |
56 | sizeof(*reaper), reaper); |
57 | return NULL; |
58 | } |
59 | |
60 | return reaper; |
61 | } |
62 | |
63 | void lightrec_reaper_destroy(struct reaper *reaper) |
64 | { |
65 | pthread_mutex_destroy(&reaper->mutex); |
66 | lightrec_free(reaper->state, MEM_FOR_LIGHTREC, sizeof(*reaper), reaper); |
67 | } |
68 | |
69 | int lightrec_reaper_add(struct reaper *reaper, reap_func_t f, void *data) |
70 | { |
71 | struct reaper_elm *reaper_elm; |
72 | struct slist_elm *elm; |
73 | int ret = 0; |
74 | |
75 | pthread_mutex_lock(&reaper->mutex); |
76 | |
77 | for (elm = reaper->reap_list.next; elm; elm = elm->next) { |
78 | reaper_elm = container_of(elm, struct reaper_elm, slist); |
79 | |
80 | if (reaper_elm->data == data) |
81 | goto out_unlock; |
82 | } |
83 | |
84 | reaper_elm = lightrec_malloc(reaper->state, MEM_FOR_LIGHTREC, |
85 | sizeof(*reaper_elm)); |
86 | if (!reaper_elm) { |
87 | pr_err("Cannot add reaper entry: Out of memory\n"); |
88 | ret = -ENOMEM; |
89 | goto out_unlock; |
90 | } |
91 | |
92 | reaper_elm->func = f; |
93 | reaper_elm->data = data; |
94 | slist_append(&reaper->reap_list, &reaper_elm->slist); |
95 | |
96 | out_unlock: |
97 | pthread_mutex_unlock(&reaper->mutex); |
98 | return ret; |
99 | } |
100 | |
101 | void lightrec_reaper_reap(struct reaper *reaper) |
102 | { |
103 | struct reaper_elm *reaper_elm; |
104 | struct slist_elm *elm; |
105 | |
106 | pthread_mutex_lock(&reaper->mutex); |
107 | |
108 | while (!!(elm = slist_first(&reaper->reap_list))) { |
109 | slist_remove(&reaper->reap_list, elm); |
110 | pthread_mutex_unlock(&reaper->mutex); |
111 | |
112 | reaper_elm = container_of(elm, struct reaper_elm, slist); |
113 | |
114 | (*reaper_elm->func)(reaper_elm->data); |
115 | |
116 | lightrec_free(reaper->state, MEM_FOR_LIGHTREC, |
117 | sizeof(*reaper_elm), reaper_elm); |
118 | |
119 | pthread_mutex_lock(&reaper->mutex); |
120 | } |
121 | |
122 | pthread_mutex_unlock(&reaper->mutex); |
123 | } |