X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1175edd91aa4b1e90a0cd822eda961ee21d20a54..8bed05033724fb48c90cdc76f30a4d5ebcc050a5:/src/cxx/Process.cxx diff --git a/src/cxx/Process.cxx b/src/cxx/Process.cxx index 0ee57edf9e..856ce5e842 100644 --- a/src/cxx/Process.cxx +++ b/src/cxx/Process.cxx @@ -1,11 +1,41 @@ +/* + * Process.cxx + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right 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. + * + */ + + /* Process member functions implementation. + */ + #include + +#include +#include +#include + +#include +#include + +#include +#include +#include + + + namespace SimGrid { namespace Msg { - Process* Process::currentProcess = NULL; - + + MSG_IMPLEMENT_DYNAMIC(Process, Object); + // Default constructor. Process::Process() { @@ -13,7 +43,7 @@ namespace SimGrid } Process::Process(const char* hostName, const char* name) - throw(InvalidArgumentException, HostNotFoundException) + throw(NullPointerException, HostNotFoundException, BadAllocException) { // check the parameters @@ -46,11 +76,8 @@ namespace SimGrid if(!name) throw NullPointerException("name"); - if(!hostName) - throw NullPointerException("hostName"); - if(argc < 0) - throw InvalidArgument("argc (must be positive)"); + throw InvalidArgumentException("argc (must be positive)"); if(!argc && argv) throw LogicException("argv is not NULL but argc is zero"); @@ -61,8 +88,8 @@ namespace SimGrid create(rHost, name, argc, argv); } - Process::Process(const char* hostname, const char* name, int argc, char** argv) - throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException) + Process::Process(const char* hostName, const char* name, int argc, char** argv) + throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException, BadAllocException) { // check the parameters @@ -73,7 +100,7 @@ namespace SimGrid throw NullPointerException("hostName"); if(argc < 0) - throw InvalidArgument("argc (must be positive)"); + throw InvalidArgumentException("argc (must be positive)"); if(!argc && argv) throw LogicException("argv is not NULL but argc is zero"); @@ -81,7 +108,7 @@ namespace SimGrid if(argc && !argv) throw LogicException("argv is NULL but argc is not zero"); - Host host = Host::getByName(hostname); + Host host = Host::getByName(hostName); create(host, name, argc, argv); } @@ -105,9 +132,9 @@ namespace SimGrid throw MsgException("MSG_process_resume() failed"); } - bool Process::isSuspended(void) + int Process::isSuspended(void) { - return (bool)MSG_process_is_suspended(nativeProcess); + return MSG_process_is_suspended(nativeProcess); } Host& Process::getHost(void) @@ -187,13 +214,13 @@ namespace SimGrid void Process::migrate(const Host& rHost) throw(MsgException) { - if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost)) + if(MSG_OK != MSG_process_change_host(rHost.nativeHost)) throw MsgException("MSG_process_change_host()"); } void Process::sleep(double seconds) - throw(throw(InvalidArgumentException, MsgException)) + throw(InvalidArgumentException, MsgException) { // check the parameters. if(seconds <= 0) @@ -204,7 +231,7 @@ namespace SimGrid } - void Process::putTask(const Host& rHost, int channel, const Task& rTask) + void Process::putTask(const Host& rHost, int channel, Task* task) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -212,11 +239,11 @@ namespace SimGrid if(channel < 0) throw InvalidArgumentException("channel (must not be negative)"); - if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0)) + if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, rHost.nativeHost, channel, -1.0)) throw MsgException("MSG_task_put_with_timeout()"); } - void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout) + void Process::putTask(const Host& rHost, int channel, Task* task, double timeout) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -226,11 +253,11 @@ namespace SimGrid if(timeout < 0 && timeout != -1.0) throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)"); - if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout)) + if(MSG_OK != MSG_task_put_with_timeout(task->nativeTask, rHost.nativeHost, channel, timeout)) throw MsgException("MSG_task_put_with_timeout() failed"); } - Task& Process::getTask(int channel) + Task* Process::getTask(int channel) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -243,10 +270,10 @@ namespace SimGrid if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL)) throw MsgException("MSG_task_get_ext() failed"); - return (*((Task*)(nativeTask->data))); + return (Task*)(nativeTask->data); } - Task& Process::getTask(int channel, double timeout) + Task* Process::getTask(int channel, double timeout) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -261,10 +288,10 @@ namespace SimGrid if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL)) throw MsgException("MSG_task_get_ext() failed"); - return (*((Task*)(nativeTask->data))); + return (Task*)(nativeTask->data); } - Task& Process::getTask(int channel, const Host& rHost) + Task* Process::getTask(int channel, const Host& rHost) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -276,10 +303,10 @@ namespace SimGrid if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost)) throw MsgException("MSG_task_get_ext() failed"); - return (*((Task*)(nativeTask->data))); + return (Task*)(nativeTask->data); } - Task& Process::getTask(int channel, double timeout, const Host& rHost) + Task* Process::getTask(int channel, double timeout, const Host& rHost) throw(InvalidArgumentException, MsgException) { // check the parameters @@ -294,10 +321,10 @@ namespace SimGrid if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost)) throw MsgException("MSG_task_get_ext() failed"); - return (*((Task*)(nativeTask->data))); + return (Task*)(nativeTask->data); } - void Process::sendTask(const char* alias, const Task& rTask, double timeout) + void Process::sendTask(const char* alias, Task* task, double timeout) throw(NullPointerException, InvalidArgumentException, MsgException) { // check the parameters @@ -308,12 +335,12 @@ namespace SimGrid if(timeout < 0 && timeout !=-1.0) throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout)) + if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias ,timeout)) throw MsgException("MSG_task_send_with_timeout()"); } - void Process::sendTask(const char* alias, const Task& rTask) + void Process::sendTask(const char* alias, Task* task) throw(NullPointerException, MsgException) { // check the parameters @@ -321,21 +348,21 @@ namespace SimGrid if(!alias) throw NullPointerException("alias"); - if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0)) + if(MSG_OK != MSG_task_send_with_timeout(task->nativeTask, alias ,-1.0)) throw MsgException("MSG_task_send_with_timeout()"); } - void Process::sendTask(const Task& rTask) + void Process::sendTask(Task* task) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc( strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name); - MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0); + MSG_error_t rv = MSG_task_send_with_timeout(task->nativeTask, alias ,-1.0); free(alias); @@ -343,7 +370,7 @@ namespace SimGrid throw MsgException("MSG_task_send_with_timeout()"); } - void Process::sendTask(const Task& rTask, double timeout) + void Process::sendTask(Task* task, double timeout) throw(BadAllocException, InvalidArgumentException, MsgException) { // check the parameters @@ -351,14 +378,14 @@ namespace SimGrid if(timeout < 0 && timeout !=-1.0) throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2 , sizeof(char)); if(!alias) throw BadAllocException("alias"); sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name); - MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout); + MSG_error_t rv = MSG_task_send_with_timeout(task->nativeTask, alias ,timeout); free(alias); @@ -366,7 +393,7 @@ namespace SimGrid throw MsgException("MSG_task_send_with_timeout()"); } - Task& Process::receiveTask(const char* alias) + Task* Process::receiveTask(const char* alias) throw(NullPointerException, MsgException) { // check the parameters @@ -379,15 +406,15 @@ namespace SimGrid if (MSG_OK != MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL)) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(void) + Task* Process::receiveTask(void) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -403,11 +430,11 @@ namespace SimGrid if(MSG_OK != rv) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(const char* alias, double timeout) + Task* Process::receiveTask(const char* alias, double timeout) throw(NullPointerException, InvalidArgumentException, MsgException) { // check the parameters @@ -423,11 +450,11 @@ namespace SimGrid if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(double timeout) + Task* Process::receiveTask(double timeout) throw(InvalidArgumentException, BadAllocException, MsgException) { // check the parameters @@ -436,7 +463,7 @@ namespace SimGrid throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -452,11 +479,11 @@ namespace SimGrid if(MSG_OK != rv) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost) + Task* Process::receiveTask(const char* alias, double timeout, const Host& rHost) throw(NullPointerException, InvalidArgumentException, MsgException) { // check the parameters @@ -472,11 +499,11 @@ namespace SimGrid if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(double timeout, const Host& rHost) + Task* Process::receiveTask(double timeout, const Host& rHost) throw(BadAllocException, InvalidArgumentException, MsgException) { // check the parameters @@ -484,7 +511,7 @@ namespace SimGrid if(timeout < 0 && timeout !=-1.0) throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)"); - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -500,11 +527,11 @@ namespace SimGrid if(MSG_OK != rv) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(const char* alias, const Host& rHost) + Task* Process::receiveTask(const char* alias, const Host& rHost) throw(NullPointerException, MsgException) { @@ -518,13 +545,13 @@ namespace SimGrid if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - Task& Process::receiveTask(const Host& rHost) + Task* Process::receiveTask(const Host& rHost) throw(BadAllocException, MsgException) { - char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2); + char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char)); if(!alias) throw BadAllocException("alias"); @@ -540,28 +567,34 @@ namespace SimGrid if(MSG_OK != rv) throw MsgException("MSG_task_receive_ext() failed"); - return (*((Task*)nativeTask->data)); + return (Task*)(nativeTask->data); } - + void Process::create(const Host& rHost, const char* name, int argc, char** argv) - throw(HostNotFoundException) + throw(InvalidArgumentException) { - smx_process_t nativeCurrentProcess = NULL; + char alias[MAX_ALIAS_NAME + 1] = {0}; + msg_mailbox_t mailbox; - // allocate the native process - this->nativeProcess = xbt_new0(s_smx_process_t, 1); - - // allocate the simulation data of the native process - smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1); // try to retrieve the host where to createt the process from its name - smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName()); + m_host_t nativeHost = rHost.nativeHost; if(!nativeHost) - throw HostNotFoundException(rHost.getName()); + throw InvalidArgumentException("rHost"); + + /* allocate the data of the simulation */ + this->nativeProcess = xbt_new0(s_m_process_t,1); + this->nativeProcess->simdata = xbt_new0(s_simdata_process_t,1); + this->nativeProcess->name = _strdup(name); + this->nativeProcess->simdata->m_host = nativeHost; + this->nativeProcess->simdata->PID = msg_global->PID++; // realloc the list of the argument to add the pointer to this process instance at the end - argv = (char**)realloc(argc + 1, sizeo(char*)); + if(argc) + argv = (char**)realloc(argv , (argc + 1) * sizeof(char*)); + else + argv = (char**)calloc(1 ,sizeof(char*)); // add the pointer to this instance at the end of the list of the arguments of the process // so the static method Process::run() (passed as argument of the MSG function xbt_context_new()) @@ -569,52 +602,69 @@ namespace SimGrid // so Process::run() can call the method main() of the good process // for more detail see Process::run() method argv[argc] = (char*)this; + + this->nativeProcess->simdata->argc = argc; + this->nativeProcess->simdata->argv = argv; - // Simulator Data - simdata->smx_host = nativeHost; - simdata->mutex = NULL; - simdata->cond = NULL; - simdata->argc = argc; - simdata->argv = argv; - - // create the context of the process. - simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv); + this->nativeProcess->simdata->s_process = SIMIX_process_create( + this->nativeProcess->name, + Process::run, + (void*)this->nativeProcess, + nativeHost->name, + argc, + argv, + NULL); - /* Process structure */ - this->nativeProcess->name = xbt_strdup(name); - this->nativeProcess->simdata = simdata; + if (SIMIX_process_self()) + {/* someone created me */ + this->nativeProcess->simdata->PPID = MSG_process_get_PID((m_process_t)SIMIX_process_self()->data); + } + else + { + this->nativeProcess->simdata->PPID = -1; + } - // Set process data - this->nativeProcess->data = NULL; + this->nativeProcess->simdata->last_errno = MSG_OK; - // Set process properties - simdata->properties = NULL; + /* add the process to the list of the processes of the simulation */ + xbt_fifo_unshift(msg_global->process_list, this->nativeProcess); - xbt_swag_insert(this->nativeProcess, nativeHost->simdata->process_list); + sprintf(alias,"%s:%s",(this->nativeProcess->simdata->m_host->simdata->smx_host)->name,this->nativeProcess->name); - /* fix current_process, about which xbt_context_start mocks around */ - nativeCurrentProcess = simix_global->current_process; - xbt_context_start(this->nativeProcess->simdata->context); - simix_global->current_process = nativeCurrentProcess; + mailbox = MSG_mailbox_new(alias); - xbt_swag_insert(this->nativeProcess, simix_global->process_list); - DEBUG2("Inserting %s(%s) in the to_run list", this->nativeProcess->name, nativeHost->name); - xbt_swag_insert(this->nativeProcess, simix_global->process_to_run); + MSG_mailbox_set_hostname(mailbox, this->nativeProcess->simdata->m_host->simdata->smx_host->name); } - Process& Process::fromNativeProcess(m_process_t nativeProcess) + Process* Process::fromNativeProcess(m_process_t nativeProcess) { - return (*((Process*)(nativeProcess->simdata->arg[nativeProcess->argc]))); + return ((Process*)(nativeProcess->simdata->argv[nativeProcess->simdata->argc])); } int Process::run(int argc, char** argv) { // the last argument of the process is the pointer to the process to run - // for mor detail see Process::create() method + // for more detail see Process::create() method return ((Process*)argv[argc])->main(argc, argv); } + + int Process::main(int argc, char** argv) + { + throw LogicException("Process::main() not implemented"); + } + + /*void* Process::operator new(size_t size) + { + // TODO + } + + void Process::operator delete(void* p) + { + // TODO + }*/ } // namespace Msg -} // namespace SimGrid \ No newline at end of file +} // namespace SimGrid +