void DotExpressions::jsAdd(const FunctionCallbackInfo<Value>& args) { TryCatchCpp(args, [&args] { auto pThis = UnwrapThis<DotExpressions>(args.This()); return pThis->AddWorker(args); }); }
int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId) { int rc = EOUTOFMEM; int tempId = -1; long totalJobs; ThreadPoolJob *temp = NULL; if (!tp || !job) return EINVAL; ithread_mutex_lock(&tp->mutex); totalJobs = tp->highJobQ.size + tp->lowJobQ.size + tp->medJobQ.size; if (totalJobs >= tp->attr.maxJobsTotal) { fprintf(stderr, "total jobs = %ld, too many jobs", totalJobs); goto exit_function; } if (!jobId) jobId = &tempId; *jobId = INVALID_JOB_ID; temp = CreateThreadPoolJob(job, tp->lastJobId, tp); if (!temp) goto exit_function; switch (job->priority) { case HIGH_PRIORITY: if (ListAddTail(&tp->highJobQ, temp)) rc = 0; break; case MED_PRIORITY: if (ListAddTail(&tp->medJobQ, temp)) rc = 0; break; default: if (ListAddTail(&tp->lowJobQ, temp)) rc = 0; } /* AddWorker if appropriate */ AddWorker(tp); /* Notify a waiting thread */ if (rc == 0) ithread_cond_signal(&tp->condition); else FreeThreadPoolJob(tp, temp); *jobId = tp->lastJobId++; exit_function: ithread_mutex_unlock(&tp->mutex); return rc; }
void IoServicesImpl::RunWork(const std::function<void(void)>& work) { // Make as many workers as needed to support the concurrent load. if (_freeWorkerCount == 0) { AddWorker(); } // We will take a copy of work. auto workWrapper = [this, work]()->void { try { --_freeWorkerCount; work(); } catch (const std::exception& ex) { _exceptionNotifier("Exception in work: ", ex); } _freeWorkerCount++; }; // MJ Review move. _ioService.post(std::move(workWrapper)); }
/**************************************************************************** * Function: ThreadPoolAdd * * Description: * Adds a job to the thread pool. * Job will be run as soon as possible. * Parameters: * tp - valid thread pool pointer * func - ThreadFunction to run * arg - argument to function. * priority - priority of job. * jobId - id of job * duration - whether or not this is a persistent thread * free_function - function to use when freeing argument * Returns: * 0 on success, nonzero on failure * EOUTOFMEM if not enough memory to add job. *****************************************************************************/ int ThreadPoolAdd( ThreadPool *tp, ThreadPoolJob *job, int *jobId ) { int rc = EOUTOFMEM; int tempId = -1; int totalJobs; ThreadPoolJob *temp = NULL; assert( tp != NULL ); assert( job != NULL ); if( ( tp == NULL ) || ( job == NULL ) ) { return EINVAL; } ithread_mutex_lock( &tp->mutex ); assert( job->priority == LOW_PRIORITY || job->priority == MED_PRIORITY || job->priority == HIGH_PRIORITY ); totalJobs = tp->highJobQ.size + tp->lowJobQ.size + tp->medJobQ.size; if (totalJobs >= tp->attr.maxJobsTotal) { fprintf(stderr, "total jobs = %d, too many jobs", totalJobs); ithread_mutex_unlock( &tp->mutex ); return rc; } if( jobId == NULL ) { jobId = &tempId; } *jobId = INVALID_JOB_ID; temp = CreateThreadPoolJob( job, tp->lastJobId, tp ); if( temp == NULL ) { ithread_mutex_unlock( &tp->mutex ); return rc; } if( job->priority == HIGH_PRIORITY ) { if( ListAddTail( &tp->highJobQ, temp ) ) { rc = 0; } } else if( job->priority == MED_PRIORITY ) { if( ListAddTail( &tp->medJobQ, temp ) ) { rc = 0; } } else { if( ListAddTail( &tp->lowJobQ, temp ) ) { rc = 0; } } // AddWorker if appropriate AddWorker( tp ); // Notify a waiting thread if( rc == 0 ) { ithread_cond_signal( &tp->condition ); } else { FreeThreadPoolJob( tp, temp ); } *jobId = tp->lastJobId++; ithread_mutex_unlock( &tp->mutex ); return rc; }