[f7f07f6] | 1 | #include <stdio.h> |
---|
| 2 | #include "locks.hfa" |
---|
| 3 | #include <stdlib.hfa> |
---|
| 4 | #include <thread.hfa> |
---|
| 5 | #include <containers/array.hfa> |
---|
| 6 | |
---|
| 7 | static unsigned int taskCount = 4; |
---|
| 8 | static unsigned int threadCount = 2; |
---|
| 9 | static unsigned int lockCount = 1; |
---|
[cf444b6] | 10 | static unsigned int total_times = 320000; |
---|
| 11 | static unsigned int num_times; |
---|
[f7f07f6] | 12 | static const int workBufferSize = 16; |
---|
| 13 | static unsigned int work_unlocked = 10000; |
---|
| 14 | static unsigned int work_locked = 10000; |
---|
| 15 | |
---|
| 16 | // taken from martin's thread_test |
---|
| 17 | static inline void dowork(volatile int* buffer, unsigned int steps) { |
---|
| 18 | int value = 0; |
---|
| 19 | for (unsigned int i = 0; i < steps; i += 1) { |
---|
| 20 | // a little more work than just a single memory access helps with stability |
---|
| 21 | value += (buffer[i % workBufferSize] * 17) / 23 + 55; |
---|
| 22 | } |
---|
| 23 | buffer[0] += value; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | thread worker { |
---|
| 27 | linear_backoff_then_block_lock * locks; |
---|
[cf444b6] | 28 | bool improved; |
---|
[f7f07f6] | 29 | }; |
---|
| 30 | |
---|
[cf444b6] | 31 | void ?{}( worker & w, linear_backoff_then_block_lock * locks, bool improved ) { |
---|
[f7f07f6] | 32 | w.locks = locks; |
---|
[cf444b6] | 33 | w.improved = improved; |
---|
[f7f07f6] | 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | void main( worker & this ) with(this) { |
---|
| 38 | int buffer[workBufferSize]; |
---|
| 39 | for (int i = 0; i < workBufferSize; i += 1) buffer[i] = rand() % 1024; |
---|
| 40 | unsigned int lck = rand() % lockCount; |
---|
[cf444b6] | 41 | linear_backoff_then_block_lock * curr_lock = &locks[lck]; |
---|
[f7f07f6] | 42 | for (unsigned int i = 0; i < num_times; i++) { |
---|
| 43 | dowork(buffer, work_unlocked); |
---|
[cf444b6] | 44 | if (improved) lock_improved(*curr_lock); |
---|
| 45 | else lock(*curr_lock); |
---|
[f7f07f6] | 46 | dowork(buffer, work_locked); |
---|
[cf444b6] | 47 | unlock(*curr_lock); |
---|
[f7f07f6] | 48 | lck = rand() % lockCount; |
---|
[cf444b6] | 49 | curr_lock = &locks[lck]; |
---|
[f7f07f6] | 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
[cf444b6] | 53 | |
---|
[f7f07f6] | 54 | int main(int argc, char* argv[]) { |
---|
| 55 | switch (argc) { |
---|
[cf444b6] | 56 | case 7: |
---|
| 57 | work_unlocked = atoi(argv[5]); |
---|
| 58 | case 6: |
---|
| 59 | work_locked = atoi(argv[5]); |
---|
[f7f07f6] | 60 | case 5: |
---|
| 61 | num_times = atoi(argv[4]); |
---|
| 62 | case 4: |
---|
| 63 | lockCount = atoi(argv[3]); |
---|
| 64 | case 3: |
---|
| 65 | threadCount = atoi(argv[2]); |
---|
| 66 | case 2: |
---|
| 67 | taskCount = atoi(argv[1]); |
---|
| 68 | case 1: |
---|
| 69 | break; |
---|
| 70 | default: |
---|
[8f1a99e] | 71 | break; |
---|
[f7f07f6] | 72 | } |
---|
| 73 | processor p[threadCount]; |
---|
[cf444b6] | 74 | linear_backoff_then_block_lock locks[lockCount]; |
---|
[f7f07f6] | 75 | worker * worker_arr[taskCount]; |
---|
[cf444b6] | 76 | num_times = total_times / taskCount; |
---|
[f7f07f6] | 77 | |
---|
[cf444b6] | 78 | //printf("Start Test: martin lock simple\n"); |
---|
[f7f07f6] | 79 | clock_t begin = clock(); |
---|
| 80 | for (unsigned int i = 0; i < taskCount; i++) { |
---|
[cf444b6] | 81 | worker_arr[i] = new( locks, false ); |
---|
[f7f07f6] | 82 | } |
---|
| 83 | for (unsigned int i = 0; i < taskCount; i++) { |
---|
[cf444b6] | 84 | delete( worker_arr[i] ); |
---|
[f7f07f6] | 85 | } |
---|
| 86 | clock_t end = clock(); |
---|
| 87 | double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; |
---|
[cf444b6] | 88 | printf("norm: %f\n", time_spent); |
---|
| 89 | |
---|
| 90 | //printf("Start Test: martin lock improved\n"); |
---|
| 91 | begin = clock(); |
---|
| 92 | for (unsigned int i = 0; i < taskCount; i++) { |
---|
| 93 | worker_arr[i] = new( locks, true ); |
---|
| 94 | } |
---|
| 95 | for (unsigned int i = 0; i < taskCount; i++) { |
---|
| 96 | delete( worker_arr[i] ); |
---|
| 97 | } |
---|
| 98 | end = clock(); |
---|
| 99 | time_spent = (double)(end - begin) / CLOCKS_PER_SEC; |
---|
| 100 | printf("improved: %f\n", time_spent); |
---|
[f7f07f6] | 101 | } |
---|