void MainLogicLoop(){ //TODO lock the camera //lockCamera(&processHandle); //read the data at these pointers, now that offsets have been added and we have a static address ReadPlayer(&Enemy, processHandle, EnemyId); ReadPlayer(&Player, processHandle, PlayerId); //log distance in memory AppendDistance(distance(&Player, &Enemy)); //start the neural network threads WakeThread(defense_mind_input); WakeThread(attack_mind_input); ResetVJoyController(); //begin reading enemy state, and handle w logic and subroutines char attackImminent = EnemyStateProcessing(&Player, &Enemy); WaitForThread(defense_mind_input); guiPrint(LocationDetection",1:Defense Neural Network detected %d, and Attack %d", DefenseChoice, AttackChoice); #if DebuggingPacifyDef DefenseChoice = 0; #endif //defense mind makes choice to defend or not(ex backstab metagame decisions). //handles actually backstab checks, plus looks at info from obveous direct attacks from aboutToBeHit if (attackImminent == ImminentHit || inActiveDodgeSubroutine() || (DefenseChoice>0)){ dodge(&Player, &Enemy, &iReport, attackImminent, DefenseChoice); } WaitForThread(attack_mind_input); guiPrint(LocationDetection",2:Attack Neural Network decided %d", AttackChoice); #if DebuggingPacifyAtk AttackChoice = 0; #endif //attack mind make choice about IF to attack or not, and how to attack //enter when we either have a Attack neural net action or a attackImminent action if (inActiveAttackSubroutine() || attackImminent != ImminentHit || (AttackChoice && DefenseChoice<=0)){ attack(&Player, &Enemy, &iReport, attackImminent, AttackChoice); } //unset neural network desisions DefenseChoice = 0; AttackChoice = 0; //handle subroutine safe exits SafelyExitSubroutines(); guiPrint(LocationDetection",5:Current Subroutine States ={%d,%d,%d,%d}", subroutine_states[0], subroutine_states[1], subroutine_states[2], subroutine_states[3]); //send this struct to the driver (only 1 call for setting all controls, much faster) guiPrint(LocationJoystick",0:AxisX:%d\nAxisY:%d\nButtons:0x%x", iReport.wAxisX, iReport.wAxisY, iReport.lButtons); UpdateVJD(iInterface, (PVOID)&iReport); //SetForegroundWindow(h); //SetFocus(h); }
/* Stops the main loop of the TCP server. Can be called in the user-defined functions passed in parameters of MainLoopSRVTCP() if DontBlock is not set. SRVTCP* pSRVTCP : (IN,OUT) pointer to the data structure representing the server Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int StopLoopSRVTCP(SRVTCP* pSRVTCP) { int EndThread = 0; if ((pSRVTCP == NULL) || (pSRVTCP->initialized != 1)) { return EXIT_FAILURE; } memcpy_ts(&EndThread, &pSRVTCP->EndThread, sizeof(int), &pSRVTCP->CSEndThread); // Thread-safe copy if (EndThread == 0) { // A thread is currently running EndThread = 1; memcpy_ts((char*)&pSRVTCP->EndThread, (char*)&EndThread, sizeof(int), &pSRVTCP->CSEndThread); // Thread-safe copy if (pSRVTCP->DontBlock) { if (WaitForThread(pSRVTCP->ThrId) != EXIT_SUCCESS) { return EXIT_FAILURE; } } } else { // No thread was running, this function should not have been called return EXIT_FAILURE; } return EXIT_SUCCESS; }
/* Stops the TCP client. CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int StopCLITCP(CLITCP* pCLITCP) { int EndThread = 1; if((pCLITCP == NULL) || (pCLITCP->initialized != 1)) { return EXIT_FAILURE; } pCLITCP->initialized = 0; memcpy_ts((char*)&pCLITCP->EndThread, (char*)&EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy if (WaitForThread(pCLITCP->ThrId) != EXIT_SUCCESS) { DelCriticalSection(&pCLITCP->CSEndThread); DelCriticalSection(&pCLITCP->CSconnected); StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf); free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } DelCriticalSection(&pCLITCP->CSEndThread); DelCriticalSection(&pCLITCP->CSconnected); if(StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) != EXIT_SUCCESS) { free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_SUCCESS; }
void CClientProxy::WaitForObject() { if( m_hProxyThread==NULL )return; WaitForThread( m_hProxyThread ); CloseHandle( m_hProxyThread ); m_hProxyThread = NULL; }
EMBeThread::~EMBeThread() { if(m_vIsAlive) { Kill(); WaitForThread(); } }
/* Stops the TCP server. Should never be called in the user-defined functions passed in parameters of MainLoopSRVTCP(). Should not be used to stop the thread created by MainLoopSRVTCP(), use StopLoopSRVTCP() before. SRVTCP* pSRVTCP : (IN,OUT) pointer to the data structure representing the server Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int StopSRVTCP(SRVTCP* pSRVTCP) { int errflag = 0; int EndThread = 0; SOCKET_DATA* sd = NULL; SOCKET_DATA* sd_tmp = NULL; if ((pSRVTCP == NULL) || (pSRVTCP->initialized != 1)) { return EXIT_FAILURE; } pSRVTCP->initialized = 0; memcpy_ts(&EndThread, &pSRVTCP->EndThread, sizeof(int), &pSRVTCP->CSEndThread); // Thread-safe copy if (EndThread == 0) { // A thread is currently running, so it should be stopped EndThread = 1; memcpy_ts((char*)&pSRVTCP->EndThread, (char*)&EndThread, sizeof(int), &pSRVTCP->CSEndThread); // Thread-safe copy // Wait until the thread has terminated. if (WaitForThread(pSRVTCP->ThrId) != EXIT_SUCCESS) { errflag = 1; } } DeleteCriticalSection(&pSRVTCP->CSNbConnections); DeleteCriticalSection(&pSRVTCP->CSEndThread); // Close and remove every client socket of the fd_list and the fd_set sd = pSRVTCP->sock_list.first->next; // The server socket is excluded while (sd) { ShutdownTCP(&sd->sock, SD_BOTH); DisconnectTCP(&sd->sock); sd_tmp = sd->next; FD_REMOVE(sd, &pSRVTCP->sock_list, &pSRVTCP->sock_set); sd = sd_tmp; } // Removes the server socket from the fd_list and the fd_set FD_REMOVE(pSRVTCP->sock_list.first, &pSRVTCP->sock_list, &pSRVTCP->sock_set); ShutdownTCP(&pSRVTCP->ListenSocket, SD_BOTH); if (DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf) != EXIT_SUCCESS) { errflag = 1; } free(pSRVTCP->address);pSRVTCP->address = NULL; free(pSRVTCP->port);pSRVTCP->port = NULL; if (errflag) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
static void waitForThreads() { // Wait for all threads to be created. while (NumOfThreads() != ROLE_SIZE) { DoYield(); } // Wait for the ROLE_CANCELED thread to be ready for cancelation while (!readyToCancel) { DoYield(); } // Send a cancel request to the ROLE_CANCELED thread. CancelThread(threads[ROLE_CANCELED]); // Wait for all threads that are supposed to exit. WaitForThread(threads[ROLE_FINISH]); WaitForThread(threads[ROLE_EXIT]); // The timing of the effect of pthread_cancel is undefined, this could cause the aplication to hang. // Currently commented out. WaitForThread(threads[ROLE_CANCELED]); }
static enum MqErrorE SysWait ( struct MqS * const context, const struct MqIdS *idP ) { errno = 0; switch (idP->type) { case MQ_ID_PROCESS: { break; } case MQ_ID_THREAD: { WaitForThread((GoChan)idP->val); break; } case MQ_ID_UNUSED: { break; } } return MQ_OK; }
/* Stops the TCP client. CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int StopCLITCP(CLITCP* pCLITCP) { int errflag = 0; int EndThread = 1; if ((pCLITCP == NULL) || (pCLITCP->initialized != 1)) { return EXIT_FAILURE; } pCLITCP->initialized = 0; memcpy_ts(&EndThread, &pCLITCP->EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy if (EndThread == 0) { // A thread is currently running, so it should be stopped EndThread = 1; memcpy_ts((char*)&pCLITCP->EndThread, (char*)&EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy // Wait until the thread has terminated. if (WaitForThread(pCLITCP->ThrId) != EXIT_SUCCESS) { errflag = 1; } } DeleteCriticalSection(&pCLITCP->CSconnected); DeleteCriticalSection(&pCLITCP->CSEndThread); ShutdownTCP(&pCLITCP->ConnectSocket, SD_BOTH); if (DestroySocketTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) != EXIT_SUCCESS) { errflag = 1; } free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; if (errflag) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
CHostSet::~CHostSet() { //send the listen thread quit message SOCKET sock = Socket( PF_INET, SOCK_DGRAM, 0 ); char buf[MAXLINE]; if( gethostname( buf, ELEMENTS(buf) ) == 0 ){ HANDLE hListenThread = m_hListenThread; //send quit message to the monitor thread struct hostent* phent = gethostbyname( buf ); SOCKADDR_IN addr; addr.sin_family = AF_INET; bcopy( phent->h_addr, &addr.sin_addr, sizeof(IN_ADDR) ); addr.sin_port = htons( NCP_HOST_REGIST_PORT ); int nCode = htonl( NCP_REGISTER_EXIT ); sendto( sock, (char*)&nCode, sizeof(int), 0, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN) ); //synchronize the listen thread WaitForThread( hListenThread ); CloseHandle( hListenThread ); } closesocket( sock ); //free the all the host objects Lock(); m_trHosts.VisitTree( FreeHostObject ); Unlock(); CloseHandle( m_mxLock ); CloseHandle( m_smAlives ); if( m_pUser )delete m_pUser; if( m_pPassword )delete m_pPassword; }
/* ======================== idSysThread::StopThread ======================== */ void idSysThread::StopThread( bool wait ) { if( !isRunning ) { return; } if( isWorker ) { signalMutex.Lock(); moreWorkToDo = true; signalWorkerDone.Clear(); isTerminating = true; signalMoreWorkToDo.Raise(); signalMutex.Unlock(); } else { isTerminating = true; } if( wait ) { WaitForThread(); } }
void threadlib_waitforclose(THREAD_HANDLE *thread) { WaitForThread(thread->thread_handle); }
/* Main loop of the server. Waits for incoming connections. When a new client is successfully connected to the server, the user-defined function OnNewCli() is called. When a client sends data to the server, the user-defined function OnCliReq() is called to handle the client request (only in REQSRVTCP mode). At each loop of the server, the user-defined function OnLoop() is called for every connected client. Every client should be disconnected if a SendSRVTCP() or RecvSRVTCP() fails in one of the user-defined functions by returning EXIT_FAILURE. TooManyConnectionsAction should be DENY_CONNECTION if new incoming connections should be denied if there is too many clients currently connected, or REMOVE_UNUSED if new incoming connections should replace oldest unused connections (only in REQSRVTCP mode). If DontBlock is set, this function return immediately, leaving a thread running. This thread should be stopped by calling StopLoopSRVTCP(). Otherwise, this function should block until StopLoopSRVTCP() is successfully called. The REQSRVTCP mode is designed for servers that wait for requests from the clients while the STREAMSRVTCP mode is designed for servers that continuously sends data to the clients without waiting for requests. SRVTCP* pSRVTCP : (IN,OUT) pointer to the data structure representing the server int (*OnLoop)(SRVTCP*, SOCKET, void*) : (IN) function called at every server loop, for each connected socket void* OnLoopParams : (IN) parameter passed to every call to OnLoopParams() int (*OnNewCli)(SRVTCP*, SOCKET, void*) : (IN) function called when a new client socket is successfully connected void* OnNewCliParams : (IN) parameter passed to every call to OnNewCliParams() int (*OnCliReq)(SRVTCP*, SOCKET, void*) : (IN) function called when a client socket sends data to the server (only in REQSRVTCP mode) void* OnCliReqParams : (IN) parameter passed to every call to OnCliReq() (only in REQSRVTCP mode) int TooManyConnectionsAction : (IN) DENY_CONNECTION or REMOVE_UNUSED (only in REQSRVTCP mode) int DontBlock : (IN) 1 or 0 int mode : (IN) REQSRVTCP or STREAMSRVTCP Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int MainLoopSRVTCP( SRVTCP* pSRVTCP, int (*OnLoop)(SRVTCP*, SOCKET, void*), void* OnLoopParams, int (*OnNewCli)(SRVTCP*, SOCKET, void*), void* OnNewCliParams, int (*OnCliReq)(SRVTCP*, SOCKET, void*), void* OnCliReqParams, int TooManyConnectionsAction, int DontBlock, int mode ) { struct THRPARAMS { SRVTCP* pSRVTCP; int (*OnLoop)(SRVTCP*, SOCKET, void*); void* OnLoopParams; int (*OnNewCli)(SRVTCP*, SOCKET, void*); void* OnNewCliParams; int (*OnCliReq)(SRVTCP*, SOCKET, void*); void* OnCliReqParams; int TooManyConnectionsAction; }; struct THRPARAMS params; int EndThread = 0; if ((pSRVTCP == NULL) || (pSRVTCP->initialized != 1)) { return EXIT_FAILURE; } if ((OnLoop == NULL) && (OnNewCli == NULL) && (OnCliReq == NULL)) { return EXIT_FAILURE; } memcpy_ts(&EndThread, &pSRVTCP->EndThread, sizeof(int), &pSRVTCP->CSEndThread); // Thread-safe copy if (EndThread == 0) { // A thread is already running and only one thread should be running at a time return EXIT_FAILURE; // This function should not have been called } pSRVTCP->EndThread = 0; // Notifies that a thread should be running // Thread parameters params.pSRVTCP = pSRVTCP; params.OnLoop = OnLoop; params.OnLoopParams = OnLoopParams; params.OnNewCli = OnNewCli; params.OnNewCliParams = OnNewCliParams; params.OnCliReq = OnCliReq; params.OnCliReqParams = OnCliReqParams; params.TooManyConnectionsAction = TooManyConnectionsAction; // If DontBlock is not set, this function will block until StopLoopSRVTCP() is successfully called. // Otherwise, it returns immediately after the creation of the thread pSRVTCP->DontBlock = DontBlock; if (mode == REQSRVTCP) { if (CreateDefaultThread(REQSRVTCPThrProc, ¶ms, &pSRVTCP->ThrId) != EXIT_SUCCESS) { pSRVTCP->EndThread = 1; // Notifies that no thread is running as its creation failed return EXIT_FAILURE; } if (!pSRVTCP->DontBlock) { // Should return after a successful call to StopLoopSRVTCP() if (WaitForThread(pSRVTCP->ThrId) != EXIT_SUCCESS) { return EXIT_FAILURE; } } } else { if (CreateDefaultThread(STREAMSRVTCPThrProc, ¶ms, &pSRVTCP->ThrId) != EXIT_SUCCESS) { pSRVTCP->EndThread = 1; // Notifies that no thread is running as its creation failed return EXIT_FAILURE; } if (!pSRVTCP->DontBlock) { // Should return after a successful call to StopLoopSRVTCP() if (WaitForThread(pSRVTCP->ThrId) != EXIT_SUCCESS) { return EXIT_FAILURE; } } } return EXIT_SUCCESS; }
/* Initializes the TCP client. CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client char* address : (IN) server address to connect char* port : (IN) server port to connect Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error */ int InitCLITCP(CLITCP* pCLITCP, char* address, char* port) { int EndThread = 1; if((pCLITCP == NULL) || (pCLITCP->initialized == 1)) { return EXIT_FAILURE; } pCLITCP->address = (char*)calloc(strlen(address)+1, sizeof(char)); sprintf(pCLITCP->address, "%s", address); pCLITCP->port = (char*)calloc(strlen(port)+1, sizeof(char)); sprintf(pCLITCP->port, "%s", port); pCLITCP->ConnectSocket = INVALID_SOCKET; pCLITCP->addrinf = NULL; pCLITCP->EndThread = 0; pCLITCP->connected = 0; if ( (InitTCP(&pCLITCP->ConnectSocket, pCLITCP->address, pCLITCP->port, &pCLITCP->addrinf) == EXIT_SUCCESS)&& // (SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS)&& // Setting timeouts on the client socket (ConnectTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) == EXIT_SUCCESS)//&& // (SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS) // Setting timeouts on the client socket ) { fprintf(stdout, "Connection successful\n"); pCLITCP->connected = 1; } else fprintf(stdout, "Unable to connect\n"); if (InitCriticalSection(&pCLITCP->CSEndThread) != EXIT_SUCCESS) { StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf); free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } if (InitCriticalSection(&pCLITCP->CSconnected) != EXIT_SUCCESS) { DelCriticalSection(&pCLITCP->CSEndThread); StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf); free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } if (CreateDefaultThread(ConnectThrProc, pCLITCP, &pCLITCP->ThrId) != EXIT_SUCCESS) { DelCriticalSection(&pCLITCP->CSEndThread); DelCriticalSection(&pCLITCP->CSconnected); StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf); free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } if (SetThreadDefaultPriority(pCLITCP->ThrId, THREAD_PRIORITY_NORMAL) != EXIT_SUCCESS) { EndThread = 1; memcpy_ts((char*)&pCLITCP->EndThread, (char*)&EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy WaitForThread(pCLITCP->ThrId); DelCriticalSection(&pCLITCP->CSEndThread); DelCriticalSection(&pCLITCP->CSconnected); StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf); free(pCLITCP->address);pCLITCP->address = NULL; free(pCLITCP->port);pCLITCP->port = NULL; return EXIT_FAILURE; } pCLITCP->initialized = 1; return EXIT_SUCCESS; }