PIRQueryGenerator::~PIRQueryGenerator() 
{
	joinThread();
  cleanQueryBuffer();

	delete[] coord;
}
int main(int argc, char **argv)
{
    char buf[10];
    setSignalAction();
    startThread();

    while (!getExitApp()) {
        
        printf("Press return to start recording.\n");
        getchar();
        setRun(1);
        pthread_barrier_wait(&startBarrier);
        
        printf("Press return to end recording.\n");
        getchar();
        setRun(0);
        pthread_barrier_wait(&endBarrier);
        
        printf("Recording stopped.\n");
    }
    
    joinThread();

    return 0;
}
Exemple #3
0
void activeClose(Connection *conn) {
	Segment fin;

	cancelThread(conn->sender);

	joinThread(conn->sender);

	fin = createSegment(RUSP_FIN, 0, getWindowNext(&(conn->sndwnd)), 0, NULL);

	addSgmBuff(&(conn->sndsgmbuff), fin, RUSP_NACK);

	setConnectionState(conn, RUSP_FINWT1);

	sendSegment(conn, fin);

	slideWindowNext(&(conn->sndwnd), 1);

	DBGPRINT(RUSP_DEBUG, "SND (NXT): base:%u nxt:%u end:%u SNDUSRBUFF:%zu SNDSGMBUFF:%ld", getWindowBase(&(conn->sndwnd)), getWindowNext(&(conn->sndwnd)), getWindowEnd(&(conn->sndwnd)), getStrBuffSize(&(conn->sndusrbuff)), getSgmBuffSize(&(conn->sndsgmbuff)));

	joinThread(conn->receiver);
}
static omr_error_t
waitForChildThread(OMRTestVM *testVM, omrthread_t childThread, ChildThreadData *childData)
{
	OMRPORT_ACCESS_FROM_OMRPORT(testVM->portLibrary);
	omr_error_t childRc = OMR_ERROR_INTERNAL;

	if (J9THREAD_SUCCESS == joinThread(childThread)) {
		childRc = childData->childRc;
	}
	omrmem_free_memory(childData);
	return childRc;
}
PIRQueryGenerator_internal::~PIRQueryGenerator_internal() 
{
	joinThread();
  cleanQueryBuffer();
}
Exemple #6
0
	void join()
	{
		joinThread();
	}
