~ubuntu-branches/ubuntu/raring/clamav/raring-updates

« back to all changes in this revision

Viewing changes to win32/3rdparty/pthreads/tests/stress1.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-06-18 11:56:34 UTC
  • mfrom: (0.35.21 sid)
  • Revision ID: james.westby@ubuntu.com-20110618115634-u2lovivet0qx34d0
Tags: 0.97.1+dfsg-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * stress1.c
3
 
 *
4
 
 *
5
 
 * --------------------------------------------------------------------------
6
 
 *
7
 
 *      Pthreads-win32 - POSIX Threads Library for Win32
8
 
 *      Copyright(C) 1998 John E. Bossom
9
 
 *      Copyright(C) 1999,2005 Pthreads-win32 contributors
10
 
 * 
11
 
 *      Contact Email: rpj@callisto.canberra.edu.au
12
 
 * 
13
 
 *      The current list of contributors is contained
14
 
 *      in the file CONTRIBUTORS included with the source
15
 
 *      code distribution. The list can also be seen at the
16
 
 *      following World Wide Web location:
17
 
 *      http://sources.redhat.com/pthreads-win32/contributors.html
18
 
 * 
19
 
 *      This library is free software; you can redistribute it and/or
20
 
 *      modify it under the terms of the GNU Lesser General Public
21
 
 *      License as published by the Free Software Foundation; either
22
 
 *      version 2 of the License, or (at your option) any later version.
23
 
 * 
24
 
 *      This library is distributed in the hope that it will be useful,
25
 
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
 
 *      Lesser General Public License for more details.
28
 
 * 
29
 
 *      You should have received a copy of the GNU Lesser General Public
30
 
 *      License along with this library in the file COPYING.LIB;
31
 
 *      if not, write to the Free Software Foundation, Inc.,
32
 
 *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33
 
 *
34
 
 * --------------------------------------------------------------------------
35
 
 *
36
 
 * Test Synopsis:
37
 
 * - Stress test condition variables, mutexes, semaphores.
38
 
 *
39
 
 * Test Method (Validation or Falsification):
40
 
 * - Validation
41
 
 *
42
 
 * Requirements Tested:
43
 
 * - Correct accounting of semaphore and condition variable waiters.
44
 
 *
45
 
 * Features Tested:
46
 
 * - 
47
 
 *
48
 
 * Cases Tested:
49
 
 * - 
50
 
 *
51
 
 * Description:
52
 
 * Attempting to expose race conditions in cond vars, semaphores etc.
53
 
 * - Master attempts to signal slave close to when timeout is due.
54
 
 * - Master and slave do battle continuously until main tells them to stop.
