Rev 2050 | Rev 2071 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2050 | Rev 2053 | ||
|---|---|---|---|
| Line 32... | Line 32... | ||
| 32 | #include <print.h> |
32 | #include <print.h> |
| 33 | #include <proc/thread.h> |
33 | #include <proc/thread.h> |
| 34 | 34 | ||
| 35 | #include <synch/rwlock.h> |
35 | #include <synch/rwlock.h> |
| 36 | 36 | ||
| 37 | #define READERS 50 |
37 | #define THREADS 4 |
| 38 | #define WRITERS 50 |
- | |
| 39 | 38 | ||
| - | 39 | static atomic_t thread_count; |
|
| 40 | static rwlock_t rwlock; |
40 | static rwlock_t rwlock; |
| - | 41 | static bool sh_quiet; |
|
| 41 | 42 | ||
| 42 | static void reader(void *arg) |
43 | static void reader(void *arg) |
| 43 | { |
44 | { |
| 44 | thread_detach(THREAD); |
45 | thread_detach(THREAD); |
| 45 | 46 | ||
| - | 47 | if (!sh_quiet) |
|
| 46 | printf("cpu%d, tid %d: trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); |
48 | printf("cpu%d, tid %d: trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); |
| - | 49 | ||
| 47 | rwlock_read_lock(&rwlock); |
50 | rwlock_read_lock(&rwlock); |
| 48 | rwlock_read_unlock(&rwlock); |
51 | rwlock_read_unlock(&rwlock); |
| - | 52 | ||
| - | 53 | if (!sh_quiet) { |
|
| 49 | printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid); |
54 | printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid); |
| 50 | - | ||
| 51 | printf("cpu%d, tid %d: trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); |
55 | printf("cpu%d, tid %d: trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); |
| - | 56 | } |
|
| 52 | 57 | ||
| 53 | rwlock_write_lock(&rwlock); |
58 | rwlock_write_lock(&rwlock); |
| 54 | rwlock_write_unlock(&rwlock); |
59 | rwlock_write_unlock(&rwlock); |
| - | 60 | ||
| - | 61 | if (!sh_quiet) |
|
| 55 | printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid); |
62 | printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid); |
| - | 63 | ||
| - | 64 | atomic_dec(&thread_count); |
|
| 56 | } |
65 | } |
| 57 | 66 | ||
| 58 | char * test_rwlock3(bool quiet) |
67 | char * test_rwlock3(bool quiet) |
| 59 | { |
68 | { |
| 60 | int i; |
69 | int i; |
| 61 | thread_t *thrd; |
70 | thread_t *thrd; |
| - | 71 | sh_quiet = quiet; |
|
| - | 72 | ||
| - | 73 | atomic_set(&thread_count, THREADS); |
|
| 62 | 74 | ||
| 63 | rwlock_initialize(&rwlock); |
75 | rwlock_initialize(&rwlock); |
| 64 | rwlock_write_lock(&rwlock); |
76 | rwlock_write_lock(&rwlock); |
| 65 | 77 | ||
| 66 | for (i = 0; i < 4; i++) { |
78 | for (i = 0; i < THREADS; i++) { |
| 67 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
79 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
| 68 | if (thrd) |
80 | if (thrd) |
| 69 | thread_ready(thrd); |
81 | thread_ready(thrd); |
| 70 | else |
82 | else if (!quiet) |
| 71 | printf("Could not create reader %d\n", i); |
83 | printf("Could not create reader %d\n", i); |
| 72 | } |
84 | } |
| 73 | 85 | ||
| 74 | thread_sleep(1); |
86 | thread_sleep(1); |
| 75 | - | ||
| 76 | rwlock_write_unlock(&rwlock); |
87 | rwlock_write_unlock(&rwlock); |
| 77 | 88 | ||
| - | 89 | while (atomic_get(&thread_count) > 0) { |
|
| - | 90 | if (!quiet) |
|
| - | 91 | printf("Threads left: %d\n", atomic_get(&thread_count)); |
|
| - | 92 | thread_sleep(1); |
|
| - | 93 | } |
|
| - | 94 | ||
| 78 | return NULL; |
95 | return NULL; |
| 79 | } |
96 | } |