@@ -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,20 +164,28 @@ 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+ py::gil_scoped_acquire acquire;
171+ if (!cache->eviction_params ) {
172+ // No params, just free the cache structure
173+ cache_struct_free (cache);
167174 return ;
168175 }
169176
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-
177+ auto * raw_params = static_cast <pypluginCache_params_t*>(cache->eviction_params );
178+ cache->eviction_params = nullptr ;
174179 // The smart pointer destructor will handle cleanup automatically
180+ std::unique_ptr<pypluginCache_params_t, PypluginCacheParamsDeleter> params (raw_params);
181+ params.reset ();
182+
175183 cache_struct_free (cache);
176184}
177185
178186static bool pypluginCache_get (cache_t * cache, const request_t * req) {
179187 bool hit = cache_get_base (cache, req);
188+ py::gil_scoped_acquire acquire;
180189 pypluginCache_params_t* params =
181190 (pypluginCache_params_t*)cache->eviction_params ;
182191
@@ -204,6 +213,7 @@ static cache_obj_t* pypluginCache_to_evict(cache_t* cache,
204213}
205214
206215static void pypluginCache_evict (cache_t * cache, const request_t * req) {
216+ py::gil_scoped_acquire acquire;
207217 pypluginCache_params_t* params =
208218 (pypluginCache_params_t*)cache->eviction_params ;
209219
@@ -223,6 +233,7 @@ static void pypluginCache_evict(cache_t* cache, const request_t* req) {
223233}
224234
225235static bool pypluginCache_remove (cache_t * cache, const obj_id_t obj_id) {
236+ py::gil_scoped_acquire acquire;
226237 pypluginCache_params_t* params =
227238 (pypluginCache_params_t*)cache->eviction_params ;
228239
@@ -568,7 +579,8 @@ void export_cache(py::module& m) {
568579 bytes_req > 0 ? 1.0 - (double )bytes_hit / bytes_req : 0.0 ;
569580 return std::make_tuple (obj_miss_ratio, byte_miss_ratio);
570581 },
571- " cache" _a, " reader" _a, " start_req" _a = 0 , " max_req" _a = -1 );
582+ " cache" _a, " reader" _a, " start_req" _a = 0 , " max_req" _a = -1 ,
583+ py::call_guard<py::gil_scoped_release>());
572584}
573585
574586} // namespace libcachesim
0 commit comments