| 1 | #include <stdio.h>
 | 
|---|
| 2 | #include <locks.hfa>
 | 
|---|
| 3 | #include <stdlib.hfa>
 | 
|---|
| 4 | #include <thread.hfa>
 | 
|---|
| 5 | #include <collections/array.hfa>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | static unsigned int taskCount = 4;
 | 
|---|
| 8 | static unsigned int threadCount = 2;
 | 
|---|
| 9 | static unsigned int lockCount = 1;
 | 
|---|
| 10 | static unsigned int total_times = 320000;
 | 
|---|
| 11 | static unsigned int num_times;
 | 
|---|
| 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 |     exp_backoff_then_block_lock * locks;
 | 
|---|
| 28 |     bool improved;
 | 
|---|
| 29 | };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | void ?{}( worker & w, exp_backoff_then_block_lock * locks, bool improved ) {
 | 
|---|
| 32 |         w.locks = locks;
 | 
|---|
| 33 |     w.improved = improved;
 | 
|---|
| 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;
 | 
|---|
| 41 |     exp_backoff_then_block_lock * curr_lock = &locks[lck];
 | 
|---|
| 42 |     for (unsigned int i = 0; i < num_times; i++) {
 | 
|---|
| 43 |         dowork(buffer, work_unlocked);
 | 
|---|
| 44 |         if (improved) lock_improved(*curr_lock);
 | 
|---|
| 45 |         else lock(*curr_lock);
 | 
|---|
| 46 |         dowork(buffer, work_locked);
 | 
|---|
| 47 |         unlock(*curr_lock);
 | 
|---|
| 48 |         lck = rand() % lockCount;
 | 
|---|
| 49 |         curr_lock = &locks[lck];
 | 
|---|
| 50 |     }
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | int doOne = 0;
 | 
|---|
| 54 | int main(int argc, char* argv[]) {
 | 
|---|
| 55 |     switch (argc) {
 | 
|---|
| 56 |         case 8:
 | 
|---|
| 57 |             doOne = atoi(argv[7]);
 | 
|---|
| 58 |         case 7:
 | 
|---|
| 59 |             work_unlocked = atoi(argv[6]);
 | 
|---|
| 60 |         case 6:
 | 
|---|
| 61 |             work_locked = atoi(argv[5]);
 | 
|---|
| 62 |         case 5:
 | 
|---|
| 63 |             total_times = atoi(argv[4]);
 | 
|---|
| 64 |         case 4:
 | 
|---|
| 65 |             lockCount = atoi(argv[3]);
 | 
|---|
| 66 |         case 3:
 | 
|---|
| 67 |             threadCount = atoi(argv[2]);
 | 
|---|
| 68 |         case 2:
 | 
|---|
| 69 |             taskCount = atoi(argv[1]);
 | 
|---|
| 70 |         case 1:
 | 
|---|
| 71 |             break;
 | 
|---|
| 72 |         default:
 | 
|---|
| 73 |             break;
 | 
|---|
| 74 |     }
 | 
|---|
| 75 |         processor p[threadCount];
 | 
|---|
| 76 |     exp_backoff_then_block_lock locks[lockCount];
 | 
|---|
| 77 |     worker * worker_arr[taskCount];
 | 
|---|
| 78 |     num_times = total_times  / taskCount;
 | 
|---|
| 79 |     //printf("%d\n", doOne);
 | 
|---|
| 80 |         //
 | 
|---|
| 81 |         //clock_t begin = clock();
 | 
|---|
| 82 |     if (doOne == 1) {
 | 
|---|
| 83 |         printf("Start Test: martin lock simple %d\n", num_times);
 | 
|---|
| 84 |         for (unsigned int i = 0; i < taskCount; i++) {
 | 
|---|
| 85 |         worker_arr[i] = new( locks, false );
 | 
|---|
| 86 |     }
 | 
|---|
| 87 |     for (unsigned int i = 0; i < taskCount; i++) {
 | 
|---|
| 88 |         delete( worker_arr[i] );
 | 
|---|
| 89 |     }
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |         //clock_t end = clock();
 | 
|---|
| 92 |         //double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
 | 
|---|
| 93 |         //printf("norm: %f\n", time_spent);
 | 
|---|
| 94 | 
 | 
|---|
| 95 |     //printf("Start Test: martin lock improved\n");
 | 
|---|
| 96 |         //begin = clock();
 | 
|---|
| 97 |     if (doOne == 2) {
 | 
|---|
| 98 |         for (unsigned int i = 0; i < taskCount; i++) {
 | 
|---|
| 99 |         worker_arr[i] = new( locks, true );
 | 
|---|
| 100 |     }
 | 
|---|
| 101 |     for (unsigned int i = 0; i < taskCount; i++) {
 | 
|---|
| 102 |         delete( worker_arr[i] );
 | 
|---|
| 103 |     }
 | 
|---|
| 104 |     }
 | 
|---|
| 105 |         //end = clock();
 | 
|---|
| 106 |         //time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
 | 
|---|
| 107 |         //printf("improved: %f\n", time_spent);
 | 
|---|
| 108 | }
 | 
|---|