/** @brief Pop something from the message exchange queue, with a timeout. * * @see #xbt_queue_pop * */ void xbt_queue_pop_timed(xbt_queue_t queue, void *const dst, double delay) { double begin = xbt_time(); xbt_mutex_acquire(queue->mutex); if (delay == 0) { if (xbt_dynar_is_empty(queue->data)) { xbt_mutex_release(queue->mutex); THROWF(timeout_error, 0, "Delay = 0, and queue is empty"); } } else { while ((xbt_dynar_is_empty(queue->data)) && (delay < 0 || (xbt_time() - begin) <= delay)) { XBT_DEBUG("Queue %p empty. Waiting", queue); TRY { xbt_cond_timedwait(queue->not_empty, queue->mutex, delay < 0 ? -1 : delay - (xbt_time() - begin)); } CATCH_ANONYMOUS { xbt_mutex_release(queue->mutex); RETHROW; } } } xbt_dynar_pop(queue->data, dst); xbt_cond_signal(queue->not_full); xbt_mutex_release(queue->mutex); }
/** The guy we will move from host to host. It move alone and then is moved by policeman back */ static int emigrant(int argc, char *argv[]) { msg_task_t task; XBT_INFO ("I'll look for a new job on another machine where the grass is greener."); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin")); XBT_INFO("Yeah, found something to do"); task = MSG_task_create("job", 98095000, 0, NULL); MSG_task_execute(task); MSG_task_destroy(task); MSG_process_sleep(2); XBT_INFO("Moving back home after work"); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin")); MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin")); MSG_process_sleep(4); xbt_mutex_acquire(mutex); process_to_migrate = MSG_process_self(); xbt_cond_broadcast(cond); xbt_mutex_release(mutex); MSG_process_suspend(MSG_process_self()); msg_host_t h = MSG_process_get_host(MSG_process_self()); XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h)); XBT_INFO("Uh, nothing to do here. Stopping now"); return 0; } /* end_of_emigrant */
/** @brief Get the queue size */ unsigned long xbt_queue_length(const xbt_queue_t queue) { unsigned long res; xbt_mutex_acquire(queue->mutex); res = xbt_dynar_length(queue->data); xbt_mutex_release(queue->mutex); return res; }
/** @brief Pop something from the message exchange queue. * * This is blocking if the queue is empty. * * @see #xbt_dynar_pop * */ void xbt_queue_pop(xbt_queue_t queue, void *const dst) { xbt_mutex_acquire(queue->mutex); while (xbt_dynar_is_empty(queue->data)) { XBT_DEBUG("Queue %p empty. Waiting", queue); xbt_cond_wait(queue->not_empty, queue->mutex); } xbt_dynar_pop(queue->data, dst); xbt_cond_signal(queue->not_full); xbt_mutex_release(queue->mutex); }
static int sender(int argc, char *argv[]) { char* message_name = argv[1]; #ifndef DISABLE_THE_MUTEX xbt_mutex_acquire(mutex); #endif MSG_task_send(MSG_task_create(message_name, 0.0, 0.0, NULL), BOX_NAME); #ifndef DISABLE_THE_MUTEX xbt_mutex_release(mutex); #endif return 0; }
JNIEXPORT void JNICALL Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) { xbt_mutex_t mutex; mutex = (xbt_mutex_t) (uintptr_t) env->GetLongField(obj, jsyncro_field_Mutex_bind); xbt_ex_t e; TRY { xbt_mutex_acquire(mutex); } CATCH(e) { xbt_ex_free(e); } }
/** @brief Push something to the message exchange queue. * * This is blocking if the declared capacity is non-nul, and if this amount is reached. * * @see #xbt_dynar_push */ void xbt_queue_push(xbt_queue_t queue, const void *src) { xbt_mutex_acquire(queue->mutex); while (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) { XBT_DEBUG("Capacity of %p exceeded (=%d). Waiting", queue, queue->capacity); xbt_cond_wait(queue->not_full, queue->mutex); } xbt_dynar_push(queue->data, src); xbt_cond_signal(queue->not_empty); xbt_mutex_release(queue->mutex); }
/* This function move the emigrant on Jacquelin */ static int policeman(int argc, char *argv[]) { xbt_mutex_acquire(mutex); XBT_INFO("Wait a bit before migrating the emigrant."); while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex); MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin")); XBT_INFO("I moved the emigrant"); MSG_process_resume(process_to_migrate); xbt_mutex_release(mutex); return 0; } /* end_of_policeman */
void xbt_thread_join(xbt_thread_t thread) { xbt_mutex_acquire(thread->mutex); xbt_assert(thread->joinable, "Cannot join on %p: wasn't created joinable", thread); if (!thread->done) { xbt_cond_wait(thread->cond, thread->mutex); xbt_mutex_release(thread->mutex); } xbt_mutex_destroy(thread->mutex); xbt_cond_destroy(thread->cond); free(thread->name); free(thread); }
static int xbt_thread_create_wrapper(int argc, char *argv[]) { smx_process_t self = SIMIX_process_self(); xbt_thread_t t = (xbt_thread_t) SIMIX_process_self_get_data(self); simcall_process_set_data(self, t->father_data); t->code(t->userparam); if (t->joinable) { t->done = 1; xbt_mutex_acquire(t->mutex); xbt_cond_broadcast(t->cond); xbt_mutex_release(t->mutex); } else { xbt_mutex_destroy(t->mutex); xbt_cond_destroy(t->cond); free(t->name); free(t); } return 0; }
/** @brief Unshift something to the message exchange queue, with a timeout. * * @see #xbt_queue_unshift */ void xbt_queue_unshift_timed(xbt_queue_t queue, const void *src, double delay) { double begin = xbt_time(); xbt_mutex_acquire(queue->mutex); if (delay == 0) { if (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) { xbt_mutex_release(queue->mutex); THROWF(timeout_error, 0, "Capacity of %p exceeded (=%d), and delay = 0", queue, queue->capacity); } } else { while (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data) && (delay < 0 || (xbt_time() - begin) <= delay)) { XBT_DEBUG("Capacity of %p exceeded (=%d). Waiting", queue, queue->capacity); TRY { xbt_cond_timedwait(queue->not_full, queue->mutex, delay < 0 ? -1 : delay - (xbt_time() - begin)); } CATCH_ANONYMOUS { xbt_mutex_release(queue->mutex); RETHROW; } } } xbt_dynar_unshift(queue->data, src); xbt_cond_signal(queue->not_empty); xbt_mutex_release(queue->mutex); }