bool DUChainLock::lockForRead(unsigned int timeout) { ///Step 1: Increase the own reader-recursion. This will make sure no further write-locks will succeed d->changeOwnReaderRecursion(1); if(d->m_writer == 0 || d->m_writer == QThread::currentThreadId()) { //Successful lock: Either there is no writer, or we hold the write-lock by ourselves }else{ ///Step 2: Start spinning until there is no writer any more timeval startTime; gettimeofday(&startTime, 0); while(d->m_writer) { timeval currentTime; gettimeofday(¤tTime, 0); timeval waited; timersub(¤tTime, &startTime, &waited); if(!timeout || toMilliSeconds(waited) < timeout) { usleep(uSleepTime); } else { //Fail! d->changeOwnReaderRecursion(-1); return false; } } } return true; }
bool DUChainLock::lockForWrite(uint timeout) { //It is not allowed to acquire a write-lock while holding read-lock Q_ASSERT(d->ownReaderRecursion() == 0); if(d->m_writer == QThread::currentThreadId()) { //We already hold the write lock, just increase the recursion count and return d->m_writerRecursion.fetchAndAddRelaxed(1); return true; } timeval startTime; gettimeofday(&startTime, 0); while(1) { //Try acquiring the write-lcok if(d->m_totalReaderRecursion == 0 && d->m_writerRecursion.testAndSetOrdered(0, 1)) { //Now we can be sure that there is no other writer, as we have increased m_writerRecursion from 0 to 1 d->m_writer = QThread::currentThreadId(); if(d->m_totalReaderRecursion == 0) { //There is still no readers, we have successfully acquired a write-lock return true; }else{ //There may be readers.. we have to continue spinning d->m_writer = 0; d->m_writerRecursion = 0; } } timeval currentTime; gettimeofday(¤tTime, 0); timeval waited; timersub(¤tTime, &startTime, &waited); if(!timeout || toMilliSeconds(waited) < timeout) { usleep(uSleepTime); } else { //Fail! return false; } } return false; }
QString DateTime::toString(const QString &rFormat) const { QString format(rFormat); if (format.isEmpty()) return QString(); if (!isValid()) return QString(); if (format == QLatin1String("NONE")) return QString(); if (format == QLatin1String("RELATIVE")) return QString::number(toMilliSeconds() - InitialisationHelper::startTime()); if (format == QLatin1String("ISO8601")) format = QLatin1String("yyyy-MM-dd hh:mm:ss.zzz"); if (format == QLatin1String("ABSOLUTE")) format = QLatin1String("HH:mm:ss.zzz"); if (format == QLatin1String("DATE")) format = QLatin1String("dd MMM YYYY HH:mm:ss.zzzz"); return formatDateTime(format); }
int main(int argc, char *argv[]) { /* Packet Thread */ /* Initialize the mutex */ pthread_mutex_init(&m, 0); int i; char *lambda = "0.5"; char *mu = "0.35"; char *r = "1.5"; char *B = "10"; char *P = "3"; char *n = "20"; char *FILENAME = NULL; AVAILABLE = 0; DROPPED = 0; DROPPED_PKT = 0; TOTAL = 0; TOTAL_SERVED = 0; SERVER_DIE = 0; /* Read Options */ for(i=1;i<argc;i=i+2){ if (i%2!=0 && argv[i][0]=='-'){ if ((strcmp(argv[i]+1, "lambda") == 0) && ((i+1)<argc)){ lambda = argv[i+1]; if(check_num(lambda)== -1){ fprintf(stderr, "Value of lambda is not a number.\n"); exit(0); } continue; } else if ((strcmp(argv[i]+1, "mu") == 0) && ((i+1)<argc)){ mu = argv[i+1]; if(check_num(mu)== -1){ fprintf(stderr, "Value of mu is not a number.\n"); exit(0); } continue; } else if ((strcmp(argv[i]+1, "r") == 0) && ((i+1)<argc)){ r = argv[i+1]; if(check_num(r)== -1){ fprintf(stderr, "Value of r is not a number.\n"); exit(0); } continue; } else if ((strcmp(argv[i]+1, "B") == 0) && ((i+1)<argc)){ B = argv[i+1]; if(isNum(B)==-1){ fprintf(stderr, "Value of B is not a number.\n"); exit(0); } continue; } else if((strcmp(argv[i]+1, "P") == 0) && ((i+1)<argc)){ P = argv[i+1]; if(isNum(P) == -1){ fprintf(stderr, "Value of P is not a number.\n"); exit(0); } continue; } else if ((strcmp(argv[i]+1, "n") == 0) && ((i+1)<argc)){ n = argv[i+1]; if(isNum(n)==-1){ fprintf(stderr, "Value of n is not a number.\n"); exit(0); } continue; } else if ((strcmp(argv[i]+1, "t") == 0) && ((i+1)<argc)){ FILENAME = argv[i+1]; continue; } } fprintf(stderr, "Wrong command line argument\n"); exit(0); break; } /*Allocate memory to list*/ Q1 = malloc(sizeof(My402List)); Q2 = malloc(sizeof(My402List)); /*Initilialize the list*/ My402ListInit(Q1); My402ListInit(Q2); /* Block Signal from Main thread */ block_signal(); if(FILENAME!=NULL){ FILE *fp = fopen(FILENAME, "r"); if(fp==NULL){ perror("Error: Unable to open the file "); exit(0); } fclose(fp); } print_input(lambda, mu, FILENAME, r, B, P, n); /* Create packet thread */ fprintf(stdout, "\n"); /* Initialize the stats */ init_stats(); struct timeval current = diff_timeval(START_TIMEVAL, PKT_BEFORE); fprintf(stdout, "%08llu.%03ldms: emulation begins\n", toMilliSeconds(current), current.tv_usec%MILLI); /* Create threads */ create_packet_thread(lambda, mu, FILENAME, r, B, P, n); /* Print statistics */ //print_stats(); return(0); }