@@ -107,6 +107,7 @@ cache_t* pypluginCache_init(
107107 py::function cache_init_hook, py::function cache_hit_hook,
108108 py::function cache_miss_hook, py::function cache_eviction_hook,
109109 py::function cache_remove_hook, py::function cache_free_hook) {
110+ py::gil_scoped_acquire acquire;
110111 // Initialize base cache structure with exception safety
111112 cache_t * cache = nullptr ;
112113 std::unique_ptr<pypluginCache_params_t, PypluginCacheParamsDeleter> params;
@@ -163,19 +164,26 @@ cache_t* pypluginCache_init(
163164}
164165
165166static void pypluginCache_free (cache_t * cache) {
166- if (!cache || !cache->eviction_params ) {
167+ if (!cache) {
168+ return ;
169+ }
170+ if (!cache->eviction_params ) {
171+ // No params, just free the cache structure
172+ cache_struct_free (cache);
167173 return ;
168174 }
169175
170- // Use smart pointer for automatic cleanup
171- std::unique_ptr<pypluginCache_params_t, PypluginCacheParamsDeleter> params (
172- static_cast <pypluginCache_params_t*>(cache->eviction_params ));
173-
176+ auto * raw_params = static_cast <pypluginCache_params_t*>(cache->eviction_params );
177+ cache->eviction_params = nullptr ;
174178 // The smart pointer destructor will handle cleanup automatically
179+ std::unique_ptr<pypluginCache_params_t, PypluginCacheParamsDeleter> params (raw_params);
180+ params.reset ();
181+
175182 cache_struct_free (cache);
176183}
177184
178185static bool pypluginCache_get (cache_t * cache, const request_t * req) {
186+ py::gil_scoped_acquire acquire;
179187 bool hit = cache_get_base (cache, req);
180188 pypluginCache_params_t* params =
181189 (pypluginCache_params_t*)cache->eviction_params ;
@@ -204,6 +212,7 @@ static cache_obj_t* pypluginCache_to_evict(cache_t* cache,
204212}
205213
206214static void pypluginCache_evict (cache_t * cache, const request_t * req) {
215+ py::gil_scoped_acquire acquire;
207216 pypluginCache_params_t* params =
208217 (pypluginCache_params_t*)cache->eviction_params ;
209218
@@ -223,6 +232,7 @@ static void pypluginCache_evict(cache_t* cache, const request_t* req) {
223232}
224233
225234static bool pypluginCache_remove (cache_t * cache, const obj_id_t obj_id) {
235+ py::gil_scoped_acquire acquire;
226236 pypluginCache_params_t* params =
227237 (pypluginCache_params_t*)cache->eviction_params ;
228238
@@ -568,7 +578,8 @@ void export_cache(py::module& m) {
568578 bytes_req > 0 ? 1.0 - (double )bytes_hit / bytes_req : 0.0 ;
569579 return std::make_tuple (obj_miss_ratio, byte_miss_ratio);
570580 },
571- " cache" _a, " reader" _a, " start_req" _a = 0 , " max_req" _a = -1 );
581+ " cache" _a, " reader" _a, " start_req" _a = 0 , " max_req" _a = -1 ,
582+ py::call_guard<py::gil_scoped_release>());
572583}
573584
574585} // namespace libcachesim
0 commit comments