cvar2.c

来自「Netscape NSPR库源码」· C语言 代码 · 共 1,006 行 · 第 1/3 页

C
1,006
字号
                         count,                         PR_MillisecondsToInterval(50),                         &tcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_TRUE,                         PR_GLOBAL_THREAD);        index++;        list[index].lock = PR_NewLock();        list[index].cvar = PR_NewCondVar(list[index].lock);        CreateTestThread(&list[index],                         index,                         list[index].lock,                         list[index].cvar,                         count,                         PR_MillisecondsToInterval(50),                         &tcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_FALSE,                         PR_LOCAL_THREAD);        index++;        list[index].lock = PR_NewLock();        list[index].cvar = PR_NewCondVar(list[index].lock);        CreateTestThread(&list[index],                         index,                         list[index].lock,                         list[index].cvar,                         count,                         PR_MillisecondsToInterval(50),                         &tcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_FALSE,                         PR_GLOBAL_THREAD);        index++;    }    for (loops = 0; loops < count; loops++) {        /* Wait for threads to finish */        PR_Lock(exitlock);        while(exitcount < arg*4)            PR_WaitCondVar(exitcvar, PR_SecondsToInterval(60));        PR_ASSERT(exitcount >= arg*4);        exitcount -= arg*4;        PR_Unlock(exitlock);    }    /* Join all the threads */    for(index=0; index<(arg*4); index++) {        PR_JoinThread(list[index].thread);        if (list[index].internal) {            PR_Lock(list[index].lock);            PR_DestroyCondVar(list[index].cvar);            PR_Unlock(list[index].lock);            PR_DestroyLock(list[index].lock);        }    }    PR_DestroyCondVar(sharedcvar);    PR_DestroyLock(sharedlock);    PR_DestroyCondVar(exitcvar);    PR_DestroyLock(exitlock);    PR_DELETE(list);}void CondVarMixedTest(void *_arg){    PRInt32 arg = (PRInt32)_arg;    PRInt32 index, loops;    threadinfo *list;    PRLock *sharedlock;    PRCondVar *sharedcvar;    PRLock *exitlock;    PRCondVar *exitcvar;    PRInt32 *ptcount;    exitcount=0;    tcount=0;    list = (threadinfo *)PR_MALLOC(sizeof(threadinfo) * (arg * 4));    ptcount = (PRInt32 *)PR_CALLOC(sizeof(*ptcount) * (arg * 4));    sharedlock = PR_NewLock();    sharedcvar = PR_NewCondVar(sharedlock);    exitlock = PR_NewLock();    exitcvar = PR_NewCondVar(exitlock);    /* Create the threads */    for(index=0; index<arg*4; ) {        CreateTestThread(&list[index],                         index,                         sharedlock,                         sharedcvar,                         count,                         PR_MillisecondsToInterval(50),                         &tcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_TRUE,                         PR_LOCAL_THREAD);        index++;        CreateTestThread(&list[index],                         index,                         sharedlock,                         sharedcvar,                         count,                         PR_MillisecondsToInterval(50),                         &tcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_TRUE,                         PR_GLOBAL_THREAD);        index++;        list[index].lock = PR_NewLock();        list[index].cvar = PR_NewCondVar(list[index].lock);        CreateTestThread(&list[index],                         index,                         list[index].lock,                         list[index].cvar,                         count,                         PR_MillisecondsToInterval(50),                         ptcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_FALSE,                         PR_LOCAL_THREAD);        index++;	ptcount++;        list[index].lock = PR_NewLock();        list[index].cvar = PR_NewCondVar(list[index].lock);        CreateTestThread(&list[index],                         index,                         list[index].lock,                         list[index].cvar,                         count,                         PR_MillisecondsToInterval(50),                         ptcount,                         exitlock,                         exitcvar,                         &exitcount,                         PR_FALSE,                         PR_GLOBAL_THREAD);        index++;	ptcount++;    }    /* Notify every 3rd thread */    for (loops = 0; loops < count; loops++) {        /* Notify the threads */        for(index=0; index<(arg*4); index+=3) {            PR_Lock(list[index].lock);            *list[index].tcount++;            PR_NotifyCondVar(list[index].cvar);            PR_Unlock(list[index].lock);        }        /* Wait for threads to finish */        PR_Lock(exitlock);        while(exitcount < arg*4)            PR_WaitCondVar(exitcvar, PR_SecondsToInterval(60));        PR_ASSERT(exitcount >= arg*4);        exitcount -= arg*4;        PR_Unlock(exitlock);    }    /* Join all the threads */    for(index=0; index<(arg*4); index++) {        PR_JoinThread(list[index].thread);        if (list[index].internal) {            PR_Lock(list[index].lock);            PR_DestroyCondVar(list[index].cvar);            PR_Unlock(list[index].lock);            PR_DestroyLock(list[index].lock);        }    }    PR_DestroyCondVar(sharedcvar);    PR_DestroyLock(sharedlock);    PR_DELETE(list);}void CondVarCombinedTest(void *arg){    PRThread *threads[3];    threads[0] = PR_CreateThread(PR_USER_THREAD,                                 CondVarTest,                                 (void *)arg,                                 PR_PRIORITY_NORMAL,                                 PR_GLOBAL_THREAD,                                 PR_JOINABLE_THREAD,                                 0);    threads[1] = PR_CreateThread(PR_USER_THREAD,                                 CondVarTimeoutTest,                                 (void *)arg,                                 PR_PRIORITY_NORMAL,                                 PR_GLOBAL_THREAD,                                 PR_JOINABLE_THREAD,                                 0);    threads[2] = PR_CreateThread(PR_USER_THREAD,                                 CondVarMixedTest,                                 (void *)arg,                                 PR_PRIORITY_NORMAL,                                 PR_GLOBAL_THREAD,                                 PR_JOINABLE_THREAD,                                 0);    PR_JoinThread(threads[0]);    PR_JoinThread(threads[1]);    PR_JoinThread(threads[2]);}/************************************************************************/static void Measure(void (*func)(void *), PRInt32 arg, const char *msg){    PRIntervalTime start, stop;    double d;    start = PR_IntervalNow();    (*func)((void *)arg);    stop = PR_IntervalNow();    d = (double)PR_IntervalToMicroseconds(stop - start);    printf("%40s: %6.2f usec\n", msg, d / count);}static PRIntn PR_CALLBACK RealMain(int argc, char **argv){    PRInt32 threads, default_threads = DEFAULT_THREADS;	PLOptStatus os;	PLOptState *opt = PL_CreateOptState(argc, argv, "vc:t:");	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))    {		if (PL_OPT_BAD == os) continue;        switch (opt->option)        {        case 'v':  /* debug mode */			_debug_on = 1;            break;        case 'c':  /* loop counter */			count = atoi(opt->value);            break;        case 't':  /* number of threads involved */			default_threads = atoi(opt->value);            break;         default:            break;        }    }	PL_DestroyOptState(opt);    if (0 == count) count = DEFAULT_COUNT;    if (0 == default_threads) default_threads = DEFAULT_THREADS;#ifdef XP_MAC	SetupMacPrintfLog("cvar2.log");#endif    printf("\n\CondVar Test:                                                           \n\                                                                        \n\Simple test creates several local and global threads; half use a single,\n\shared condvar, and the other half have their own condvar.  The main    \n\thread then loops notifying them to wakeup.                             \n\                                                                        \n\The timeout test is very similar except that the threads are not        \n\notified.  They will all wakeup on a 1 second timeout.                  \n\                                                                        \n\The mixed test combines the simple test and the timeout test; every     \n\third thread is notified, the other threads are expected to timeout     \n\correctly.                                                              \n\                                                                        \n\Lastly, the combined test creates a thread for each of the above three  \n\cases and they all run simultaneously.                                  \n\                                                                        \n\This test is run with %d, %d, %d, and %d threads of each type.\n\n",default_threads, default_threads*2, default_threads*3, default_threads*4);    PR_SetConcurrency(2);    for (threads = default_threads; threads < default_threads*5; threads+=default_threads) {        printf("\n%ld Thread tests\n", threads);        Measure(CondVarTestSUU, threads, "Condvar simple test shared UU");        Measure(CondVarTestSUK, threads, "Condvar simple test shared UK");        Measure(CondVarTestPUU, threads, "Condvar simple test priv UU");        Measure(CondVarTestPUK, threads, "Condvar simple test priv UK");#ifdef XP_MAC	/* Mac heaps can't handle thread*4 stack allocations at a time for (10, 15, 20)*4 */        Measure(CondVarTest, 5, "Condvar simple test All");        Measure(CondVarTimeoutTest, 5,  "Condvar timeout test");#else        Measure(CondVarTest, threads, "Condvar simple test All");        Measure(CondVarTimeoutTest, threads,  "Condvar timeout test");#endif#if 0        Measure(CondVarMixedTest, threads,  "Condvar mixed timeout test");        Measure(CondVarCombinedTest, threads, "Combined condvar test");#endif    }    printf("PASS\n");    return 0;}PRIntn main(PRIntn argc, char *argv[]){    PRIntn rv;        PR_STDIO_INIT();    rv = PR_Initialize(RealMain, argc, argv, 0);    return rv;}  /* main */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?