Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use pointers instead references.
[simgrid.git] / src / cxx / Process.cxx
index bceac32..856ce5e 100644 (file)
@@ -1,12 +1,19 @@
-#include <Process.hpp>\r
+/*\r
+ * Process.cxx\r
+ *\r
+ * Copyright 2006,2007 Martin Quinson, Malek Cherier           \r
+ * All right reserved. \r
+ *\r
+ * This program is free software; you can redistribute \r
+ * it and/or modify it under the terms of the license \r
+ *(GNU LGPL) which comes with this package. \r
+ *\r
+ */\r
\r
+ /* Process member functions implementation.\r
+  */  \r
 \r
-#include <MsgException.hpp>\r
-#include <NullPointerException.hpp>\r
-#include <HostNotFoundException.hpp>\r
-#include <ProcessNotFoundException.hpp>\r
-#include <InvalidArgumentException.hpp>\r
-#include <BadAllocException.hpp>\r
-#include <LogicException.hpp>\r
+#include <Process.hpp>\r
 \r
 \r
 #include <ApplicationHandler.hpp>\r
@@ -21,6 +28,7 @@
 #include <msg/mailbox.h>\r
 \r
 \r
+\r
 namespace SimGrid\r
 {\r
        namespace Msg\r
@@ -35,7 +43,7 @@ namespace SimGrid
                }\r
                \r
                Process::Process(const char* hostName, const char* name)\r
-               throw(InvalidArgumentException, HostNotFoundException)\r
+               throw(NullPointerException, HostNotFoundException, BadAllocException)\r
                {\r
                        // check the parameters\r
                        \r
@@ -81,7 +89,7 @@ namespace SimGrid
                }\r
                \r
                Process::Process(const char* hostName, const char* name, int argc, char** argv)\r
-               throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException)\r
+               throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException, BadAllocException)\r
                {\r
                        // check the parameters\r
                        \r
@@ -223,7 +231,7 @@ namespace SimGrid
                        \r
                }\r
                \r
-               void Process::putTask(const Host& rHost, int channel, const Task& rTask)\r
+               void Process::putTask(const Host& rHost, int channel, Task* task)\r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -231,11 +239,11 @@ namespace SimGrid
                        if(channel < 0)\r
                                throw InvalidArgumentException("channel (must not be negative)");\r
                                \r
-                       if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))\r
+                       if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, rHost.nativeHost, channel, -1.0))\r
                                throw MsgException("MSG_task_put_with_timeout()");\r
                }\r
                \r
-               void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout) \r
+               void Process::putTask(const Host& rHost, int channel, Task* task, double timeout) \r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -245,11 +253,11 @@ namespace SimGrid
                        if(timeout < 0 && timeout != -1.0)\r
                                throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");\r
                                \r
-                       if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))\r
+                       if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, rHost.nativeHost, channel, timeout))\r
                                throw MsgException("MSG_task_put_with_timeout() failed");\r
                }\r
                \r
-               Task& Process::getTask(int channel) \r
+               Task* Process::getTask(int channel) \r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -262,10 +270,10 @@ namespace SimGrid
                        if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL)) \r
                                throw MsgException("MSG_task_get_ext() failed");\r
                        \r
-                       return (*((Task*)(nativeTask->data)));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
-               Task& Process::getTask(int channel, double timeout) \r
+               Task* Process::getTask(int channel, double timeout) \r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -280,10 +288,10 @@ namespace SimGrid
                        if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL)) \r
                                throw MsgException("MSG_task_get_ext() failed");\r
                        \r
-                       return (*((Task*)(nativeTask->data)));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
-               Task& Process::getTask(int channel, const Host& rHost) \r
+               Task* Process::getTask(int channel, const Host& rHost) \r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -295,10 +303,10 @@ namespace SimGrid
                        if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost)) \r
                                throw MsgException("MSG_task_get_ext() failed");\r
                        \r
-                       return (*((Task*)(nativeTask->data)));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
-               Task& Process::getTask(int channel, double timeout, const Host& rHost)\r
+               Task* Process::getTask(int channel, double timeout, const Host& rHost)\r
                throw(InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -313,10 +321,10 @@ namespace SimGrid
                        if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost)) \r
                                throw MsgException("MSG_task_get_ext() failed");\r
                        \r
