void S_create_push_threads(pTHX, HV* options, SV* startup) { struct thread_create thread_options; int clone_number; int counter; Zero(&thread_options, 1, struct thread_create); clone_number = prepare_thread_create(aTHX, &thread_options, options, startup); for (counter = 0; counter < clone_number; ++counter) { mthread* thread; const message* to_run; const message* modules; thread = mthread_alloc(aTHX); if (thread_options.monitor) thread_add_listener(aTHX, thread->id, thread_options.parent_id); modules = message_clone(thread_options.modules); queue_enqueue(thread->queue, modules, NULL); push_thread(aTHX, thread); to_run = message_clone(thread_options.to_run); queue_enqueue(thread->queue, to_run, NULL); start_thread(thread, thread_options.stack_size); } destroy_message(thread_options.to_run); if (thread_options.modules) destroy_message(thread_options.modules); }
int opal_show_help(const char *filename, const char *topic, bool want_error_header, ...) { int ret; va_list arglist; char **array = NULL; if (OPAL_SUCCESS != (ret = open_file(filename, topic))) { return ret; } if (OPAL_SUCCESS != (ret = find_topic(filename, topic))) { fclose(opal_show_help_yyin); return ret; } ret = read_message(&array); opal_show_help_finish_parsing(); fclose(opal_show_help_yyin); if (OPAL_SUCCESS != ret) { destroy_message(array); return ret; } va_start(arglist, want_error_header); output(want_error_header, array, filename, topic, arglist); va_end(arglist); destroy_message(array); return ret; }
/** * AppMessage Callbacks */ void message_handler_outbox_sent_handler(DictionaryIterator *iterator, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "message_handler_outbox_sent_handler"); // Should the message queue be empty, just return if (message_queue == NULL) { return; } message_t *message = message_queue; // Set that we are done sending a message messages_sending = false; // Set the next head message message_queue = message->next; // Call the message success_callback if (message->success_callback != NULL) { message->success_callback(); } // Destroy the message destroy_message(message); // Continue onto the next message send_next_message(); }
void obtener_siguiente_hilo(void) { t_msg *msg = id_message(CPU_TCB); if(enviar_mensaje(Kernel,msg) == -1) { puts("ERROR: No se pudo obtener el siguiente hilo a ejecutar."); exit(EXIT_FAILURE); } destroy_message(msg); if((msg = recibir_mensaje(Kernel)) == NULL || msg->header.id != NEXT_TCB) { puts("ERROR: No se pudo obtener el siguiente hilo a ejecutar."); exit(EXIT_FAILURE); } Quantum = msg->argv[0]; Execution_State = RETURN_TCB; memcpy(&Hilo,msg->stream,sizeof(t_hilo)); destroy_message(msg); }
void inicializar_CPU(char *path, uint32_t *retardo) { t_config* config = config_create(path); *retardo = config_get_int_value(config,"RETARDO"); uint16_t puertoMSP = config_get_int_value(config,"PUERTO_MSP"); char *direccionIpMSP = config_get_string_value(config,"IP_MSP"); uint16_t puertoKernel = config_get_int_value(config,"PUERTO_KERNEL"); char *direccionIpKernel = config_get_string_value(config,"IP_KERNEL"); if((Kernel = client_socket(direccionIpKernel, puertoKernel)) < 0) { puts("ERROR: No se pudo conectar al Kernel."); exit(EXIT_FAILURE); } t_msg *handshake = id_message(CPU_CONNECT); enviar_mensaje(Kernel, handshake); destroy_message(handshake); if((MSP = client_socket(direccionIpMSP, puertoMSP)) < 0) { puts("ERROR: No se pudo conectar a la MSP."); exit(EXIT_FAILURE); } config_destroy(config); MapRegistros['A'-'A'] = &Registros.registros_programacion[0]; MapRegistros['B'-'A'] = &Registros.registros_programacion[1]; MapRegistros['C'-'A'] = &Registros.registros_programacion[2]; MapRegistros['D'-'A'] = &Registros.registros_programacion[3]; MapRegistros['E'-'A'] = &Registros.registros_programacion[4]; MapRegistros['K'-'A'] = &Registros.K; MapRegistros['I'-'A'] = &Registros.I; MapRegistros['M'-'A'] = &Registros.M; MapRegistros['P'-'A'] = &Registros.P; MapRegistros['S'-'A'] = &Registros.S; MapRegistros['X'-'A'] = &Registros.X; inicializar_tabla_instrucciones(); }
static void S_node_destroy(pTHX_ message** current) { while (*current != NULL) { message** next = &(*current)->next; destroy_message(*current); *current = NULL; current = next; } }
void devolver_hilo() { t_msg *msg = Kernel_Msg == NULL ? tcb_message(Execution_State, &Hilo, 0) : Kernel_Msg; if(enviar_mensaje(Kernel, msg) == -1) { puts("ERROR: No se pudo enviar el TCB del hilo en ejecución al Kernel."); exit(EXIT_FAILURE); } destroy_message(msg); Kernel_Msg = NULL; }
void destruir_segmento(uint32_t baseSegmento) { t_msg *msg = argv_message(DESTROY_SEGMENT,2,PID,baseSegmento); if(enviar_mensaje(MSP,msg) == -1) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } destroy_message(msg); if((msg = recibir_mensaje(MSP)) == NULL) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } if(msg->header.id != OK_DESTROY) { Quantum = 0; KernelMode = false; Execution_State = CPU_ABORT; } destroy_message(msg); }
void escribir_memoria(uint32_t pid,uint32_t direccionLogica,char *bytesAEscribir,uint32_t size) { t_msg *msg = string_message(WRITE_MEMORY,bytesAEscribir,2,pid,direccionLogica); msg->header.length = size; if(enviar_mensaje(MSP,msg) == -1) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } destroy_message(msg); free(bytesAEscribir); if((msg = recibir_mensaje(MSP)) == NULL) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } if(msg->header.id != OK_WRITE) { Quantum = 0; KernelMode = false; Execution_State = CPU_ABORT; } destroy_message(msg); }
/** * Destroy all messages in queue */ void destroy_messages(void) { // APP_LOG(APP_LOG_LEVEL_DEBUG, "destroy_messages"); message_t *message; while(message_queue != NULL) { message = message_queue; message_queue = message_queue->next; destroy_message(message); } }
uint32_t crear_segmento(uint32_t size) { uint32_t aux; t_msg *msg = argv_message(CREATE_SEGMENT,2,PID,size); if(enviar_mensaje(MSP,msg) == -1) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } destroy_message(msg); if((msg = recibir_mensaje(MSP)) == NULL) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } if(msg->header.id == OK_CREATE) aux = (uint32_t) msg->argv[0]; else { Quantum = 0; KernelMode = false; Execution_State = CPU_ABORT; } destroy_message(msg); return aux; }
void *loader(void *arg) { while (1) { if(sem_wait(&sem_loader) == -1) { perror("loader"); exit(EXIT_FAILURE); } if(!thread_alive) return NULL; pthread_mutex_lock(&loader_mutex); t_msg *recibido = queue_pop(loader_queue); pthread_mutex_unlock(&loader_mutex); t_console *console = new_console(recibido->argv[0]); pthread_mutex_lock(&console_list_mutex); list_add(console_list, console); pthread_mutex_unlock(&console_list_mutex); conexion_consola(console->pid); log_trace(logger_old, "[NEW_CONNECTION @ LOADER]: (CONSOLE_ID %u, CONSOLE_SOCK %u).", console->pid, console->sock_fd); t_hilo *new_tcb = ult_tcb(console->pid); int status = reservar_memoria(new_tcb, recibido); if (status == -1) { log_warning(logger_old, "No se pudo cargar a memoria el hilo principal de la Consola %u.", console->pid); new_tcb->cola = EXIT; list_add(process_list, new_tcb); t_msg *msg = string_message(KILL_CONSOLE, "Finalizando consola. Motivo: no hay espacio suficiente en MSP.", 0); if(enviar_mensaje(console->sock_fd, msg) == -1) { log_warning(logger_old, "Se perdio la conexion con la consola %u.", console->pid); remove_console_by_sock_fd(console->sock_fd); } destroy_message(msg); } else { pthread_mutex_lock(&process_list_mutex); list_add(process_list, new_tcb); pthread_mutex_unlock(&process_list_mutex); log_trace(logger_old, "[LOADER]: (PID %u, TID %u) => NEW.", new_tcb->pid, new_tcb->tid); sem_post(&sem_planificador); } } return NULL; }
char* solicitar_memoria(uint32_t pid,uint32_t direccionLogica,uint32_t size) { char *buffer = malloc(size); t_msg *msg = argv_message(REQUEST_MEMORY,3,pid,direccionLogica,size); if(enviar_mensaje(MSP,msg) == -1) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } destroy_message(msg); if((msg = recibir_mensaje(MSP)) == NULL) { puts("ERROR: Se ha perdido la conexión con la MSP."); exit(EXIT_FAILURE); } if(msg->header.id == OK_REQUEST) memcpy(buffer,msg->stream,size); else { free(buffer); buffer = NULL; Quantum = 0; KernelMode = false; Execution_State = CPU_ABORT; } destroy_message(msg); return buffer; }
int main(void) { t_msg *msg[100]; int sock_fd = client_socket("127.0.0.1", 1122); if(sock_fd < 0) { puts("No se pudo conectar con el Kernel."); return 1; } int i; for(i = 0; i < 100; i++) msg[i] = beso_message(KILL_CONSOLE, "../file.bin", 0); for(i = 0; i < 100; i++) { int j = enviar_mensaje(sock_fd, msg[i]); printf("Enviado mensaje nro. %d, length %d.\n", i + 1, j); destroy_message(msg[i]); } return 0; }
void handshakeMarta() { char* str_mensaje = string_new(); int ret; string_append(&str_mensaje, configuracion->resultado); string_append(&str_mensaje, "|"); string_append(&str_mensaje, configuracion->archivos); t_msg* mensaje = string_message(CONEXION_JOB, str_mensaje, 1, configuracion->combiner); log_debug_interno("Enviando mensaje de solicitud de inicio de Job a MaRTA. Header.ID: %s - Archivos: %s - Resultado: %s", id_string(CONEXION_JOB), configuracion->archivos, configuracion->resultado); ret = enviar_mensaje(marta_sock, mensaje); if (ret < 0) { log_error_consola("Fallo el envio de mensaje de solicitud de inicio de Job"); exit(1); } destroy_message(mensaje); }
void ejecuta_maps(t_job* job) { void _ejecuta_map(t_map* map) { if (map->estado == PENDIENTE || map->estado == FIN_ERROR) { char* stream = string_duplicate(map->arch_tmp.nodo.ip); string_append(&stream, "|"); string_append(&stream, map->arch_tmp.nodo.nombre); string_append(&stream, "|"); string_append(&stream, string_duplicate(map->arch_tmp.nombre)); t_msg* message = string_message(EJECUTAR_MAP, stream, 4, map->arch_tmp.nodo.puerto, map->id, job->id, map->arch_tmp.nodo.numero_bloque); map->estado = EN_EJECUCION; enviar_mensaje(job->socket, message); log_info_interno("Enviando MAP. job: %d, id_operacion: %d, ip_nodo: %s,puerto_nodo: %d, bloque: %d,nombre_temp: %s", job->id, map->id, map->arch_tmp.nodo.ip, map->arch_tmp.nodo.puerto, map->arch_tmp.nodo.numero_bloque, map->arch_tmp.nombre); destroy_message(message); } } log_debug_consola("creando map threads. Job: %d", job->id); list_iterate(job->maps, (void*) _ejecuta_map); log_debug_consola("map threads creados. Job: %d", job->id); }
int hiloMap(void* dato) { //sumar_hilo(); t_msg* mensaje; t_msg* mensaje_respuesta; t_params_hiloMap* args = (t_params_hiloMap*) dato; int nodo_sock = client_socket(args->ip, args->puerto); int res = 0; if (nodo_sock < 0) { log_error_consola("No se pudo conectar al proceso %s - IP: %s - Puerto: %d", args->nombre_nodo, args->ip, args->puerto); mensaje_respuesta = argv_message(FIN_MAP_ERROR, 2, args->id_operacion, args->id_job); } else { mensaje = string_message(EJECUTAR_MAP, args->archivo_final, 2, args->id_operacion, args->bloque); log_debug_interno("Enviando mensaje de solicitud de reduce. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje EJECUTAR_MAP"); shutdown(nodo_sock, 2); } mensaje = rutina_message(RUTINA, configuracion->mapper, configuracion->tamanio_mapper); log_debug_interno("Enviando mensaje de rutina. Header.ID: %s - Argc: %d - Largo Stream: %d Al nodo: %s", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length, args->nombre_nodo); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje RUTINA"); shutdown(nodo_sock, 2); } log_info_consola("Se envio SOLICITUD DE MAP correctamente. id_op: %d, nodo: %s", args->id_operacion, args->nombre_nodo); mensaje = recibir_mensaje_sin_mutex(nodo_sock); if (!mensaje) { //Significa que recibir_mensaje devolvio NULL o sea que hubo un error en el recv o el nodo se desconecto log_info_consola("El MAP id_op: %d en el nodo: %s fallo, enviando a MaRTA", args->id_operacion, args->nombre_nodo); mensaje_respuesta = argv_message(FIN_MAP_ERROR, 2, args->id_operacion, args->id_job); } else { log_info_consola("El MAP id_op: %d, en el nodo: %s finalizo bien. enviando a MaRTA", args->id_operacion, args->nombre_nodo); mensaje_respuesta = argv_message(mensaje->header.id, 2, args->id_operacion, args->id_job); log_debug_interno("Se recibio mensaje de %s. Header.Id: %s - Argc: %d - Largo Stream: %d", args->nombre_nodo, id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); destroy_message(mensaje); } } //Se envia el resultado del map a marta log_debug_interno("Enviando mensaje respuesta a MaRTA. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje_respuesta->header.id), mensaje_respuesta->header.argc, mensaje_respuesta->header.length); res = enviar_mensaje(marta_sock, mensaje_respuesta); if (res == -1) { log_error_consola("Fallo envio de Mensaje a MaRTA."); shutdown(nodo_sock, 2); return res; } log_info_consola("Respuesta MAP enviada a MaRTA. id_op: %d, nodo: %s", args->id_operacion, args->nombre_nodo); destroy_message(mensaje_respuesta); //CERRAR CONEXIoN CON EL NODO// shutdown(nodo_sock, 2); //restar_hilo(); return res; }
int hiloReduce(void* dato) { //sumar_hilo(); t_msg* mensaje; t_msg* mensaje_respuesta; int res = 0; int i; t_params_hiloReduce* args = (t_params_hiloReduce*) dato; int nodo_sock = client_socket(args->ip, args->puerto); if (nodo_sock < 0) { log_error_consola("No se pudo conectar al proceso %s - IP: %s - Puerto: %d", args->nombre_nodo, args->ip, args->puerto); mensaje_respuesta = argv_message(FIN_REDUCE_ERROR, 1, args->id_operacion); } else { log_debug_consola("Se conecto al proceso %s - IP: %s - Puerto: %d", args->nombre_nodo, args->ip, args->puerto); mensaje = string_message(EJECUTAR_REDUCE, args->archivo_final, 1, args->id_operacion); log_debug_interno("Enviando mensaje de solicitud de reduce. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje EJECUTAR_REDUCE"); shutdown(nodo_sock, 2); return res; } mensaje = rutina_message(RUTINA, configuracion->reduce, configuracion->tamanio_reduce); log_debug_interno("Enviando mensaje rutina. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje RUTINA"); shutdown(nodo_sock, 2); return res; } int cant_elementos = queue_size(args->archivos_tmp); for (i = 0; i < cant_elementos; i++) { mensaje = queue_pop(args->archivos_tmp); log_debug_interno("Enviando mensaje archivos de reduce. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje ARCHIVOS_NODOS_REDUCE"); shutdown(nodo_sock, 2); return res; } } mensaje = id_message(FIN_ENVIO_MENSAJE); log_debug_interno("Enviando mensaje fin de Mensaje. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); res = enviar_mensaje(nodo_sock, mensaje); if (res == -1) { log_error_consola("Fallo envio mensaje FIN_ENVIO_MENSAJE"); shutdown(nodo_sock, 2); return res; } log_info_consola("Envio REDUCE a Job OK. id_op:%d , nodo: %s ", args->id_operacion, args->nombre_nodo); mensaje = recibir_mensaje_sin_mutex(nodo_sock); if (!mensaje) { //Significa que recibir_mensaje devolvio NULL o sea que hubo un error en el recv o el nodo se desconecto log_info_consola("Respuesta REDUCE Error. id_op: %d, nodo: %s, enviando mensaje a MaRTA", args->id_operacion, args->nombre_nodo); mensaje_respuesta = argv_message(FIN_REDUCE_ERROR, 2, args->id_operacion, args->id_job); } else { log_info_consola("Respuesta REDUCE OK. id_op: %d, nodo: %s, enviando mensaje a MaRTA", args->id_operacion, args->nombre_nodo); mensaje_respuesta = argv_message(mensaje->header.id, 2, args->id_operacion, args->id_job); log_debug_interno("Se recibio mensaje de %s. Header.Id: %s - Argc: %d - Largo Stream: %d", args->nombre_nodo, id_string(mensaje->header.id), mensaje->header.argc, mensaje->header.length); } } //Se reenvia el resultado del reduce a marta log_debug_interno("Enviando mensaje respuesta a MaRTA. Header.ID: %s - Argc: %d - Largo Stream: %d", id_string(mensaje_respuesta->header.id), mensaje_respuesta->header.argc, mensaje_respuesta->header.length); res = enviar_mensaje(marta_sock, mensaje_respuesta); if (res == -1) { log_error_consola("fallo mensaje respuesta a MaRTA"); shutdown(nodo_sock, 2); return res; } log_info_consola("Respuesta REDUCE enviada. id_op: %d, nodo: %s", args->id_operacion, args->nombre_nodo); destroy_message(mensaje_respuesta); shutdown(nodo_sock, 2); //restar_hilo(); return res; }
static void* run_thread(void* arg) { mthread* thread = (mthread*) arg; PerlInterpreter* my_perl = construct_perl(); const message *to_run, *modules, *message; SV *call, *status; perl_mutex* shutdown_mutex; thread->interp = my_perl; #ifndef WIN32 S_set_sigmask(&thread->initial_sigmask); #endif PERL_SET_CONTEXT(my_perl); store_self(my_perl, thread); { dSP; modules = queue_dequeue(thread->queue, NULL); load_modules(my_perl, modules); to_run = queue_dequeue(thread->queue, NULL); ENTER; SAVETMPS; call = SvRV(message_load_value(to_run)); PUSHMARK(SP); mXPUSHs(newSVpvn("exit", 4)); status = newSVpvn("normal", 6); mXPUSHs(status); mXPUSHs(newSViv(thread->id)); ENTER; PUSHMARK(SP); PUTBACK; call_sv(call, G_SCALAR|G_EVAL); SPAGAIN; if (SvTRUE(ERRSV)) { sv_setpvn(status, "error", 5); warn("Thread %"UVuf" got error %s\n", thread->id, SvPV_nolen(ERRSV)); PUSHs(ERRSV); } message_from_stack_pushed(message); LEAVE; send_listeners(thread, message); destroy_message(message); FREETMPS; LEAVE; } shutdown_mutex = get_shutdown_mutex(); MUTEX_LOCK(shutdown_mutex); perl_destruct(my_perl); MUTEX_UNLOCK(shutdown_mutex); mthread_destroy(thread); PerlMemShared_free(thread); perl_free(my_perl); return NULL; }
/** * Try to send the next message in queue */ static void send_next_message(void) { // APP_LOG(APP_LOG_LEVEL_DEBUG, "send_next_message"); // Grab the first message in queue message_t *message = message_queue; // See if we are already in progress of sending a message if (messages_sending) { return; } // See if the queue is empty else if (message == NULL) { return; } // See if the current message has anymore retries else if (message->retries <= 0) { // Call the failure_callback if (message->failure_callback != NULL) { message->failure_callback(); } // Set the next head message message_queue = message->next; // Destroy the message destroy_message(message); // Continue onto the next message send_next_message(); return; } /** * Send the message! */ DictionaryIterator *iter; app_message_outbox_begin(&iter); APP_LOG(APP_LOG_LEVEL_DEBUG, "Sending message to phone:"); // Add our dicts uint8_t i; for(i=0;i<message->dicts_length;i++) { if (message->dicts[i]->type == CSTRING) { dict_write_cstring(iter, message->dicts[i]->key, message->dicts[i]->value.cstring); APP_LOG(APP_LOG_LEVEL_DEBUG, "key: %d cstring: %s", message->dicts[i]->key, message->dicts[i]->value.cstring); } else if (message->dicts[i]->type == UINT8) { dict_write_uint8(iter, message->dicts[i]->key, message->dicts[i]->value.uint8); APP_LOG(APP_LOG_LEVEL_DEBUG, "key: %d uint8: %d", message->dicts[i]->key, message->dicts[i]->value.uint8); } } // Fire away! app_message_outbox_send(); // Set that we are sending a message messages_sending = true; // Decrement the retries counter message->retries--; }