Код управления переключением контекста потока
Для решения проблемы инверсии приоритетов я использовал решение наследования приоритетов с помощью posix-функций в C. (pthread_mutexattr_setprotocol (& mutexInheritAttr,PTHREAD_PRIO_INHERIT)))
Я проверяю, посмотрев на значение счетчика, изменился ли приоритет Thread_Low. Также я также хочу проверить переключатели контекста. Как я могу это сделать?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread_np.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdint.h>
#define LOW_PRIORITY 100
#define MEDIUM_PRIORITY 150
#define HIGH_PRIORITY 200
pthread_t threadLow,threadMedium,threadHigh;
pthread_attr_t threadAttr;
struct sched_param param;
int pid;
pthread_mutex_t mutexInheritence;
uint8_t counter=0;
int getCurrentPriority(pthread_t id);
void *MutexThreadLow(void *p);
void *MutexThreadMedium(void *p);
void *MutexThreadHigh(void *p);
int Counter=0;
int getCurrentPriority(pthread_t id)
{
char buffer[500];
int i=0,prio=0,len=500;
char *pch;
pthread_getinfo_np(id,buffer,len);
pch=strtok(buffer," ");
for(i=0;i<3;i++)
pch=strtok(NULL," ");
prio=atoi(pch);
return prio;
}
void *MutexThreadLow(void *p)
{
int priority;
printf("LowStarted\n\r");
priority=getCurrentPriority(threadLow);
printf("ThreadLow first prio %d \n\r",priority);
if(priority==LOW_PRIORITY){
counter++;}
// prio inheritance lock
if(0!=pthread_mutex_lock(&mutexInheritence))
{
perror("MutexThreadLow : pthread_mutex_lock error\n\r");
pthread_exit((void *)-1);
}
printf("Mutex is Locked by ThreadLow\n\r");
// HIGH
param.sched_priority=HIGH_PRIORITY;
if(0!=pthread_attr_setschedparam(&threadAttr,¶m))
{
perror("test : pthread_attr_setschedparam error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_create(&threadHigh,&threadAttr,MutexThreadHigh,NULL)) // Create low priority thread
{
perror("test : pthread_create error\n\r");
pthread_exit((void *)-1);
}
priority=getCurrentPriority(threadLow);
printf("ThreadLow second prio %d \n\r",priority);
if(priority==HIGH_PRIORITY){
counter++;}
printf("Low unlocks R1 resource \n\r");
if(0!=pthread_mutex_unlock(&mutexInheritence))
{
perror("MutexThreadLow : pthread_mutex_unlock error\n\r");
pthread_exit((void *)-1);
}
priority=getCurrentPriority(threadLow);
printf("ThreadLow last prio %d \n\r",priority);
if(priority==LOW_PRIORITY){
counter++;}
return p;
}
void *MutexThreadMedium(void *p)
{
int i;
printf("MediumStarted\n\r");
return p;
}
void *MutexThreadHigh(void *p)
{
printf("HighStarted\n\r");
// prio inheritance lock
// MEDIUM
param.sched_priority=MEDIUM_PRIORITY;
if(0!=pthread_attr_setschedparam(&threadAttr,¶m))
{
perror("test : pthread_attr_setschedparam error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_create(&threadMedium,&threadAttr,MutexThreadMedium,NULL)) // Create low priority thread
{
perror("test : pthread_create error\n\r");
pthread_exit((void *)-1);
}
printf("MediumCreated\n\r");
printf("High locks R1 resource \n\r");
if(0!=pthread_mutex_lock(&mutexInheritence))
{
perror("MutexThreadHigh : pthread_mutex_lock error\n\r");
pthread_exit((void *)-1);
}
printf("High unlocks R1 resource \n\r");
if(0!=pthread_mutex_unlock(&mutexInheritence))
{
perror("MutexThreadHigh : pthread_mutex_unlock error\n\r");
pthread_exit((void *)-1);
}
return p;
}
int main(void) {
pthread_mutexattr_t mutexInheritAttr;
if(0!=pthread_attr_init(&threadAttr))
{
perror("test : pthread_attr_init error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_mutexattr_init(&mutexInheritAttr))
{
perror("test : pthread_mutexattr_init error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_mutexattr_setprotocol(&mutexInheritAttr,PTHREAD_PRIO_INHERIT))
{
perror("test : pthread_mutexattr_init error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_mutex_init(&mutexInheritence,&mutexInheritAttr))
{
perror("test : pthread_mutex_init error\n\r");
pthread_exit((void *)-1);
}
// LOW
param.sched_priority=LOW_PRIORITY;
if(0!=pthread_attr_setschedparam(&threadAttr,¶m))
{
perror("test : pthread_attr_setschedparam error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_create(&threadLow,&threadAttr,MutexThreadLow,NULL))
//Create low priority thread
{
perror("test : pthread_create error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_join(threadLow,NULL))
{
perror("test : pthread_join error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_join(threadMedium,NULL))
{
perror("test : pthread_join error\n\r");
pthread_exit((void *)-1);
}
if(0!=pthread_join(threadHigh,NULL))
{
perror("test : pthread_join error\n\r");
pthread_exit((void *)-1);
}
if(counter==3)
{
printf("Mutex test pass \n\r");
counter=0;
}
else
printf("Mutex test fail \n\r");
return 0;
}