-                       return (*((Task*)(nativeTask->data)));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
-               void Process::sendTask(const char* alias, const Task& rTask, double timeout) \r
+               void Process::sendTask(const char* alias, Task* task, double timeout) \r
                throw(NullPointerException, InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -327,12 +335,12 @@ namespace SimGrid
                        if(timeout < 0 && timeout !=-1.0)\r
                                throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
                        \r
-                       if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))\r
+                       if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias ,timeout))\r
                                throw MsgException("MSG_task_send_with_timeout()");\r
                                \r
                }\r
                \r
-               void Process::sendTask(const char* alias, const Task& rTask) \r
+               void Process::sendTask(const char* alias, Task* task) \r
                throw(NullPointerException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -340,11 +348,11 @@ namespace SimGrid
                        if(!alias)\r
                                throw NullPointerException("alias");\r
                                \r
-                       if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))\r
+                       if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias ,-1.0))\r
                                throw MsgException("MSG_task_send_with_timeout()");\r
                }\r
                \r
-               void Process::sendTask(const Task& rTask) \r
+               void Process::sendTask(Task* task) \r
                throw(BadAllocException, MsgException)\r
                {\r
                        char* alias = (char*)calloc( strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
@@ -354,7 +362,7 @@ namespace SimGrid
                                \r
                        sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
                        \r
-                       MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);\r
+                       MSG_error_t rv = MSG_task_send_with_timeout(task->nativeTask, alias ,-1.0);\r
                        \r
                        free(alias);\r
                        \r
@@ -362,7 +370,7 @@ namespace SimGrid
                                throw MsgException("MSG_task_send_with_timeout()");\r
                }\r
                \r
-               void Process::sendTask(const Task& rTask, double timeout) \r
+               void Process::sendTask(Task* task, double timeout) \r
                throw(BadAllocException, InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -377,7 +385,7 @@ namespace SimGrid
                                \r
                        sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
                        \r
-                       MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);\r
+                       MSG_error_t rv = MSG_task_send_with_timeout(task->nativeTask, alias ,timeout);\r
                        \r
                        free(alias);\r
                        \r
@@ -385,7 +393,7 @@ namespace SimGrid
                                throw MsgException("MSG_task_send_with_timeout()");     \r
                }\r
                \r
-               Task& Process::receiveTask(const char* alias) \r
+               Task* Process::receiveTask(const char* alias) \r
                throw(NullPointerException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -398,11 +406,11 @@ namespace SimGrid
                        if (MSG_OK !=  MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL)) \r
                                throw MsgException("MSG_task_receive_ext() failed");\r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(void) \r
