/* threadtest.c * by: john stultz (johnstul@us.ibm.com) * (C) Copyright IBM 2004, 2005, 2006, 2012 * Licensed under the GPLv2 * * To build: * $ gcc threadtest.c -o threadtest -lrt * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.
*/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/time.h> #include <pthread.h> #include"../kselftest.h"
struct timespec global_list[LISTSIZE]; int listcount = 0;
void checklist(conststruct timespec *list, int size)
{ int i, j; conststruct timespec *a, *b;
/* scan the list */ for (i = 0; i < size-1; i++) {
a = &list[i];
b = &list[i+1];
/* look for any time inconsistencies */ if ((b->tv_sec <= a->tv_sec) &&
(b->tv_nsec < a->tv_nsec)) {
/* flag other threads */
done = 1;
/*serialize printing to avoid junky output*/
pthread_mutex_lock(&print_lock);
/* dump the list */
printf("\n"); for (j = 0; j < size; j++) { if (j == i)
printf("---------------\n");
printf("%lu:%lu\n", list[j].tv_sec, list[j].tv_nsec); if (j == i+1)
printf("---------------\n");
}
printf("[FAILED]\n");
pthread_mutex_unlock(&print_lock);
}
}
}
/* The shared thread shares a global list * that each thread fills while holding the lock. * This stresses clock synchronization across cpus.
*/ void *shared_thread(void *arg)
{ while (!done) { /* protect the list */
pthread_mutex_lock(&list_lock);
/* see if we're ready to check the list */ if (listcount >= LISTSIZE) {
checklist(global_list, LISTSIZE);
listcount = 0;
}
clock_gettime(CLOCK_MONOTONIC, &global_list[listcount++]);
/* Each independent thread fills in its own * list. This stresses clock_gettime() lock contention.
*/ void *independent_thread(void *arg)
{ struct timespec my_list[LISTSIZE]; int count;
while (!done) { /* fill the list */ for (count = 0; count < LISTSIZE; count++)
clock_gettime(CLOCK_MONOTONIC, &my_list[count]);
checklist(my_list, LISTSIZE);
} return NULL;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.