⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutex3.c

📁 ecos实时嵌入式操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
    } while ( now < (then + 3) );#ifdef _POSIX_THREAD_PRIO_INHERIT    CYG_TEST_INFO( "Checking for mutex priority inheritance" );    CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );#else    CYG_TEST_INFO( "Checking for NO mutex priority inheritance" );    CYG_TEST_CHECK( 0 == t3ran, "Thread 3 DID run" );    CYG_TEST_CHECK( 0 == got_it, "Thread 1 DID get the mutex" );#endif    CYG_TEST_CHECK( 0 == t3ended, "Thread 3 ended prematurely [T2,1]" );    for( i = 0; i < DELAYFACTOR * 20; i++ )            nanosleep( &sleeptime, NULL );      // let those threads run      CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );    CYG_TEST_CHECK( 1 == t3ended, "Thread 3 has not ended" );    for ( i = 0; i < 3; i++ )        if ( (1 << i) & (data | data >> 4) ) // bits 0-2 and 4-6 control            CYG_TEST_CHECK( 1 == extras[i+1], "Extra thread did not run" );        else            CYG_TEST_CHECK( 0 == extras[i+1], "Extra thread ran" );    CYG_TEST_PASS( "Thread 2 exiting, AOK" );    // That's all: restart the control thread.    sem_post( &hold[0] );    return 0;    }// ------------------------------------------------------------------------static void *t3( void *arg ){    int i;    int id = (int)arg;    //CYG_ADDRWORD data = thread_data[id];        // Emulate resume behaviour    sem_wait( &hold[id] );    if( all_exit ) return 0;        CYG_TEST_INFO( "Thread 3 running" );    pthread_mutex_lock( &mutex );    for( i = 0; i < DELAYFACTOR * 5; i++ )            nanosleep( &sleeptime, NULL );      // let thread 3a run    sem_post( &hold[2] );               // resume thread 2    while ( 0 == go_flag )        nanosleep( &sleeptime, NULL );  // wait until we are told to go    t3ran ++;                           // record the fact    CYG_TEST_CHECK( 0 == got_it, "Thread 1 claims to have got my mutex" );    pthread_mutex_unlock( &mutex );    t3ended ++;                         // record that we came back    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );    CYG_TEST_INFO( "Thread 3 exit" );    return 0;}// ------------------------------------------------------------------------static void *control_thread( void *arg ){    int i,z;    int id = (int)arg;    //CYG_ADDRWORD data = thread_data[id];        // Emulate resume behaviour    sem_wait( &hold[id] );    if( all_exit ) return 0;        // one tick sleep time    sleeptime.tv_nsec = 10000000;    sleeptime.tv_sec = 0;        CYG_TEST_INIT();    CYG_TEST_INFO( "Control Thread running" );    // Go through the 27 possibilitied of resuming the extra threads    //     0: not at all    //     1: early in the process    //     2: later on    // which are represented by bits 0-3 and 4-6 resp in the argument to    // thread 2 (none set means no resume at all).    for ( i = 0; i < 27; i++ ) {        static int xx[] = { 0, 1, 16 };        int j = i % 3;        int k = (i / 3) % 3;        int l = (i / 9) % 3;        int d = xx[j] | (xx[k]<<1) | (xx[l]<<2) ;        if ( cyg_test_is_simulator && (0 != i && 13 != i && 26 != i) )            continue;    // 13 is 111 base 3, 26 is 222 base 3#ifdef _POSIX_THREAD_PRIO_INHERIT        // If the simple scheme plus relay enhancement, or any other        // *complete* scheme, we can run all three ancillary threads no        // problem, so no special action here.#else        // If no priority inheritance at all, running threads 1a and 2a is        // OK, but not thread 3a; it blocks the world.        if ( l )                        // Cannot run thread 3a if no            break;                      //     priority inheritance at all.#endif        // Reinitialize mutex to provide priority inheritance        {            pthread_mutexattr_t attr;            pthread_mutexattr_init( &attr );            pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );            pthread_mutex_init( &mutex, &attr );        }        got_it  = 0;        t3ran   = 0;        t3ended = 0;        for ( z = 0; z < 4; z++ ) extras[z] = 0;        go_flag = 0;                new_thread( t1, 0, 15, 1 );            // Slot 1        new_thread( t2, d, 10, 1 );            // Slot 2        new_thread( t3, 0,  5, 1 );            // Slot 3                new_thread( extra_thread, 1, 17, j );  // Slot 4        new_thread( extra_thread, 2, 12, k );  // Slot 5        new_thread( extra_thread, 3,  8, l );  // Slot 6                {            static char *a[] = { "inactive", "run early", "run late" };            diag_printf( "\n----- [%2d] New Cycle: 0x%02x, Threads 1a %s, 2a %s, 3a %s -----\n",                         i, d,  a[j], a[k], a[l] );        }        sem_wait( &hold[0] );                kill_threads();        pthread_mutex_destroy( &mutex );    }    CYG_TEST_EXIT( "Control Thread exit" );    return 0;}// ------------------------------------------------------------------------static sem_t main_sem;externC intmain( int argc, char **argv ){     new_thread( control_thread, 0, 20, 1 );    // We have nothing for main to do here, so put it to sleep on    // its own semaphore. We cannot let it just exit since that    // will end the whole program.    sem_init( &main_sem, 0, 0 );        for(;;) sem_wait( &main_sem );}// ------------------------------------------------------------------------// Documentation: enclosed is the design of this test.//// It has been carefully constructed so that it does NOT use other kernel// facilities (aside from delay-task) to test that priority inheritance is// working, or not, as intended by the configuration.//// These notes describe the flow of control in one run of the test with the// ancillary tasks optionally interspersed.  The details of how those extra// tasks are or are not allowed to run are not described.// // // // The only change in the test that depends on whether there is inheritance or// not is the check in thread 2 on "3-ran" and "got it" flags marked ****// // // volatile &c booleans://         "got it"     = FALSE//         "3-ran"      = FALSE//         "3-ended"    = FALSE//         "extras"[3]  = FALSE// // thread 1.  prio 5, self-suspend.// // thread 1a, prio 8, self-suspend.// // thread 2.  prio 10, self-suspend.// // thread 2a, prio 12, self-suspend.// // thread 3.  prio 15, runs, lock mutex, resume(2)// // thread 3a, prio 17, self-suspend.// //        2.  runs,//        2.  resume(3a) +++OPTIONAL//        2.  resume(2a) +++OPTIONAL//        2.  resume(1a) +++OPTIONAL//        [1a lock-fail]	thread 3->prio := 8// //        [3. runs maybe, does the looping thing]// //        2.  sleep a while...// //        [2a lock-fail]	thread 3->prio := 12// //        [3. runs maybe, does the looping thing]// //        [3a lock-fail]   thread 3->prio unchanged// //        [3. runs maybe, does the looping thing]// //        2.  lock scheduler//        2.  set "go-flag"//        2.  resume(1)//        2.  resume(1a) +++OPTIONAL//        2.  resume(2a) +++OPTIONAL//        2.  resume(3a) +++OPTIONAL//        2.  unlock scheduler// //        1.  runs, lock mutex - thread 3 has it locked////        2.  busy-waits a bit for thread 3 to come out of its delay() loop.//            This must be a *busy*wait so that 3 can only run via the//            inherited raised priority.// //        [xa. all do the same: lock mutex,                ]//        [xa. unlock mutex                                ]//        [xa. set a flag "extras"[x] to say we are done.  ]//        [xa. exit                                        ]// // // // INHERIT// -------// //                 thread 3->prio := 5// //        3.  runs,//        3.  set a flag to say "3-ran",//        3.  loop with a sleep(1) until "go-flag" is set.//        3.  check "got it" is false,//        3.  then unlock mutex,// //                 thread 3->prio := 15// //        1.  runs, set a flag to say "got it",//        1.  check "3-ended" flag is false//        1.  unlock mutex,//        1.  check "3-ended" flag is still false//        1.  exit.// //        [1a locks, unlocks, exits]// //        2.  runs, check "3-ran" and "got it" flags are TRUE ****//        2.  check "3-ended" flag is false//        2.  sleeps for a while so that...// //        [2a locks, unlocks, exits]//            //        3.  runs, set "3-ended" flag,//        3.  check "3-ran" and "got it" flags//        3.  exit// //        [3a locks, unlocks, exits]// //        2.  awakens, checks all flags true,//        2.  check that all "extra" threads that we started have indeed run//        2.  end of test.// // // // // NO-INHERIT// ----------//                 thread 1 is waiting on the mutex// //        [1a lock-fail]// //        2.  runs, checks that "3-ran" and "got it" flags are FALSE ****//        2.  check "3-ended" flag is false//        2.  sleeps for a while so that...// //        [2a. lock-fail]//            //        3.  runs, set a flag to say "3-ran",//        3.  check "got it" is false,//        3.  then unlock mutex,// //        1.  runs, set a flag to say "got it",//        1.  check "3-ended" flag is false//        1.  unlock mutex,//        1.  check "3-ended" flag is still false//        1.  exit.// //        [1a locks, unlocks, exits]//        [2a locks, unlocks, exits]// //        3.  runs, set "3-ended" flag,//        3.  check "3-ran" and "got it" flags//        3.  exit// //        [3a locks, unlocks, exits]//                //        2.  awakens, checks all flags true, //        2.  check that all "extra" threads that we started have indeed run//        2.  end of test.// // // (the end)// // // ------------------------------------------------------------------------#endif// EOF mutex3.cxx

⌨️ 快捷键说明

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