X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/92cd12d0b61dd5a17500ef7d38f7b95a80301037..29e444d885c6079db52621b1edfd4f5411ef7bff:/src/s4u/s4u_Mailbox.cpp diff --git a/src/s4u/s4u_Mailbox.cpp b/src/s4u/s4u_Mailbox.cpp index d9d6c6c97b..01cb3eb499 100644 --- a/src/s4u/s4u_Mailbox.cpp +++ b/src/s4u/s4u_Mailbox.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2006-2020. 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. */ @@ -34,29 +34,38 @@ Mailbox* Mailbox::by_name(const std::string& name) return &mbox->piface_; } -bool Mailbox::empty() +bool Mailbox::empty() const { return pimpl_->comm_queue_.empty(); } -bool Mailbox::listen() +bool Mailbox::listen() const { return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()); } -bool Mailbox::ready() +aid_t Mailbox::listen_from() const +{ + kernel::activity::CommImplPtr comm = front(); + if (comm && comm->src_actor_) + return comm->src_actor_->get_pid(); + else + return -1; +} + +bool Mailbox::ready() const { bool comm_ready = false; if (not pimpl_->comm_queue_.empty()) { - comm_ready = pimpl_->comm_queue_.front()->state_ == SIMIX_DONE; - + comm_ready = pimpl_->comm_queue_.front()->state_ == kernel::activity::State::DONE; + } else if (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()) { - comm_ready = pimpl_->done_comm_queue_.front()->state_ == SIMIX_DONE; + comm_ready = pimpl_->done_comm_queue_.front()->state_ == kernel::activity::State::DONE; } return comm_ready; } -kernel::activity::CommImplPtr Mailbox::front() +kernel::activity::CommImplPtr Mailbox::front() const { return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front(); } @@ -67,7 +76,7 @@ void Mailbox::set_receiver(ActorPtr actor) } /** @brief get the receiver (process associated to the mailbox) */ -ActorPtr Mailbox::get_receiver() +ActorPtr Mailbox::get_receiver() const { if (pimpl_->permanent_receiver_ == nullptr) return ActorPtr(); @@ -76,25 +85,27 @@ ActorPtr Mailbox::get_receiver() CommPtr Mailbox::put_init() { - CommPtr res = CommPtr(new s4u::Comm()); - res->sender_ = SIMIX_process_self(); + CommPtr res(new Comm()); + res->sender_ = kernel::actor::ActorImpl::self(); res->mailbox_ = this; return res; } -s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes) + +CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes) { - s4u::CommPtr res = put_init(); + CommPtr res = put_init(); res->set_remaining(simulated_size_in_bytes); res->src_buff_ = data; res->src_buff_size_ = sizeof(void*); return res; } -s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes) + +CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes) { xbt_assert(payload != nullptr, "You cannot send nullptr"); - s4u::CommPtr res = put_init(payload, simulated_size_in_bytes); - res->start(); + CommPtr res = put_init(payload, simulated_size_in_bytes); + res->vetoable_start(); return res; } void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes) @@ -104,6 +115,7 @@ void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes) CommPtr c = put_init(); c->set_remaining(simulated_size_in_bytes); c->set_src_data(payload); + c->vetoable_start(); c->wait(); } /** Blocking send with timeout */ @@ -114,22 +126,23 @@ void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeou CommPtr c = put_init(); c->set_remaining(simulated_size_in_bytes); c->set_src_data(payload); - // c->start() is optional. + c->vetoable_start(); c->wait_for(timeout); } -s4u::CommPtr Mailbox::get_init() +CommPtr Mailbox::get_init() { - CommPtr res = CommPtr(new s4u::Comm()); - res->receiver_ = SIMIX_process_self(); + CommPtr res(new Comm()); + res->receiver_ = kernel::actor::ActorImpl::self(); res->mailbox_ = this; return res; } -s4u::CommPtr Mailbox::get_async(void** data) + +CommPtr Mailbox::get_async(void** data) { - s4u::CommPtr res = get_init(); + CommPtr res = get_init(); res->set_dst_data(data, sizeof(*data)); - res->start(); + res->vetoable_start(); return res; } @@ -138,6 +151,7 @@ void* Mailbox::get() void* res = nullptr; CommPtr c = get_init(); c->set_dst_data(&res, sizeof(res)); + c->vetoable_start(); c->wait(); return res; } @@ -146,11 +160,13 @@ void* Mailbox::get(double timeout) void* res = nullptr; CommPtr c = get_init(); c->set_dst_data(&res, sizeof(res)); + c->vetoable_start(); c->wait_for(timeout); return res; } -smx_activity_t Mailbox::iprobe(int type, int (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data) +kernel::activity::ActivityImplPtr +Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data) { return kernel::actor::simcall([this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); }); } @@ -158,6 +174,16 @@ smx_activity_t Mailbox::iprobe(int type, int (*match_fun)(void*, void*, kernel:: } // namespace simgrid /* **************************** Public C interface *************************** */ +sg_mailbox_t sg_mailbox_by_name(const char* alias) +{ + return simgrid::s4u::Mailbox::by_name(alias); +} + +const char* sg_mailbox_get_name(const_sg_mailbox_t mailbox) +{ + return mailbox->get_cname(); +} + /** @brief Set the mailbox to receive in asynchronous mode * * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call. @@ -181,3 +207,34 @@ int sg_mailbox_listen(const char* alias) { return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0; } + +void* sg_mailbox_get(sg_mailbox_t mailbox) +{ + return mailbox->get(); +} + +sg_comm_t sg_mailbox_get_async(sg_mailbox_t mailbox, void** data) +{ + auto comm = mailbox->get_async(data); + comm->add_ref(); + return comm.get(); +} + +void sg_mailbox_put(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes) +{ + mailbox->put(payload, simulated_size_in_bytes); +} + +sg_comm_t sg_mailbox_put_async(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes) +{ + auto comm = mailbox->put_async(payload, simulated_size_in_bytes); + comm->add_ref(); + return comm.get(); +} + +sg_comm_t sg_mailbox_put_init(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes) +{ + auto comm = mailbox->put_init(payload, simulated_size_in_bytes); + comm->add_ref(); + return comm.get(); +}