Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | /* * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <ztest.h> #include <pthread.h> #include <sys/util.h> #define N_THR 3 #define STACKSZ (1024 + CONFIG_TEST_EXTRA_STACKSIZE) K_THREAD_STACK_ARRAY_DEFINE(stack, N_THR, STACKSZ); pthread_rwlock_t rwlock; static void *thread_top(void *p1) { pthread_t pthread; u32_t policy, ret = 0U; struct sched_param param; int id = POINTER_TO_INT(p1); pthread = (pthread_t) pthread_self(); pthread_getschedparam(pthread, &policy, ¶m); printk("Thread %d scheduling policy = %d & priority %d started\n", id, policy, param.sched_priority); ret = pthread_rwlock_tryrdlock(&rwlock); if (ret) { printk("Not able to get RD lock on trying, try again\n"); zassert_false(pthread_rwlock_rdlock(&rwlock), "Failed to acquire write lock"); } printk("Thread %d got RD lock\n", id); usleep(USEC_PER_MSEC); printk("Thread %d releasing RD lock\n", id); zassert_false(pthread_rwlock_unlock(&rwlock), "Failed to unlock"); printk("Thread %d acquiring WR lock\n", id); ret = pthread_rwlock_trywrlock(&rwlock); if (ret != 0U) { zassert_false(pthread_rwlock_wrlock(&rwlock), "Failed to acquire WR lock"); } printk("Thread %d acquired WR lock\n", id); usleep(USEC_PER_MSEC); printk("Thread %d releasing WR lock\n", id); zassert_false(pthread_rwlock_unlock(&rwlock), "Failed to unlock"); pthread_exit(NULL); return NULL; } void test_posix_rw_lock(void) { s32_t i, ret; pthread_attr_t attr[N_THR]; struct sched_param schedparam; pthread_t newthread[N_THR]; struct timespec time; void *status; time.tv_sec = 1; time.tv_nsec = 0; zassert_equal(pthread_rwlock_destroy(&rwlock), EINVAL, NULL); zassert_equal(pthread_rwlock_rdlock(&rwlock), EINVAL, NULL); zassert_equal(pthread_rwlock_wrlock(&rwlock), EINVAL, NULL); zassert_equal(pthread_rwlock_trywrlock(&rwlock), EINVAL, NULL); zassert_equal(pthread_rwlock_tryrdlock(&rwlock), EINVAL, NULL); zassert_equal(pthread_rwlock_timedwrlock(&rwlock, &time), EINVAL, NULL); zassert_equal(pthread_rwlock_timedrdlock(&rwlock, &time), EINVAL, NULL); zassert_equal(pthread_rwlock_unlock(&rwlock), EINVAL, NULL); zassert_false(pthread_rwlock_init(&rwlock, NULL), "Failed to create rwlock"); printk("\nmain acquire WR lock and 3 threads acquire RD lock\n"); zassert_false(pthread_rwlock_timedwrlock(&rwlock, &time), "Failed to acquire write lock"); /* Creating N premptive threads in increasing order of priority */ for (i = 0; i < N_THR; i++) { zassert_equal(pthread_attr_init(&attr[i]), 0, "Unable to create pthread object attrib"); /* Setting scheduling priority */ schedparam.sched_priority = i + 1; pthread_attr_setschedparam(&attr[i], &schedparam); /* Setting stack */ pthread_attr_setstack(&attr[i], &stack[i][0], STACKSZ); ret = pthread_create(&newthread[i], &attr[i], thread_top, INT_TO_POINTER(i)); zassert_false(ret, "Low memory to thread new thread"); } /* Delay to give change to child threads to run */ usleep(USEC_PER_MSEC); printk("Parent thread releasing WR lock\n"); zassert_false(pthread_rwlock_unlock(&rwlock), "Failed to unlock"); /* Let child threads acquire RD Lock */ usleep(USEC_PER_MSEC); printk("Parent thread acquiring WR lock again\n"); time.tv_sec = 2; time.tv_nsec = 0; ret = pthread_rwlock_timedwrlock(&rwlock, &time); if (ret) { zassert_false(pthread_rwlock_wrlock(&rwlock), "Failed to acquire write lock"); } printk("Parent thread acquired WR lock again\n"); usleep(USEC_PER_MSEC); printk("Parent thread releasing WR lock again\n"); zassert_false(pthread_rwlock_unlock(&rwlock), "Failed to unlock"); printk("\n3 threads acquire WR lock\n"); printk("Main thread acquiring RD lock\n"); ret = pthread_rwlock_timedrdlock(&rwlock, &time); if (ret != 0) { zassert_false(pthread_rwlock_rdlock(&rwlock), "Failed to lock"); } printk("Main thread acquired RD lock\n"); usleep(USEC_PER_MSEC); printk("Main thread releasing RD lock\n"); zassert_false(pthread_rwlock_unlock(&rwlock), "Failed to unlock"); for (i = 0; i < N_THR; i++) { zassert_false(pthread_join(newthread[i], &status), "Failed to join"); } zassert_false(pthread_rwlock_destroy(&rwlock), "Failed to destroy rwlock"); } |