liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (c) International Business Machines  Corp., 2004.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
/**********************************************************
 *
 *    TEST IDENTIFIER   : nptl01
 *
 *    EXECUTED BY       : root
 *
 *    TEST TITLE        : NPTL test for pthread_cond_timedwait() error
 *              path bug.
 *
 *    TEST CASE TOTAL   : 1
 *
 *    AUTHOR            : Neil Richards <neil_richards@uk.ibm.com>
 *
 *    DESCRIPTION
 *      This is a test for a bug found in the pthread_cond_timedwait() system call.
 *    of the Native POSIX Thread Library (NPTL) library code.
 *      There was an error path in the system call, where the sequence counters were
 *      getting updated w/o holding the internal condvar lock. A FAIL is indicated
 *    by the test hanging and not completing execution.
 *
 ****************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "test.h"
 
#define MAXTIME 72000        /* Maximum # of secs to wait before failing */
#define NUMLOOPS 100000        /* # of loops */
 
char *TCID = "nptl01";        /* Test program identifier.    */
int TST_TOTAL = 1;        /* Total number of test cases. */
void cleanup();
 
pthread_mutex_t req;
pthread_mutex_t ack;
pthread_mutex_t wait;
pthread_cond_t parent;
pthread_cond_t child;
int idle_count = 0;
 
/*
 * The time to wait should be set appropriately so that the waiting thread
 * is coming out of the wait at around the same time as the other thread is
 * signalling it.
 * The value of 1000 seems to work (ie. demonstrate the problem) on my
 * 8 way (hyperthreaded) 2GHz Xeon box.
 */
#define NSECS_TO_WAIT    (1)
 
