+/** @brief Returns true if acquiring this semaphore would block */
+XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
+{
+ XBT_IN("(%p)",sem);
+ XBT_OUT();
+ return (sem->value <= 0);
+}
+
+/** @brief Returns the current capacity of the semaphore */
+int SIMIX_sem_get_capacity(smx_sem_t sem)
+{
+ XBT_IN("(%p)",sem);
+ XBT_OUT();
+ return sem->value;
+}
+
+static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
+ smx_simcall_t simcall)
+{
+ XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
+ smx_action_t sync_act = NULL;
+
+ XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
+ if (sem->value <= 0) {
+ sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
+ xbt_fifo_unshift(sync_act->simcalls, simcall);
+ issuer->waiting_action = sync_act;
+ xbt_swag_insert(issuer, sem->sleeping);
+ } else {
+ sem->value--;
+ SIMIX_simcall_answer(simcall);
+ }
+ XBT_OUT();
+}
+
+/**
+ * \brief Handles a sem acquire simcall without timeout.
+ * \param simcall the simcall
+ */
+void SIMIX_pre_sem_acquire(smx_simcall_t simcall)
+{
+ XBT_IN("(%p)",simcall);
+ _SIMIX_sem_wait(simcall->sem_acquire.sem, -1, simcall->issuer, simcall);
+ XBT_OUT();
+}
+
+/**
+ * \brief Handles a sem acquire simcall with timeout.
+ * \param simcall the simcall
+ */
+void SIMIX_pre_sem_acquire_timeout(smx_simcall_t simcall)
+{
+ XBT_IN("(%p)",simcall);
+ _SIMIX_sem_wait(simcall->sem_acquire_timeout.sem,
+ simcall->sem_acquire_timeout.timeout, simcall->issuer, simcall);
+ XBT_OUT();
+}