From: Frederic Suter Date: Sat, 17 Nov 2018 19:40:23 +0000 (+0100) Subject: back to business! X-Git-Tag: v3_22~796^2~2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/54bfe4859cf046c6cd9ea2ffb9069b106725f8ac back to business! MSG semaphores become legacy as they are now implemented through S4U. --- diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index fd0ab86093..4da608426a 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -176,6 +176,7 @@ typedef simgrid::s4u::Barrier s4u_Barrier; typedef simgrid::s4u::Host s4u_Host; typedef simgrid::s4u::Link s4u_Link; typedef simgrid::s4u::File s4u_File; +typedef simgrid::s4u::Semaphore s4u_Semaphore; typedef simgrid::s4u::Storage s4u_Storage; typedef simgrid::s4u::NetZone s4u_NetZone; typedef simgrid::s4u::VirtualMachine s4u_VM; @@ -195,6 +196,7 @@ typedef struct s4u_Barrier s4u_Barrier; typedef struct s4u_Host s4u_Host; typedef struct s4u_Link s4u_Link; typedef struct s4u_File s4u_File; +typedef struct s4u_Semaphore s4u_Semaphore; typedef struct s4u_Storage s4u_Storage; typedef struct s4u_NetZone s4u_NetZone; typedef struct s4u_VM s4u_VM; @@ -209,6 +211,7 @@ typedef struct s_smx_mailbox* smx_mailbox_t; #endif typedef s4u_Barrier* sg_bar_t; +typedef s4u_Semaphore* sg_sem_t; typedef s4u_NetZone* sg_netzone_t; typedef s4u_Host* sg_host_t; typedef s4u_Link* sg_link_t; diff --git a/include/simgrid/msg.h b/include/simgrid/msg.h index a80493e6d8..d4d157777b 100644 --- a/include/simgrid/msg.h +++ b/include/simgrid/msg.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -428,14 +429,11 @@ XBT_PUBLIC const char* MSG_task_get_category(msg_task_t task); */ XBT_PUBLIC void MSG_mailbox_set_async(const char* alias); -/** @brief Opaque type representing a semaphore - * @hideinitializer - */ -typedef struct s_smx_sem_t* msg_sem_t; // Yeah that's a rename of the smx_sem_t which doesnt require smx_sem_t to be - // declared here +/** @brief Opaque type representing a semaphore */ +typedef sg_sem_t msg_sem_t; XBT_PUBLIC msg_sem_t MSG_sem_init(int initial_value); XBT_PUBLIC void MSG_sem_acquire(msg_sem_t sem); -XBT_PUBLIC msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout); +XBT_PUBLIC int MSG_sem_acquire_timeout(msg_sem_t sem, double timeout); XBT_PUBLIC void MSG_sem_release(msg_sem_t sem); XBT_PUBLIC int MSG_sem_get_capacity(msg_sem_t sem); XBT_PUBLIC void MSG_sem_destroy(msg_sem_t sem); diff --git a/include/simgrid/s4u/Semaphore.hpp b/include/simgrid/s4u/Semaphore.hpp index 6be8c9f039..749db63404 100644 --- a/include/simgrid/s4u/Semaphore.hpp +++ b/include/simgrid/s4u/Semaphore.hpp @@ -29,13 +29,13 @@ class XBT_PUBLIC Semaphore { smx_sem_t sem_; std::atomic_int_fast32_t refcount_{0}; - explicit Semaphore(unsigned int initial_capacity); - ~Semaphore(); - friend void intrusive_ptr_add_ref(Semaphore* sem); friend void intrusive_ptr_release(Semaphore* sem); public: + explicit Semaphore(unsigned int initial_capacity); + ~Semaphore(); + // No copy: /** You cannot create a new semaphore by copying an existing one. Use SemaphorePtr instead */ Semaphore(Semaphore const&) = delete; @@ -46,7 +46,10 @@ public: static SemaphorePtr create(unsigned int initial_capacity); void acquire(); + int acquire_timeout(double timeout); void release(); + int get_capacity(); + int would_block(); }; }} // namespace simgrid::s4u diff --git a/include/simgrid/semaphore.h b/include/simgrid/semaphore.h new file mode 100644 index 0000000000..61beb124a7 --- /dev/null +++ b/include/simgrid/semaphore.h @@ -0,0 +1,25 @@ +/* Public interface to the Link datatype */ + +/* Copyright (c) 2018. 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 INCLUDE_SIMGRID_SEMAPHORE_H_ +#define INCLUDE_SIMGRID_SEMAPHORE_H_ + +#include + +/* C interface */ +SG_BEGIN_DECL() +XBT_PUBLIC sg_sem_t sg_sem_init(int initial_value); +XBT_PUBLIC void sg_sem_acquire(sg_sem_t sem); +XBT_PUBLIC int sg_sem_acquire_timeout(sg_sem_t sem, double timeout); +XBT_PUBLIC void sg_sem_release(sg_sem_t sem); +XBT_PUBLIC int sg_sem_get_capacity(sg_sem_t sem); +XBT_PUBLIC void sg_sem_destroy(sg_sem_t sem); +XBT_PUBLIC int sg_sem_would_block(sg_sem_t sem); + +SG_END_DECL() + +#endif /* INCLUDE_SIMGRID_SEMAPHORE_H_ */ diff --git a/src/msg/msg_legacy.cpp b/src/msg/msg_legacy.cpp index 733a70aff4..8fe21648ef 100644 --- a/src/msg/msg_legacy.cpp +++ b/src/msg/msg_legacy.cpp @@ -378,3 +378,32 @@ int MSG_barrier_wait(sg_bar_t bar) { return sg_barrier_wait(bar); } + +sg_sem_t MSG_sem_init(int initial_value) +{ + return sg_sem_init(initial_value); +} +void MSG_sem_acquire(sg_sem_t sem) +{ + sg_sem_acquire(sem); +} +int MSG_sem_acquire_timeout(sg_sem_t sem, double timeout) +{ + sg_sem_acquire_timeout(sem, timeout); +} +void MSG_sem_release(sg_sem_t sem) +{ + sg_sem_release(sem); +} +int MSG_sem_get_capacity(sg_sem_t sem) +{ + return sg_sem_get_capacity(sem); +} +void MSG_sem_destroy(sg_sem_t sem) +{ + sg_sem_destroy(sem); +} +int MSG_sem_would_block(sg_sem_t sem) +{ + return sg_sem_would_block(sem); +} diff --git a/src/msg/msg_synchro.cpp b/src/msg/msg_synchro.cpp deleted file mode 100644 index c24469b2ba..0000000000 --- a/src/msg/msg_synchro.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright (c) 2013-2018. 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. */ - -#include "msg_private.hpp" -#include "simgrid/Exception.hpp" -#include "src/simix/smx_private.hpp" -#include "src/simix/smx_synchro_private.hpp" -#include "xbt/synchro.h" - -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_synchro, msg, "Logging specific to MSG (synchro)"); - -/** @addtogroup msg_synchro - * - * @{ - */ - -/** @brief creates a semaphore object of the given initial capacity */ -msg_sem_t MSG_sem_init(int initial_value) { - return simgrid::simix::simcall([initial_value] { return SIMIX_sem_init(initial_value); }); -} - -/** @brief locks on a semaphore object */ -void MSG_sem_acquire(msg_sem_t sem) { - simcall_sem_acquire(sem); -} - -/** @brief locks on a semaphore object up until the provided timeout expires */ -msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout) { - return simcall_sem_acquire_timeout(sem, timeout) ? MSG_TIMEOUT : MSG_OK; -} - -/** @brief releases the semaphore object */ -void MSG_sem_release(msg_sem_t sem) { - simgrid::simix::simcall([sem] { SIMIX_sem_release(sem); }); -} - -int MSG_sem_get_capacity(msg_sem_t sem) { - return simgrid::simix::simcall([sem] { return SIMIX_sem_get_capacity(sem); }); -} - -void MSG_sem_destroy(msg_sem_t sem) { - SIMIX_sem_destroy(sem); -} - -/** @brief returns a boolean indicating if this semaphore would block at this very specific time - * - * Note that the returned value may be wrong right after the function call, when you try to use it... - * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here. - */ -int MSG_sem_would_block(msg_sem_t sem) { - return simgrid::simix::simcall([sem] { return SIMIX_sem_would_block(sem); }); -} - -/**@}*/ diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp index 191094aeb9..a1df83b057 100644 --- a/src/s4u/s4u_Semaphore.cpp +++ b/src/s4u/s4u_Semaphore.cpp @@ -7,6 +7,7 @@ #include "src/simix/smx_synchro_private.hpp" #include "xbt/log.h" +#include "simgrid/forward.h" #include "simgrid/s4u/Semaphore.hpp" namespace simgrid { @@ -32,11 +33,26 @@ void Semaphore::acquire() simcall_sem_acquire(sem_); } +int Semaphore::acquire_timeout(double timeout) +{ + return simcall_sem_acquire_timeout(sem_, timeout); +} + void Semaphore::release() { simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); }); } +int Semaphore::get_capacity() +{ + return simgrid::simix::simcall([this] { return SIMIX_sem_get_capacity(sem_); }); +} + +int Semaphore::would_block() +{ + return simgrid::simix::simcall([this] { return SIMIX_sem_would_block(sem_); }); +} + void intrusive_ptr_add_ref(Semaphore* sem) { xbt_assert(sem); @@ -54,3 +70,47 @@ void intrusive_ptr_release(Semaphore* sem) } } +/* **************************** Public C interface *************************** */ +/** @brief creates a semaphore object of the given initial capacity */ +sg_sem_t sg_sem_init(int initial_value) +{ + return new simgrid::s4u::Semaphore(initial_value); +} + +/** @brief locks on a semaphore object */ +void sg_sem_acquire(sg_sem_t sem) +{ + sem->acquire(); +} + +/** @brief locks on a semaphore object up until the provided timeout expires */ +int sg_sem_acquire_timeout(sg_sem_t sem, double timeout) +{ + return sem->acquire_timeout(timeout); +} + +/** @brief releases the semaphore object */ +void sg_sem_release(sg_sem_t sem) +{ + sem->release(); +} + +int sg_sem_get_capacity(sg_sem_t sem) +{ + return sem->get_capacity(); +} + +void sg_sem_destroy(sg_sem_t sem) +{ + delete sem; +} + +/** @brief returns a boolean indicating if this semaphore would block at this very specific time + * + * Note that the returned value may be wrong right after the function call, when you try to use it... + * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here. + */ +int sg_sem_would_block(sg_sem_t sem) +{ + return sem->would_block(); +} diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 71f0815b68..5353dc066d 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -453,7 +453,6 @@ set(MSG_SRC src/msg/msg_gos.cpp src/msg/msg_legacy.cpp src/msg/msg_process.cpp - src/msg/msg_synchro.cpp src/msg/msg_task.cpp ) @@ -690,6 +689,7 @@ set(headers_to_install include/simgrid/kernel/future.hpp include/simgrid/host.h include/simgrid/link.h + include/simgrid/semaphore.h include/simgrid/storage.h include/simgrid/vm.h include/simgrid/zone.h