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;
|
---|
10 | static unsigned int num_times = 10000;
|
---|
11 | static const int workBufferSize = 16;
|
---|
12 | static unsigned int work_unlocked = 10000;
|
---|
13 | static unsigned int work_locked = 10000;
|
---|
14 |
|
---|
15 | // taken from martin's thread_test
|
---|
16 | static inline void dowork(volatile int* buffer, unsigned int steps) {
|
---|
17 | int value = 0;
|
---|
18 | for (unsigned int i = 0; i < steps; i += 1) {
|
---|
19 | // a little more work than just a single memory access helps with stability
|
---|
20 | value += (buffer[i % workBufferSize] * 17) / 23 + 55;
|
---|
21 | }
|
---|
22 | buffer[0] += value;
|
---|
23 | }
|
---|
24 |
|
---|
25 | thread worker {
|
---|
26 | linear_backoff_then_block_lock * locks;
|
---|
27 | };
|
---|
28 |
|
---|
29 | void ?{}( worker & w, linear_backoff_then_block_lock * locks ) {
|
---|
30 | w.locks = locks;
|
---|
31 | }
|
---|
32 |
|
---|
33 | linear_backoff_then_block_lock norm_lock;
|
---|
34 |
|
---|
35 | void main( worker & this ) with(this) {
|
---|
36 | int buffer[workBufferSize];
|
---|
37 | for (int i = 0; i < workBufferSize; i += 1) buffer[i] = rand() % 1024;
|
---|
38 | unsigned int lck = rand() % lockCount;
|
---|
39 | linear_backoff_then_block_lock * curr_lock = locks;//[lck];
|
---|
40 | for (unsigned int i = 0; i < num_times; i++) {
|
---|
41 | dowork(buffer, work_unlocked);
|
---|
42 | lock(*curr_lock);
|
---|
43 | //printf("lock: %d %p ENTER\n", i, &curr_lock);
|
---|
44 | //lock(norm_lock);
|
---|
45 | dowork(buffer, work_locked);
|
---|
46 | //printf("lock: %d %p LEAVE\n", i, &curr_lock);
|
---|
47 | unlock(*curr_lock);
|
---|
48 | //unlock(norm_lock);
|
---|
49 | lck = rand() % lockCount;
|
---|
50 | //curr_lock = locks[lck];
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | int main(int argc, char* argv[]) {
|
---|
55 | switch (argc) {
|
---|
56 | case 5:
|
---|
57 | num_times = atoi(argv[4]);
|
---|
58 | case 4:
|
---|
59 | lockCount = atoi(argv[3]);
|
---|
60 | case 3:
|
---|
61 | threadCount = atoi(argv[2]);
|
---|
62 | case 2:
|
---|
63 | taskCount = atoi(argv[1]);
|
---|
64 | case 1:
|
---|
65 | break;
|
---|
66 | default:
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | processor p[threadCount];
|
---|
70 | linear_backoff_then_block_lock locks;//[lockCount];
|
---|
71 | printf("lock allocation address: %p \n", &locks);
|
---|
72 | worker * worker_arr[taskCount];
|
---|
73 |
|
---|
74 | printf("Start Test: martin lock simple\n");
|
---|
75 | clock_t begin = clock();
|
---|
76 | for (unsigned int i = 0; i < taskCount; i++) {
|
---|
77 | worker_arr[i] = new( &locks );
|
---|
78 | }
|
---|
79 | for (unsigned int i = 0; i < taskCount; i++) {
|
---|
80 | free( worker_arr[i] );
|
---|
81 | }
|
---|
82 | clock_t end = clock();
|
---|
83 | double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
|
---|
84 | printf("Done Test, time: %f\n", time_spent);
|
---|
85 | }
|
---|