forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_liblock.c
140 lines (130 loc) · 3.8 KB
/
test_liblock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/******************************************************************************
* Copyright (C) 2014-2015
* file: test_liblock.c
* author: gozfree <[email protected]>
* created: 2016-06-22 14:09:11
* updated: 2016-06-22 14:09:11
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <sys/time.h>
#include "liblock.h"
static spin_lock_t *spin = NULL;
static mutex_lock_t *mutex = NULL;
static int64_t value = 0;
struct thread_arg {
int flag;
uint64_t count;
};
void usage(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s <type> <count>\n", argv[0]);
printf("type: spin | mutex\n");
printf("count: 2 ~ 10\n");
exit(0);
}
}
static void *print_mutex_lock(void *arg)
{
struct thread_arg *argp = (struct thread_arg *)arg;
int c = argp->flag;
uint64_t n = argp->count;
uint64_t i;
pthread_t tid = pthread_self();
printf("c = %d\n", c);
for (i = 0; i < n; ++ i) {
if (c) {
mutex_lock(mutex);
++ value;
if (!(i % 100) && 0) {
printf("tid=%d, c=%d, value = %"PRId64"\n", (int)tid, c, value);
}
mutex_unlock(mutex);
} else {
mutex_lock(mutex);
-- value;
if (!(i % 100) && 0) {
printf("tid=%d, c=%d, value = %"PRId64"\n", (int)tid, c, value);
}
mutex_unlock(mutex);
}
}
return NULL;
}
static void *print_spin_lock(void *arg)
{
struct thread_arg *argp = (struct thread_arg *)arg;
int c = argp->flag;
uint64_t n = argp->count;
uint64_t i;
pthread_t tid = pthread_self();
printf("c = %d\n", c);
for (i = 0; i < n; ++ i) {
if (c) {
spin_lock(spin);
++ value;
if (!(i % 100) && 0) {
printf("tid=%d, c=%d, value = %"PRId64"\n", (int)tid, c, value);
}
spin_unlock(spin);
} else {
spin_lock(spin);
-- value;
if (!(i % 100) && 0) {
printf("tid=%d, c=%d, value = %"PRId64"\n", (int)tid, c, value);
}
spin_unlock(spin);
}
}
return NULL;
}
int main(int argc, char **argv)
{
usage(argc, argv);
pthread_t tid1, tid2;
struct timeval start;
struct timeval end;
uint64_t times = strtoul((const char*)argv[2], (char**)NULL, 10);
value = times;
struct thread_arg arg1, arg2;
arg1.flag = 0;
arg1.count = times;
arg2.flag = 1;
arg2.count = times;
if (!strcmp(argv[1], "spin")) {
gettimeofday(&start, NULL);
spin = spin_lock_init();
pthread_create(&tid1, NULL, print_spin_lock, (void *)&arg1);
pthread_create(&tid2, NULL, print_spin_lock, (void *)&arg2);
} else if (!strcmp(argv[1], "mutex")) {
gettimeofday(&start, NULL);
mutex = mutex_lock_init();
pthread_create(&tid1, NULL, print_mutex_lock, (void *)&arg1);
pthread_create(&tid2, NULL, print_mutex_lock, (void *)&arg2);
}
printf("tid1=%d, tid2=%d\n", (int)tid1, (int)tid2);
if (tid1 && tid2) {
uint64_t startUs = 0;
uint64_t endUs = 0;
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
gettimeofday(&end, NULL);
startUs = start.tv_sec * 1000000 + start.tv_usec;
endUs = end.tv_sec * 1000000 + end.tv_usec;
fprintf(stdout, "Value is %" PRIu64 ", Used %" PRIu64 "us:%" PRIu64 "\n",
value, (endUs - startUs) / 1000000, (endUs - startUs) % 1000000);
}
if (mutex) {
mutex_lock_deinit(mutex);
}
if (spin) {
spin_lock_deinit(spin);
}
return 0;
}