Changeset cb01e1e in mainline for kernel/test/synch
- Timestamp:
- 2009-04-04T00:26:27Z (17 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a85aebd
- Parents:
- 171f9a1
- Location:
- kernel/test/synch
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/test/synch/rwlock1.c
r171f9a1 rcb01e1e 36 36 #include <synch/rwlock.h> 37 37 38 #define READERS 5039 #define WRITERS 5038 #define READERS 50 39 #define WRITERS 50 40 40 41 41 static rwlock_t rwlock; 42 42 43 char * test_rwlock1(bool quiet)43 char *test_rwlock1(void) 44 44 { 45 45 rwlock_initialize(&rwlock); 46 46 47 47 rwlock_write_lock(&rwlock); 48 rwlock_write_unlock(&rwlock); 49 50 rwlock_read_lock(&rwlock); 51 rwlock_read_lock(&rwlock); 48 rwlock_write_unlock(&rwlock); 49 52 50 rwlock_read_lock(&rwlock); 53 51 rwlock_read_lock(&rwlock); 54 52 rwlock_read_lock(&rwlock); 55 53 rwlock_read_lock(&rwlock); 54 rwlock_read_lock(&rwlock); 55 56 56 rwlock_read_unlock(&rwlock); 57 rwlock_read_unlock(&rwlock); 57 rwlock_read_unlock(&rwlock); 58 58 rwlock_read_unlock(&rwlock); 59 59 rwlock_read_unlock(&rwlock); … … 61 61 62 62 rwlock_write_lock(&rwlock); 63 rwlock_write_unlock(&rwlock); 64 63 rwlock_write_unlock(&rwlock); 64 65 65 rwlock_read_lock(&rwlock); 66 66 rwlock_read_unlock(&rwlock); 67 67 68 68 rwlock_write_lock(&rwlock); 69 rwlock_write_unlock(&rwlock); 70 69 rwlock_write_unlock(&rwlock); 70 71 71 rwlock_read_lock(&rwlock); 72 72 rwlock_read_unlock(&rwlock); -
kernel/test/synch/rwlock2.c
r171f9a1 rcb01e1e 35 35 #include <synch/rwlock.h> 36 36 37 #define READERS 5038 #define WRITERS 5037 #define READERS 50 38 #define WRITERS 50 39 39 40 40 static rwlock_t rwlock; 41 static bool sh_quiet;42 41 43 42 static void writer(void *arg) 44 43 { 45 if (!sh_quiet) 46 printf("Trying to lock rwlock for writing....\n"); 44 TPRINTF("Trying to lock rwlock for writing....\n"); 47 45 48 46 rwlock_write_lock(&rwlock); 49 47 rwlock_write_unlock(&rwlock); 50 48 51 if (!sh_quiet) 52 printf("Trying to lock rwlock for reading....\n"); 49 TPRINTF("Trying to lock rwlock for reading....\n"); 53 50 54 51 rwlock_read_lock(&rwlock); 55 rwlock_read_unlock(&rwlock); 52 rwlock_read_unlock(&rwlock); 56 53 } 57 54 58 char * test_rwlock2(bool quiet)55 char *test_rwlock2(void) 59 56 { 60 57 thread_t *thrd; 61 sh_quiet = quiet;62 58 63 59 rwlock_initialize(&rwlock); 64 60 65 61 rwlock_read_lock(&rwlock); 66 62 rwlock_read_lock(&rwlock); 67 63 rwlock_read_lock(&rwlock); 68 rwlock_read_lock(&rwlock); 64 rwlock_read_lock(&rwlock); 69 65 70 66 thrd = thread_create(writer, NULL, TASK, 0, "writer", false); … … 73 69 else 74 70 return "Could not create thread"; 75 71 76 72 thread_sleep(1); 77 73 -
kernel/test/synch/rwlock3.c
r171f9a1 rcb01e1e 35 35 #include <synch/rwlock.h> 36 36 37 #define THREADS 437 #define THREADS 4 38 38 39 39 static atomic_t thread_count; 40 40 static rwlock_t rwlock; 41 static bool sh_quiet;42 41 43 42 static void reader(void *arg) … … 45 44 thread_detach(THREAD); 46 45 47 if (!sh_quiet) 48 printf("cpu%u, tid %" PRIu64 ": trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); 46 TPRINTF("cpu%u, tid %" PRIu64 ": trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); 49 47 50 48 rwlock_read_lock(&rwlock); 51 49 rwlock_read_unlock(&rwlock); 52 50 53 if (!sh_quiet) { 54 printf("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); 55 printf("cpu%u, tid %" PRIu64 ": trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); 56 } 57 51 TPRINTF("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); 52 TPRINTF("cpu%u, tid %" PRIu64 ": trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); 53 58 54 rwlock_write_lock(&rwlock); 59 55 rwlock_write_unlock(&rwlock); 60 56 61 if (!sh_quiet) 62 printf("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); 57 TPRINTF("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); 63 58 64 59 atomic_dec(&thread_count); 65 60 } 66 61 67 char * test_rwlock3(bool quiet)62 char *test_rwlock3(void) 68 63 { 69 64 int i; 70 65 thread_t *thrd; 71 sh_quiet = quiet;72 66 73 67 atomic_set(&thread_count, THREADS); … … 80 74 if (thrd) 81 75 thread_ready(thrd); 82 else if (!quiet)83 printf("Could not create reader %d\n", i);76 else 77 TPRINTF("Could not create reader %d\n", i); 84 78 } 85 79 86 80 thread_sleep(1); 87 81 rwlock_write_unlock(&rwlock); 88 82 89 83 while (atomic_get(&thread_count) > 0) { 90 if (!quiet) 91 printf("Threads left: %ld\n", atomic_get(&thread_count)); 84 TPRINTF("Threads left: %ld\n", atomic_get(&thread_count)); 92 85 thread_sleep(1); 93 86 } -
kernel/test/synch/rwlock4.c
r171f9a1 rcb01e1e 41 41 #include <synch/spinlock.h> 42 42 43 #define READERS 5044 #define WRITERS 5043 #define READERS 50 44 #define WRITERS 50 45 45 46 46 static atomic_t thread_count; 47 47 static rwlock_t rwlock; 48 48 static atomic_t threads_fault; 49 static bool sh_quiet;50 49 51 50 SPINLOCK_INITIALIZE(rw_lock); … … 58 57 { 59 58 uint32_t rc; 60 61 spinlock_lock(&rw_lock); 59 60 spinlock_lock(&rw_lock); 62 61 rc = seed % max; 63 62 seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc; … … 71 70 thread_detach(THREAD); 72 71 waitq_sleep(&can_start); 73 72 74 73 to = random(40000); 75 74 76 if (!sh_quiet) 77 printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); 75 TPRINTF("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); 78 76 79 77 rc = rwlock_write_lock_timeout(&rwlock, to); 80 78 if (SYNCH_FAILED(rc)) { 81 if (!sh_quiet) 82 printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); 79 TPRINTF("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); 83 80 atomic_dec(&thread_count); 84 81 return; 85 82 } 86 83 87 if (!sh_quiet) 88 printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); 89 84 TPRINTF("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); 85 90 86 if (rwlock.readers_in) { 91 if (!sh_quiet) 92 printf("Oops."); 87 TPRINTF("Oops.\n"); 93 88 atomic_inc(&threads_fault); 94 89 atomic_dec(&thread_count); 95 90 return; 96 91 } 92 97 93 thread_usleep(random(1000000)); 94 98 95 if (rwlock.readers_in) { 99 if (!sh_quiet) 100 printf("Oops."); 96 TPRINTF("Oops.\n"); 101 97 atomic_inc(&threads_fault); 102 98 atomic_dec(&thread_count); 103 99 return; 104 100 } 105 101 106 102 rwlock_write_unlock(&rwlock); 107 103 108 if (!sh_quiet) 109 printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); 104 TPRINTF("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); 110 105 atomic_dec(&thread_count); 111 106 } … … 119 114 to = random(2000); 120 115 121 if (!sh_quiet) 122 printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); 116 TPRINTF("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); 123 117 124 118 rc = rwlock_read_lock_timeout(&rwlock, to); 125 119 if (SYNCH_FAILED(rc)) { 126 if (!sh_quiet) 127 printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); 120 TPRINTF("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); 128 121 atomic_dec(&thread_count); 129 122 return; 130 123 } 131 124 132 if (!sh_quiet) 133 printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); 125 TPRINTF("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); 134 126 135 127 thread_usleep(30000); 136 128 rwlock_read_unlock(&rwlock); 137 129 138 if (!sh_quiet) 139 printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); 130 TPRINTF("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); 140 131 atomic_dec(&thread_count); 141 132 } 142 133 143 char * test_rwlock4(bool quiet)134 char *test_rwlock4(void) 144 135 { 145 136 context_t ctx; 146 137 uint32_t i; 147 sh_quiet = quiet;148 138 149 139 waitq_initialize(&can_start); … … 159 149 160 150 context_save(&ctx); 161 if (!quiet) { 162 printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); 163 printf("Creating %" PRIu32 " readers\n", rd); 164 } 151 TPRINTF("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); 152 TPRINTF("Creating %" PRIu32 " readers\n", rd); 165 153 166 154 for (i = 0; i < rd; i++) { … … 168 156 if (thrd) 169 157 thread_ready(thrd); 170 else if (!quiet)171 printf("Could not create reader %" PRIu32 "\n", i);158 else 159 TPRINTF("Could not create reader %" PRIu32 "\n", i); 172 160 } 173 174 if (!quiet) 175 printf("Creating %" PRIu32 " writers\n", wr); 161 162 TPRINTF("Creating %" PRIu32 " writers\n", wr); 176 163 177 164 for (i = 0; i < wr; i++) { … … 179 166 if (thrd) 180 167 thread_ready(thrd); 181 else if (!quiet)182 printf("Could not create writer %" PRIu32 "\n", i);168 else 169 TPRINTF("Could not create writer %" PRIu32 "\n", i); 183 170 } 184 171 … … 187 174 188 175 while (atomic_get(&thread_count) > 0) { 189 if (!quiet) 190 printf("Threads left: %ld\n", atomic_get(&thread_count)); 176 TPRINTF("Threads left: %ld\n", atomic_get(&thread_count)); 191 177 thread_sleep(1); 192 178 } -
kernel/test/synch/rwlock5.c
r171f9a1 rcb01e1e 36 36 #include <synch/rwlock.h> 37 37 38 #define READERS 5039 #define WRITERS 5038 #define READERS 50 39 #define WRITERS 50 40 40 41 41 static rwlock_t rwlock; … … 48 48 { 49 49 thread_detach(THREAD); 50 50 51 51 waitq_sleep(&can_start); 52 52 53 53 rwlock_write_lock(&rwlock); 54 54 atomic_inc(&items_written); … … 59 59 { 60 60 thread_detach(THREAD); 61 61 62 62 waitq_sleep(&can_start); 63 63 … … 67 67 } 68 68 69 char * test_rwlock5(bool quiet)69 char *test_rwlock5(void) 70 70 { 71 71 int i, j, k; … … 77 77 for (i = 1; i <= 3; i++) { 78 78 thread_t *thrd; 79 79 80 80 atomic_set(&items_read, 0); 81 81 atomic_set(&items_written, 0); 82 82 83 83 readers = i * READERS; 84 84 writers = (4 - i) * WRITERS; 85 86 printf("Creating %ld readers and %ld writers...", readers, writers);85 86 TPRINTF("Creating %ld readers and %ld writers...", readers, writers); 87 87 88 88 for (j = 0; j < (READERS + WRITERS) / 2; j++) { … … 92 92 thread_ready(thrd); 93 93 else 94 printf("Could not create reader %d\n", k);94 TPRINTF("Could not create reader %d\n", k); 95 95 } 96 96 for (k = 0; k < (4 - i); k++) { … … 99 99 thread_ready(thrd); 100 100 else 101 printf("Could not create writer %d\n", k);101 TPRINTF("Could not create writer %d\n", k); 102 102 } 103 103 } 104 105 printf("ok\n");106 104 105 TPRINTF("ok\n"); 106 107 107 thread_sleep(1); 108 108 waitq_wakeup(&can_start, WAKEUP_ALL); 109 109 110 110 while ((items_read.count != readers) || (items_written.count != writers)) { 111 printf("%d readers remaining, %d writers remaining, readers_in=%d\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in);111 TPRINTF("%d readers remaining, %d writers remaining, readers_in=%d\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in); 112 112 thread_usleep(100000); 113 113 } -
kernel/test/synch/semaphore1.c
r171f9a1 rcb01e1e 36 36 #include <synch/semaphore.h> 37 37 38 #define AT_ONCE 339 #define PRODUCERS 5040 #define CONSUMERS 5038 #define AT_ONCE 3 39 #define PRODUCERS 50 40 #define CONSUMERS 50 41 41 42 42 static semaphore_t sem; … … 48 48 static void producer(void *arg) 49 49 { 50 thread_detach(THREAD); 51 50 thread_detach(THREAD); 51 52 52 waitq_sleep(&can_start); 53 53 54 54 semaphore_down(&sem); 55 55 atomic_inc(&items_produced); … … 60 60 static void consumer(void *arg) 61 61 { 62 thread_detach(THREAD); 62 thread_detach(THREAD); 63 63 64 64 waitq_sleep(&can_start); … … 70 70 } 71 71 72 char * test_semaphore1(bool quiet)72 char *test_semaphore1(void) 73 73 { 74 74 int i, j, k; … … 77 77 waitq_initialize(&can_start); 78 78 semaphore_initialize(&sem, AT_ONCE); 79 79 80 80 for (i = 1; i <= 3; i++) { 81 81 thread_t *thrd; 82 82 83 83 atomic_set(&items_produced, 0); 84 84 atomic_set(&items_consumed, 0); … … 87 87 producers = (4 - i) * PRODUCERS; 88 88 89 printf("Creating %d consumers and %d producers...", consumers, producers);90 89 TPRINTF("Creating %d consumers and %d producers...", consumers, producers); 90 91 91 for (j = 0; j < (CONSUMERS + PRODUCERS) / 2; j++) { 92 92 for (k = 0; k < i; k++) { … … 95 95 thread_ready(thrd); 96 96 else 97 printf("could not create consumer %d\n", i);97 TPRINTF("could not create consumer %d\n", i); 98 98 } 99 99 for (k = 0; k < (4 - i); k++) { … … 102 102 thread_ready(thrd); 103 103 else 104 printf("could not create producer %d\n", i);104 TPRINTF("could not create producer %d\n", i); 105 105 } 106 106 } 107 108 printf("ok\n");109 107 108 TPRINTF("ok\n"); 109 110 110 thread_sleep(1); 111 111 waitq_wakeup(&can_start, WAKEUP_ALL); 112 112 113 113 while ((items_consumed.count != consumers) || (items_produced.count != producers)) { 114 printf("%d consumers remaining, %d producers remaining\n", consumers - items_consumed.count, producers - items_produced.count);114 TPRINTF("%d consumers remaining, %d producers remaining\n", consumers - items_consumed.count, producers - items_produced.count); 115 115 thread_sleep(1); 116 116 } -
kernel/test/synch/semaphore2.c
r171f9a1 rcb01e1e 51 51 { 52 52 uint32_t rc; 53 54 spinlock_lock(&sem_lock); 53 54 spinlock_lock(&sem_lock); 55 55 rc = seed % max; 56 56 seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc; … … 68 68 69 69 to = random(20000); 70 printf("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to);70 TPRINTF("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to); 71 71 rc = semaphore_down_timeout(&sem, to); 72 72 if (SYNCH_FAILED(rc)) { 73 printf("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid);73 TPRINTF("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid); 74 74 return; 75 75 } 76 76 77 printf("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid);77 TPRINTF("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid); 78 78 thread_usleep(random(30000)); 79 79 80 80 semaphore_up(&sem); 81 printf("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid);81 TPRINTF("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid); 82 82 } 83 83 84 char * test_semaphore2(bool quiet)84 char *test_semaphore2(void) 85 85 { 86 86 uint32_t i, k; … … 92 92 93 93 k = random(7) + 1; 94 printf("Creating %" PRIu32 " consumers\n", k);94 TPRINTF("Creating %" PRIu32 " consumers\n", k); 95 95 for (i = 0; i < k; i++) { 96 96 thrd = thread_create(consumer, NULL, TASK, 0, "consumer", false); … … 98 98 thread_ready(thrd); 99 99 else 100 printf("Error creating thread\n");100 TPRINTF("Error creating thread\n"); 101 101 } 102 102
Note:
See TracChangeset
for help on using the changeset viewer.
