api: fix memory error in multi-thread environment
[vpp.git] / src / vlibmemory / memory_api.c
index 18a87b3..4287bd3 100644 (file)
@@ -65,6 +65,7 @@ memclnt_queue_callback (vlib_main_t * vm)
 {
   int i;
   api_main_t *am = vlibapi_get_main ();
+  int have_pending_rpcs;
 
   if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
                     1 + vec_len (am->vlib_private_rps)))
@@ -103,7 +104,12 @@ memclnt_queue_callback (vlib_main_t * vm)
          break;
        }
     }
-  if (vec_len (vm->pending_rpc_requests))
+
+  clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
+  have_pending_rpcs = vec_len (vm->pending_rpc_requests) > 0;
+  clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
+
+  if (have_pending_rpcs)
     {
       vm->queue_signal_pending = 1;
       vm->api_queue_nonempty = 1;
@@ -583,10 +589,10 @@ vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
   vec_reset_length (confused_indices);
 
   /* *INDENT-OFF* */
-  pool_foreach (regpp, am->vl_clients, ({
+  pool_foreach (regpp, am->vl_clients)  {
       vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
                                           &confused_indices);
-  }));
+  }
   /* *INDENT-ON* */
 
   /* This should "never happen," but if it does, fix it... */
@@ -893,15 +899,15 @@ vl_api_ring_command (vlib_main_t * vm,
 
       /* For horizontal scaling, add a hash table... */
       /* *INDENT-OFF* */
-      pool_foreach (regpp, am->vl_clients,
-      ({
+      pool_foreach (regpp, am->vl_clients)
+       {
         regp = *regpp;
         if (regp && regp->vlib_rp == vlib_rp)
           {
             vlib_cli_output (vm, "%s segment rings:", regp->name);
             goto found;
           }
-      }));
+      }
       vlib_cli_output (vm, "regp %llx not found?", regp);
       continue;
       /* *INDENT-ON* */