From: Frederic Suter Date: Wed, 10 Jul 2019 10:20:20 +0000 (+0200) Subject: untested C interface to Condition Variables X-Git-Tag: v3.24~349 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/a31a5aabed1a30637c9ccdd30361b1eda8155968?ds=sidebyside untested C interface to Condition Variables --- diff --git a/MANIFEST.in b/MANIFEST.in index 516d08fbdf..b58b8948a4 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1896,6 +1896,7 @@ include include/simgrid/Exception.hpp include include/simgrid/actor.h include include/simgrid/barrier.h include include/simgrid/chrono.hpp +include include/simgrid/cond.h include include/simgrid/config.h.in include include/simgrid/engine.h include include/simgrid/forward.h @@ -1925,6 +1926,7 @@ include include/simgrid/link.h include include/simgrid/mailbox.h include include/simgrid/modelchecker.h include include/simgrid/msg.h +include include/simgrid/mutex.h include include/simgrid/plugins/dvfs.h include include/simgrid/plugins/energy.h include include/simgrid/plugins/file_system.h diff --git a/include/simgrid/cond.h b/include/simgrid/cond.h new file mode 100644 index 0000000000..08c3318f18 --- /dev/null +++ b/include/simgrid/cond.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2019. 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_COND_H_ +#define INCLUDE_SIMGRID_COND_H_ + +#include + +/* C interface */ +SG_BEGIN_DECL() +/** @brief Creates a condition variable */ +XBT_PUBLIC sg_cond_t sg_cond_init(); + +/** @brief Blocks onto the given condition variable */ +XBT_PUBLIC void sg_cond_wait(sg_cond_t cond, sg_mutex_t mutex); +/** @brief Blocks onto the given condition variable, but only for the given amount of time. + * @return 0 on success, 1 on timeout */ +XBT_PUBLIC int sg_cond_wait_for(sg_cond_t cond, sg_mutex_t mutex, double delay); +/** @brief Signals the given mutex variable */ +XBT_PUBLIC void sg_cond_notify_one(sg_cond_t cond); +/** @brief Broadcasts the given mutex variable */ +XBT_PUBLIC void sg_cond_notify_all(sg_cond_t cond); +/** @brief Destroys the given mutex variable */ +XBT_PUBLIC void sg_cond_destroy(sg_cond_t cond); + +SG_END_DECL() + +#endif /* INCLUDE_SIMGRID_COND_H_ */ diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index 65b416c976..8d4d26c986 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -179,6 +179,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::ConditionVariable s4u_ConditionVariable; typedef simgrid::s4u::Mutex s4u_Mutex; typedef simgrid::s4u::Semaphore s4u_Semaphore; typedef simgrid::s4u::Storage s4u_Storage; @@ -200,6 +201,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_ConditionVariable s4u_ConditionVariable; typedef struct s4u_Mutex s4u_Mutex; typedef struct s4u_Semaphore s4u_Semaphore; typedef struct s4u_Storage s4u_Storage; @@ -217,6 +219,7 @@ typedef struct s_smx_sem* smx_sem_t; #endif typedef s4u_Barrier* sg_bar_t; +typedef s4u_ConditionVariable* sg_cond_t; typedef s4u_Mutex* sg_mutex_t; typedef s4u_Semaphore* sg_sem_t; typedef s4u_NetZone* sg_netzone_t; diff --git a/include/simgrid/s4u/ConditionVariable.hpp b/include/simgrid/s4u/ConditionVariable.hpp index 523024d987..f11df5c7a7 100644 --- a/include/simgrid/s4u/ConditionVariable.hpp +++ b/include/simgrid/s4u/ConditionVariable.hpp @@ -28,9 +28,8 @@ private: friend kernel::activity::ConditionVariableImpl; kernel::activity::ConditionVariableImpl* const cond_; - explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {} - public: + explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {} ConditionVariable(ConditionVariable const&) = delete; ConditionVariable& operator=(ConditionVariable const&) = delete; diff --git a/src/s4u/s4u_ConditionVariable.cpp b/src/s4u/s4u_ConditionVariable.cpp index e9f2c458ec..0e3713dfa3 100644 --- a/src/s4u/s4u_ConditionVariable.cpp +++ b/src/s4u/s4u_ConditionVariable.cpp @@ -3,6 +3,8 @@ /* 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 "simgrid/cond.h" +#include "simgrid/forward.h" #include "simgrid/s4u/ConditionVariable.hpp" #include "simgrid/simix.h" #include "src/kernel/activity/ConditionVariableImpl.hpp" @@ -85,3 +87,38 @@ void intrusive_ptr_release(ConditionVariable* cond) } // namespace s4u } // namespace simgrid + +/* **************************** Public C interface *************************** */ +sg_cond_t sg_cond_init() +{ + simgrid::kernel::activity::ConditionVariableImpl* cond = + simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); }); + + return new simgrid::s4u::ConditionVariable(cond); +} + +void sg_cond_wait(sg_cond_t cond, sg_mutex_t mutex) +{ + cond->wait(mutex); +} + +int sg_cond_wait_for(sg_cond_t cond, sg_mutex_t mutex, double delay) +{ + std::unique_lock lock(*mutex); + return static_cast(cond->wait_for(lock, delay)); +} + +void sg_cond_notify_one(sg_cond_t cond) +{ + cond->notify_one(); +} + +void sg_cond_notify_all(sg_cond_t cond) +{ + cond->notify_all(); +} + +void sg_cond_destroy(sg_cond_t cond) +{ + delete cond; +} diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index b05a4a8939..8185799a51 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -693,6 +693,7 @@ set(headers_to_install include/simgrid/kernel/future.hpp include/simgrid/host.h include/simgrid/link.h + include/simgrid/cond.h include/simgrid/mutex.h include/simgrid/semaphore.h include/simgrid/storage.h