7 pthread_mutex_t m
[MANY
];
11 static Level level
[LEVEL
];
13 static int stat_mutex_init
= 0;
14 static int stat_mutex_lock
= 0;
15 static int stat_mutex_unlock
= 0;
16 static int stat_mutex_destroy
= 0;
18 /* t2t.c : test program for the laog data structure performance testing
19 and "shaking" : it creates, locks/unlocks and destroy mutex.
21 USAGE: t2t [many] [level] [loops]
22 many (default 100) : how many locks are created/locked/unlocked at a certain level.
23 level (default 1) : how many levels of "nested locks" are done
24 loops : how many times these locks are created and destroyed and locked/unlocked) */
25 #define check if (ret != 0) printf("error %d at line %d\n", ret, __LINE__)
26 int doit(int argc
, char*argv
[])
34 if (argc
>= 2) clo_many
= atoi(argv
[1]);
35 if (argc
>= 3) clo_level
= atoi(argv
[2]);
37 if (clo_many
> MANY
) {
38 printf("error argv[1] (many arg) %d > max MANY %d\n", clo_many
, MANY
);
42 if (clo_level
> LEVEL
) {
43 printf("error argv[2] (level arg) %d > max LEVEL %d\n", clo_level
, LEVEL
);
47 printf ("many %d level %d total_locks: %d\n",
49 clo_many
* clo_level
+ clo_level
* (clo_level
== 1 ? 0 : 1));
51 for (l
= 0; l
< clo_level
; l
++) {
52 printf ("init level %d\n", l
);
53 for (i
= 0; i
< clo_many
; i
++) {
54 ret
= pthread_mutex_init (&level
[l
].m
[i
], NULL
);
59 ret
= pthread_mutex_init (&level
[l
].d
, NULL
);
65 for (l
= 0; l
< clo_level
; l
++) {
66 printf ("locking level %d\n", l
);
67 for (i
= 0; i
< clo_many
; i
++) {
68 ret
= pthread_mutex_lock (&level
[l
].m
[i
]);
73 ret
= pthread_mutex_lock (&level
[l
].d
);
79 for (l
= 0; l
< clo_level
; l
++) {
80 printf ("unlocking level %d\n", l
);
81 for (i
= 0; i
< clo_many
; i
++) {
82 ret
= pthread_mutex_unlock (&level
[l
].m
[i
]);
87 ret
= pthread_mutex_unlock (&level
[l
].d
);
93 for (l
= 0; l
< clo_level
; l
++) {
94 printf ("deleting level %d\n", l
);
96 ret
= pthread_mutex_destroy (&level
[l
].d
);
97 /// this tests the influence of the deletion in another order.
101 for (i
= 0; i
< clo_many
; i
++) {
102 ret
= pthread_mutex_destroy (&level
[l
].m
[i
]);
104 stat_mutex_destroy
++;
110 int main(int argc
, char*argv
[])
114 if (argc
>= 4) loops
= atoi(argv
[3]);
116 printf ("loops %d\n", loops
);
117 for (i
= 0; i
< loops
; i
++)
120 printf ("stats: init %d lock %d unlock %d destroy %d\n",
121 stat_mutex_init
, stat_mutex_lock
,
122 stat_mutex_unlock
, stat_mutex_destroy
);