Пример #1
0
int main() {
	HDLL dll = loadFunctions();
	if(!dll) {
		return 0;
	}
	Debug::info("dll has loaded");
	//read - reads to buff
	//write - append buff
	for(int i = 0; i<FILE_INPUT_AMOUNT; ++i) {
		Thread threadReader(reader, Thread::packThreadData(inputFiles[i]));
		threadReader.startSync();
		Thread threadWriter(writer, Thread::packThreadData(outFile));
		threadWriter.startSync();
	}
	DllLoader::free(dll);
	cout<<endl;
	return 0;
}
Пример #2
0
int logInit(unsigned logLevel, unsigned flags, const char * filename){
    int des;
    int rc;
    int pipefd[2];// I am using pipe as circular buffer. It is thread safe.
    rc = __sync_val_compare_and_swap(&logMainInfo.isStarted, LOG_STOPPED, LOG_STARTED);
    if ( rc == LOG_STARTED ){
        LOGMESG(LOG_ERROR, "LOG IS CURRENTLY STARTED!");
        logChangeLvl(logLevel);
        logChangeFlags(flags);
        return -1;
    }

    if ( logLevel >= LOG_LEVELS_COUNT || logLevel < LOG_ALL ){
        goto log_exit_0;
    }

    if (filename == NULL){
        des = 2; // stderr
    } else{
        des = open(filename, O_APPEND | O_CREAT | O_WRONLY, 0666);
        if ( des == -1 )
        {
            goto log_exit_0;
        }
    }
    rc = pipe( pipefd );
    if ( rc == -1 ){
        goto log_exit_1;
    }

    logMainInfo.flags = flags;
    logMainInfo.readBufDes = pipefd[0] ;
    logMainInfo.writeBufDes = pipefd[1] ;
    logMainInfo.logLevel = logLevel ;
    logMainInfo.logDes = des ;
    logMainInfo.isStarted = 1;
    gettimeofday(&logMainInfo.startTime,0);

    rc = fork();
    if ( rc == 0 ){ //child
        close(logMainInfo.writeBufDes);
        threadWriter( NULL);
    }else if ( rc > 0){ // parent
        writerProcId = rc;
        close(logMainInfo.readBufDes);
    }else{
        close( pipefd[1]);
        goto log_exit_1;
    }
    LOGMESG(LOG_ERROR,"Log started successfully.");
    return 0;

    log_exit_2:
    close( pipefd[0]);
    close( pipefd[1]);
    log_exit_1:
    close( des);
    log_exit_0:
    logMainInfo.isStarted = LOG_STOPPED;
    return -1;
}