int MPID_nem_tcp_init (MPIDI_PG_t *pg_p, int pg_rank, char **bc_val_p, int *val_max_sz_p) { int mpi_errno = MPI_SUCCESS; MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_TCP_INIT); MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_TCP_INIT); MPID_nem_net_module_vc_dbg_print_sendq = MPID_nem_tcp_vc_dbg_print_sendq; /* first make sure that our private fields in the vc fit into the area provided */ MPIU_Assert(sizeof(MPID_nem_tcp_vc_area) <= MPIDI_NEM_VC_NETMOD_AREA_LEN); /* set up listener socket */ mpi_errno = set_up_listener(); if (mpi_errno) MPIR_ERR_POP(mpi_errno); /* create business card */ mpi_errno = MPID_nem_tcp_get_business_card(pg_rank, bc_val_p, val_max_sz_p); if (mpi_errno) MPIR_ERR_POP(mpi_errno); mpi_errno = MPID_nem_tcp_sm_init(); if (mpi_errno) MPIR_ERR_POP(mpi_errno); mpi_errno = MPID_nem_tcp_send_init(); if (mpi_errno) MPIR_ERR_POP(mpi_errno); #ifdef HAVE_SIGNAL { /* In order to be able to handle socket errors on our own, we need to ignore SIGPIPE. This may cause problems for programs that intend to handle SIGPIPE or count on being killed, but I expect such programs are very rare, and I'm not sure what the best solution would be anyway. */ void *ret; ret = signal(SIGPIPE, SIG_IGN); MPIR_ERR_CHKANDJUMP1(ret == SIG_ERR, mpi_errno, MPI_ERR_OTHER, "**signal", "**signal %s", MPIU_Strerror(errno)); if (ret != SIG_DFL && ret != SIG_IGN) { /* The app has set its own signal handler. Replace the previous handler. */ ret = signal(SIGPIPE, ret); MPIR_ERR_CHKANDJUMP1(ret == SIG_ERR, mpi_errno, MPI_ERR_OTHER, "**signal", "**signal %s", MPIU_Strerror(errno)); } } #endif fn_exit: MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_TCP_INIT); /* fprintf(stdout, FCNAME " Exit\n"); fflush(stdout); */ return mpi_errno; fn_fail: /* fprintf(stdout, "failure. mpi_errno = %d\n", mpi_errno); */ goto fn_exit; }
void listen_for_connection(host_port *info) { pthread_t thread; int connection, connect_result, listener; connect_result = set_up_listener(info->port, &listener); connect_result = -1; while(1) { do { connect_result = wait_for_connection(listener, &connection); } while(connect_result < 0); connect_result = 0; safe_recv(connection, &connect_result, sizeof(int)); if(connect_result) { pthread_create(&thread, NULL, (void *(*)(void *))receive_int, (void *)&connection); } } }
void listener_set_up(host_port *info) { int connect_result; listener = malloc(sizeof(int)); connect_result = set_up_listener(info->port, listener); pthread_create(&listener_thread, NULL, (void *(*)(void *))listen_for_connection, listener); }