source: tests/unified_locking/thread_test.cfa@ c86ee4c

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c86ee4c was 8f1a99e, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Step 3 Fixed tests

  • Property mode set to 100644
File size: 2.4 KB
Line 
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;
10static unsigned int num_times = 10000;
11static const int workBufferSize = 16;
12static unsigned int work_unlocked = 10000;
13static unsigned int work_locked = 10000;
14
15// taken from martin's thread_test
16static 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
25thread worker {
26 linear_backoff_then_block_lock * locks;
27};
28
29void ?{}( worker & w, linear_backoff_then_block_lock * locks ) {
30 w.locks = locks;
31}
32
33linear_backoff_then_block_lock norm_lock;
34
35void 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
54int 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}
Note: See TracBrowser for help on using the repository browser.