From b25ae93e1b2c1e66fbe2368b8554bf055e0fa2b5 Mon Sep 17 00:00:00 2001 From: lijiande Date: Fri, 3 Nov 2023 09:28:06 +0800 Subject: [PATCH] commit mutex&read write lock --- mutex/gjb_S0100506GN_1.c | 245 ++++++++++++++++++++++++++++++++ mutex/gjb_S0100506GN_2.c | 98 +++++++++++++ mutex/gjb_S0100509GN_1.c | 88 ++++++++++++ mutex/gjb_S0100509GN_2.c | 219 ++++++++++++++++++++++++++++ readwrite_lock/gjb_S0101001GN.c | 167 ++++++++++++++++++++++ readwrite_lock/gjb_S0101002GN.c | 125 ++++++++++++++++ readwrite_lock/gjb_S0101003GN.c | 93 ++++++++++++ 7 files changed, 1035 insertions(+) create mode 100644 mutex/gjb_S0100506GN_1.c create mode 100644 mutex/gjb_S0100506GN_2.c create mode 100644 mutex/gjb_S0100509GN_1.c create mode 100644 mutex/gjb_S0100509GN_2.c create mode 100644 readwrite_lock/gjb_S0101001GN.c create mode 100644 readwrite_lock/gjb_S0101002GN.c create mode 100644 readwrite_lock/gjb_S0101003GN.c diff --git a/mutex/gjb_S0100506GN_1.c b/mutex/gjb_S0100506GN_1.c new file mode 100644 index 0000000..1a401d5 --- /dev/null +++ b/mutex/gjb_S0100506GN_1.c @@ -0,0 +1,245 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100506GN_1.c +** +** 文件创建日期: 2021 年 1 月 13 日 +** +** 描 述: 互斥量嵌套申请测试, 创建恰当互斥量,同一个任务/进程对其嵌套申请; +** 允许互斥信号量的拥有者多次调用该互斥量 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOCK_TIME 33 +/********************************************************************************************/ +/*********************************** Test case *****************************************/ +/********************************************************************************************/ +int err_count_5061; + +static pthread_mutex_t mtx_06GN; +static sem_t sem_06GN; + +/** child thread function **/ +void * threaded(void * arg) +{ + int ret; + /* Try to lock the mutex once. The call must fail here. */ + ret = pthread_mutex_trylock(&mtx_06GN); + if (ret == 0) { + printf("Child first trylock succeeded,TEST FAILED!\n"); + err_count_5061++; + } + + /* Free the parent thread and lock the mutex (must success)*/ + if ((ret = sem_post(&sem_06GN))) { + printf("1st post sem_06GN in child failed,TEST UNRESOLVED\n"); + err_count_5061++; + return NULL; + } + + /* + * 这里加锁也是成功, 因为允许递归 + */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Child lock failed,TEST UNRESOLVED\n"); + err_count_5061++; + } + + /* + * Wait for the parent to let us go on + */ + if ((ret = sem_post(&sem_06GN))) { + printf("2nd post sem_06GN in child failed,TEST UNRESOLVED\n"); + err_count_5061++; + } + + /* Unlock and exit */ + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Unlock in child failed,TEST UNRESOLVED\n"); + err_count_5061++; + } + + return NULL; +} + +/** parent thread function **/ +int main(int argc, char **argv) +{ + int ret; + int i; + pthread_mutexattr_t ma; + pthread_t child; + + /* + * Initialize the semaphore + */ + if ((ret = sem_init(&sem_06GN, 0, 0))) { + printf("Sem init failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * We initialize the recursive mutex + */ + if ((ret = pthread_mutexattr_init(&ma))) { + printf("Mutex attribute init failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * 设置互斥量允许递归 + */ + if ((ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE))) { + printf("Set type RECURSIVE failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * 初始化互斥量 + */ + if ((ret = pthread_mutex_init(&mtx_06GN, &ma))) { + printf("Recursive mutex init failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * 这里互斥量属性已经被设置, 所以可以被销毁掉了 + */ + if ((ret = pthread_mutexattr_destroy(&ma))) { + printf("Mutex attribute destroy failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* -- The mutex is now ready for testing -- */ + + /* First, we lock it twice and unlock once */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("First lock failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * 再次加锁, 可以成功 + */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Second lock failed, errno=%d.TEST FAILED.\n",errno); + goto __errno_handle; + } else { + printf("second lock success.\n"); + } + + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("First unlock failed, errno=%d.TEST FAILED.\n",errno); + goto __errno_handle; + } + + printf("The mutex has been locked twice and unlocked once, start the thread now.\n"); + + /* Here this thread owns the mutex and the internal count is "1" */ + + /* + * We create the child thread + */ + if ((ret = pthread_create(&child, NULL, threaded, NULL))) { + printf("Unable to create child thread, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * then wait for child to be ready + */ + if ((ret = sem_wait(&sem_06GN))) { + printf("Wait sem_06GN in child failed, errno=%d\n",errno); + goto __errno_handle; + } + + printf("[main] unlock the mutex.\n"); + + /* + * We can now unlock the mutex + */ + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Second unlock failed, errno=%d\n.TEST FAILED.",errno); + goto __errno_handle; + } + + /* We wait for the child to lock the mutex */ + if ((ret = sem_wait(&sem_06GN))) { + printf("Wait sem_06GN in child failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * Then, try to unlock the mutex (owned by the child or unlocked) + * */ + ret = pthread_mutex_unlock(&mtx_06GN); + if (ret == 0) { + printf("Unlock of unowned mutex succeeds.TEST FAILED."); + goto __errno_handle; + } + + /* Everything seems OK here */ + if ((ret = pthread_join(child, NULL))) { + printf("Child join failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* Simple loop to double-check */ + + printf("[main] joined the thread.\n"); + printf("Lock & unlock the mutex 50 times.\n"); + + for (i = 0; i < LOCK_TIME; i++) { + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Lock failed in loop %d..TEST FAILED.\n", i + 1); + goto __errno_handle; + } + } + + for (i = 0; i < LOCK_TIME; i++) { + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Unlock failed in loop %d..TEST FAILED.\n", i + 1); + goto __errno_handle; + } + } + + ret = pthread_mutex_unlock(&mtx_06GN); + if (ret == 0) { + printf("Unlock succeeds after the loop.\n"); + goto __errno_handle; + } + + printf("Everything went OK; destroy the mutex.\n"); + + /* The test passed, we destroy the mutex */ + if ((ret = pthread_mutex_destroy(&mtx_06GN))) { + printf("Final mutex destroy failed, errno=%d\n",errno); + goto __errno_handle; + } + + if (err_count_5061) { + goto __errno_handle; + } + + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100506GN_2.c b/mutex/gjb_S0100506GN_2.c new file mode 100644 index 0000000..5c2b24b --- /dev/null +++ b/mutex/gjb_S0100506GN_2.c @@ -0,0 +1,98 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100506GN_2.c +** +** 文件创建日期: 2021 年 1 月 13 日 +** +** 描 述: 互斥量嵌套申请测试, 创建恰当互斥量,同一个任务/进程对其嵌套申请; +** 允许互斥信号量的拥有者多次调用该互斥量 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int ret; + int i; + pthread_mutexattr_t ma; + pthread_mutex_t mtx; + + /* We initialize the recursive mutex */ + if ((ret = pthread_mutexattr_init(&ma))) { + printf("Mutex attribute init failed, errno=%d\n", errno); + goto __errno_handle; + } + + /* + * 设置互斥量允许递归 + */ + if ((ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE))) { + printf("Set type RECURSIVE failed, errno=%d\n",errno); + goto __errno_handle; + } + + if ((ret = pthread_mutex_init(&mtx, &ma))) { + printf("Recursive mutex init failed, errno=%d\n", errno); + goto __errno_handle; + } + + if ((ret = pthread_mutexattr_destroy(&ma))) { + printf("Mutex attribute destroy failed, errno=%d\n", errno); + goto __errno_handle; + } + + /* -- The mutex is now ready for testing -- */ + + //50次锁定互斥量 + for (i = 0; i < 33; i++) { + if ((ret = pthread_mutex_lock(&mtx))) { + printf("Lock failed in loop %d..TEST FAILED.\n",i+1); + goto __errno_handle; + } + } + + //50次解锁互斥量 + for (i = 0; i < 33; i++) { + if ((ret = pthread_mutex_unlock(&mtx))) { + printf("Unlock failed in loop %d..TEST FAILED.\n",i+1); + goto __errno_handle; + } + } + + //再次释放互斥量,应该返回EPERM (任务无权解锁指定的互斥量, 因为当前任务并未锁定互斥量。) + ret = pthread_mutex_unlock(&mtx); + if (ret == 0) { + printf("Unlock succeeds after the loop,TEST FAILED\n."); + goto __errno_handle; + return -1; + + } else { + printf("Unlock fail after the loop,TEST PASS\n"); + } + + if ((ret = pthread_mutex_destroy(&mtx))) { + printf("Final mutex destroy failed。\n"); + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100509GN_1.c b/mutex/gjb_S0100509GN_1.c new file mode 100644 index 0000000..fd475c8 --- /dev/null +++ b/mutex/gjb_S0100509GN_1.c @@ -0,0 +1,88 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100509GN_1.c +** +** 文件创建日期: 2021 年 1 月 13 日 +** +** 描 述: 给定任务/进程成功申请到互斥量后,调用互斥量释放函数将其释放. +** 测试一般释放和嵌套释放能力; +** 互斥量释放符合预期功能要求, +** 释放互斥量测试(一般释放) +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static pthread_mutex_t mtx_06GN; + +int main(int argc, char **argv) +{ + int ret; + pthread_mutexattr_t ma; + + /* We initialize the recursive mutex */ + if ((ret = pthread_mutexattr_init(&ma))) { + printf("Mutex attribute init failed, errno=%d\n",errno); + goto __errno_handle; + } else { + printf("pthread_mutexattr_init init success\n"); + } + + if ((ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_NORMAL))) { + printf("Set type RECURSIVE failed, errno=%d\n",errno); + goto __errno_handle; + } else { + printf("pthread_mutexattr_settype set PTHREAD_MUTEX_NORMAL success"); + } + + if ((ret = pthread_mutex_init(&mtx_06GN, &ma))) { + printf("Recursive mutex init failed, errno=%d\n", errno); + goto __errno_handle; + } else { + printf("pthread_mutex_init init success\n"); + } + + if ((ret = pthread_mutexattr_destroy(&ma))) { + printf("Mutex attribute destroy failed, errno=%d\n", errno); + goto __errno_handle; + } else { + printf("pthread_mutexattr_destroy destroy success\n"); + } + + /* -- The mutex is now ready for testing -- */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Second lock failed, errno=%d.TEST FAILED.\n", errno); + goto __errno_handle; + + } else { + printf("pthread_mutex_lock lock success"); + } + + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("First unlock failed, errno=%d.TEST FAILED.\n", errno); + goto __errno_handle; + + } else { + printf("pthread_mutex_unlock unlock success\n"); + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/mutex/gjb_S0100509GN_2.c b/mutex/gjb_S0100509GN_2.c new file mode 100644 index 0000000..f52fc88 --- /dev/null +++ b/mutex/gjb_S0100509GN_2.c @@ -0,0 +1,219 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100509GN_2.c +** +** 文件创建日期: 2021 年 1 月 13 日 +** +** 描 述: 给定任务/进程成功申请到互斥量后,调用互斥量释放函数将其释放. +** 测试一般释放和嵌套释放能力; +** 互斥量释放符合预期功能要求, +** +** 释放互斥量测试(嵌套释放) +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define LOCK_TIME 33 +/********************************************************************************************/ +/*********************************** Test case *****************************************/ +/********************************************************************************************/ +int err_count_5092; +static pthread_mutex_t mtx_06GN; +static sem_t sem_06GN; + +/** child thread function **/ +void * threaded_S0100509GN_2(void * arg) +{ + int ret; + + /* Try to lock the mutex once. The call must fail here. */ + ret = pthread_mutex_trylock(&mtx_06GN); + if (ret == 0) { + printf("Child first trylock succeeded,TEST FAILED!\n"); + err_count_5092++; + } + + /* Free the parent thread and lock the mutex (must success)*/ + if ((ret = sem_post(&sem_06GN))) { + printf("1st post sem_06GN in child failed,TEST UNRESOLVED\n"); + err_count_5092++; + } + + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Child lock failed,TEST UNRESOLVED\n"); + err_count_5092++; + } + + /* Wait for the parent to let us go on */ + if ((ret = sem_post(&sem_06GN))) { + printf("2nd post sem_06GN in child failed,TEST UNRESOLVED\n"); + err_count_5092++; + } + + /* Unlock and exit */ + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Unlock in child failed,TEST UNRESOLVED\n"); + err_count_5092++; + } + + return NULL; +} + +/** parent thread function **/ +int main(int argc, char **argv) +{ + int ret; + int i; + pthread_mutexattr_t ma; + pthread_t child; + + /* Initialize the semaphore */ + if ((ret = sem_init(&sem_06GN, 0, 0))) { + printf("Sem init failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* We initialize the recursive mutex */ + if ((ret = pthread_mutexattr_init(&ma))) { + printf("Mutex attribute init failed, errno=%d\n",errno); + goto __errno_handle; + } + + if ((ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE))) { + printf("Set type RECURSIVE failed, errno=%d\n",errno); + goto __errno_handle; + } + + if ((ret = pthread_mutex_init(&mtx_06GN, &ma))) { + printf("Recursive mutex init failed, errno=%d\n",errno); + goto __errno_handle; + } + + if ((ret = pthread_mutexattr_destroy(&ma))) { + printf("Mutex attribute destroy failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* -- The mutex is now ready for testing -- */ + + /* First, we lock it twice and unlock once */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("First lock failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* + * 第二次加锁也是成功 + */ + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Second lock failed, errno=%d.TEST FAILED.\n",errno); + goto __errno_handle; + } + + /* + * 解锁 + */ + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("First unlock failed, errno=%d.TEST FAILED.\n",errno); + goto __errno_handle; + } + + printf("The mutex has been locked twice and unlocked once, start the thread now.\n"); + + + /* Here this thread owns the mutex and the internal count is "1" */ + + /* We create the child thread */ + if ((ret = pthread_create(&child, NULL, threaded_S0100509GN_2, NULL))) { + printf("Unable to create child thread, errno=%d\n",errno); + goto __errno_handle; + } + + /* then wait for child to be ready */ + if ((ret = sem_wait(&sem_06GN))) { + printf("Wait sem_06GN in child failed, errno=%d\n",errno); + goto __errno_handle; + } + + printf("[main] unlock the mutex.\n"); + + /* We can now unlock the mutex */ + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Second unlock failed, errno=%d\n.TEST FAILED.",errno); + goto __errno_handle; + } + + /* We wait for the child to lock the mutex */ + if ((ret = sem_wait(&sem_06GN))) { + printf("Wait sem_06GN in child failed, errno=%d\n",errno); + goto __errno_handle; + } + + /* Then, try to unlock the mutex (owned by the child or unlocked) */ + ret = pthread_mutex_unlock(&mtx_06GN); + if (ret == 0) { + printf("Unlock of unowned mutex succeeds.TEST FAILED."); + goto __errno_handle; + } + + /* Everything seems OK here */ + // 因为多核情况下, 到这个地方, child 线程已经推出了, 所以这里不能判断join返回值 + pthread_join(child, NULL); + + /* Simple loop to double-check */ + + printf("[main] joined the thread.\n"); + printf("Lock & unlock the mutex 50 times.\n"); + + for (i = 0; i < LOCK_TIME; i++) { + if ((ret = pthread_mutex_lock(&mtx_06GN))) { + printf("Lock failed in loop %d..TEST FAILED.\n",i+1); + goto __errno_handle; + } + } + + for (i = 0; i < LOCK_TIME; i++) { + if ((ret = pthread_mutex_unlock(&mtx_06GN))) { + printf("Unlock failed in loop %d..TEST FAILED.\n",i+1); + goto __errno_handle; + } + } + + ret = pthread_mutex_unlock(&mtx_06GN); + if (ret == 0) { + printf("Unlock succeeds after the loop.\n"); + goto __errno_handle; + } + + printf("Everything went OK; destroy the mutex.\n"); + + /* The test passed, we destroy the mutex */ + if ((ret = pthread_mutex_destroy(&mtx_06GN))) { + printf("Final mutex destroy failed, errno=%d\n",errno); + goto __errno_handle; + } + + if (err_count_5092) { + goto __errno_handle; + } + + printf("..................................................[PASS]\n"); + return 0; + +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/readwrite_lock/gjb_S0101001GN.c b/readwrite_lock/gjb_S0101001GN.c new file mode 100644 index 0000000..9df21b9 --- /dev/null +++ b/readwrite_lock/gjb_S0101001GN.c @@ -0,0 +1,167 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101001GN.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 创建和删除读锁功能测试 +*********************************************************************************************************/ +#include +#include +#include +#define ERROR_NONE 0 + + +int main(int argc, char **argv) +{ + int rc = 0; + pthread_rwlock_t rwlock1,rwlock2,rwlock3; + //pthread_rwlockattr_t rwlockattr1 = ((TRUE << 1) | FALSE), rwlockattr2 = ((TRUE << 1) | FALSE); + pthread_rwlockattr_t rwlockattr1 , rwlockattr2; + /* + * 初始化一个读写锁 + */ + if ((rc = pthread_rwlock_init(&rwlock1, NULL)) != 0) { + printf("Failure to initialize rwlock1 using null,rc=%d\n",rc ); + goto __errno_handle; + + } else { + printf("Initialization of rwlock1 using null succeeded,rc=%d\n",rc); + } + + /* + * 加读锁 + */ + if ((rc = pthread_rwlock_rdlock(&rwlock1)) != ERROR_NONE) { + printf("Failed to apply for read lock,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("Successfully applied for read lock,rc=%d\n",rc); + } + + /* + * 解锁 + */ + if ((rc = pthread_rwlock_unlock(&rwlock1)) != ERROR_NONE) { + printf("Failed to release read lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Release the read lock successfully,rc=%d\n",rc); + } + + /* + * 销毁读写锁 + */ + if ((rc =pthread_rwlock_destroy(&rwlock1))!= 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + /* + * 初始化一个读写锁 + */ + //Create and delete read-write locks with rwlockattr1 attribute + if ((rc = pthread_rwlock_init(&rwlock2, &rwlockattr1))!= 0) { + printf("Failure to initialize rwlock2 with rwlockattr1 attribute,rc=%d\n",rc); + goto __errno_handle; + } else { + printf("Initialization of rwlock2 with rwlockattr1 attribute succeeded,rc=%d\n",rc); + } + + /* + * 尝试加一个读写锁 + */ + if ((rc = pthread_rwlock_tryrdlock(&rwlock2)) != ERROR_NONE) { + printf("Failed to apply for read lock,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("Successfully applied for read lock,rc=%d\n",rc); + } + + /* + * 解锁 + */ + if ((rc = pthread_rwlock_unlock(&rwlock2)) != ERROR_NONE) { + printf("Failed to release read lock,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("Release the read lock successfully,rc=%d\n",rc); + } + + /* + * 销毁读写锁 + */ + if ((rc =pthread_rwlock_destroy(&rwlock2))!= 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + struct timespec time_out; + clock_gettime(CLOCK_REALTIME, &time_out); + time_out.tv_sec += 10; + + /* + * 初始化一个读写锁 + */ + //Create and delete read-write locks with rwlockattr2 attribute + if ((rc =pthread_rwlock_init(&rwlock3, &rwlockattr2)) != 0) { + printf("Failure to initialize rwlock3 with rwlockattr2 attribute,rc=%d\n",rc); + goto __errno_handle; + + } else { + printf("Initialization of rwlock3 with rwlockattr2 attribute succeeded,rc=%d\n",rc); + } + + /* + * 带超时的方式加读锁 + */ + if ((rc = pthread_rwlock_timedrdlock(&rwlock3, &time_out)) != ERROR_NONE) { + printf("Failed to apply for read lock,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("Successfully applied for read lock,rc=%d\n",rc); + } + + /* + * 解锁 + */ + if ((rc = pthread_rwlock_unlock(&rwlock3)) != ERROR_NONE) { + printf("Failed to release read lock,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("Release the read lock successfully,rc=%d\n",rc); + } + + /* + * 销毁读写锁 + */ + if ((rc =pthread_rwlock_destroy(&rwlock3)) != 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + printf("..................................................[PASS]\n"); + return 0; +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/readwrite_lock/gjb_S0101002GN.c b/readwrite_lock/gjb_S0101002GN.c new file mode 100644 index 0000000..c9153cd --- /dev/null +++ b/readwrite_lock/gjb_S0101002GN.c @@ -0,0 +1,125 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101002GN.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 创建和删除写锁功能测试。 +*********************************************************************************************************/ +#include +#include +#include +#include +#define ERROR_NONE 0 + +int main(int argc,char **argv) +{ + int rc = 0; + pthread_rwlock_t rwlock1,rwlock2,rwlock3; + + //pthread_rwlockattr_t rwlockattr1=((TRUE << 1) | FALSE), rwlockattr2=((TRUE << 1) | FALSE); + pthread_rwlockattr_t rwlockattr1, rwlockattr2; + + if((rc =pthread_rwlock_init(&rwlock1, NULL))!= 0) { + printf("Failure to initialize rwlock1 using null,rc=%d\n",rc ); + goto __errno_handle; + } else { + printf("Initialization of rwlock1 using null succeeded,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_wrlock(&rwlock1)) != ERROR_NONE) { + printf("Failed to apply for write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Successfully applied for write lock,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_unlock(&rwlock1)) != ERROR_NONE) { + printf("Failed to release write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Release the write lock successfully,rc=%d\n",rc); + } + + if((rc =pthread_rwlock_destroy(&rwlock1))!= 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + + //Create and delete read-write locks with rwlockattr1 attribute + if((rc =pthread_rwlock_init(&rwlock2, &rwlockattr1))!= 0) { + printf("Failure to initialize rwlock2 with rwlockattr1 attribute,rc=%d\n",rc); + goto __errno_handle; + } else { + printf("Initialization of rwlock2 with rwlockattr1 attribute succeeded,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_trywrlock(&rwlock2)) != ERROR_NONE) { + printf("Failed to apply for write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Successfully applied for write lock,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_unlock(&rwlock2)) != ERROR_NONE) { + printf("Failed to release write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Release the write lock successfully,rc=%d\n",rc); + } + + if((rc =pthread_rwlock_destroy(&rwlock2))!= 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + struct timespec time_out; + clock_gettime(CLOCK_REALTIME, &time_out); + time_out.tv_sec += 10; + + //Create and delete read-write locks with rwlockattr2 attribute + if((rc =pthread_rwlock_init(&rwlock3, &rwlockattr2)) != 0) { + printf("Failure to initialize rwlock3 with rwlockattr2 attribute,rc=%d\n",rc); + goto __errno_handle; + } else { + printf("Initialization of rwlock3 with rwlockattr2 attribute succeeded,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_timedwrlock(&rwlock3, &time_out)) != ERROR_NONE) { + printf("Failed to apply for write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Successfully applied for write lock,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_unlock(&rwlock3)) != ERROR_NONE) { + printf("Failed to release write lock,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("Release the write lock successfully,rc=%d\n",rc); + } + + if ((rc = pthread_rwlock_destroy(&rwlock3))!= 0) { + printf("Failed to delete read-write lock rwlock1,rc=%d\n", rc); + goto __errno_handle; + } else { + printf("elete read-write lock rwlock1 succeeded,rc=%d\n",rc); + } + + printf("..................................................[PASS]\n"); + return 0; +__errno_handle: + printf("..................................................[FAILED]\n"); + return -1; +} diff --git a/readwrite_lock/gjb_S0101003GN.c b/readwrite_lock/gjb_S0101003GN.c new file mode 100644 index 0000000..5c497ab --- /dev/null +++ b/readwrite_lock/gjb_S0101003GN.c @@ -0,0 +1,93 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101003GN.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 读写锁发生竞争时的调度策略功能测试 +*********************************************************************************************************/ +#include +#include +#include + +#define SYSTEM_TICKS_USEC 1000 + +pthread_barrier_t barrier; +static int counter; +static pthread_rwlock_t rwlock; + +void *th_write(void *arg) +{ + int t, i; + for (i=0; i<5; i++) { + pthread_rwlock_wrlock(&rwlock); + t = counter; + usleep(100); + printf("Write Thread(%x) counter=%d, ++counter=%d\n", (int)pthread_self(), t, ++counter); + pthread_rwlock_unlock(&rwlock); + usleep(100); + } + + return (NULL); +} + + +void *th_read(void *arg) +{ + while(1) { + pthread_rwlock_rdlock(&rwlock); + printf("Read Thread(%x) counter=%d\n", (int)pthread_self(), counter); + pthread_rwlock_unlock(&rwlock); + usleep(100); + + if (counter == 15) { + pthread_delay(1000 * 1); + break; + } + } + + return (NULL); +} + + +int main(int argc, char **argv) +{ + int i; + pthread_t tid[8]; + + pthread_rwlock_init(&rwlock, NULL); + for (i=0; i<3; i++) + pthread_create(&tid[i], NULL, th_write, NULL); + for (i=0; i<5; i++) + pthread_create(&tid[i+3], NULL, th_read, NULL); + for (i=0; i<8; i++) + pthread_join(tid[i], NULL); + + pthread_rwlock_destroy(&rwlock); + + printf("..................................................[PASS]\n"); + return 0; +} + +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); + } +} \ No newline at end of file -- Gitee