Beispiel #1
0
static void *
workerLoop(UA_Worker *worker) {
    UA_Server *server = worker->server;
    UA_UInt32 *counter = &worker->counter;
    volatile UA_Boolean *running = &worker->running;

    /* Initialize the (thread local) random seed with the ram address of worker */
    UA_random_seed((uintptr_t)worker);
    rcu_register_thread();

    pthread_mutex_t mutex; // required for the condition variable
    pthread_mutex_init(&mutex, 0);
    pthread_mutex_lock(&mutex);

    while(*running) {
        struct DispatchJob *dj = (struct DispatchJob*)
            cds_wfcq_dequeue_blocking(&server->dispatchQueue_head, &server->dispatchQueue_tail);
        if(dj) {
            processJob(server, &dj->job);
            UA_free(dj);
        } else {
            /* nothing to do. sleep until a job is dispatched (and wakes up all worker threads) */
            pthread_cond_wait(&server->dispatchQueue_condition, &mutex);
        }
        uatomic_inc(counter);
    }

    pthread_mutex_unlock(&mutex);
    pthread_mutex_destroy(&mutex);
    UA_ASSERT_RCU_UNLOCKED();
    rcu_barrier(); // wait for all scheduled call_rcu work to complete
    rcu_unregister_thread();
    return NULL;
}
DWORD WINAPI ThreadPool::WorkerThread(LPVOID lpParam)
{
	CoInitialize(NULL);
	ThreadPool *obj = (ThreadPool *)lpParam;

	CARBONLOG_CLASS_PTR logger(carbonLogger::getLoggerPtr());
	CARBONLOG_INFO(logger, "[WorkerThread] : starting worker thread now..");

	while(obj->shouldContinue)
	{
		std::string jobMsg, jobOutput;
		if(obj->getJobMsg(jobMsg))
		{
			CARBONLOG_DEBUG(logger, "[WorkerThread] : Processing job ..");
			processJob(jobMsg,jobOutput);
			if(!obj->writeJobOutput(jobOutput))
				CARBONLOG_ERROR(logger, "[WorkerThread] : Failed to write packet to BI");
		}

		else
			Sleep(10);
	}

	CARBONLOG_INFO(logger, "[WorkerThread] : exiting worker thread now..");
	CoUninitialize();

	return 0;

}
Beispiel #3
0
static void
emptyDispatchQueue(UA_Server *server) {
    while(!cds_wfcq_empty(&server->dispatchQueue_head, &server->dispatchQueue_tail)) {
        struct DispatchJob *dj = (struct DispatchJob*)
            cds_wfcq_dequeue_blocking(&server->dispatchQueue_head, &server->dispatchQueue_tail);
        processJob(server, &dj->job);
        UA_free(dj);
    }
}
Beispiel #4
0
workload_result_t processWorkload(int * workload, int workloadSize) {
	workload_result_t result;
	job_eval_result_t jobResult;
	memset(&result, 0, sizeof(workload_result_t));
	// Worst case scenario we get all type 2 jobs and they give us a maximum number of integers.
	int i;
	for(i = 0; i < workloadSize; i++) {
		jobResult = processJob(workload[i]);
		result.integerArray = appendToArray(result.integerArray, jobResult.job_result.job_results, jobResult.job_result.results_size, result.arraySize);
		result.arraySize += jobResult.job_result.results_size;
		switch(workload[i]) {
			case 0:
				result.type0ProcessingTime += jobResult.sleepTime;
				break;
			case 1:
				result.type1ProcessingTime += jobResult.sleepTime;
				break;
			case 2:
				result.type2ProcessingTime += jobResult.sleepTime;
				break;
		}
	}
	return result;
}
/** parse messages (internal or IM messages), they are already decoded */
static F2FError parseBuffer(F2FPeer *srcPeer, F2FPeer *dstPeer, F2FGroup *group, 
		F2FMessageType type, const char * buffer, F2FSize buffersize )
{
	F2FError error;

	/* TODO: evaluate fromPeer to the source Peer sent in the message
	 * for doing the proper routing */
	if( group == NULL ) /* group unknown */
		return F2FErrNotFound;
	/* srcPeer can be null, if it was an invite answer,
	 * where the actual source is not known */
	if( srcPeer != NULL ) /* is in the local peer list */
		srcPeer->lastActivity = time(NULL); // update timestamp
	/* TODO: Check length of the message extensions, should match! */
	/* process different messages */
	F2FSize mesExtLen = buffersize;
	if(mesExtLen > F2FMaxMessageSize)
		mesExtLen = F2FMaxMessageSize;
	const char * mesExt = buffer/* + sizeof( F2FMessageHeader ) obsolet */;
	switch ( type )
	{
	case F2FMessageTypeInviteAnswer:
		error = processInviteMessageAnswer(
					group, srcPeer, dstPeer,
					(F2FMessageInviteAnswer *) mesExt );
		if( error != F2FErrOK ) return error;
		break;
	case F2FMessageTypeInvite:
		error = processInviteMessage(
					group, srcPeer, dstPeer,
					(F2FMessageInvite *) mesExt);
		if( error != F2FErrOK ) return error;
		break;
	case F2FMessageTypeRaw:
		fillAdapterReceiveBuffer(F2FAdapterReceiveMessageTypeData, 
				group->id.hi, group->id.lo, srcPeer->id.hi, srcPeer->id.lo, 
				dstPeer->id.hi, dstPeer->id.lo, type, 
				mesExt, mesExtLen );
		// they are parsed now return F2FErrNotParsed;
		return F2FErrOK;
		break;
	case F2FMessageTypeText:
		fillAdapterReceiveBuffer(F2FAdapterReceiveMessageTypeData, 
				group->id.hi, group->id.lo, srcPeer->id.hi, srcPeer->id.lo, 
				dstPeer->id.hi, dstPeer->id.lo, type, 
				mesExt, mesExtLen );
		// they are parsed now return F2FErrNotParsed;
		return F2FErrOK;
		break;
	case F2FMessageTypeGetJobTicket:
		error = processGetJobTicket(
					group, srcPeer, dstPeer,
					(F2FMessageGetJobTicket *) mesExt);
		if( error != F2FErrOK ) return error;
		break;
	case F2FMessageTypeGetJobTicketAnswer:
		error = processGetJobTicketAnswer(
					group, srcPeer, dstPeer,
					(F2FMessageGetJobTicketAnswer *) mesExt);
		if( error != F2FErrOK ) return error;
		break;
	case F2FMessageTypeJob:
		error = processJob(
					group, srcPeer, dstPeer,
					(F2FMessageJob *) mesExt);
		if( error != F2FErrOK ) return error;
		// they are parsed now else return F2FErrNotParsed;
		return F2FErrOK;
		break;
	case F2FMessageTypeAnnounceNewPeerInGroup:
		error = processAnnounceNewPeerInGroup(
				group, srcPeer, dstPeer,
				(F2FMessageAnnounceNewPeerInGroup *) mesExt);
		if( error != F2FErrOK ) return error;
		break;
	case F2FMessageTypePeerListInfo:
		error = processPeerListInfo(
				group, srcPeer, dstPeer,
				(F2FMessagePeerListInfo *) mesExt);
		if( error != F2FErrOK ) return error;
		break;
	default:
		return F2FErrMessageTypeUnknown;
		break;
	}
	return F2FErrOK;
}