int nSend(nTask task, void *msg) { int rc; START_CRITICAL(); pending_sends++; { nTask this_task= current_task; if (task->status==WAIT_SEND || task->status==WAIT_SEND_TIMEOUT) { if (task->status==WAIT_SEND_TIMEOUT) CancelTask(task); task->status= READY; PushTask(ready_queue, task); /* En primer lugar en la cola */ } else if (task->status==ZOMBIE) nFatalError("nSend", "El receptor es un ``zombie''\n"); /* En nReply se coloca ``this_task'' en la cola de tareas ready */ PutTask(task->send_queue, this_task); this_task->send.msg= msg; this_task->status= WAIT_REPLY; ResumeNextReadyTask(); rc= this_task->send.rc; } pending_sends--; END_CRITICAL(); return rc; }
//============================================================================ // NThreadPool::ScheduleTask : Schedule a task for execution. //---------------------------------------------------------------------------- void NThreadPool::ScheduleTask(NThreadTask *theTask) { // Validate our state NN_ASSERT(mLock.IsLocked()); // Add the task PushTask(mTasks, theTask); mHavePushed = true; // Update the workers // // If we've reached the thread limit then the existing threads will need to // process this task, so we signal to let them know there's more work to do. // // Incrementing the thread count must be done by the main thread, since a large // number of tasks may be queued up before any worker thread gets a chance to run. if (mActiveThreads < mThreadLimit) NThreadUtilities::DetachFunctor(BindSelf(NThreadPool::ExecuteTasks)); mSemaphore.Signal(); }
void Bot::AttachToUser (void) { // SyPB Pro P.29 - small change if (GetGameMod () != 0) return; /* // SyPB Pro P.15 if (!FNullEnt (m_moveTargetEntity)) return; */ // this function forces bot to join to user Array <edict_t *> foundUsers; // search friends near us for (int i = 0; i < engine->GetMaxClients (); i++) { if (!(g_clients[i].flags & CFLAG_USED) || !(g_clients[i].flags & CFLAG_ALIVE) || g_clients[i].team != GetTeam (GetEntity ()) || g_clients[i].ent == GetEntity ()) continue; if (EntityIsVisible (g_clients[i].origin) && !IsValidBot (g_clients[i].ent)) foundUsers.Push (g_clients[i].ent); } if (foundUsers.IsEmpty ()) return; m_targetEntity = foundUsers.GetRandomElement (); ChatterMessage (Chatter_LeadOnSir); PushTask (TASK_FOLLOWUSER, TASKPRI_FOLLOWUSER, -1, 0.0, true); }
bool PushAiderTask(std::vector<sub_op_task>& task_list,int cpu) { auto tr=aider_threads_[0]; tr->PushTask(task_list); return true; }
void nReply(nTask task, int rc) { START_CRITICAL(); if (task->status!=WAIT_REPLY) nFatalError("nReply","Esta tarea no espera un ``nReply''\n"); PushTask(ready_queue, current_task); task->send.rc= rc; task->status= READY; PushTask(ready_queue, task); ResumeNextReadyTask(); END_CRITICAL(); }
void EndJob(Master *m,TaskQueue *tq) { for(int i = 0;i < m->onJobNumber;++i) { PushTask(tq,EndWorker,NULL,ENDTASK); } for(int i = 0;i < m->onJobNumber;++i) { pthread_join(m->workers[i].id,NULL); } free(tq); }
void TaskProxy::ExecuteTaskGroups( bool _bUseCallerThread, TaskGroup* _aGroup, const uint32 _cnGroup ) { const Array<WorkerThread*>& rvpThreadPool = WorkerThreadManager::s_GetInstance()->_GetPool(); uint32 i, j, nThread = rvpThreadPool.size(); // Define proxy for threads for( i = 0; i < nThread; i++ ) rvpThreadPool[i]->SetTaskProxy( this ); for( j = 0; j < _cnGroup; j++ ) { const Array<Task*>& rvpTask = _aGroup[j].m_vpTask; uint32 k, nTask = rvpTask.size(); for( k = 0; k < nTask; k++ ) PushTask( rvpTask[k] ); // Signal threads for( i = 0; i < nThread; i++ ) rvpThreadPool[i]->ForceBusy(); uint32 iFinishedTask = 0; if( _bUseCallerThread ) { Task* pTask = NULL; while( (pTask = PopTask()) != NULL ) { pTask->Execute(); iFinishedTask++; } } // Wait other threads completion while( iFinishedTask != nTask ) { for( i = 0; i < nThread; i++ ) { if( rvpThreadPool[i]->PopFinishedTask() ) iFinishedTask++; } } } // [Remove proxy once finished for( i = 0; i < nThread; i++ ) rvpThreadPool[i]->SetTaskProxy( NULL ); }
int TeleTaskProducer::PushTaskBatch() { int i; int n = 0; pthread_mutex_lock(&task_lock_); if (fetch_time_str_.length() != 12) { LOG(INFO) << "please wait for the fetch time str"; pthread_mutex_unlock(&task_lock_); return 0; } char buf[4]; strcpy(buf, fetch_time_str_.c_str() + 10); int minute = atoi(buf); memset(buf, 0, sizeof(buf)); strncpy(buf, fetch_time_str_.c_str() + 8, 2); int hour = atoi(buf); for (i = index_; i < index_ + BATCHNUMS && i < MAX_FETCH_INDEX; i++) { int minute_index = hour * 60 + minute; if (!IsSlave()) { CLEAR_SET(tele::GetShareMem(), minute_index, i); } else if (IS_SET(tele::GetShareMem(), minute_index, i)) { n++; continue; } TeleTask * task = TeleTaskFactory::BuildTask(i, token_str_, fetch_time_str_, TASK_NORMAL); if (!task) { n++; continue; } task->minute_index = minute_index; if (PushTask(task) < 0) { delete task; break; } else { n++; } } index_ += n; pthread_mutex_unlock(&task_lock_); if (n > 0) { pthread_cond_broadcast(¬_empty_); } return n; }
void Bot::NewRound (void) { // this function initializes a bot after creation & at the start of each round int i = 0; // delete all allocated path nodes DeleteSearchNodes (); m_waypointOrigin = nullvec; m_destOrigin = nullvec; m_currentWaypointIndex = -1; m_currentTravelFlags = 0; m_desiredVelocity = nullvec; m_prevGoalIndex = -1; m_chosenGoalIndex = -1; m_loosedBombWptIndex = -1; m_moveToC4 = false; m_duckDefuse = false; m_duckDefuseCheckTime = 0.0f; m_prevWptIndex[0] = -1; m_prevWptIndex[1] = -1; m_prevWptIndex[2] = -1; m_prevWptIndex[3] = -1; m_prevWptIndex[4] = -1; m_navTimeset = engine->GetTime (); switch (m_personality) { case PERSONALITY_NORMAL: m_pathType = engine->RandomInt (0, 100) > 50 ? 0 : 1; break; case PERSONALITY_RUSHER: m_pathType = 0; break; case PERSONALITY_CAREFUL: m_pathType = 1; break; } // clear all states & tasks m_states = 0; ResetTasks (); m_isVIP = false; m_isLeader = false; m_hasProgressBar = false; m_canChooseAimDirection = true; m_timeTeamOrder = 0.0f; m_askCheckTime = 0.0f; m_minSpeed = 260.0f; m_prevSpeed = 0.0f; m_prevOrigin = Vector (9999.0, 9999.0, 9999.0f); m_prevTime = engine->GetTime (); m_blindRecognizeTime = engine->GetTime (); m_viewDistance = 4096.0f; m_maxViewDistance = 4096.0f; m_pickupItem = null; m_itemIgnore = null; m_itemCheckTime = 0.0f; m_breakableEntity = null; m_breakable = nullvec; m_timeDoorOpen = 0.0f; ResetCollideState (); ResetDoubleJumpState (); m_enemy = null; m_lastVictim = null; m_lastEnemy = null; m_lastEnemyOrigin = nullvec; m_trackingEdict = null; m_timeNextTracking = 0.0f; m_buttonPushTime = 0.0f; m_enemyUpdateTime = 0.0f; m_seeEnemyTime = 0.0f; m_shootAtDeadTime = 0.0f; m_oldCombatDesire = 0.0f; m_avoidGrenade = null; m_needAvoidGrenade = 0; m_lastDamageType = -1; m_voteKickIndex = 0; m_lastVoteKick = 0; m_voteMap = 0; m_doorOpenAttempt = 0; m_aimFlags = 0; m_burstShotsFired = 0; m_position = nullvec; m_idealReactionTime = g_skillTab[m_skill / 20].minSurpriseTime; m_actualReactionTime = g_skillTab[m_skill / 20].minSurpriseTime; m_targetEntity = null; m_followWaitTime = 0.0f; for (i = 0; i < Const_MaxHostages; i++) m_hostages[i] = null; for (i = 0; i < Chatter_Total; i++) m_voiceTimers[i] = -1.0f; m_isReloading = false; m_reloadState = RSTATE_NONE; m_reloadCheckTime = 0.0f; m_shootTime = engine->GetTime (); m_playerTargetTime = engine->GetTime (); m_firePause = 0.0f; m_timeLastFired = 0.0f; m_grenadeCheckTime = 0.0f; m_isUsingGrenade = false; m_skillOffset = (100 - m_skill) / 100.0f; m_blindButton = 0; m_blindTime = 0.0f; m_jumpTime = 0.0f; m_jumpFinished = false; m_isStuck = false; m_sayTextBuffer.timeNextChat = engine->GetTime (); m_sayTextBuffer.entityIndex = -1; m_sayTextBuffer.sayText[0] = 0x0; m_buyState = 0; if (!m_notKilled) // if bot died, clear all weapon stuff and force buying again { memset (&m_ammoInClip, 0, sizeof (m_ammoInClip)); memset (&m_ammo, 0, sizeof (m_ammo)); m_currentWeapon = 0; } m_knifeAttackTime = engine->GetTime () + engine->RandomFloat (1.3f, 2.6f); m_nextBuyTime = engine->GetTime () + engine->RandomFloat (0.6f, 1.2f); m_buyPending = false; m_inBombZone = false; m_shieldCheckTime = 0.0f; m_zoomCheckTime = 0.0f; m_strafeSetTime = 0.0f; m_combatStrafeDir = 0; m_fightStyle = 0; m_lastFightStyleCheck = 0.0f; m_checkWeaponSwitch = true; m_checkKnifeSwitch = true; m_buyingFinished = false; m_radioEntity = null; m_radioOrder = 0; m_defendedBomb = false; m_timeLogoSpray = engine->GetTime () + engine->RandomFloat (0.5, 2.0f); m_spawnTime = engine->GetTime (); m_lastChatTime = engine->GetTime (); pev->v_angle.y = pev->ideal_yaw; m_timeCamping = 0; m_campDirection = 0; m_nextCampDirTime = 0; m_campButtons = 0; m_soundUpdateTime = 0.0f; m_heardSoundTime = engine->GetTime (); // clear its message queue for (i = 0; i < 32; i++) m_messageQueue[i] = CMENU_IDLE; m_actMessageIndex = 0; m_pushMessageIndex = 0; // and put buying into its message queue PushMessageQueue (CMENU_BUY); PushTask (TASK_NORMAL, TASKPRI_NORMAL, -1, 0.0, true); }
/* do interpreted callback to process message from process */ static void spawn_callback(void *vproc, int err) { spawn_proc *proc = vproc; char *msg = 0; Array *msga; Instruction code[12]; Symbol *ctable; long callback = -1; if (err != 2) { long nbytes = 0; if (proc && proc->proc) callback = err? proc->callerr : proc->callout; if (callback<0) { /* probably a bug, but unclear that calling YError * would prevent the possible fault loop */ return; } /* read the message from process * - this just reads all available bytes, up to 2048 */ msg = p_malloc(2048); nbytes = p_recv(proc->proc, msg, 2048); if (nbytes <= 0) { p_free(msg); /* can get this when an unrelated process finishes, just ignore */ /* YError("spawn process read error in callback"); */ /* also could get this if the fd gave POLLERR, possibly should * find some way to figure out what's going on */ return; } msg = p_realloc(msg, nbytes+1); msg[nbytes] ='\0'; } else { if (!proc || proc->callout<0) return; callback = proc->callout; proc->callout = -1; p_spawf(proc->proc, 1); proc->proc = 0; } if (callback < 0) return; /* task constant table contains only message string */ msga = NewArray(&stringStruct, (Dimension *)0); msga->value.q[0] = msg; ctable = p_malloc(sizeof(Symbol)); ctable->ops = &dataBlockSym; ctable->value.db = (DataBlock *)msga; /* fill in function code */ code[0].Action = &PushVariable; code[1].index = callback; code[2].Action = &PushString; code[3].constant = ctable; code[4].Action = &Eval; code[5].count = 1; code[6].Action = &DropTop; code[7].Action = &PushNil; code[8].Action = &Return; code[9].Action = 0; code[10].index = 11; /* NewFunction moves this to beginning */ code[11].index = Globalize("*callback*", 0L); PushTask(NewFunction(ctable, 1, 0, 0, 0, 0, 2, code, 11)); }
void YpFunc(int isMain, int eol) { long codeSize, i, pc; Function *parsedFunc= 0; DataBlock *oldDB; Symbol *cTable; if (!eol || ypErrors) { if (!eol) YpError("garbage after func or struct definition"); ClearParser((void *)0); return; } if (isMain && nextPC==0) return; /* NOTE- if stackDepth!=0 here, there is a bug in the parser... (unless a previous error involving matrix multiplication?) */ if (previousOp!=&Return) YpReturn(NONE); if (isMain) nLocal= 0; /* end of function marked by code.Action==&Return followed by code.Action==0, followed by code.index==codeSize to enable error recovery to find beginning of function */ if (CheckCodeSpace(2)) { ClearParser((void *)0); return; } vmCode[nextPC++].Action= previousOp= 0; vmCode[nextPC].index= codeSize= 1+nPos+(hasPosList&1)+nKey+nLocal+ nextPC; nextPC++; /* fill in all forward-referenced goto targets */ if (nTarget) { YpError("missing goto label at end of func"); ClearParser((void *)0); return; } for (i=0 ; i<nGotoTargets ; i++) { pc= gotoTargets[i]; SetBranchTarget(pc, (literalTypes[vmCode[pc].index]>>3)); } nGotoTargets= 0; /* shorten constant table to its final size */ if (nConstants) { constantTable= p_realloc(constantTable, nConstants*sizeof(Symbol)); maxConstants= nConstants; } /* fill in all references to constants */ if (!reparsing) cTable= constantTable; else cTable= reparsing->constantTable; for (i=0 ; i<nConstantRefs ; i++) { pc= constantRefs[i]; vmCode[pc].constant= cTable+vmCode[pc].index; } nConstantRefs= 0; /* locate or create all referenced variable names in globTab -- reuse literalTypes array to hold globTab index */ if (CheckCodeSpace(1+nPos+(hasPosList&1)+nKey+nLocal)) { ClearParser((void *)0); return; } if (isMain) { for (i=0 ; i<literalTable.nItems ; i++) if (literalTypes[i]&(L_REFERENCE|L_LOCAL)) literalTypes[i]= Globalize(literalTable.names[i], 0L); vmCode[nextPC++].index= Globalize(isMain==1? "*main*" : literalTable.names[0], 0L); } else { for (i=0 ; i<literalTable.nItems ; i++) { /* Note that function name is first, then positional parameters, then optional *va* parameter, then keyword parameters. */ if (literalTypes[i]&(L_REFERENCE|L_LOCAL)) { if ((literalTypes[i]&(L_REFERENCE|L_LOCAL)) == (L_REFERENCE|L_LOCAL)) vmCode[nextPC++].index= literalTypes[i]= Globalize(literalTable.names[i], 0L); else literalTypes[i]= Globalize(literalTable.names[i], 0L); } } } /* fill in all references to variables */ for (i=0 ; i<nVariableRefs ; i++) { pc= variableRefs[i]; vmCode[pc].index= literalTypes[vmCode[pc].index]; } nVariableRefs= 0; /* done with literal table */ HashClear(&literalTable); /* sets literalTable.maxItems==0 */ p_free(literalTypes); literalTypes= 0; if (!reparsing) parsedFunc= NewFunction(constantTable, nConstants, nPos, nKey, nLocal, hasPosList, maxStackDepth, vmCode, codeSize); else ypReMatch= YpReCompare(reparsing, constantTable, nConstants, nPos, nKey, nLocal, hasPosList, maxStackDepth, vmCode, codeSize); nConstants= maxConstants= 0; constantTable= 0; if (reparsing) return; i= parsedFunc->code[0].index; oldDB= globTab[i].value.db; globTab[i].value.db= (DataBlock *)parsedFunc; if (globTab[i].ops==&dataBlockSym) { Unref(oldDB); } else globTab[i].ops= &dataBlockSym; /* A main function must be pushed onto the stack here; anything else (func or struct definitions) is recorded in the sourceList for the current include file (see also YpExtern). */ if (isMain) PushTask(parsedFunc); else parsedFunc->isrc = RecordSource(i); }
void AutoScaleFramework::PlugTask(TaskPtr& task, queue_type type) { // set executor info for the task task->SetInfo(m_executor_info); PushTask(task, type); }