📄 tm_basic.cxx
字号:
// Set my priority lower than any I plan to create
schedparam.sched_priority = 5;
pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam );
// Initiaize thread creation attributes
pthread_attr_init( &attr );
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr, SCHED_RR );
schedparam.sched_priority = 10;
pthread_attr_setschedparam( &attr, &schedparam );
// Set up for full mutex unlock/lock test
pthread_mutexattr_init( &mattr );
pthread_mutex_init(&test_mutexes[0], &mattr);
sem_init(&synchro, 0, 0);
pthread_attr_setstackaddr( &attr, &stacks[0][STACK_SIZE] );
pthread_attr_setstacksize( &attr, STACK_SIZE );
pthread_create( &mutex_test_thread_handle,
&attr,
mutex_test,
(void *)0
);
// Need to raise priority so that this thread will block on the "lock"
schedparam.sched_priority = 20;
pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam );
for (i = 0; i < nmutexes; i++) {
sem_post(&synchro);
pthread_mutex_lock(&test_mutexes[0]);
HAL_CLOCK_READ(&mutex_ft[i].end);
pthread_mutex_unlock(&test_mutexes[0]);
sem_wait(&synchro);
}
pthread_join(mutex_test_thread_handle, &retval);
show_times(mutex_ft, nmutexes, "Unlock/Lock mutex");
}
//--------------------------------------------------------------------------
// Message queue tests
// Currently disabled, pending implementation of POSIX message queues
#if 0
void
run_mbox_tests(void)
{
int i, cnt;
void *item;
// Mailbox primitives
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_create(&test_mbox_handles[i], &test_mboxes[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Create mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cnt = cyg_mbox_peek(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Peek [empty] mbox");
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_put(test_mbox_handles[i], (void *)i);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Put [first] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cnt = cyg_mbox_peek(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Peek [1 msg] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_put(test_mbox_handles[i], (void *)i);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Put [second] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cnt = cyg_mbox_peek(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Peek [2 msgs] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_get(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Get [first] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_get(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Get [second] mbox");
#endif // ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_tryput(test_mbox_handles[i], (void *)i);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Tryput [first] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_peek_item(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Peek item [non-empty] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_tryget(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Tryget [non-empty] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_peek_item(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Peek item [empty] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
item = cyg_mbox_tryget(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Tryget [empty] mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_waiting_to_get(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Waiting to get mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_waiting_to_put(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Waiting to put mbox");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nmboxes; i++) {
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_delete(test_mbox_handles[i]);
HAL_CLOCK_READ(&mbox_ft[i].end);
}
show_times(mbox_ft, nmboxes, "Delete mbox");
run_mbox_circuit_test();
end_of_test_group();
}
//--------------------------------------------------------------------------
void
run_mbox_circuit_test(void)
{
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
int i;
// Set my priority lower than any I plan to create
cyg_thread_set_priority(cyg_thread_self(), 3);
// Set up for full mbox put/get test
cyg_mbox_create(&test_mbox_handles[0], &test_mboxes[0]);
cyg_semaphore_init(&synchro, 0);
cyg_thread_create(2, // Priority - just a number
mbox_test, // entry
0, // index
thread_name("thread", 0), // Name
&stacks[0][0], // Stack
STACK_SIZE, // Size
&mbox_test_thread_handle, // Handle
&mbox_test_thread // Thread data structure
);
cyg_thread_resume(mbox_test_thread_handle);
for (i = 0; i < nmboxes; i++) {
wait_for_tick(); // Wait until the next clock tick to minimize aberations
HAL_CLOCK_READ(&mbox_ft[i].start);
cyg_mbox_put(test_mbox_handles[0], (void *)i);
cyg_semaphore_wait(&synchro);
}
cyg_thread_delete(mbox_test_thread_handle);
show_times(mbox_ft, nmboxes, "Put/Get mbox");
#endif
}
#endif
//--------------------------------------------------------------------------
void
run_semaphore_tests(void)
{
int i;
int sem_val;
// Semaphore primitives
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_init(&test_semaphores[i], 0, 0);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Init semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_post(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Post [0] semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_wait(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Wait [1] semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_trywait(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Trywait [0] semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
sem_post(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_trywait(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Trywait [1] semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_getvalue(&test_semaphores[i], &sem_val);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Get value of semaphore");
wait_for_tick(); // Wait until the next clock tick to minimize aberations
for (i = 0; i < nsemaphores; i++) {
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_destroy(&test_semaphores[i]);
HAL_CLOCK_READ(&semaphore_ft[i].end);
}
show_times(semaphore_ft, nsemaphores, "Destroy semaphore");
run_semaphore_circuit_test();
end_of_test_group();
}
//--------------------------------------------------------------------------
void
run_semaphore_circuit_test(void)
{
int i;
struct sched_param schedparam;
pthread_attr_t attr;
void *retval;
// Set my priority lower than any I plan to create
schedparam.sched_priority = 5;
pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam );
// Initiaize thread creation attributes
pthread_attr_init( &attr );
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
pthread_attr_setschedpolicy( &attr, SCHED_RR );
schedparam.sched_priority = 10;
pthread_attr_setschedparam( &attr, &schedparam );
// Set up for full semaphore post/wait test
sem_init(&test_semaphores[0], 0, 0);
sem_init(&synchro, 0, 0);
pthread_attr_setstackaddr( &attr, &stacks[0][STACK_SIZE] );
pthread_attr_setstacksize( &attr, STACK_SIZE );
pthread_create( &semaphore_test_thread_handle,
&attr,
semaphore_test,
(void *)0
);
for (i = 0; i < nsemaphores; i++) {
wait_for_tick(); // Wait until the next clock tick to minimize aberations
HAL_CLOCK_READ(&semaphore_ft[i].start);
sem_post(&test_semaphores[0]);
sem_wait(&synchro);
}
pthread_join(semaphore_test_thread_handle, &retval);
show_times(semaphore_ft, nsemaphores, "Post/Wait semaphore");
}
//--------------------------------------------------------------------------
// Timer callback function
void
sigrt0(int signo, siginfo_t *info, void *context)
{
diag_printf("sigrt0 called\n");
// empty call back
}
// Callback used to test determinancy
static volatile int timer_cnt;
void
sigrt1(int signo, siginfo_t *info, void *context)
{
if (timer_cnt == nscheds) return;
sched_ft[timer_cnt].start = 0;
HAL_CLOCK_READ(&sched_ft[timer_cnt++].end);
if (timer_cnt == nscheds) {
sem_post(&synchro);
}
}
static sem_t timer_sem;
static void
sigrt2(int signo, siginfo_t *info, void *context)
{
if (timer_cnt == nscheds) {
sem_post(&synchro);
sem_post(&timer_sem);
} else {
sched_ft[timer_cnt].start = 0;
sem_post(&timer_sem);
}
}
// Null thread, used to keep scheduler busy
void *
timer_test(void * id)
{
while (true) {
cyg_thread_yield();
pthread_testcancel();
}
return id;
}
// Thread that suspends itself at the first opportunity
void *
timer_test2(void *id)
{
while (timer_cnt != nscheds) {
HAL_CLOCK_READ(&sched_ft[timer_cnt++].end);
sem_wait(&timer_sem);
}
return id;
}
void
run_timer_tests(void)
{
int res;
int i;
struct sigaction sa;
struct sigevent sigev;
struct itimerspec tp;
// Install signal handlers
sigemptyset( &sa.sa_mask );
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = sigrt0;
sigaction( SIGRTMIN, &sa, NULL );
sa.sa_sigaction = sigrt1;
sigaction( SIGRTMIN+1, &sa, NULL );
sa.sa_sigaction = sigrt2;
sigaction( SIGRTMIN+2, &sa, NULL );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -