static void check_test_memory_usage(void) { /* Wait a little bit to let any threads terminate */ usleep(HALF_SECOND_USEC); /* Get the current memory usage */ #ifdef CONFIG_CAN_PASS_STRUCTS g_mmafter = mallinfo(); #else (void)mallinfo(&g_mmafter); #endif /* Show the change from the previous time */ printf("\nEnd of test memory usage:\n"); show_memory_usage(&g_mmprevious, &g_mmafter); /* Set up for the next test */ #ifdef CONFIG_CAN_PASS_STRUCTS g_mmprevious = g_mmafter; #else memcpy(&g_mmprevious, &g_mmafter, sizeof(struct mallinfo)); #endif /* If so enabled, show the use of priority inheritance resources */ dump_nfreeholders("user_main:"); }
void priority_inheritance(void) { #if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) pthread_t lowpri[NLOWPRI_THREADS]; pthread_t medpri; pthread_t highpri[NHIGHPRI_THREADS]; pthread_addr_t result; pthread_attr_t attr; struct sched_param sparam; int my_pri; int status; int i; printf("priority_inheritance: Started\n"); g_middlestate = NOTSTARTED; for (i = 0; i < NHIGHPRI_THREADS; i++) { g_highstate[i] = NOTSTARTED; } for (i = 0; i < NLOWPRI_THREADS; i++) { g_lowstate[i] = NOTSTARTED; } status = sched_getparam(getpid(), &sparam); if (status != 0) { printf("priority_inheritance: sched_getparam failed\n"); sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY; } my_pri = sparam.sched_priority; g_highpri = sched_get_priority_max(SCHED_FIFO); g_lowpri = sched_get_priority_min(SCHED_FIFO); g_medpri = my_pri - 1; sem_init(&g_sem, 0, NLOWPRI_THREADS); dump_nfreeholders("priority_inheritance:"); /* Start the low priority threads */ for (i = 0; i < NLOWPRI_THREADS; i++) { int threadno = i + 1; printf("priority_inheritance: Starting lowpri_thread-%d (of %d) at %d\n", threadno, NLOWPRI_THREADS, g_lowpri); status = pthread_attr_init(&attr); if (status != 0) { printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); } sparam.sched_priority = g_lowpri; status = pthread_attr_setschedparam(&attr, & sparam); if (status != OK) { printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); } else { printf("priority_inheritance: Set lowpri_thread-%d priority to %d\n", threadno, sparam.sched_priority); } status = pthread_create(&lowpri[i], &attr, lowpri_thread, (void *)threadno); if (status != 0) { printf("priority_inheritance: pthread_create failed, status=%d\n", status); } } printf("priority_inheritance: Waiting...\n"); sleep(2); dump_nfreeholders("priority_inheritance:"); /* Start the medium priority thread */ printf("priority_inheritance: Starting medpri_thread at %d\n", g_medpri); status = pthread_attr_init(&attr); if (status != 0) { printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); } sparam.sched_priority = g_medpri; status = pthread_attr_setschedparam(&attr, & sparam); if (status != OK) { printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); } else { printf("priority_inheritance: Set medpri_thread priority to %d\n", sparam.sched_priority); } FFLUSH(); status = pthread_create(&medpri, &attr, medpri_thread, NULL); if (status != 0) { printf("priority_inheritance: pthread_create failed, status=%d\n", status); } printf("priority_inheritance: Waiting...\n"); sleep(1); dump_nfreeholders("priority_inheritance:"); /* Start the high priority threads */ for (i = 0; i < NHIGHPRI_THREADS; i++) { int threadno = i + 1; printf("priority_inheritance: Starting highpri_thread-%d (of %d) at %d\n", threadno, NHIGHPRI_THREADS, g_highpri); status = pthread_attr_init(&attr); if (status != 0) { printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status); } sparam.sched_priority = g_highpri - i; status = pthread_attr_setschedparam(&attr, & sparam); if (status != OK) { printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status); } else { printf("priority_inheritance: Set highpri_thread-%d priority to %d\n", threadno, sparam.sched_priority); } FFLUSH(); status = pthread_create(&highpri[i], &attr, highpri_thread, (void *)threadno); if (status != 0) { printf("priority_inheritance: pthread_create failed, status=%d\n", status); } } dump_nfreeholders("priority_inheritance:"); FFLUSH(); /* Wait for all thread instances to complete */ for (i = 0; i < NHIGHPRI_THREADS; i++) { printf("priority_inheritance: Waiting for highpri_thread-%d to complete\n", i + 1); FFLUSH(); (void)pthread_join(highpri[i], &result); dump_nfreeholders("priority_inheritance:"); } printf("priority_inheritance: Waiting for medpri_thread to complete\n"); FFLUSH(); (void)pthread_join(medpri, &result); dump_nfreeholders("priority_inheritance:"); for (i = 0; i < NLOWPRI_THREADS; i++) { printf("priority_inheritance: Waiting for lowpri_thread-%d to complete\n", i + 1); FFLUSH(); (void)pthread_join(lowpri[i], &result); dump_nfreeholders("priority_inheritance:"); } printf("priority_inheritance: Finished\n"); sem_destroy(&g_sem); dump_nfreeholders("priority_inheritance:"); FFLUSH(); #endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ }