X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=deps%2Flightrec%2Freaper.c;h=2e32cae5d0fe0afaa6ea9394aaecc270e6912d66;hb=98fa08a56df29aeed65dfbc7697603c90bc66144;hp=377685c1ed10e2c53de627e50cc36eee9fd62efb;hpb=a59e553648d65481f8a85822bdd4749fe70371ad;p=pcsx_rearmed.git diff --git a/deps/lightrec/reaper.c b/deps/lightrec/reaper.c index 377685c1..2e32cae5 100644 --- a/deps/lightrec/reaper.c +++ b/deps/lightrec/reaper.c @@ -1,15 +1,6 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later /* - * Copyright (C) 2020 Paul Cercueil - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. + * Copyright (C) 2020-2021 Paul Cercueil */ #include "blockcache.h" @@ -21,6 +12,7 @@ #include #include +#include #include struct reaper_elm { @@ -33,6 +25,8 @@ struct reaper { struct lightrec_state *state; pthread_mutex_t mutex; struct slist_elm reap_list; + + atomic_uint sem; }; struct reaper *lightrec_reaper_init(struct lightrec_state *state) @@ -47,6 +41,7 @@ struct reaper *lightrec_reaper_init(struct lightrec_state *state) } reaper->state = state; + reaper->sem = 0; slist_init(&reaper->reap_list); ret = pthread_mutex_init(&reaper->mutex, NULL); @@ -98,6 +93,11 @@ out_unlock: return ret; } +static bool lightrec_reaper_can_reap(struct reaper *reaper) +{ + return !atomic_load_explicit(&reaper->sem, memory_order_relaxed); +} + void lightrec_reaper_reap(struct reaper *reaper) { struct reaper_elm *reaper_elm; @@ -105,13 +105,14 @@ void lightrec_reaper_reap(struct reaper *reaper) pthread_mutex_lock(&reaper->mutex); - while (!!(elm = slist_first(&reaper->reap_list))) { + while (lightrec_reaper_can_reap(reaper) && + !!(elm = slist_first(&reaper->reap_list))) { slist_remove(&reaper->reap_list, elm); pthread_mutex_unlock(&reaper->mutex); reaper_elm = container_of(elm, struct reaper_elm, slist); - (*reaper_elm->func)(reaper_elm->data); + (*reaper_elm->func)(reaper->state, reaper_elm->data); lightrec_free(reaper->state, MEM_FOR_LIGHTREC, sizeof(*reaper_elm), reaper_elm); @@ -121,3 +122,13 @@ void lightrec_reaper_reap(struct reaper *reaper) pthread_mutex_unlock(&reaper->mutex); } + +void lightrec_reaper_pause(struct reaper *reaper) +{ + atomic_fetch_add_explicit(&reaper->sem, 1, memory_order_relaxed); +} + +void lightrec_reaper_continue(struct reaper *reaper) +{ + atomic_fetch_sub_explicit(&reaper->sem, 1, memory_order_relaxed); +}