1 /* Task-related bindings to ruby */
3 /* Copyright 2010. The SimGrid Team. All right reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "bindings/ruby_bindings.h"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
14 void rb_task_free(m_task_t tk) {
15 //MSG_task_destroy(tk); ( This cause a bug !! is it really necessary ?!! not really sure !! )
19 VALUE rb_task_new(VALUE class, VALUE name,VALUE comp_size,VALUE comm_size) {
20 m_task_t task = MSG_task_create(RSTRING(name)->ptr,NUM2INT(comp_size),NUM2INT(comm_size),NULL);
21 // Wrap m_task_t to a Ruby Value
22 return Data_Wrap_Struct(class, 0, rb_task_free, task);
26 //Get Computation Size
27 VALUE rb_task_comp(VALUE class,VALUE task) {
30 // Wrap Ruby Value to m_task_t struct
31 Data_Get_Struct(task, s_m_task_t, tk);
32 size = MSG_task_get_compute_duration(tk);
33 return rb_float_new(size);
37 VALUE rb_task_name(VALUE class,VALUE task) {
39 // Wrap Ruby Value to m_task_t struct
41 Data_Get_Struct(task, s_m_task_t, tk);
42 return rb_str_new2(MSG_task_get_name(tk));
46 VALUE rb_task_execute(VALUE class,VALUE task) {
48 // Wrap Ruby Value to m_task_t struct
50 Data_Get_Struct(task, s_m_task_t, tk);
51 return INT2NUM(MSG_task_execute(tk));
55 void rb_task_send(VALUE class,VALUE task,VALUE mailbox) {
58 // Wrap Ruby Value to m_task_t struct
60 Data_Get_Struct(task, s_m_task_t, tk);
61 INFO1("Sending task %p",tk);
62 rv = MSG_task_send(tk,RSTRING(mailbox)->ptr);
65 if (rv == MSG_TRANSFER_FAILURE )
66 rb_raise(rb_eRuntimeError,"Transfer failure while Sending");
67 else if ( rv == MSG_HOST_FAILURE )
68 rb_raise(rb_eRuntimeError,"Host failure while Sending");
69 else if ( rv == MSG_TIMEOUT_FAILURE )
70 rb_raise(rb_eRuntimeError,"Timeout failure while Sending");
72 rb_raise(rb_eRuntimeError,"MSG_task_send failed");
76 // Receiving Task (returns a Task)
77 VALUE rb_task_receive(VALUE class, VALUE mailbox) {
78 // We must put the location where we copy the task
79 // pointer to on the heap, because the stack may move
80 // during the context switches (damn ruby internals)
81 m_task_t *ptask = malloc(sizeof(m_task_t));
84 INFO2("Receiving a task on mailbox '%s', store it into %p",RSTRING(mailbox)->ptr,&task);
85 MSG_task_receive(ptask,RSTRING(mailbox)->ptr);
88 return Data_Wrap_Struct(class, 0, rb_task_free, task);
91 // It Return a Native Process ( m_process_t )
92 VALUE rb_task_sender(VALUE class,VALUE task) {
94 Data_Get_Struct(task,s_m_task_t,tk);
96 return 0;//MSG_task_get_sender(tk);
100 VALUE rb_task_source(VALUE class,VALUE task) {
102 Data_Get_Struct(task,s_m_task_t,tk);
104 m_host_t host = MSG_task_get_source(tk);
106 rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
114 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
119 Data_Get_Struct(task,s_m_task_t,tk);
120 p_alias = RSTRING(alias)->ptr;
122 rv = MSG_task_listen(p_alias);
130 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
137 Data_Get_Struct(task,s_m_task_t,tk);
138 Data_Get_Struct(host,s_m_host_t,ht);
139 p_alias = RSTRING(alias)->ptr;
141 rv = MSG_task_listen_from_host(p_alias,ht);