@@ -50,6 +50,9 @@ static volatile bool m_peer_delete_deferred;
50
50
51
51
static struct bm_zms_fs fs ;
52
52
53
+ /* Keeps track of the number of peers currently under delete processing. */
54
+ static atomic_t delete_counter ;
55
+
53
56
/* Function for dispatching events to all registered event handlers. */
54
57
static void pds_evt_send (pm_evt_t * p_event )
55
58
{
@@ -244,6 +247,9 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt)
244
247
} else if (p_evt -> result < 0 ) {
245
248
/* Unrecoverable error. */
246
249
LOG_ERR ("BM_ZMS delete failed with error %d" , p_evt -> result );
250
+
251
+ atomic_dec (& delete_counter );
252
+
247
253
pds_evt .evt_id = PM_EVT_PEER_DELETE_FAILED ;
248
254
pds_evt .params .peer_delete_failed .error = NRF_ERROR_INTERNAL ;
249
255
pds_evt_send (& pds_evt );
@@ -253,8 +259,14 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt)
253
259
254
260
ret = find_next_data_entry_in_peer (peer_id , & next_entry_id );
255
261
if (ret == NRF_SUCCESS ) {
262
+ /* Process the next entry for the peer. */
256
263
m_peer_delete_deferred = true;
257
264
} else if (ret == NRF_ERROR_NOT_FOUND ) {
265
+ atomic_dec (& delete_counter );
266
+
267
+ /* Process the next deleted peers, if any are present. */
268
+ m_peer_delete_deferred = true;
269
+
258
270
pds_evt .evt_id = PM_EVT_PEER_DELETE_SUCCEEDED ;
259
271
peer_id_free (pds_evt .peer_id );
260
272
pds_evt_send (& pds_evt );
@@ -467,7 +479,14 @@ uint32_t pds_peer_id_free(pm_peer_id_t peer_id)
467
479
VERIFY_PEER_ID_IN_RANGE (peer_id );
468
480
469
481
(void )peer_id_delete (peer_id );
470
- peer_data_delete_process ();
482
+
483
+ /* Only start processing on the first delete request.
484
+ * `peer_data_delete_process` will iteratively take care of processing all the peers marked
485
+ * for deletion.
486
+ */
487
+ if (atomic_inc (& delete_counter ) == 0 ) {
488
+ peer_data_delete_process ();
489
+ }
471
490
472
491
return NRF_SUCCESS ;
473
492
}
0 commit comments