+               Task* Process::receiveTask(void) \r
                throw(BadAllocException, MsgException)\r
                {\r
                        \r
@@ -422,11 +430,11 @@ namespace SimGrid
                        if(MSG_OK !=  rv) \r
                                throw MsgException("MSG_task_receive_ext() failed");    \r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(const char* alias, double timeout) \r
+               Task* Process::receiveTask(const char* alias, double timeout) \r
                throw(NullPointerException, InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -442,11 +450,11 @@ namespace SimGrid
                        if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
                                throw MsgException("MSG_task_receive_ext() failed");            \r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(double timeout) \r
+               Task* Process::receiveTask(double timeout) \r
                throw(InvalidArgumentException, BadAllocException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -471,11 +479,11 @@ namespace SimGrid
                        if(MSG_OK !=  rv) \r
                                throw MsgException("MSG_task_receive_ext() failed");    \r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost) \r
+               Task* Process::receiveTask(const char* alias, double timeout, const Host& rHost) \r
                throw(NullPointerException, InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -491,11 +499,11 @@ namespace SimGrid
                        if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
                                throw MsgException("MSG_task_receive_ext() failed");\r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(double timeout, const Host& rHost) \r
+               Task* Process::receiveTask(double timeout, const Host& rHost) \r
                throw(BadAllocException, InvalidArgumentException, MsgException)\r
                {\r
                        // check the parameters\r
@@ -519,11 +527,11 @@ namespace SimGrid
                        if(MSG_OK !=  rv) \r
                                throw MsgException("MSG_task_receive_ext() failed");\r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
                \r
-               Task& Process::receiveTask(const char* alias, const Host& rHost) \r
+               Task* Process::receiveTask(const char* alias, const Host& rHost) \r
                throw(NullPointerException, MsgException)\r
                {\r
                        \r
@@ -537,10 +545,10 @@ namespace SimGrid
                        if(MSG_OK !=   MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
                                throw MsgException("MSG_task_receive_ext() failed");\r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
                \r
-               Task& Process::receiveTask(const Host& rHost) \r
+               Task* Process::receiveTask(const Host& rHost) \r
                throw(BadAllocException, MsgException)\r
                {\r
                        char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
@@ -559,67 +567,8 @@ namespace SimGrid
                        if(MSG_OK !=  rv) \r
                                throw MsgException("MSG_task_receive_ext() failed");\r
                \r
-                       return (*((Task*)nativeTask->data));\r
+                       return (Task*)(nativeTask->data);\r
                }\r
-               \r
-               /*void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
-               throw(HostNotFoundException)\r
-               {\r
-                       smx_process_t nativeCurrentProcess = NULL;\r
-                       \r
-                       // allocate the native process\r
-                       this->nativeProcess = xbt_new0(s_smx_process_t, 1);\r
-                       \r
-                       // allocate the simulation data of the native process\r
-                       smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);\r
-                       \r
-                       // try to retrieve the host where to createt the process from its name\r
-                       smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());\r
-                       \r
-                       if(!nativeHost)\r
-                               throw HostNotFoundException(rHost.getName());\r
-                       \r
-                       // realloc the list of the argument to add the pointer to this process instance at the end\r
-                       argv = (char**)realloc(argc + 1, sizeof(char*));\r
-                       \r
-                       // add the pointer to this instance at the end of the list of the arguments of the process\r
-                       // so the static method Process::run() (passed as argument of the MSG function xbt_context_new())\r
-                       // can retrieve the concerned process object by the run\r
-                       // so Process::run() can call the method main() of the good process\r
-                       // for more detail see Process::run() method\r
-                       argv[argc] = (char*)this;\r
-                       \r
-                       // Simulator Data\r
-                       simdata->smx_host = nativeHost;\r
-                       simdata->mutex = NULL;\r
-                       simdata->cond = NULL;\r
-                       simdata->argc = argc;\r
-                       simdata->argv = argv;\r
-                       \r
-                       // create the context of the process.\r
-                       simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);\r
-                       \r
-                       // Process structure \r
-                       this->nativeProcess->name = xbt_strdup(name);\r
-                       this->nativeProcess->simdata = simdata;\r
-                       \r
-                       // Set process data\r
-                       this->nativeProcess->data = NULL;\r
-                       \r
-                       // Set process properties\r
-                       simdata->properties = NULL;\r
-                       \r
-                       xbt_swag_insert(this->nativeProcess, nativeHost->simdata->process_list);\r
-                       \r
-                       // fix current_process, about which xbt_context_start mocks around \r
-                       nativeCurrentProcess = simix_global->current_process;\r
-                       xbt_context_start(this->nativeProcess->simdata->context);\r
-                       simix_global->current_process = nativeCurrentProcess;\r
-                       \r
-                       xbt_swag_insert(this->nativeProcess, simix_global->process_list);\r
-                       DEBUG2("Inserting %s(%s) in the to_run list", this->nativeProcess->name, nativeHost->name);\r
-                       xbt_swag_insert(this->nativeProcess, simix_global->process_to_run);\r
-               }*/\r
 \r
                void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
                throw(InvalidArgumentException)\r
@@ -684,9 +633,7 @@ namespace SimGrid
                        \r
                        mailbox = MSG_mailbox_new(alias);\r
                        \r
-                       MSG_mailbox_set_hostname(mailbox, this->nativeProcess->simdata->m_host->simdata->smx_host->name);\r
-\r
-                       \r
+                       MSG_mailbox_set_hostname(mailbox, this->nativeProcess->simdata->m_host->simdata->smx_host->name);       \r
                }\r
                \r
                Process* Process::fromNativeProcess(m_process_t nativeProcess)\r
@@ -698,7 +645,7 @@ namespace SimGrid
                {\r
                        \r
                        // the last argument of the process is the pointer to the process to run\r
-                       // for mor detail see Process::create() method\r
+                       // for more detail see Process::create() method\r
                        return ((Process*)argv[argc])->main(argc, argv);\r
                }\r
 \r
@@ -706,7 +653,18 @@ namespace SimGrid
                {\r
                        throw LogicException("Process::main() not implemented");\r
                }\r
+\r
+               /*void* Process::operator new(size_t size)\r
+               {\r
+                       // TODO\r
+               }\r
+\r
+               void Process::operator delete(void* p)\r
+               {\r
+                       // TODO\r
+               }*/\r
                \r
        } // namespace Msg\r
 \r
-} // namespace SimGrid
\ No newline at end of file
+} // namespace SimGrid\r
+\r