Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
props dict does not always exists anymore. Check if not NULL before
[simgrid.git] / src / bindings / ruby / rb_msg_task.c
index d54b542..bf24c21 100644 (file)
-#include "rb_msg_task.h"
+/* Task-related bindings to ruby  */
 
+/* Copyright (c) 2010. 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. */
+
+
+#include "bindings/ruby_bindings.h"
+
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
 
 // Free Method
-static void task_free(m_task_t tk) {
-  MSG_task_destroy(tk);
+void rb_task_free(m_task_t tk)
+{
+  //MSG_task_destroy(tk); ( This cause a bug !! is it really necessary ?!! not really sure !! )
 }
 
 // New Method
-static VALUE task_new(VALUE class, VALUE name,VALUE comp_size,VALUE comm_size)
+VALUE rb_task_new(VALUE class, VALUE name, VALUE comp_size,
+                  VALUE comm_size)
 {
-  
-  //char * t_name = RSTRING(name)->ptr;
-  m_task_t task = MSG_task_create(RSTRING(name)->ptr,NUM2INT(comp_size),NUM2INT(comm_size),NULL);
+  m_task_t task = MSG_task_create(RSTRING_PTR(name), NUM2INT(comp_size),
+                                  NUM2INT(comm_size), NULL);
+  rb_data_t data = malloc(sizeof(s_ruby_data_t));
+  data->ruby_task = NULL;
+  data->user_data = NULL;
+  MSG_task_set_data(task, (void *) data);
   // Wrap m_task_t to a Ruby Value
-  return Data_Wrap_Struct(class, 0, task_free, task);
-
+  return Data_Wrap_Struct(class, 0, rb_task_free, task);
 }
 
 //Get Computation Size
-static VALUE task_comp(VALUE class,VALUE task)
+VALUE rb_task_comp(VALUE class, VALUE task)
 {
   double size;
   m_task_t tk;
   // Wrap Ruby Value to m_task_t struct
-  Data_Get_Struct(task, m_task_t, tk);
+  Data_Get_Struct(task, s_m_task_t, tk);
   size = MSG_task_get_compute_duration(tk);
   return rb_float_new(size);
 }
 
-
 //Get Name
-
-static VALUE task_name(VALUE class,VALUE task)
+VALUE rb_task_name(VALUE class, VALUE task)
 {
-  
+
   // Wrap Ruby Value to m_task_t struct
-  
   m_task_t tk;
-  Data_Get_Struct(task, m_task_t, tk);
+  Data_Get_Struct(task, s_m_task_t, tk);
   return rb_str_new2(MSG_task_get_name(tk));
-   
 }
 
-
-
-
 // Execute Task
-
-static VALUE task_execute(VALUE class,VALUE task)
+VALUE rb_task_execute(VALUE class, VALUE task)
 {
-  
+
   // Wrap Ruby Value to m_task_t struct
   m_task_t tk;
-  Data_Get_Struct(task, m_task_t, tk);
+  Data_Get_Struct(task, s_m_task_t, tk);
   return INT2NUM(MSG_task_execute(tk));
-  
-  
 }
 
 // Sending Task
+void rb_task_send(VALUE class, VALUE task, VALUE mailbox)
+{
 
-static VALUE task_send(VALUE class,VALUE task,VALUE mailbox)
+  MSG_error_t rv;
+  rb_data_t data;
+  // Wrap Ruby Value to m_task_t struct
+  m_task_t tk;
+  Data_Get_Struct(task, s_m_task_t, tk);
+  data = MSG_task_get_data(tk);
+  data->ruby_task = (void *) task;
+  MSG_task_set_data(tk, (void *) data);
+  XBT_DEBUG("Sending task %p", tk);
+  rv = MSG_task_send(tk, RSTRING_PTR(mailbox));
+  if (rv != MSG_OK) {
+    if (rv == MSG_TRANSFER_FAILURE)
+      rb_raise(rb_eRuntimeError, "Transfer failure while Sending");
+    else if (rv == MSG_HOST_FAILURE)
+      rb_raise(rb_eRuntimeError, "Host failure while Sending");
+    else if (rv == MSG_TIMEOUT)
+      rb_raise(rb_eRuntimeError, "Timeout failure while Sending");
+    else
+      rb_raise(rb_eRuntimeError, "MSG_task_send failed");
+  }
+}
+
+// Receiving Task (returns a Task)
+VALUE rb_task_receive(VALUE class, VALUE mailbox)
+{
+  // We must put the location where we copy the task
+  // pointer to on the heap, because the stack may move
+  // during the context switches (damn ruby internals)
+  m_task_t *ptask = malloc(sizeof(m_task_t));
+  m_task_t task;
+  *ptask = NULL;
+  rb_data_t data = NULL;
+  XBT_DEBUG("Receiving a task on mailbox '%s', store it into %p",
+         RSTRING_PTR(mailbox), &task);
+  MSG_task_receive(ptask, RSTRING_PTR(mailbox));
+  task = *ptask;
+  free(ptask);
+  data = MSG_task_get_data(task);
+  if (data == NULL)
+    printf("Empty task while receving");
+  return (VALUE) data->ruby_task;
+}
+
+// It Return a Native Process ( m_process_t )
+VALUE rb_task_sender(VALUE class, VALUE task)
 {
-  
-    // Wrap Ruby Value to m_task_t struct
   m_task_t tk;
-  Data_Get_Struct(task, m_task_t, tk);
-  return INT2NUM(MSG_task_send(tk,RSTRING(mailbox)->ptr));
-  
-  
+  Data_Get_Struct(task, s_m_task_t, tk);
+  THROW_UNIMPLEMENTED;
+  return 0;                     //MSG_task_get_sender(tk);
 }
 
-// Recieving Task 
+// it return a Host 
+VALUE rb_task_source(VALUE class, VALUE task)
+{
+  m_task_t tk;
+  Data_Get_Struct(task, s_m_task_t, tk);
 
-/**
-*It Return a Task 
-*/
+  m_host_t host = MSG_task_get_source(tk);
+  if (!host->data) {
+    rb_raise(rb_eRuntimeError, "MSG_task_get_source() failed");
+    return Qnil;
+  }
+  THROW_UNIMPLEMENTED;
+  return 0;                     //host;
+}
 
-static VALUE task_receive(VALUE class,VALUE mailbox)
+// Return Boolean
+VALUE rb_task_listen(VALUE class, VALUE task, VALUE alias)
 {
-  m_task_t tk; 
-  MSG_task_receive(tk,RSTRING(mailbox)->ptr); 
-  return Data_Wrap_Struct(class, 0, task_free, tk);
+  m_task_t tk;
+  const char *p_alias;
+  int rv;
+
+  Data_Get_Struct(task, s_m_task_t, tk);
+  p_alias = RSTRING_PTR(alias);
+
+  rv = MSG_task_listen(p_alias);
+
+  if (rv)
+    return Qtrue;
+
+  return Qfalse;
 }
 
-// Recieve Task 2
-// Not Appreciated 
-static VALUE task_receive2(VALUE class,VALUE task,VALUE mailbox)
+// return Boolean
+VALUE rb_task_listen_host(VALUE class, VALUE task, VALUE alias, VALUE host)
 {
+
   m_task_t tk;
-  Data_Get_Struct(task, m_task_t, tk);
-  return INT2NUM(MSG_task_receive(tk,RSTRING(mailbox)->ptr)); 
-  
+  m_host_t ht;
+  const char *p_alias;
+  int rv;
+
+  Data_Get_Struct(task, s_m_task_t, tk);
+  Data_Get_Struct(host, s_m_host_t, ht);
+  p_alias = RSTRING_PTR(alias);
+  rv = MSG_task_listen_from_host(p_alias, ht);
+  if (rv)
+    return Qtrue;
+  return Qfalse;
 }
 
 
-// It Return a Native Process ( m_process_t )
-static VALUE task_sender(VALUE class,VALUE task)
+// Set Priority
+void rb_task_set_priority(VALUE class, VALUE task, VALUE priority)
 {
+
   m_task_t tk;
-  Data_Get_Struct(task,m_task_t,tk);
-  return MSG_task_get_sender(tk);
-  
+  double prt = NUM2DBL(priority);
+  Data_Get_Struct(task, s_m_task_t, tk);
+  MSG_task_set_priority(tk, prt);
+
 }
 
-// it return a Host 
-static VALUE task_source(VALUE class,VALUE task)
+// Cancel
+void rb_task_cancel(VALUE class, VALUE task)
 {
   m_task_t tk;
-  Data_Get_Struct(task,m_task_t,tk);
-  
-  m_host_t host = MSG_task_get_source(tk);
-  if(!host->data)
-  {
-    rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
-    return Qnil;
-  }
-  return host;
-  
+  Data_Get_Struct(task, s_m_task_t, tk);
+  MSG_task_cancel(tk);
+
 }
 
-// Return Boolean
-static VALUE task_listen(VALUE class,VALUE task,VALUE alias)
+void rb_task_set_data(VALUE class, VALUE task, VALUE data)
 {
- m_task_t tk;
- const char *p_alias;
- int rv;
- Data_Get_Struct(task,m_task_t,tk);
- p_alias = RSTRING(alias)->ptr;
- rv = MSG_task_listen(p_alias);
- if(rv) return Qtrue;
- return Qfalse;
+  m_task_t tk;
+  rb_data_t rb_data;
+  Data_Get_Struct(task, s_m_task_t, tk);
+  rb_data = MSG_task_get_data(tk);
+  rb_data->user_data = (void *) data;
+  MSG_task_set_data(tk, (void *) rb_data);
 
 }
 
-// return Boolean
-static VALUE task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host)
-{
-  
- m_task_t tk;
- m_host_t ht;
- const char *p_alias;
- int rv;
- Data_Get_Struct(task,m_task_t,tk);
- Data_Get_Struct(host,m_host_t,ht);
- p_alias = RSTRING(alias)->ptr;
- rv = MSG_task_listen_from_host(p_alias,ht);
- if (rv) return Qtrue;
- return Qfalse;
-  
-}
\ No newline at end of file
+VALUE rb_task_get_data(VALUE class, VALUE task)
+{
+  m_task_t tk;
+  Data_Get_Struct(task, s_m_task_t, tk);
+  rb_data_t rb_data = MSG_task_get_data(tk);
+  if (!rb_data->user_data)
+    XBT_ERROR("the task %s contain no user data", MSG_task_get_name(tk));
+
+  return (VALUE) rb_data->user_data;
+}
+
+VALUE rb_task_has_data(VALUE class, VALUE task)
+{
+  m_task_t tk;
+  Data_Get_Struct(task, s_m_task_t, tk);
+  rb_data_t rb_data = MSG_task_get_data(tk);
+  if (!rb_data->user_data)
+    return Qfalse;
+  return Qtrue;
+}