X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ad02a1b4a1730fb36f36295f8749d4811f4d9a65..e49d3a1ebc59da7d2e1a02ccf9a56ddc5223beef:/src/sthread/sthread_impl.cpp diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index fda1411572..75a6dba68e 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -6,6 +6,7 @@ /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */ #include "simgrid/s4u/Barrier.hpp" +#include "simgrid/s4u/ConditionVariable.hpp" #include "smpi/smpi.h" #include "xbt/asserts.h" #include "xbt/ex.h" @@ -238,6 +239,91 @@ int sthread_barrier_destroy(sthread_barrier_t* barrier){ return 0; } +int sthread_cond_init(sthread_cond_t* cond, sthread_condattr_t* attr) +{ + auto cv = sg4::ConditionVariable::create(); + intrusive_ptr_add_ref(cv.get()); + + cond->cond = cv.get(); + cond->mutex = nullptr; + return 0; +} +int sthread_cond_signal(sthread_cond_t* cond) +{ + XBT_DEBUG("%s(%p)", __func__, cond); + + if (cond->mutex == nullptr) + XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond); + else { + auto* owner = static_cast(cond->mutex)->get_owner(); + if (owner == nullptr) + XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling " + "pthread_cond_signal(). The signal could get lost.", + cond); + else if (owner != simgrid::s4u::Actor::self()) + XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling " + "calling pthread_cond_signal(). The signal could get lost.", + cond, owner->get_cname()); + } + + static_cast(cond->cond)->notify_one(); + return 0; +} +int sthread_cond_broadcast(sthread_cond_t* cond) +{ + XBT_DEBUG("%s(%p)", __func__, cond); + + if (cond->mutex == nullptr) + XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond); + else { + auto* owner = static_cast(cond->mutex)->get_owner(); + if (owner == nullptr) + XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling " + "pthread_cond_broadcast(). The signal could get lost.", + cond); + else if (owner != simgrid::s4u::Actor::self()) + XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling " + "calling pthread_cond_broadcast(). The signal could get lost.", + cond, owner->get_cname()); + } + + static_cast(cond->cond)->notify_all(); + return 0; +} +int sthread_cond_wait(sthread_cond_t* cond, sthread_mutex_t* mutex) +{ + XBT_DEBUG("%s(%p)", __func__, cond); + + if (cond->mutex == nullptr) + cond->mutex = mutex->mutex; + else if (cond->mutex != mutex->mutex) + XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may " + "not work with such a dangerous code.", + cond, cond->mutex, mutex->mutex); + + static_cast(cond->cond)->wait(static_cast(mutex->mutex)); + return 0; +} +int sthread_cond_timedwait(sthread_cond_t* cond, sthread_mutex_t* mutex, const struct timespec* abs_timeout) +{ + XBT_DEBUG("%s(%p)", __func__, cond); + + if (cond->mutex == nullptr) + cond->mutex = mutex->mutex; + else if (cond->mutex != mutex->mutex) + XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may " + "not work with such a dangerous code.", + cond, cond->mutex, mutex->mutex); + + THROW_UNIMPLEMENTED; +} +int sthread_cond_destroy(sthread_cond_t* cond) +{ + XBT_DEBUG("%s(%p)", __func__, cond); + intrusive_ptr_release(static_cast(cond->cond)); + return 0; +} + int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value) { auto s = sg4::Semaphore::create(value); @@ -305,30 +391,3 @@ int sthread_usleep(double seconds) simgrid::s4u::this_actor::sleep_for(seconds); return 0; } - -#if 0 -int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) { - *cond = sg_cond_init(); - return 0; -} - -int pthread_cond_signal(pthread_cond_t *cond) { - sg_cond_notify_one(*cond); - return 0; -} - -int pthread_cond_broadcast(pthread_cond_t *cond) { - sg_cond_notify_all(*cond); - return 0; -} - -int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { - sg_cond_wait(*cond, *mutex); - return 0; -} - -int pthread_cond_destroy(pthread_cond_t *cond) { - sg_cond_destroy(*cond); - return 0; -} -#endif