-/* 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. */
#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 <queue>
#include <string>
-XBT_LOG_NEW_CATEGORY(producer_consumer, "Producer-Consumer plugin logging category");
+XBT_LOG_EXTERNAL_CATEGORY(producer_consumer);
/** Stock implementation of a generic monitored queue to solve the producer-consumer problem */
template <typename T> class ProducerConsumer;
template <typename T> using ProducerConsumerPtr = boost::intrusive_ptr<ProducerConsumer<T>>;
-static unsigned long pc_id = 0;
+XBT_PUBLIC_DATA unsigned long pc_id;
template <typename T> class ProducerConsumer {
public:
{
xbt_assert(max_queue_size > 0, "Max queue size of 0 is not allowed");
- id = std::string("ProducerConsumer") + std::to_string(pc_id);
+ id = "ProducerConsumer" + std::to_string(pc_id);
pc_id++;
mutex_ = s4u::Mutex::create();
while (size() >= max_queue_size_)
can_put_->wait(lock);
if (tmode_ == TransferMode::MAILBOX) {
- comm = mbox_->put_async(data, simulated_size_in_bytes);
+ comm = mbox_->put_init(data, simulated_size_in_bytes)
+ ->set_copy_data_callback(s4u::Comm::copy_pointer_callback)
+ ->start();
} else
queue_.push(data);
can_get_->notify_all();
while (empty())
can_get_->wait(lock);
if (tmode_ == TransferMode::MAILBOX)
- comm = mbox_->get_async<T>(data);
+ comm = mbox_->get_init()
+ ->set_dst_data(reinterpret_cast<void**>(data), sizeof(void*))
+ ->set_copy_data_callback(s4u::Comm::copy_pointer_callback)
+ ->start();
else {
*data = queue_.front();
queue_.pop();
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();
}