-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestring.cc
More file actions
166 lines (149 loc) · 3.25 KB
/
Copy pathtestring.cc
File metadata and controls
166 lines (149 loc) · 3.25 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <cstdlib>
#include <iostream>
#include <errno.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <stdio.h>
#include "posix_ringbuf.h"
// Tuning
static const int read_usleep = 1;
static const int write_usleep = 1;
static const int verbose = 1;
#define USE_POSIX
#define DEFAULT_RUN_SECONDS 30
class Dummy
{
public:
int serialNumber;
double doubleMember;
char stringMember[32];
};
#ifdef USE_POSIX
static Posix_Ringbuf<Dummy> rbuf (11);
#else
static Ringbuf<Dummy> rbuf (11);
#endif
static void* Reader (void* arg);
static void* Writer (void* arg);
static void Usage_exit (int exit_val);
int main (int argc, char* argv[])
{
int run_seconds;
switch (argc)
{
case 1:
run_seconds = DEFAULT_RUN_SECONDS;
break;
case 2:
if (sscanf (argv[1], "%d", &run_seconds) != 1)
{
std::cerr << "Illegal numeric expression \"" << argv[1] <<
"\"" << std::endl;
exit (1);
}
if (run_seconds < 0)
{
std::cerr << "Please enter a non-negative number or nothing" <<
std::endl;
Usage_exit (1);
}
break;
default:
Usage_exit (0);
break;
}
pthread_t hReader, hWriter;
if (pthread_create (&hReader, NULL, Reader, NULL) != 0)
{
std::cerr << "pthread_create failed (1) " << strerror(errno) <<
std::endl;
exit (1);
}
if (pthread_create (&hWriter, NULL, Writer, NULL) != 0)
{
std::cerr << "pthread_create failed (2) " << strerror(errno) <<
std::endl;
exit (1);
}
sleep (run_seconds);
if (pthread_cancel (hWriter) != 0)
{
std::cerr << "pthread_cancel failed (1) " << strerror(errno) <<
std::endl;
exit (1);
}
if (pthread_cancel (hReader) != 0)
{
std::cerr << "pthread_cancel failed (2) " << strerror(errno) <<
std::endl;
exit (1);
}
std::cout << "No compare errors found" << std::endl;
exit (0);
}
static void* Writer (void* arg)
{
static __thread int serial = 0;
while (true)
{
usleep (write_usleep);
Dummy dummy;
dummy.serialNumber = ++serial;
try
{
rbuf << dummy;
}
catch (RingbufFullException)
{
if (verbose >= 2)
std::cout << "Write failure " << serial << std::endl;
--serial;
continue;
}
if (verbose >= 3)
std::cout << "Put " << serial << std::endl;
}
return NULL;
}
static void* Reader (void* arg)
{
static __thread int serial = 0;
while (true)
{
usleep (read_usleep);
++serial;
Dummy dummy;
try
{
rbuf >> dummy;
}
catch (RingbufEmptyException)
{
if (verbose >= 2)
std::cout << "Read failure " << serial << std::endl;
--serial;
continue;
}
if (dummy.serialNumber != serial)
{
if (verbose >= 1)
{
std::cout << "*** ERROR *** ";
std::cout << "Pop " << serial << std::endl;
exit (1);
}
}
else
{
if (verbose >= 3)
std::cout << "Pop " << serial << std::endl;
}
}
return NULL;
}
static void Usage_exit (int exit_val)
{
std::cerr << "Usage: test_ring [run_seconds]" << std::endl;
exit (exit_val);
}