+/*\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 <Process.hpp>\r
\r
+\r
+#include <ApplicationHandler.hpp>\r
+#include <Host.hpp>\r
+#include <Task.hpp>\r
+\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+\r
+#include <msg/msg.h>\r
+#include <msg/private.h>\r
+#include <msg/mailbox.h>\r
+\r
+\r
+\r
namespace SimGrid\r
{\r
namespace Msg\r
{\r
- Process* Process::currentProcess = NULL;\r
- \r
+\r
+ MSG_IMPLEMENT_DYNAMIC(Process, Object);\r
+\r
// Default constructor.\r
Process::Process()\r
{\r
}\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
if(!name)\r
throw NullPointerException("name");\r
\r
- if(!hostName)\r
- throw NullPointerException("hostName");\r
- \r
if(argc < 0)\r
- throw InvalidArgument("argc (must be positive)");\r
+ throw InvalidArgumentException("argc (must be positive)");\r
\r
if(!argc && argv)\r
throw LogicException("argv is not NULL but argc is zero");\r
create(rHost, name, argc, argv); \r
}\r
\r
- Process::Process(const char* hostname, const char* name, int argc, char** argv)\r
- throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException)\r
+ Process::Process(const char* hostName, const char* name, int argc, char** argv)\r
+ throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException, BadAllocException)\r
{\r
// check the parameters\r
\r
throw NullPointerException("hostName");\r
\r
if(argc < 0)\r
- throw InvalidArgument("argc (must be positive)");\r
+ throw InvalidArgumentException("argc (must be positive)");\r
\r
if(!argc && argv)\r
throw LogicException("argv is not NULL but argc is zero");\r
if(argc && !argv)\r
throw LogicException("argv is NULL but argc is not zero");\r
\r
- Host host = Host::getByName(hostname);\r
+ Host host = Host::getByName(hostName);\r
\r
create(host, name, argc, argv); \r
}\r
throw MsgException("MSG_process_resume() failed");\r
}\r
\r
- bool Process::isSuspended(void)\r
+ int Process::isSuspended(void)\r
{\r
- return (bool)MSG_process_is_suspended(nativeProcess);\r
+ return MSG_process_is_suspended(nativeProcess);\r
} \r
\r
Host& Process::getHost(void) \r
void Process::migrate(const Host& rHost)\r
throw(MsgException)\r
{\r
- if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost))\r
+ if(MSG_OK != MSG_process_change_host(rHost.nativeHost))\r
throw MsgException("MSG_process_change_host()");\r
\r
}\r
\r
void Process::sleep(double seconds)\r
- throw(throw(InvalidArgumentException, MsgException))\r
+ throw(InvalidArgumentException, MsgException)\r
{\r
// check the parameters.\r
if(seconds <= 0)\r
\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
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
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
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
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
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
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
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
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);\r
+ char* alias = (char*)calloc( strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias");\r
\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
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
if(timeout < 0 && timeout !=-1.0)\r
throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
\r
- char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2 , sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias");\r
\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
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
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
- char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias"); \r
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
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
throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
\r
\r
- char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias");\r
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
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
if(timeout < 0 && timeout !=-1.0)\r
throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
\r
- char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias");\r
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
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);\r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
\r
if(!alias)\r
throw BadAllocException("alias");\r
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
void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
- throw(HostNotFoundException)\r
+ throw(InvalidArgumentException)\r
{\r
- smx_process_t nativeCurrentProcess = NULL;\r
- nativeProcess = xbt_new0(s_smx_process_t, 1);\r
- smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);\r
- smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());\r
+ char alias[MAX_ALIAS_NAME + 1] = {0};\r
+ msg_mailbox_t mailbox;\r
+ \r
+ \r
+ // try to retrieve the host where to createt the process from its name\r
+ m_host_t nativeHost = rHost.nativeHost;\r
\r
- throw HostNotFoundException(rHost.getName());\r
+ if(!nativeHost)\r
+ throw InvalidArgumentException("rHost"); \r
\r
- argv = (char**)realloc(argc + 1, sizeo(char*));\r
+ /* allocate the data of the simulation */\r
+ this->nativeProcess = xbt_new0(s_m_process_t,1);\r
+ this->nativeProcess->simdata = xbt_new0(s_simdata_process_t,1);\r
+ this->nativeProcess->name = _strdup(name);\r
+ this->nativeProcess->simdata->m_host = nativeHost;\r
+ this->nativeProcess->simdata->PID = msg_global->PID++;\r
\r
+ // realloc the list of the argument to add the pointer to this process instance at the end\r
+ if(argc)\r
+ argv = (char**)realloc(argv , (argc + 1) * sizeof(char*));\r
+ else\r
+ argv = (char**)calloc(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
+ this->nativeProcess->simdata->argc = argc;\r
+ this->nativeProcess->simdata->argv = argv;\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
- simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);\r
+ this->nativeProcess->simdata->s_process = SIMIX_process_create(\r
+ this->nativeProcess->name,\r
+ Process::run, \r
+ (void*)this->nativeProcess,\r
+ nativeHost->name, \r
+ argc,\r
+ argv, \r
+ NULL);\r
\r
- /* Process structure */\r
- nativeProcess->name = xbt_strdup(name);\r
- nativeProcess->simdata = simdata;\r
+ if (SIMIX_process_self()) \r
+ {/* someone created me */\r
+ this->nativeProcess->simdata->PPID = MSG_process_get_PID((m_process_t)SIMIX_process_self()->data);\r
+ } \r
+ else \r
+ {\r
+ this->nativeProcess->simdata->PPID = -1;\r
+ }\r
\r
- // Set process data\r
- nativeProcess->data = NULL;\r
+ this->nativeProcess->simdata->last_errno = MSG_OK;\r
\r
- // Set process properties\r
- simdata->properties = NULL;\r
+ /* add the process to the list of the processes of the simulation */\r
+ xbt_fifo_unshift(msg_global->process_list, this->nativeProcess);\r
\r
- xbt_swag_insert(nativeProcess, nativeHost->simdata->process_list);\r
+ sprintf(alias,"%s:%s",(this->nativeProcess->simdata->m_host->simdata->smx_host)->name,this->nativeProcess->name);\r
\r
- /* fix current_process, about which xbt_context_start mocks around */\r
- nativeCurrentProcess = simix_global->current_process;\r
- xbt_context_start(nativeProcess->simdata->context);\r
- simix_global->current_process = nativeCurrentProcess;\r
+ mailbox = MSG_mailbox_new(alias);\r
\r
- xbt_swag_insert(nativeProcess, simix_global->process_list);\r
- DEBUG2("Inserting %s(%s) in the to_run list", nativeProcess->name, nativeHost->name);\r
- xbt_swag_insert(nativeProcess, simix_global->process_to_run);\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
+ Process* Process::fromNativeProcess(m_process_t nativeProcess)\r
{\r
- return (*((Process*)(nativeProcess->simdata->arg[nativeProcess->argc])));\r
+ return ((Process*)(nativeProcess->simdata->argv[nativeProcess->simdata->argc]));\r
}\r
\r
int Process::run(int argc, char** argv)\r
{\r
- Process* process =(Process*)argv[argc];\r
\r
- return process->main(argc, argv);\r
+ // the last argument of the process is the pointer to the process to run\r
+ // for more detail see Process::create() method\r
+ return ((Process*)argv[argc])->main(argc, argv);\r
}\r
+\r
+ int Process::main(int argc, char** argv)\r
+ {\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