2 * Copyright (C) 2020 Paul Cercueil <paul@crapouillou.net>
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.
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.
15 #include "blockcache.h"
17 #include "lightrec-private.h"
18 #include "memmanager.h"
29 struct slist_elm slist;
33 struct lightrec_state *state;
34 pthread_mutex_t mutex;
35 struct slist_elm reap_list;
38 struct reaper *lightrec_reaper_init(struct lightrec_state *state)
40 struct reaper *reaper;
43 reaper = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*reaper));
45 pr_err("Cannot create reaper: Out of memory\n");
49 reaper->state = state;
50 slist_init(&reaper->reap_list);
52 ret = pthread_mutex_init(&reaper->mutex, NULL);
54 pr_err("Cannot init mutex variable: %d\n", ret);
55 lightrec_free(reaper->state, MEM_FOR_LIGHTREC,
56 sizeof(*reaper), reaper);
63 void lightrec_reaper_destroy(struct reaper *reaper)
65 pthread_mutex_destroy(&reaper->mutex);
66 lightrec_free(reaper->state, MEM_FOR_LIGHTREC, sizeof(*reaper), reaper);
69 int lightrec_reaper_add(struct reaper *reaper, reap_func_t f, void *data)
71 struct reaper_elm *reaper_elm;
72 struct slist_elm *elm;
75 pthread_mutex_lock(&reaper->mutex);
77 for (elm = reaper->reap_list.next; elm; elm = elm->next) {
78 reaper_elm = container_of(elm, struct reaper_elm, slist);
80 if (reaper_elm->data == data)
84 reaper_elm = lightrec_malloc(reaper->state, MEM_FOR_LIGHTREC,
87 pr_err("Cannot add reaper entry: Out of memory\n");
93 reaper_elm->data = data;
94 slist_append(&reaper->reap_list, &reaper_elm->slist);
97 pthread_mutex_unlock(&reaper->mutex);
101 void lightrec_reaper_reap(struct reaper *reaper)
103 struct reaper_elm *reaper_elm;
104 struct slist_elm *elm;
106 pthread_mutex_lock(&reaper->mutex);
108 while (!!(elm = slist_first(&reaper->reap_list))) {
109 slist_remove(&reaper->reap_list, elm);
110 pthread_mutex_unlock(&reaper->mutex);
112 reaper_elm = container_of(elm, struct reaper_elm, slist);
114 (*reaper_elm->func)(reaper_elm->data);
116 lightrec_free(reaper->state, MEM_FOR_LIGHTREC,
117 sizeof(*reaper_elm), reaper_elm);
119 pthread_mutex_lock(&reaper->mutex);
122 pthread_mutex_unlock(&reaper->mutex);