From 24f41ab490ec4805197a8193860cb4d4c7eab555 Mon Sep 17 00:00:00 2001 From: lijiande Date: Fri, 27 Oct 2023 09:12:03 +0800 Subject: [PATCH] mutex testcases commit --- Inter_management/gjb_S0101301GN.c | 168 ----- .../gjb_S0101305GN_S0101306GN_2.c | 687 ------------------ mutex/gjb_S0100504GN_1.c | 83 +++ mutex/gjb_S0100504GN_3.c | 94 +++ mutex/gjb_S0100504GN_4.c | 135 ++++ mutex/gjb_S0100505GN_1.c | 83 +++ mutex/gjb_S0100505GN_3.c | 99 +++ mutex/gjb_S0100505GN_4.c | 124 ++++ 8 files changed, 618 insertions(+), 855 deletions(-) delete mode 100644 Inter_management/gjb_S0101301GN.c delete mode 100644 Inter_management/gjb_S0101305GN_S0101306GN_2.c create mode 100644 mutex/gjb_S0100504GN_1.c create mode 100644 mutex/gjb_S0100504GN_3.c create mode 100644 mutex/gjb_S0100504GN_4.c create mode 100644 mutex/gjb_S0100505GN_1.c create mode 100644 mutex/gjb_S0100505GN_3.c create mode 100644 mutex/gjb_S0100505GN_4.c diff --git a/Inter_management/gjb_S0101301GN.c b/Inter_management/gjb_S0101301GN.c deleted file mode 100644 index 8c957cf..0000000 --- a/Inter_management/gjb_S0101301GN.c +++ /dev/null @@ -1,168 +0,0 @@ -/********************************************************************************************************* -** -** GJB 标准测试集 -** -** Copyright All Rights Reserved -** -**--------------文件信息-------------------------------------------------------------------------------- -** -** 文 件 名: gjb_S0101301GN.c -** -** 文件创建日期: 2021 年 1 月 12 日 -** -** 描 述: 中断默认行为测试 (非龙芯平台) -*********************************************************************************************************/ -#ifdef SYLIXOS -#define __SYLIXOS_KERNEL -#endif - -#include "gjb.h" - -static volatile int nCount_S0101301GN[256] = {0}; - -static pthread_attr_t thread_attr5; -static pthread_t thread_id5; - -/* - * 系统中所有中断进入服务函数的时候会自动调用该回调函数 - * 将中断的向量号记录到 数组中,通过查看数组中的值证明中断进入了 - * 注意: 不能直接在中断函数中打印, 因为中断对于整个系统的优先级是最高的 - * 如果打印,则会造成系统持续在中断中, 而影响系统的调度. - */ -typedef struct { - unsigned short offset_low; - unsigned short segment; - unsigned char zero; - unsigned char flags; - unsigned short offset_high; -} __attribute__((packed)) idt_entry_t; - -void default_int_show_action (unsigned long v, unsigned long net) -{ - nCount_S0101301GN[v] = v; -} - -/* - * 这个函数将我们的数组清0, 以防止随机数据影响我们的判断 - */ -void default_int_clear_my_array (void) -{ - int i; - - for (i = 0; i < 256; ++i) { - nCount_S0101301GN[i] = 0; - } -} - -void *Fun5_default_thread (void *arg) -{ - int i, j; - - gjb_int_show_start(default_int_show_action); - - for (j = 0; j < 10; j++) { - for (i = 0; i < 256; ++i) { - if (nCount_S0101301GN[i]) { - gjb_os_printk("find int %lu entry.\n", nCount_S0101301GN[i]); - } - } - - /* - * 将数组中的值清除 - */ - default_int_clear_my_array(); - sleep(1); - } - - gjb_int_show_end(default_int_show_action); - - return NULL; -} -/********************************************************************************************************* - * 函数名称: Test_interrup4_gn - * 功能描述: 缺省中断服务程序测试 - * 输入参数: 无 - * 输出参数: 无 - * 返 回 值: 无 - * 其它说明: 无 - ********************************************************************************************************/ -int Test_interrup4_gn (void) -{ - int res; - - /* - * 初始化一个线程属性对象 - */ - pthread_attr_init(&thread_attr5); - - //thread_attr5.schedparam.sched_priority = 100; - struct sched_param param; - param.sched_priority =100; - pthread_attr_setschedparam(&thread_attr5, ¶m); - - default_int_clear_my_array(); - /* - * 创建一个线程 - */ - res = pthread_create(&thread_id5, &thread_attr5, Fun5_default_thread, NULL); - if (res == 0) { - GJB_PRT_INFO("444--Pass--pthread_create( &thread_id1,&thread_attr5, Fun5, NULL )\n"); - } else { - GJB_PRT_ERROR_INFO("445--Fail--pthread_create( &thread_id1,&thread_attr1, Fun1, NULL )\n"); - goto error; - } - - sleep(5); - - /* - * !!!!!!! 通过命令中断查看打印以确认中断是否进入了 - */ - pthread_join(thread_id5, NULL); - - return (0); - -error: - return (-1); -} -/********************************************************************** - * 函数名称: void usrMain(void) - * 功能描述: 用户程序入口 - * 输入参数: 无 - * 输出参数: 无 - * 返 回 值: 无 - * 其它说明: 无 - **************************************************************************/ -int main(int argc, char **argv) -{ - int result; - -#if !defined(__mips__) && !defined(__mips64) && !defined(__loongarch__) - gjb_os_init_vector(); - - result = Test_interrup4_gn(); /* int install */ - if (result != 0) { - return (GJB_PRI_FAIL()); - } -#else - - GJB_PRT_INFO("loongson platform need exec "); -#endif - - return (GJB_PRI_PASS()); -} - - -int NR_VECTORS=1024; -void gjb_os_init_vector(){ - idt_entry_t idt_table[NR_VECTORS]; // 外部定义 IDT 数组 - int i; - - // 遍历中断向量表 - for (i = 0; i < NR_VECTORS; i++) { - // 设置 IDT 的入口 - idt_table[i].zero = 0; - idt_table[i].flags = 0x8E; // 中断门,特权级为0,存在标志位 - } -} -void gjb_int_show_start(void (*default_int_show_action)()){} -void gjb_int_show_end(void (*default_int_show_action)()){} \ No newline at end of file diff --git a/Inter_management/gjb_S0101305GN_S0101306GN_2.c b/Inter_management/gjb_S0101305GN_S0101306GN_2.c deleted file mode 100644 index 2c06df8..0000000 --- a/Inter_management/gjb_S0101305GN_S0101306GN_2.c +++ /dev/null @@ -1,687 +0,0 @@ -/********************************************************************************************************* -** -** GJB 标准测试集 -** -** Copyright All Rights Reserved -** -**--------------文件信息-------------------------------------------------------------------------------- -** -** 文 件 名: gjb_S0101305GN_S0101306GN_2.c -** -** 文件创建日期: 2021 年 1 月 12 日 -** -** 描 述: 中断 int_enable_pic int_disable_pic 测试 -** -** S0101305GN 测试使能向量中断功能 (单个中断) -** S0101306GN 测试屏蔽向量中断功能 (单个中断) -*********************************************************************************************************/ -#ifdef SYLIXOS -#define __SYLIXOS_KERNEL -#endif - -#include "gjb.h" - -#define VECNUM_START 90 -#define VECNUM_END 125 - -static int nCount1_int__pic = 0; -static int nCount2_int__pic = 0; -static char cstr_int_pic[2] = {'A','B'}; - -/********************************************************************************************************* - * 函数名称: Fun - * 功能描述: 中断处理函数 - * 输入参数: 无 - * 输出参数: 无 - * 返 回 值: 无 - * 其它说明: 无 - ********************************************************************************************************/ -void Fun3 (void *arg) -{ - gjb_os_printk("int triger."); - - nCount1_int__pic++; -} -/********************************************************************************************************* - * 函数名称: Test_interrup3_gn - * 功能描述: 单个中断锁定、解锁 - * 输入参数: 无 - * 输出参数: 无 - * 返 回 值: 无 - * 其它说明: 无 - ********************************************************************************************************/ -int Test_interrup3_gn (void) -{ - int res; /* 函数返回值 */ - char cname[] = "ITR Name"; - int vecnum; /* 中断向量号 */ - int priority; /* 中断优先级 */ - int error = 0; - - vecnum = 4; - priority = 20; - - /* - * 绑定中断处理函数 - */ - nCount1_int__pic = 1; - nCount2_int__pic = 1; - - for (vecnum = VECNUM_START; vecnum < VECNUM_END; vecnum++) { - if (gjb_os_exclude_vector(vecnum)) { - continue; - } - - res = int_install_handler(cname, vecnum, priority, Fun3, cstr_int_pic); - if (res == 0) { - GJB_PRT_INFO("430--Pass--int_install_handler(cname,vecnum,priority,Fun,cstr)\n"); - nCount2_int__pic++; - - } else { - GJB_PRT_ERROR_INFO("431--Fail--int_install_handler(cname,vecnum,priority,Fun,cstr)\tvecnum = %d\n", vecnum); - error++; - } - } - - if(nCount2_int__pic != nCount1_int__pic) { - GJB_PRT_INFO("432--Pass--int_install_handler\n"); - - } else { - GJB_PRT_ERROR_INFO("433--Fail--int_install_handler\n"); - error++; - } - - for (vecnum = VECNUM_START; vecnum < VECNUM_END; vecnum++) { - if (gjb_os_exclude_vector(vecnum)) { - continue; - } - - int_disable_pic(vecnum); - - nCount1_int__pic = 5; - gjb_os_triger_int(vecnum); - - if (nCount1_int__pic == 5) { - GJB_PRT_INFO("435--Pass--int_disable_pic\n"); - - } else { - GJB_PRT_ERROR_INFO("436--Fail--int_disable_pic\n"); - error++; - } - - usleep(50000); - int_enable_pic(vecnum); - usleep(50000); - - if (nCount1_int__pic == 6) { - GJB_PRT_INFO("441--Pass--int_enable_pic\n"); - - } else { - GJB_PRT_ERROR_INFO("442--Fail--int_enable_pic nCount1 = %d\n", nCount1_int__pic); - error++; - } - } - - for (vecnum = VECNUM_START; vecnum < VECNUM_END; vecnum++) { - if (gjb_os_exclude_vector(vecnum)) { - continue; - } - - int_uninstall_handler(vecnum); - } - - if (error) { - return (-1); - } - - return (0); -} -/************************************************************************** - * 函数名称: void usrMain(void) - * 功能描述: 用户程序入口 - * 输入参数: 无 - * 输出参数: 无 - * 返 回 值: 无 - * 其它说明: 无 - **************************************************************************/ -int main(int argc, char **argv) -{ - int result; - - gjb_os_init_vector(); - result = Test_interrup3_gn(); /* int install */ - if (result != 0) { - return (GJB_PRI_FAIL()); - } - - return (GJB_PRI_PASS()); -} - - - - -int g_tty_flag; -int g_tty_fd; -int b_isr_init; -int g_int_lock; -s_isr_attr ISR_REALITY[256]; -int g_lock_trigger_vector; -u64 g_pthread_delay_tick; -int is_set_sys_rate; -u32 g_sys_uleep_tick; - -void* ISR_TTY() -{ - unsigned char buf = '0'; - binding(pthread_self(), 3); - // printf("start ISR_TTY\n"); - // TODO:根据硬件条件修改串口设备 - g_tty_fd = init_com(0, 115200); - send_tty_msg(g_tty_fd, &buf, 1); - while(g_tty_flag) - { - // printf("recv ISR_TTY\n"); - rec_tty_msg(g_tty_fd, &buf, 1); - // printf("recv tty %x\n", buf); - // archIntHandle(58, LW_TRUE); - g_lock_trigger_vector = 58; - sleep(0); - } - g_lock_trigger_vector = -1; - close_com(g_tty_fd); - return NULL; -} - -void init_isr_reality(void) -{ - int i = 0, j = 0; - for (i = 0; i < 256; i++) - { - memset(ISR_REALITY[i].isr_name, 0, 256); - ISR_REALITY[i].isr_num = i; - ISR_REALITY[i].flag = 0; - for(j = 0; j < 16; j++) - { - ISR_REALITY[i].ISR_HANDLE[j]._handler = NULL; - ISR_REALITY[i].ISR_HANDLE[j].args = NULL; - } - ISR_REALITY[i].used = 0; - ISR_REALITY[i].triggerd = 0; - ISR_REALITY[i].enabled = 1; - ISR_REALITY[i].pic_thread = 0; - } - b_isr_init = 1; -} - -int int_install_handler(char *isr_name, int isr_num, int priority, _ISR_HANDLER isr_func, void * args) -{ - // int i = 0; - // int j = 0; - if( isr_num > 255 || isr_num < 0 || isr_func == NULL) - { - errno = EINVAL; - return EINVAL; - } - if ((priority <= 0 ) || (priority >= 100)) - { - errno = EINVAL; - return EINVAL; - } - if( args == NULL|| isr_name == NULL) - { - errno = EINVAL; - return EINVAL; - } - if(b_isr_init == 0) - { - init_isr_reality(); - } - ISR_REALITY[isr_num].isr_num = isr_num; - if(isr_name != NULL) - sprintf(ISR_REALITY[isr_num].isr_name, "%s", isr_name); - ISR_REALITY[isr_num].flag = priority; - // printf("int_install_handler 0 0x%x\n", isr_func); - ISR_REALITY[isr_num].ISR_HANDLE[0]._handler = isr_func; - ISR_REALITY[isr_num].ISR_HANDLE[0].args = args; - ISR_REALITY[isr_num].used = 1; - pid_t pid = getpid(); - struct sched_param param; - param.sched_priority = priority; - sched_setscheduler(pid, SCHED_RR, ¶m);//SCHED_FIFO SCHED_RR - pthread_setschedparam(pthread_self(), SCHED_RR, ¶m); - cpu_set_t mask; - CPU_ZERO(&mask); - CPU_SET(1, &mask); - CPU_SET(2, &mask); - sched_setaffinity(0, sizeof(cpu_set_t), &mask); - // printf("install ISR handler %d %d\n", isr_num, g_tty_flag); - if(isr_num == 58 || g_tty_flag == 0)//???? - { - g_tty_flag = 1; - // pthread_attr_t attr; - // struct sched_param param; - // pthread_attr_init(&attr); - // pthread_attr_setschedpolicy(&attr, SCHED_RR); - // param.sched_priority = 99; - // pthread_attr_setschedparam(&attr, ¶m); - // pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); - - //pthread_create(&tty_t, NULL, ISR_TTY, NULL); - } - return 0; -} - -int int_uninstall_handler(int isr_num) -{ - int flag = 0; - if(isr_num > 255 || isr_num < 0) - return -1; - if(b_isr_init == 0) - { - init_isr_reality(); - } - int j = 0;; - { - for(j = 0; j < 16; j++) - { - if(ISR_REALITY[isr_num].ISR_HANDLE[j]._handler != 0) - flag = 1; - ISR_REALITY[isr_num].ISR_HANDLE[j]._handler = NULL; - ISR_REALITY[isr_num].ISR_HANDLE[j].args = NULL; - } - if(flag == 0) - return -1; - memset(ISR_REALITY[isr_num].isr_name, 0, 256); - ISR_REALITY[isr_num].flag = 0; - - ISR_REALITY[isr_num].used = 0; - ISR_REALITY[isr_num].triggerd = 0; - } - if(isr_num == 58 && g_tty_flag == 1) - { - g_tty_flag = 0; - } - return 0; -} - -int init_com(int com_index, int baud) -{ - int com_fd; - char dev[16]; - - sprintf(dev, "/dev/ttyS%d", com_index); - com_fd = open( dev, O_RDWR ); - if (-1 == com_fd) - { - printf("Can't Open Serial Port"); - return errno; - } - else - { - // printf("init_com ok\n"); - set_speed(com_fd,baud); - if(set_Parity(com_fd,8,1,'N') == -1) - { - printf("set_Parity error\n"); - return errno; - } - } - return com_fd; -} - -int send_tty_msg(int com_fd, unsigned char *buf, int size) -{ - return write(com_fd, buf, size); -} - -int rec_tty_msg(int com_fd,unsigned char *buf,int size) -{ - int err; - fd_set fdRead; - FD_ZERO(&fdRead); - FD_SET(com_fd, &fdRead); - - err = select(com_fd+1, &fdRead, NULL, NULL, NULL); - if(err > 0) - return read(com_fd, buf, 1); - else - return err; -} - -void archIntHandle(int vector, int bPreemptive) -{ - int j = 0; - if(b_isr_init == 0) - { - init_isr_reality(); - } - if(vector < 0 || vector > 255) - return; - // printf("%d %d %#x\n", vector, ISR_REALITY[vector].used, g_int_lock); - if(g_int_lock != UNLOCK_INT) - { - if(ISR_REALITY[vector].used) - { - if(g_lock_trigger_vector == -1) - { - g_lock_trigger_vector = vector; - - } - else - { - ISR_REALITY[g_lock_trigger_vector].triggerd = 0; - } - ISR_REALITY[vector].triggerd = 1; - } - return; - } - if(ISR_REALITY[vector].enabled == 0) - { - ISR_REALITY[vector].triggerd = 1; - return; - } - for(j = 0; j < 16; j++) - { - if(ISR_REALITY[vector].ISR_HANDLE[j]._handler != NULL) - { - // printf("trigger %d %d 0x%x\n", vector, j, ISR_REALITY[vector].ISR_HANDLE[j]._handler); - ISR_REALITY[vector].ISR_HANDLE[j]._handler(ISR_REALITY[vector].ISR_HANDLE[j].args); - } - } -} - -int set_Parity(int fd,int databits,int stopbits,int parity) -{ - struct termios options; - if ( tcgetattr( fd,&options) != 0) - { - perror("SetupSerial 1"); - return(-1); - } - options.c_cflag &= ~CSIZE; - switch (databits) /*设置数据位数*/ - { - case 7: - options.c_cflag |= CS7; - break; - case 8: - options.c_cflag |= CS8; - break; - default: - fprintf(stderr,"Unsupported data sizen"); return (-1); - } - switch (parity) - { - case 'n': - case 'N': - options.c_cflag &= ~PARENB; /* Clear parity enable */ - options.c_iflag &= ~INPCK; /* Enable parity checking */ - break; - case 'o': - case 'O': - options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ - options.c_iflag |= INPCK; /* Disnable parity checking */ - break; - case 'e': - case 'E': - options.c_cflag |= PARENB; /* Enable parity */ - options.c_cflag &= ~PARODD; /* 转换为偶效验*/ - options.c_iflag |= INPCK; /* Disnable parity checking */ - break; - case 'S': - case 's': /*as no parity*/ - options.c_cflag &= ~PARENB; - options.c_cflag &= ~CSTOPB;break; - default: - fprintf(stderr,"Unsupported parityn"); - return (-1); - } - /* 设置停止位*/ - switch (stopbits) - { - case 1: - options.c_cflag &= ~CSTOPB; - break; - case 2: - options.c_cflag |= CSTOPB; - break; - default: - fprintf(stderr,"Unsupported stop bitsn"); - return (-1); - } - /* Set input parity option */ - if (parity != 'n')options.c_iflag |=INPCK; - tcflush(fd,TCIFLUSH); - options.c_cc[VTIME] = 0; /* 设置超时0.2 seconds*/ - options.c_cc[VMIN] = 1; /* Update the options and do it NOW */ - //--------重要------------------------------------------------------ - options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input选择行方式输入:行方式输入是不经处理的*/ - //选择行方式输出 - options.c_oflag &= ~OPOST; /*Output*/ - - //取消软件流控制(不设置可能丢码) - options.c_iflag &=~(IXON | IXOFF | IXANY | ICRNL); - if (tcsetattr(fd,TCSANOW,&options) != 0) - { - perror("SetupSerial 3"); - return (-1); - } - return (0); -} - -void close_com(int com_fd) -{ - close(com_fd); -} - -void binding (pthread_t tid, int cpu) -{ - - cpu_set_t set; - - CPU_ZERO(&set); - CPU_SET(cpu, &set); - - pthread_setaffinity_np(tid, sizeof(set), &set); - -} - -void set_speed(int fd, int speed) -{ - unsigned int i; - int status; - struct termios Opt; - int speed_arr[8] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300}; - int name_arr[8] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300}; - tcgetattr(fd, &Opt); - for ( i= 0; i < 8; i++) - { - if (speed == name_arr[i]) - { - tcflush(fd, TCIOFLUSH); - cfsetispeed(&Opt, speed_arr[i]); - cfsetospeed(&Opt, speed_arr[i]); - status = tcsetattr(fd, TCSANOW, &Opt); - if (status != 0) - { - perror("tcsetattr fd1"); - return; - } - tcflush(fd,TCIOFLUSH); - } - } -} - -int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, - const cpu_set_t *__cpuset) -{ - return 0; -} - -int gjb_os_get_vector(){ - // 示例中断号为0x80,可以根据实际情况修改 - int interrupt_number = 0x78; - uint32_t vector = 0; - uint64_t idt_base; - - // 获取IDT的基地址 - //__asm__ __volatile__("sidt %0" : "=m" (idt_base)); - - // 计算中断向量的地址 - uint64_t vector_address = (idt_base + (interrupt_number * 16)); - - // 读取中断向量的值 - //__asm__ __volatile__("movl %1, %0" : "=r" (vector) : "m" (*(uint32_t*)vector_address)); - - return vector; -} - -void gjb_os_init_vector(){ - init_isr_reality(); -} - -void gjb_trigger_inter (int vector) -{ - archIntHandle(vector, LW_TRUE); -} - -int int_lock() -{ - g_int_lock++; - // if(g_int_lock_thread == 0) - // { - // g_int_lock_thread = 1; - // // printf("ISR int_lock&&&&&&&&&&&&&&&&\n"); - // pthread_create(&isr_t, NULL, ISR_THREAD, NULL); - // // usleep(1000); - // } - return g_int_lock; -} - -int int_unlock(int lock) -{ - int j = 0; - if(g_int_lock == UNLOCK_INT) - return -1; - g_int_lock --; - if(g_int_lock == UNLOCK_INT) - { - if(g_lock_trigger_vector == 58 && ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0]._handler != NULL) - { - ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0]._handler(ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0].args); - g_lock_trigger_vector = -1; - return 0; - } - if(g_lock_trigger_vector != -1) - { - if((ISR_REALITY[g_lock_trigger_vector].flag & SHARED_FLAG) != SHARED_FLAG) - { - if(ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0]._handler != NULL) - { - ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0]._handler(ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[0].args); - } - } - else - { - for(j = 0; j < 16; j++) - { - if(ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[j]._handler != NULL) - { - ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[j]._handler(ISR_REALITY[g_lock_trigger_vector].ISR_HANDLE[j].args); - } - } - } - } - // if(g_lock_trigger_vector != 58) - g_lock_trigger_vector = -1; - // pthread_join(isr_t, NULL); - } -} - -int pthread_delay(int ticks){ - g_pthread_delay_tick = ticks; - sched_yield(); - if(is_set_sys_rate) - { - return usleep(g_sys_uleep_tick * ticks); - } - else - { - return usleep(SYSTEM_TICKS_USEC * ticks); - } -} - -int gjb_exclude_vector (int vector) -{ - /* - * 排除 30 ????向量??, - TODO: 这里??以???加更???, 如果没有需要排除的, 则返?? 0 即可 - */ - - if (vector == 30) { - return (1); - } - - return (0); -} - -int int_enable_pic(int vector) -{ - if(vector > 255 || vector < 0) - return -1; - // printf("int_enable_pic run in line %d\n",__LINE__); - if(ISR_REALITY[vector].enabled == 0) - { - ISR_REALITY[vector].enabled = 1; - // printf("int_enable_pic run in line %d\n",__LINE__); - pthread_join(ISR_REALITY[vector].pic_t, NULL); - } - // printf("int_enable_pic run in line %d\n",__LINE__); - usleep(10000); - return 0; -} - -void* ISR_THREAD_SELF(void *args) -{ - int j = 0; - long long int vector = (long long int)args; - - // printf("ISR_THREAD_SELF********** %d\n", vector); - while(ISR_REALITY[vector].enabled != 1) - { - sleep(0); - } - // printf("ISR enable********** %d %d\n", vector, ISR_REALITY[vector].triggerd); - if(ISR_REALITY[vector].triggerd == 1) - { - for(j = 0; j < 16; j++) - { - // printf("ISR_REALITY[vector].ISR_HANDLE[j]._handler %#x\n", ISR_REALITY[vector].ISR_HANDLE[j]._handler); - if(ISR_REALITY[vector].ISR_HANDLE[j]._handler != NULL) - { - // printf("ISR_THREAD_SELF call %d %d\n", vector, j); - ISR_REALITY[vector].ISR_HANDLE[j]._handler(ISR_REALITY[vector].ISR_HANDLE[j].args); - } - } - } - // printf("ISR int_unlock\n"); - return NULL; -} - -int int_disable_pic(int vector) -{ - if(b_isr_init == 0) - { - init_isr_reality(); - } - if(vector > 255 || vector < 0) - return -1; - ISR_REALITY[vector].enabled = 0; - if(ISR_REALITY[vector].pic_thread == 0) - { - ISR_REALITY[vector].pic_thread = 1; - pthread_create(&ISR_REALITY[vector].pic_t, NULL, ISR_THREAD_SELF,(void *)vector); - } - return 0; -} - diff --git a/mutex/gjb_S0100504GN_1.c b/mutex/gjb_S0100504GN_1.c new file mode 100644 index 0000000..a353bae --- /dev/null +++ b/mutex/gjb_S0100504GN_1.c @@ -0,0 +1,83 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100504GN_1.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 永久等待方式获取互斥量测试。编制驱动代码. 创建恰当的二互斥量, 以永久等待方式申请互斥量; +** 当互斥量未被锁定时, 当前任务/进程锁定互斥量,并成功返回; +** 如果互斥量不可用, 那么任务/进程不返回,直到成功获得互斥量. +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc, char **argv) +{ + int rc = 0; + pthread_mutexattr_t mta; + pthread_mutex_t mutex1; + + /* Initialize mutex attribute objects with default values */ + if ((rc = pthread_mutexattr_init(&mta)) == 0) { + printf("Initialize mutex attribute objects with default values successful.\n"); + } else { + printf("Initialize mutex attribute objects with default values failed!, errno=%d\n",errno); + goto __errno_handle; + } + + /* Initialize the mutex with the default mutex attribute object*/ + if ((rc = pthread_mutex_init(&mutex1, &mta)) == 0) { + printf("Initialize the mutex with the default mutex attribute object successful. TEST PASS.\n"); + } else { + printf("Initialize the mutex with the default mutex attribute object failed! errno=%d.TEST FAILED\n",errno); + pthread_mutexattr_destroy(&mta); + goto __errno_handle; + } + + //lock (获取互斥量, 如果获取不能获取将永久等待) + if ((rc = pthread_mutex_lock(&mutex1)) == 0) { + printf("lock mutex successful. TEST PASS.\n"); + } else { + printf("lock mutex failed! errno=%d.TEST FAILED.\n",errno); + pthread_mutex_destroy(&mutex1); + pthread_mutexattr_destroy(&mta); + goto __errno_handle; + } + + //unlock + if ((rc = pthread_mutex_unlock(&mutex1)) != 0) { + printf("unlock mutex failed. TEST failed.\n"); + goto __errno_handle; + } + + /* Destroy Mutex mutex1*/ + if ((rc = pthread_mutex_destroy(&mutex1)) == 0) { + printf("Destroy Mutex mutex1 successful. TEST PASS.\n"); + } else { + printf("Destroy Mutex mutex1 failed! errno=%d.TEST FAILED\n",errno); + pthread_mutexattr_destroy(&mta); + goto __errno_handle; + } + + pthread_mutexattr_destroy(&mta); + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100504GN_3.c b/mutex/gjb_S0100504GN_3.c new file mode 100644 index 0000000..3c48ac0 --- /dev/null +++ b/mutex/gjb_S0100504GN_3.c @@ -0,0 +1,94 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100504GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 永久等待方式获取互斥量测试。编制驱动代码. 创建恰当的二互斥量, 以永久等待方式申请互斥量; +** 当互斥量未被锁定时, 当前任务/进程锁定互斥量,并成功返回; +** 如果互斥量不可用, 那么任务/进程不返回,直到成功获得互斥量. +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int rc; + pthread_mutex_t mutex; + pthread_mutexattr_t mta; + + pthread_mutexattr_init(&mta); + + /* Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK */ + if ((rc = pthread_mutexattr_settype(&mta,PTHREAD_MUTEX_ERRORCHECK)) != 0) { + printf("Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK failed!, errno=%d\n", errno); + goto __errno_handle; + + } else { + printf("Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK successful.\n"); + } + + if((rc = pthread_mutex_init(&mutex,&mta)) != 0) { + printf("Initialization mutex failed!, errno=%d\n",errno); + goto __errno_handle; + + } else { + printf("Initialization mutex successful.\n"); + } + + /* Locking unlocked mutexes in a blocking manner */ + rc = pthread_mutex_lock(&mutex); + if (rc != 0) { + printf("Locking unlocked mutexes in a blocking manner failed!, errno=%d\n",errno); + pthread_mutex_destroy(&mutex); + goto __errno_handle; + + } else { + printf("Locking unlocked mutexes in a blocking manner successful.\n"); + } + + /* Locking locked mutexes in a blocking manner */ + rc = pthread_mutex_lock(&mutex); + if (rc == 0) { + printf("Locking locked mutexes in a blocking manner successful.TEST FAILED!\n"); + pthread_mutex_destroy(&mutex); + goto __errno_handle; + + } else { + if (EDEADLK == rc) + printf("Locking locked mutexes in a blocking manner failed!, errno=%d=EDEADLK. TEST PASS!\n",rc); + else + printf("Locking locked mutexes in a blocking manner failed!, errno=%d!=EDEADLK. TEST FAILED!\n",errno); + } + + if (pthread_mutex_unlock(&mutex) != 0) { + printf("Unlocking Mutex Failure, errno=%d\n",errno); + goto __errno_handle; + } + + /* Destroy Mutex */ + rc = pthread_mutex_destroy(&mutex); + if (rc != 0) { + printf("Destroy Mutex failed!, errno=%d\n",errno); + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100504GN_4.c b/mutex/gjb_S0100504GN_4.c new file mode 100644 index 0000000..07cb03a --- /dev/null +++ b/mutex/gjb_S0100504GN_4.c @@ -0,0 +1,135 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100504GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 永久等待方式获取互斥量测试。编制驱动代码. 创建恰当的二互斥量, 以永久等待方式申请互斥量; +** 当互斥量未被锁定时, 当前任务/进程锁定互斥量,并成功返回; +** 如果互斥量不可用, 那么任务/进程不返回,直到成功获得互斥量. +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define THREAD_NUM 5 +#define LOOPS 4 +#define SYSTEM_TICKS_USEC 1000 + +static int err_count_5044; +static pthread_mutex_t mutex_04GN1 = PTHREAD_MUTEX_INITIALIZER; +static int value_04GN1 = 0; /* value_04GN1 protected by mutex_04GN1 */ + +void *pthread_mutex_lock_f1(void *parm) +{ + int i; + int tmp = 0; + int rc = 0; + pthread_t self = pthread_self(); + + /* + * Loopd M times to acquire the mutex_04GN1, increase the value_04GN1, + * and then release the mutex_04GN1. + */ + + for (i = 0; i < LOOPS; ++i) { + rc = pthread_mutex_lock(&mutex_04GN1); + if (rc != 0) { + printf("Error on pthread_mutex_lock(), rc=%d\n", rc); + err_count_5044++; + } + + tmp = value_04GN1; + tmp = tmp + 1; + + printf("Thread(0x%p) holds the mutex_04GN1\n",(void*)self); + pthread_delay(10); /* delay the increasement operation */ + + value_04GN1 = tmp; + + rc = pthread_mutex_unlock(&mutex_04GN1); + if (rc != 0) { + printf("Error on pthread_mutex_unlock(), rc=%d\n", rc); + err_count_5044++; + } + + sleep(1); + } + + pthread_exit(0); + return (void*)(0); +} + +int main(int argc, char **argv) +{ + int i; + pthread_attr_t pta; + pthread_t threads[THREAD_NUM]; + + pthread_attr_init(&pta); + pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE); + + /* Create threads */ + printf("Creating %d threads\n", THREAD_NUM); + + for (i = 0; i < THREAD_NUM; i++) { + pthread_create(&threads[i], &pta, pthread_mutex_lock_f1, NULL); + } + + /* Wait to join all threads */ + for (i = 0; i < THREAD_NUM; ++i) { + pthread_join(threads[i], NULL); + } + + pthread_attr_destroy(&pta); + pthread_mutex_destroy(&mutex_04GN1); + + /* Check if the final value_04GN1 is as expected */ + if (value_04GN1 != (THREAD_NUM) * LOOPS) { + printf("Using %d threads and each loops %d times\n", THREAD_NUM, LOOPS); + printf("Final value_04GN1 must be %d instead of %d.TEST FAILED!\n", (THREAD_NUM)*LOOPS, value_04GN1); + goto __errno_handle; + } + + if (err_count_5044) { + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} + + +unsigned long long g_pthread_delay_tick; +int is_set_sys_rate; +unsigned int g_sys_uleep_tick; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} diff --git a/mutex/gjb_S0100505GN_1.c b/mutex/gjb_S0100505GN_1.c new file mode 100644 index 0000000..0edbbc6 --- /dev/null +++ b/mutex/gjb_S0100505GN_1.c @@ -0,0 +1,83 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100505GN_1.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 限时等待方式获取互斥量测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int rc; + + pthread_mutex_t mutex; + pthread_mutexattr_t mta; + + struct timespec timeout; + + //Initialization waiting absolute time + + clock_gettime(CLOCK_REALTIME, &timeout); + + timeout.tv_sec += 3; + timeout.tv_nsec += 0; + + pthread_mutexattr_init(&mta); + + /* Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK */ + if ((rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK)) != 0) { + printf("Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK failed!, errno=%d\n",errno); + goto __errno_handle; + } + + if ((rc = pthread_mutex_init(&mutex, &mta)) != 0) { + printf("Initialization mutex failed!, errno=%d\n",errno); + goto __errno_handle; + } + + /* Locking unlocked mutexes 限时等待方式获取互斥量 */ + rc = pthread_mutex_timedlock(&mutex,&timeout); + if (rc != 0) { + printf("Locking unlocked mutexes in pthread_mutex_timedlock manner failed!, errno=%d. TEST FAILED.\n",errno); + pthread_mutex_destroy(&mutex); + goto __errno_handle; + + } else { + printf("Locking unlocked mutexes in pthread_mutex_timedlock manner successful.TEST PASS.\n"); + } + + if (pthread_mutex_unlock(&mutex) != 0) { + printf("Unlocking Mutex Failure, errno=%d\n",errno); + goto __errno_handle; + } + + /* Destroy Mutex */ + rc = pthread_mutex_destroy(&mutex); + if (rc != 0) { + printf("Destroy Mutex failed!, errno=%d\n",errno); + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100505GN_3.c b/mutex/gjb_S0100505GN_3.c new file mode 100644 index 0000000..4126e32 --- /dev/null +++ b/mutex/gjb_S0100505GN_3.c @@ -0,0 +1,99 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100505GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 限时等待方式获取互斥量测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int rc; + pthread_mutex_t mutex; + pthread_mutexattr_t mta; + //Initialization waiting absolute time + struct timespec timeout; + clock_gettime(CLOCK_REALTIME, &timeout); + timeout.tv_sec += 3; + timeout.tv_nsec += 0; + + pthread_mutexattr_init(&mta); + + /* Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK */ + if ((rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK)) != 0) { + printf("Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK failed!, errno=%d\n", errno); + goto __errno_handle; + + } else { + printf("Set the type attribute of the mutex to PTHREAD_MUTEX_ERRORCHECK successful.\n"); + } + + if ((rc = pthread_mutex_init(&mutex,&mta)) != 0) { + printf("Initialization mutex failed!, errno=%d\n", errno); + goto __errno_handle; + + } else { + printf("Initialization mutex successful.\n"); + } + + /* Locking unlocked mutexes */ + rc = pthread_mutex_timedlock(&mutex,&timeout); + if (rc != 0) { + printf("Locking unlocked mutexes in pthread_mutex_timedlock manner failed!, errno=%d. TEST FAILED.\n", errno); + pthread_mutex_destroy(&mutex); + goto __errno_handle; + + } else { + printf("Locking unlocked mutexes in pthread_mutex_timedlock manner successful.TEST PASS.\n"); + } + + /* Locking locked mutexes */ + rc = pthread_mutex_timedlock(&mutex,&timeout); + if (rc == 0) { + printf("Locking locked mutexes in pthread_mutex_timedlock manner successful.TEST FAILED!\n"); + pthread_mutex_destroy(&mutex); + goto __errno_handle; + + } else { + if (EDEADLK==rc) + printf("Locking locked mutexes in pthread_mutex_timedlock manner failed!, errno=%d=EDEADLK. TEST PASS!\n",errno); + else { + printf("Locking locked mutexes in pthread_mutex_timedlock manner failed!, errno=%d!=EDEADLK. TEST FAILED!\n",errno); + goto __errno_handle; + } + } + + if (pthread_mutex_unlock(&mutex) != 0) { + printf("Unlocking Mutex Failure, errno=%d\n",errno); + goto __errno_handle; + } + + /* Destroy Mutex */ + rc = pthread_mutex_destroy(&mutex); + if (rc != 0) { + printf("Destroy Mutex failed!, errno=%d\n",errno); + goto __errno_handle; + } + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100505GN_4.c b/mutex/gjb_S0100505GN_4.c new file mode 100644 index 0000000..86a432c --- /dev/null +++ b/mutex/gjb_S0100505GN_4.c @@ -0,0 +1,124 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100505GN_4.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 限时等待方式获取互斥量测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TIMEOUT 3 /* 3 seconds of timeout time for pthread_mutex_timedlock(). */ +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* The mutex */ +struct timespec currsec1, currsec2; /* Variables for saving time before and after locking the mutex using pthread_mutex_timedlock(). */ +static int err_count_5054 = 0; + +void *f1(void *parm) +{ + struct timespec timeout; + int rc; + + /* Get the current time before the mutex locked. */ + clock_gettime(CLOCK_REALTIME, &currsec1); + + /* Absolute time, not relative. */ + timeout.tv_sec = currsec1.tv_sec + TIMEOUT; + timeout.tv_nsec = currsec1.tv_nsec ; + + printf("Timed mutex lock will block for %d seconds starting from: %ld.%06ld\n", + TIMEOUT, (long)currsec1.tv_sec, (long)currsec1.tv_nsec); + + /* + * 这里再次获取信号量将获取失败并等待直到超时返回 ETIMEDOUT 错误 + */ + if ((rc = pthread_mutex_timedlock(&mutex, &timeout) ) != ETIMEDOUT) { + printf("Error in pthread_mutex_timedlock(), errno=%d\n",errno); + err_count_5054++; + return (void*)-1; + } + + /* Get time after the mutex timed out in locking. */ + clock_gettime(CLOCK_REALTIME, &currsec2); + + pthread_exit(0); + return (void*)(0); +} + +int main(int argc, char **argv) +{ + pthread_t new_th; + struct timespec time_diff; + + /* Lock the mutex. 首先获取互斥量量 */ + if (pthread_mutex_lock(&mutex) != 0) { + printf("Error in pthread_mutex_lock(). errno=%d\n",errno); + goto __errno_handle; + } + + /* + * Create a thread that will call pthread_mutex_timedlock + */ + if (pthread_create(&new_th, NULL, f1, NULL) != 0) { + printf("Error in pthread_create(), errno=%d\n",errno); + goto __errno_handle; + } + + /* + * Wait for thread to end. + */ + if (pthread_join(new_th, NULL) != 0) { + printf("Error in pthread_join(), errno=%d\n",errno); + goto __errno_handle; + } + + /* Cleaning up the mutexes. */ + if (pthread_mutex_unlock(&mutex) != 0) { + printf("Error in pthread_mutex_unlock(), errno=%d\n",errno); + goto __errno_handle; + } + + if (pthread_mutex_destroy(&mutex) != 0) { + printf("Error in pthread_mutex_destroy(), errno=%d\n",errno); + goto __errno_handle; + } + + /* Compare time before the mutex locked and after the mutex lock timed out. */ + time_diff.tv_sec = currsec2.tv_sec - currsec1.tv_sec; + time_diff.tv_nsec = currsec2.tv_nsec - currsec1.tv_nsec; + + if (time_diff.tv_nsec < 0) { + --time_diff.tv_sec; + time_diff.tv_nsec += 1000000000; + } + + if (time_diff.tv_sec < TIMEOUT) { + printf("Test FAILED: Timed lock did not wait long enough. (%d secs.). TEST FAILED!\n", TIMEOUT); + printf("time before mutex locked: %ld.%06ld, time after mutex timed out: %ld.%06ld.\n", (long)currsec1.tv_sec, (long)currsec1.tv_nsec, (long)currsec2.tv_sec, (long)currsec2.tv_nsec); + goto __errno_handle; + } + + if (err_count_5054) { + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} -- Gitee