Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++17's std::scoped_lock where appropriate.
[simgrid.git] / include / simgrid / plugins / ProducerConsumer.hpp
index 02c54da..55dc6d6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2021-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -6,10 +6,10 @@
 #ifndef SIMGRID_PLUGIN_PRODUCERCONSUMER_HPP
 #define SIMGRID_PLUGIN_PRODUCERCONSUMER_HPP
 
+#include <simgrid/s4u/Comm.hpp>
 #include <simgrid/s4u/ConditionVariable.hpp>
 #include <simgrid/s4u/Mailbox.hpp>
 #include <simgrid/s4u/Mutex.hpp>
-#include <simgrid/simix.h>
 #include <xbt/asserts.h>
 #include <xbt/log.h>
 
@@ -28,9 +28,16 @@ namespace plugin {
 template <typename T> class ProducerConsumer;
 template <typename T> using ProducerConsumerPtr = boost::intrusive_ptr<ProducerConsumer<T>>;
 
-XBT_PUBLIC_DATA unsigned long pc_id;
+class ProducerConsumerId {
+private:
+  static unsigned long pc_id;
+
+protected:
+  const std::string id = "ProducerConsumer" + std::to_string(pc_id);
+  ProducerConsumerId() { ++pc_id; }
+};
 
-template <typename T> class ProducerConsumer {
+template <typename T> class ProducerConsumer : public ProducerConsumerId {
 public:
   /** This ProducerConsumer plugin can use two different transfer modes:
    *   - TransferMode::MAILBOX: this mode induces a s4u::Comm between the actors doing the calls to put() and get().
@@ -45,8 +52,6 @@ public:
   enum class TransferMode { MAILBOX = 0, QUEUE };
 
 private:
-  std::string id;
-
   /* Implementation of a Monitor to handle the data exchanges */
   s4u::MutexPtr mutex_;
   s4u::ConditionVariablePtr can_put_;
@@ -75,9 +80,6 @@ private:
   {
     xbt_assert(max_queue_size > 0, "Max queue size of 0 is not allowed");
 
-    id = std::string("ProducerConsumer") + std::to_string(pc_id);
-    pc_id++;
-
     mutex_   = s4u::Mutex::create();
     can_put_ = s4u::ConditionVariable::create();
     can_get_ = s4u::ConditionVariable::create();
@@ -102,7 +104,7 @@ public:
    */
   ProducerConsumer* set_max_queue_size(unsigned int max_queue_size)
   {
-    std::unique_lock<s4u::Mutex> lock(*mutex_);
+    const std::lock_guard<s4u::Mutex> lock(*mutex_);
     max_queue_size_ = max_queue_size;
     return this;
   }
@@ -147,7 +149,7 @@ public:
       can_put_->wait(lock);
     if (tmode_ == TransferMode::MAILBOX) {
       comm = mbox_->put_init(data, simulated_size_in_bytes)
-                 ->set_copy_data_callback(SIMIX_comm_copy_pointer_callback)
+                 ->set_copy_data_callback(s4u::Comm::copy_pointer_callback)
                  ->start();
     } else
       queue_.push(data);
@@ -185,7 +187,7 @@ public:
     if (tmode_ == TransferMode::MAILBOX)
       comm = mbox_->get_init()
                  ->set_dst_data(reinterpret_cast<void**>(data), sizeof(void*))
-                 ->set_copy_data_callback(SIMIX_comm_copy_pointer_callback)
+                 ->set_copy_data_callback(s4u::Comm::copy_pointer_callback)
                  ->start();
     else {
       *data = queue_.front();
@@ -205,8 +207,7 @@ public:
   T* get()
   {
     T* data;
-    s4u::CommPtr comm = get_async(&data);
-    if (comm) {
+    if (s4u::CommPtr comm = get_async(&data)) {
       XBT_CDEBUG(producer_consumer, "Waiting for the data to arrive");
       comm->wait();
     }