Revision 50d6d0b6

View differences:

core/src/aos_system.c
1008 1008

  
1009 1009
/**
1010 1010
 * @brief   Stops the system and all related threads (not the thread this function is called from).
1011
 *
1012
 * @note    Only the control thread (detected by initial thread priority) may call this function.
1011 1013
 */
1012 1014
void aosSysStop(void)
1013 1015
{
1016
  // only the control thread may call this function
1017
  aosDbgAssert(chThdGetPriorityX() == AOS_THD_CTRLPRIO);
1018

  
1019
  // local variables
1020
  tprio_t prio;
1021
  thread_t* tp;
1022
#if (AMIROOS_CFG_DBG == true)
1023
  aos_timestamp_t tprint = 0;
1024
  aos_timestamp_t tnow;
1025
#endif /* (AMIROOS_CFG_DBG == true) */
1026

  
1014 1027
  // wait until the calling thread is the only remaining active thread
1015 1028
#if (CH_CFG_NO_IDLE_THREAD == TRUE)
1016 1029
  while (_numActiveThreads() > 1) {
1017 1030
#else /* (CH_CFG_NO_IDLE_THREAD == TRUE) */
1018 1031
  while (_numActiveThreads() > 2) {
1019 1032
#endif /* (CH_CFG_NO_IDLE_THREAD == TRUE) */
1020
    aosDbgPrintf("waiting for all threads to terminate...\n");
1021
    chThdYield();
1033

  
1034
#if (AMIROOS_CFG_DBG == true)
1035
    aosSysGetUptime(&tnow);
1036
    // print a message once per second
1037
    if (tnow - tprint >= MICROSECONDS_PER_SECOND) {
1038
      aosDbgPrintf("waiting for all threads to terminate...\n");
1039
      tprint = tnow;
1040
    }
1041
#endif /* (AMIROOS_CFG_DBG == true) */
1042

  
1043
    // detect highest priority of all threads but the calling one
1044
    prio = NOPRIO;
1045
    chSysLock();
1046
    tp = aosThreadGetFirst();
1047
    while (tp) {
1048
      // skip the calling thread
1049
      if (tp != chThdGetSelfX()) {
1050
#if (CH_CFG_USE_MUTEXES == TRUE)
1051
        const tprio_t tprio = tp->realprio;
1052
#else /* (CH_CFG_USE_MUTEXES == TRUE) */
1053
        const tprio_t tprio = tp->prio;
1054
#endif /* (CH_CFG_USE_MUTEXES == TRUE) */
1055
        if (tprio > prio) {
1056
          prio = tprio;
1057
        }
1058
      }
1059
      tp = aosThreadGetNext(tp);
1060
    }
1061
    chSysUnlock();
1062

  
1063
    aosDbgAssert(prio <= chThdGetPriorityX());
1064

  
1065
    // lower calling thread priority as required or yield CPU to another thread with equal priority
1066
    if (prio < chThdGetPriorityX()) {
1067
      chThdSetPriority(prio);
1068
    } else {
1069
      chThdYield();
1070
    }
1022 1071
  }
1023 1072

  
1073
  // restore priority
1074
  chThdSetPriority(AOS_THD_CTRLPRIO);
1075

  
1024 1076
  return;
1025 1077
}
1026 1078

  

Also available in: Unified diff