Exemple #7
0
static void workLoop()
{
    deviceList *list;
    int ctlSock = INVALID_PIPE;

    /* initialize the driver and device list */
    if ((list = initServer(&srvSettings)) == NULL)
        message(LOG_ERROR, "failed to initialize the device list.\n");
    else if (signal(SIGINT, quitHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGINT handler.\n");
    else if (signal(SIGTERM, quitHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGTERM handler.\n");
    else if (signal(SIGHUP, scanHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGHUP handler.\n");
    else if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        message(LOG_ERROR, "failed to ignore SIGPIPE messages.\n");
    else if (! srvSettings.justDescribe && (ctlSock = startListening(NULL)) == INVALID_PIPE)
        message(LOG_ERROR, "failed to open the control socket.\n");
    else
    {
        bool quit = false;

#if DEBUG
printf("OPEN %d %s(%d)\n", srvSettings.commPipe[0], __FILE__, __LINE__);
printf("OPEN %d %s(%d)\n", srvSettings.commPipe[1], __FILE__, __LINE__);
#endif

        /* trigger the initial device scan */
        scanHandler(SIGHUP);

#ifdef __APPLE__
        /* Support hot plug in on Mac OS X -- returns non-zero for error */
        daemon_osx_support(ids);
#endif

        /* loop, waiting for commands */
        while(! quit)
        {
            THREAD_PTR thread = INVALID_THREAD_PTR;
            void *exitVal;

            /* wait for a new ctl connection, a command from an
               existing ctl connection, or a message from an exiting
               child thread. */

            /* read a command and check for error */
            if (readPipe(srvSettings.commPipe[READ], &thread,
                         sizeof(THREAD_PTR)) != sizeof(THREAD_PTR))
            {
                message(LOG_ERROR,
                        "CommPipe read failed: %s\n", translateError(errno));
                quit = true;
            }
            /* threads trigger a join by telling the main thread their id */
            else if (thread != INVALID_THREAD_PTR)
                joinThread(thread, &exitVal);
            /* read the actual command (came from a signal handler) */
            else if (readPipe(srvSettings.commPipe[READ], &thread,
                              sizeof(THREAD_PTR)) != sizeof(THREAD_PTR))
            {
                message(LOG_ERROR,
                        "Command read failed: %s\n", translateError(errno));
                quit = true;
            }
            /* handle the shutdown command */
            else if (thread == QUIT_TRIGGER)
                quit = true;
            /* complain about unknown commands */
            else if (thread != SCAN_TRIGGER)
                message(LOG_ERROR,
                        "Unknown command from commPipe: %d\n", thread);
            /* handle the scan/rescan command */
            else
            {
                if (srvSettings.justDescribe)
                    message(LOG_NORMAL, "Detected Iguanaworks devices:\n");
                if (! updateDeviceList(list))
                    message(LOG_ERROR, "scan failed.\n");
                if (srvSettings.justDescribe)
                    break;
            }
        }

        /* wait for all the workers to finish */
        reapAllChildren(list);

        /* close up the server socket */
        stopListening(ctlSock, NULL);
    }
}
Exemple #8
0
void stopThread(Thread * thr)
{
	thr->flags += THREAD_STOP;
	joinThread(thr);
}
Exemple #9
0
void client(char *host, char* port){
	int sockfd, numbytes;
	char buff[MAXDATASIZE];
	struct addrinfo hints, *servinfo, *p;
	int rv;
	char s[INET6_ADDRSTRLEN];
	//This struct will be used to bind socket to port
	struct sockaddr_in popt;

	popt.sin_family = INADDR_ANY;
	popt.sin_port = htons(sourcePort);

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		exit(1);
	}

	// loop through all the results and connect to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("client: socket");
			continue;
		}
		if(pflag)
		{
			//Binds client to specific port
			 if (bind(sockfd, (struct sockaddr*)&popt, sizeof popt) == -1) {
			 	close(sockfd);
			 	perror("server: bind");
			 	continue;
		 	}
		}


		if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("client: connect");
			continue;
		}

		break;
	}

	if (p == NULL) {
		if(vflag){fprintf(stderr, "client: failed to connect\n");}
		exit(2);
	}

	inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
			s, sizeof s);
	/*If in debug mode print where we are connecting*/
	if(vflag){printf("client: connecting to %s\n", s);}

	freeaddrinfo(servinfo); // all done with this structure


	//Thread for sending messages
	struct Thread *send_thread = createThread(sending_thread, &sockfd);
	//Off it goes
	int threazy = runThread(send_thread, NULL);

	//Thread for reading messages
	struct Thread *receive_thread = createThread(receiving_thread, &sockfd);

	//off it goes
	int rthreazy = runThread(receive_thread, NULL);

	if(threazy == -10) {fprintf(stderr, "There was an error with the sending thread\n");}

	joinThread(send_thread, NULL);

}
Exemple #10
0
void server(char* port, int kflag){
	int sockfd, new_fd, numbytes;  // listen on sock_fd, new connection on new_fd
	struct addrinfo hints, *servinfo, *p;
	struct sockaddr_storage their_addr; // connector's address information
	socklen_t sin_size;
	struct sigaction sa;
	int yes=1;
	char s[INET6_ADDRSTRLEN];
	int rv;
	char buf[MAXDATASIZE];

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE; // use my IP

	//This checks if something went wrong. addrinfo returns 0 when it suceeds
	if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) {
	fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
	exit(1);
	}

	// loop through all the results and bind to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("server: socket");
			continue;
		}

		if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
				sizeof(int)) == -1) {
			perror("setsockopt");
			exit(1);
		}

		if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("server: bind");
			continue;
		}

		break;
	}

	freeaddrinfo(servinfo); // all done with this structure

	if (p == NULL)  {
		fprintf(stderr, "server: failed to bind\n");
		exit(1);
	}

	if (listen(sockfd, BACKLOG) == -1) {
		perror("listen");
		exit(1);
	}

	if(vflag){fprintf(stderr, "Waiting for a connection...\n");}


	/* For multiple connections */
	if(rflag)
	{		
		char *message;
		struct Clients *conns = malloc(sizeof(struct Clients));
		conns->clients = malloc(10 * sizeof(int));
		conns->length = 0;

		//Create the sending thread that reads from STDIN
		struct Thread *sender_thread = createThread(send_to_all, conns);

		//Run it 
		int sender_t = runThread(sender_thread, NULL);

		//Initialize a mutex lock
		pthread_mutex_init(&mutexDelete, NULL);


		do
		{
			
			new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
			if(new_fd == -1)
			{
				perror("accept");
				continue;
			}

			inet_ntop(their_addr.ss_family,
	 			get_in_addr((struct sockaddr *)&their_addr),
	 			s, sizeof s);

			/* Get por of incoming connection*/
			struct sockaddr_in *sin = (struct sockaddr_in*)&their_addr;
			short w = ntohs(sin->sin_port);

			/*For debugging print when we get a connection*/
			if(vflag){fprintf(stderr, "Server: got connection from %s\nAnd the port is %u\n", s, w);}

			/* Add connection to our struct of clients and prepare thread*/
			/* Mutex lock is used as conns is a shared resource*/
			pthread_mutex_lock(&mutexDelete);
				addFD(conns,new_fd);
			pthread_mutex_unlock(&mutexDelete);

			struct Thread *sniffer_thread = createThread(read_and_send_to_all, conns);

			int threazy = runThread(sniffer_thread, NULL);

			detachThread(sniffer_thread);

		} while (1);

		/*Clean up the listener socket and mutex*/
		pthread_mutex_destroy(&mutexDelete);
		close(sockfd);
		exit(0);
	}



	/* Now wait for connections , this is for regular -l*/
	do 
	{
	 	sin_size = sizeof their_addr;
	 	new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
	 	if (new_fd == -1) {
	 		perror("accept");
	 		continue;
	 	}

	 	inet_ntop(their_addr.ss_family,
	 		get_in_addr((struct sockaddr *)&their_addr),
	 		s, sizeof s);
 		
 		/* Get por of incoming connection*/
		struct sockaddr_in *sin = (struct sockaddr_in*)&their_addr;
		short w = ntohs(sin->sin_port);


	 	/*For debugging print when we get a connection*/
		if(vflag){fprintf(stderr, "Server: got connection from %s\nAnd the port is %u\n", s, w);}

		/*Let's start a thread that we will
		use for receiving messages*/
	 	struct Thread *receive_thread = createThread(receiving_thread, &new_fd);

		/* And now we send it off !*/
	 	int rthreazy = runThread(receive_thread, NULL);

		/* Now we start a thread for reading fron STDIN and run it*/
		struct Thread *send_thread = createThread(sending_thread, &new_fd);
	 	int sthreazy = runThread(send_thread, NULL);

		/* Join threads */
	 	joinThread(receive_thread, NULL);

		close(new_fd);


	} while(kflag); //if kflag is on we keep on listening after a connection closes

	/*Clean the listener socket if you are leaving*/
	close(sockfd);
}
Exemple #11
0
HttpServer::~HttpServer() {
	cancelThread();
	joinThread();
}
void ReplicationCoordinatorExternalStateImpl::shardingOnTransitionToPrimaryHook(
    OperationContext* txn) {
    auto status = ShardingStateRecovery::recover(txn);

    if (ErrorCodes::isShutdownError(status.code())) {
        // Note: callers of this method don't expect exceptions, so throw only unexpected fatal
        // errors.
        return;
    }

    if (!status.isOK()) {
        fassertFailedWithStatus(40107, status);
    }

    if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) {
        status = Grid::get(txn)->catalogManager()->initializeConfigDatabaseIfNeeded(txn);
        if (!status.isOK() && status != ErrorCodes::AlreadyInitialized) {
            if (ErrorCodes::isShutdownError(status.code())) {
                // Don't fassert if we're mid-shutdown, let the shutdown happen gracefully.
                return;
            }

            fassertFailedWithStatus(40184,
                                    Status(status.code(),
                                           str::stream()
                                               << "Failed to initialize config database on config "
                                                  "server's first transition to primary"
                                               << causedBy(status)));
        }

        if (status.isOK()) {
            // Load the clusterId into memory. Use local readConcern, since we can't use majority
            // readConcern in drain mode because the global lock prevents replication. This is
            // safe, since if the clusterId write is rolled back, any writes that depend on it will
            // also be rolled back.
            // Since we *just* wrote the cluster ID to the config.version document (via
            // ShardingCatalogManager::initializeConfigDatabaseIfNeeded), this should always
            // succeed.
            status = ClusterIdentityLoader::get(txn)->loadClusterId(
                txn, repl::ReadConcernLevel::kLocalReadConcern);

            if (ErrorCodes::isShutdownError(status.code())) {
                // Don't fassert if we're mid-shutdown, let the shutdown happen gracefully.
                return;
            }

            fassertStatusOK(40217, status);
        }

        // For upgrade from 3.2 to 3.4, check if any shards in config.shards are not yet marked as
        // shard aware, and attempt to initialize sharding awareness on them.
        auto shardAwareInitializationStatus =
            Grid::get(txn)->catalogManager()->initializeShardingAwarenessOnUnawareShards(txn);
        if (!shardAwareInitializationStatus.isOK()) {
            warning() << "Error while attempting to initialize sharding awareness on sharding "
                         "unaware shards "
                      << causedBy(shardAwareInitializationStatus);
        }

        // Free any leftover locks from previous instantiations
        auto distLockManager = Grid::get(txn)->catalogClient(txn)->getDistLockManager();
        distLockManager->unlockAll(txn, distLockManager->getProcessID());

        // If this is a config server node becoming a primary, start the balancer
        auto balancer = Balancer::get(txn);

        // We need to join the balancer here, because it might have been running at a previous time
        // when this node was a primary.
        balancer->joinThread();
        balancer->startThread(txn);
    } else if (ShardingState::get(txn)->enabled()) {
        const auto configsvrConnStr =
            Grid::get(txn)->shardRegistry()->getConfigShard()->getConnString();
        auto status = ShardingState::get(txn)->updateShardIdentityConfigString(
            txn, configsvrConnStr.toString());
        if (!status.isOK()) {
            warning() << "error encountered while trying to update config connection string to "
                      << configsvrConnStr << causedBy(status);
        }
    }

    // There is a slight chance that some stale metadata might have been loaded before the latest
    // optime has been recovered, so throw out everything that we have up to now
    ShardingState::get(txn)->clearCollectionMetadata();
}