source: tests/unified_locking/thread_test.cfa @ 01a8954

ADTast-experimental
Last change on this file since 01a8954 was 0cee082, checked in by caparsons <caparson@…>, 17 months ago

refactored naming for lock to be more accurate and cleaned up REACQ nonsense in locks.hfa

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[f7f07f6]1#include <stdio.h>
2#include "locks.hfa"
3#include <stdlib.hfa>
4#include <thread.hfa>
5#include <containers/array.hfa>
6
7static unsigned int taskCount = 4;
8static unsigned int threadCount = 2;
9static unsigned int lockCount = 1;
[cf444b6]10static unsigned int total_times = 320000;
11static unsigned int num_times;
[f7f07f6]12static const int workBufferSize = 16;
13static unsigned int work_unlocked = 10000;
14static unsigned int work_locked = 10000;
15
16// taken from martin's thread_test
17static 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
26thread worker {
[0cee082]27    exp_backoff_then_block_lock * locks;
[cf444b6]28    bool improved;
[f7f07f6]29};
30
[0cee082]31void ?{}( worker & w, exp_backoff_then_block_lock * locks, bool improved ) {
[f7f07f6]32        w.locks = locks;
[cf444b6]33    w.improved = improved;
[f7f07f6]34}
35
36
37void 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;
[0cee082]41    exp_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
[0cee082]53int doOne = 0;
[f7f07f6]54int main(int argc, char* argv[]) {
55    switch (argc) {
[0cee082]56        case 8:
57            doOne = atoi(argv[7]);
[cf444b6]58        case 7:
[0cee082]59            work_unlocked = atoi(argv[6]);
[cf444b6]60        case 6:
61            work_locked = atoi(argv[5]);
[f7f07f6]62        case 5:
[0cee082]63            total_times = atoi(argv[4]);
[f7f07f6]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:
[8f1a99e]73            break;
[f7f07f6]74    }
75        processor p[threadCount];
[0cee082]76    exp_backoff_then_block_lock locks[lockCount];
[f7f07f6]77    worker * worker_arr[taskCount];
[cf444b6]78    num_times = total_times  / taskCount;
[0cee082]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);
[f7f07f6]84        for (unsigned int i = 0; i < taskCount; i++) {
[cf444b6]85        worker_arr[i] = new( locks, false );
[f7f07f6]86    }
87    for (unsigned int i = 0; i < taskCount; i++) {
[cf444b6]88        delete( worker_arr[i] );
[f7f07f6]89    }
[0cee082]90    }
91        //clock_t end = clock();
92        //double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
93        //printf("norm: %f\n", time_spent);
[cf444b6]94
95    //printf("Start Test: martin lock improved\n");
[0cee082]96        //begin = clock();
97    if (doOne == 2) {
[cf444b6]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    }
[0cee082]104    }
105        //end = clock();
106        //time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
107        //printf("improved: %f\n", time_spent);
[f7f07f6]108}
Note: See TracBrowser for help on using the repository browser.