55
 
 * - Afterwards, the CV must be successfully destroyed (will return an
56
 
 * error if there are waiters (including any internal semaphore waiters,
57
 
 * which, if there are, cannot not be real waiters).
58
 
 *
59
 
 * Environment:
60
 
 * - 
61
 
 *
62
 
 * Input:
63
 
 * - None.
64
 
 *
65
 
 * Output:
66
 
 * - File name, Line number, and failed expression on failure.
67
 
 * - No output on success.
68
 
 *
69
 
 * Assumptions:
70
 
 * - 
71
 
 *
72
 
 * Pass Criteria:
73
 
 * - CV is successfully destroyed.
74
 
 *
75
 
 * Fail Criteria:
76
 
 * - CV destroy fails.
77
 
 */
78
 
 
79
 
#include "test.h"
80
 
#include <string.h>
81
 
#include <sys/timeb.h>
82
 
 
83
 
 
84
 
const unsigned int ITERATIONS = 1000;
85
 
 
86
 
static pthread_t master, slave;
87
 
typedef struct {
88
 
  int value;
89
 
  pthread_cond_t cv;
90
 
  pthread_mutex_t mx;
91
 
} mysig_t;
92
 
 
93
 
static int allExit;
94
 
static mysig_t control = {0, PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER};
95
 
static pthread_barrier_t startBarrier, readyBarrier, holdBarrier;
96
 
static int timeoutCount = 0;
97
 
static int signalsTakenCount = 0;
98
 
static int signalsSent = 0;
99
 
static int bias = 0;
100
 
static int timeout = 10;
101
0
// Must be > 0
102
 
 
103
 
enum {
104
 
  CTL_STOP     = -1
105
 
};
106
 
 
107
 
/*
108
 
 * Returns abstime 'milliseconds' from 'now'.
109
 
 *
110
 
 * Works for: -INT_MAX <= millisecs <= INT_MAX
111
 
 */
112
 
struct timespec *
113
 
millisecondsFromNow (struct timespec * time, int millisecs)
114
 
{
115
 
  struct _timeb currSysTime;
116
 
  int64_t nanosecs, secs;
117
 
  const int64_t NANOSEC_PER_MILLISEC = 1000000;
118
 
  const int64_t NANOSEC_PER_SEC = 1000000000;
119
 
 
120
 
  /* get current system time and add millisecs */
121
 
  _ftime(&currSysTime);
122
 
 
123
 
  secs = (int64_t)(currSysTime.time) + (millisecs / 1000);
124
 
  nanosecs = ((int64_t) (millisecs%1000 + currSysTime.millitm)) * NANOSEC_PER_MILLISEC;
125
 
  if (nanosecs >= NANOSEC_PER_SEC)
126
 
    {
127
 
      secs++;
128
 
      nanosecs -= NANOSEC_PER_SEC;
129
 
    }
130
 
  else if (nanosecs < 0)
131
 
    {
132
 
      secs--;
133
 
      nanosecs += NANOSEC_PER_SEC;
134
 
    }
135
 
 
136
 
  time->tv_nsec = (long)nanosecs;
137
 
  time->tv_sec = (long)secs;
138
 
 
139
 
  return time;
140
 
}
141
 
 
142
 
void *
143
 
masterThread (void * arg)
144
 
{
145
 
  int dither = (int) arg;
146
 
 
147
 
  timeout = (int) arg;
148
 
 
149
 
  pthread_barrier_wait(&startBarrier);
150
 
 
151
 
  do
152
 
    {
153
 
      int sleepTime;
154
 
 
155
 
      assert(pthread_mutex_lock(&control.mx) == 0);
156
 
      control.value = timeout;
157
 
      assert(pthread_mutex_unlock(&control.mx) == 0);
158
 
 
159
 
      /*
160
 
       * We are attempting to send the signal close to when the slave
161
 
       * is due to timeout. We feel around by adding some [non-random] dither.
162
 
       *
163
 
       * dither is in the range 2*timeout peak-to-peak
164
 
       * sleep time is the average of timeout plus dither.
165
 
       * e.g.
166
 
       * if timeout = 10 then dither = 20 and
167
 
       * sleep millisecs is: 5 <= ms <= 15
168
 
       *
169
 
       * The bias value attempts to apply some negative feedback to keep
170
 
       * the ratio of timeouts to signals taken close to 1:1.
171
 
       * bias changes more slowly than dither so as to average more.
172
 
       *
173
 
       * Finally, if abs(bias) exceeds timeout then timeout is incremented.
174
 
       */
175
 
      if (signalsSent % timeout == 0)
176
 
        {
177
 
          if (timeoutCount > signalsTakenCount)
178
 
            {
179
 
              bias++;
180
 
            }
181
 
          else if (timeoutCount < signalsTakenCount)
182
 
            {
183
 
              bias--;
184
 
            }
185
 
          if (bias < -timeout || bias > timeout)
186
 
            {
187
 
              timeout++;
188
 
            }
189
 
        }
190
 
      dither = (dither + 1 ) % (timeout * 2);
191
 
      sleepTime = (timeout - bias + dither) / 2;
192
 
      Sleep(sleepTime);
193
 
      assert(pthread_cond_signal(&control.cv) == 0);
194
 
      signalsSent++;
195
 
 
196
 
      pthread_barrier_wait(&holdBarrier);
197
 
      pthread_barrier_wait(&readyBarrier);
198
 
    }
199
 
  while (!allExit);
200
 
 
201
 
  return NULL;
202
 
}
203
 
 
204
 
void *
205
 
slaveThread (void * arg)
206
 
{
207
 
  struct timespec time;
208
 
 
209
 
  pthread_barrier_wait(&startBarrier);
210
 
 
211
 
  do
212
 
    {
213
 
      assert(pthread_mutex_lock(&control.mx) == 0);
214
 
      if (pthread_cond_timedwait(&control.cv,
215
 
                                 &control.mx,
216
 
                                 millisecondsFromNow(&time, control.value)) == ETIMEDOUT)
217
 
        {
218
 
          timeoutCount++;
219
 
        }
220
 
      else
221
 
        {
222
 
          signalsTakenCount++;
223
 
        }
224
 
      assert(pthread_mutex_unlock(&control.mx) == 0);
225
 
 
226
 
      pthread_barrier_wait(&holdBarrier);
227
 
      pthread_barrier_wait(&readyBarrier);
228
 
    }
229
 
  while (!allExit);
230
 
 
231
 
  return NULL;
232
 
}
233
 
 
234
 
int
235
 
main ()
236
 
{
237
 
  unsigned int i;
238
 
 
239
 
  assert(pthread_barrier_init(&startBarrier, NULL, 3) == 0);
240
 
  assert(pthread_barrier_init(&readyBarrier, NULL, 3) == 0);
241
 
  assert(pthread_barrier_init(&holdBarrier, NULL, 3) == 0);
242
 
 
243
 
  assert(pthread_create(&master, NULL, masterThread, (void *) timeout) == 0);
244
 
  assert(pthread_create(&slave, NULL, slaveThread, NULL) == 0);
245
 
 
246
 
  allExit = FALSE;
247
 
 
248
 
  pthread_barrier_wait(&startBarrier);
249
 
 
250
 
  for (i = 1; !allExit; i++)
251
 
    {
252
 
      pthread_barrier_wait(&holdBarrier);
253
 
      if (i >= ITERATIONS)
254
 
        {
255
 
          allExit = TRUE;
256
 
        }
257
 
      pthread_barrier_wait(&readyBarrier);
258
 
    }
259
 
 
260
 
  assert(pthread_join(slave, NULL) == 0);
261
 
  assert(pthread_join(master, NULL) == 0);
262
 
 
263
 
  printf("Signals sent = %d\nWait timeouts = %d\nSignals taken = %d\nBias = %d\nTimeout = %d\n",
264
 
         signalsSent,
265
 
         timeoutCount,
266
 
         signalsTakenCount,
267
 
         (int) bias,
268
 
         timeout);
269
 
 
270
 
  /* Cleanup */
271
 
  assert(pthread_barrier_destroy(&holdBarrier) == 0);
272
 
  assert(pthread_barrier_destroy(&readyBarrier) == 0);
273
 
  assert(pthread_barrier_destroy(&startBarrier) == 0);
274
 
  assert(pthread_cond_destroy(&control.cv) == 0);
275
 
  assert(pthread_mutex_destroy(&control.mx) == 0);
276
 
 
277
 
  /* Success. */
278
 
  return 0;
279
 
}