void call_mutex_init(pthread_mutex_t * mutex, char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_mutex_init(mutex, NULL)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_mutex_init failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void call_mutex_lock(pthread_mutex_t * mutex, char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_mutex_lock(mutex)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_mutex_lock failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void call_mutex_unlock(pthread_mutex_t * mutex, char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_mutex_unlock(mutex)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_mutex_unlock failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void call_cond_init(pthread_cond_t * cond, char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_cond_init(cond, NULL)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_cond_init failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void call_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
           char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_cond_wait(cond, mutex)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_cond_wait failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void call_cond_signal(pthread_cond_t * cond, char *buf, size_t buf_len)
{
   int ret;
 
   if ((ret = pthread_cond_signal(cond)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_cond_signal failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void do_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
         char *buf, size_t buf_len, int i)
{
   struct timeval tv;
   struct timespec ts;
   int ret;
 
   if (gettimeofday(&tv, NULL) != 0) {
       tst_brkm(TBROK, cleanup, "gettimeofday failed: %s",
            strerror_r(errno, buf, buf_len));
   }
 
   ts.tv_sec = tv.tv_sec;
   ts.tv_nsec = (tv.tv_usec * 1000) + NSECS_TO_WAIT;
   ts.tv_sec += ts.tv_nsec / 1000000000;
   ts.tv_nsec = ts.tv_nsec % 1000000000;
 
   call_mutex_lock(mutex, buf, buf_len);
   if ((ret = pthread_cond_timedwait(cond, mutex, &ts)) != ETIMEDOUT) {
#if DEBUG
       tst_resm(TINFO,
            "Loop %d of 1000000: pthread_cond_timedwait() didn't timeout",
            i);
       tst_resm(TINFO,
            "You may want to try reducing the value of NSECS_TO_WAIT (currently=%d)",
            NSECS_TO_WAIT);
#endif
   }
   call_mutex_unlock(mutex, buf, buf_len);
 
}
 
void *run(void *arg)
{
   char buf[1024];
 
   while (1) {
       call_mutex_lock(&ack, buf, sizeof(buf));
       idle_count = 1;
       call_cond_signal(&parent, buf, sizeof(buf));
       call_mutex_lock(&req, buf, sizeof(buf));
       call_mutex_unlock(&ack, buf, sizeof(buf));
 
       call_mutex_lock(&wait, buf, sizeof(buf));
       call_cond_signal(&parent, buf, sizeof(buf));
       call_mutex_unlock(&wait, buf, sizeof(buf));
 
       call_cond_wait(&child, &req, buf, sizeof(buf));
       call_mutex_unlock(&req, buf, sizeof(buf));
   }
}
 
void create_child_thread(char *buf, size_t buf_len)
{
   pthread_attr_t attr;
   pthread_t child_tid;
   int ret;
 
   if ((ret = pthread_attr_init(&attr)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_attr_init failed: %s",
            strerror_r(ret, buf, buf_len));
   }
   if ((ret = pthread_attr_setdetachstate(&attr,
                          PTHREAD_CREATE_DETACHED)) != 0) {
       tst_brkm(TBROK, cleanup,
            "pthread_attr_setdetachstate failed: %s",
            strerror_r(ret, buf, buf_len));
   }
   if ((ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_attr_setscope failed: %s",
            strerror_r(ret, buf, buf_len));
   }
 
   if ((ret = pthread_create(&child_tid, &attr, run, NULL)) != 0) {
       tst_brkm(TBROK, cleanup, "pthread_create failed: %s",
            strerror_r(ret, buf, buf_len));
   }
}
 
void trap_alarm(int sig)
{
   tst_brkm(TFAIL, cleanup, "Test hang longer than %d sec detected",
        MAXTIME);
}
 
static void usage(const char *progname)
{
   fprintf(stderr, "usage: %s -l loops\n", progname);
   fprintf(stderr, "\t-l specify the number of loops to carry out\n");
}
 
int main(int argc, char **argv)
{
#ifdef USING_NPTL
   char buf[1024];
   int i;
   extern char *optarg;
   int numloops = NUMLOOPS;
 
   while ((i = getopt(argc, argv, "l:")) != -1) {
       switch (i) {
       case 'l':
           if (optarg)
               numloops = atoi(optarg);
           else
               fprintf(stderr,
                   "%s: option -l requires an argument\n",
                   argv[0]);
           break;
       default:
           usage(argv[0]);
           exit(1);
       }
   }
 
   signal(SIGALRM, trap_alarm);
   alarm(MAXTIME);
 
   call_mutex_init(&req, buf, sizeof(buf));
   call_mutex_init(&ack, buf, sizeof(buf));
   call_mutex_init(&wait, buf, sizeof(buf));
   call_cond_init(&parent, buf, sizeof(buf));
   call_cond_init(&child, buf, sizeof(buf));
 
   call_mutex_lock(&ack, buf, sizeof(buf));
 
   create_child_thread(buf, sizeof(buf));
 
   tst_resm(TINFO, "Starting test, please wait.");
   for (i = 0; i < numloops; i++) {
       while (idle_count == 0) {
           call_cond_wait(&parent, &ack, buf, sizeof(buf));
       };
       idle_count = 0;
       call_mutex_unlock(&ack, buf, sizeof(buf));
 
       do_timedwait(&parent, &wait, buf, sizeof(buf), i);
 
       call_mutex_lock(&req, buf, sizeof(buf));
       call_cond_signal(&child, buf, sizeof(buf));
       call_mutex_unlock(&req, buf, sizeof(buf));
#ifdef DEBUG
       tst_resm(TINFO, "Success in loop %d", i);
#else
       if (((i % (numloops / 10)) == 0) && (i != 0))
           tst_resm(TINFO, "Success thru loop %d of %i", i,
                numloops);
#endif
       call_mutex_lock(&ack, buf, sizeof(buf));
   }
 
   alarm(0);
   tst_resm(TPASS, "Test completed successfully!");
   cleanup();
 
#else
   tst_brkm(TCONF, NULL,
        "Skipping Execution - This system is not using NPTL");
#endif
 
   return 1;
}
 
/*
 *cleanup() -  performs all ONE TIME cleanup for this test at
 *              completion or premature exit.
 */
void cleanup()
{
 
   tst_exit();
}