~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * File: condvar3_1.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
 
 * - Test timeout of multiple waits on a CV with some signaled.
38
 
 *
39
 
 * Test Method (Validation or Falsification):
40
 
 * - Validation
41
 
 *
42
 
 * Requirements Tested:
43
 
 * - 
44
 
 *
45
 
 * Features Tested:
46
 
 * - 
47
 
 *
48
 
 * Cases Tested:
49
 
 * - 
50
 
 *
51
 
 * Description:
52
 
 * - Because some CVs are never signaled, we expect their waits to time out.
53
 
 *   Some are signaled, the rest time out. Pthread_cond_destroy() will fail
54
 
 *   unless all are accounted for, either signaled or timedout.
55
 
 *
56
 
 * Environment:
57
 
 * -
58
 
 *
59
 
 * Input:
60
 
 * - None.
61
 
 *
62
 
 * Output:
63
 
 * - File name, Line number, and failed expression on failure.
64
 
 * - No output on success.
65
 
 *
66
 
 * Assumptions:
67
 
 * - 
68
 
 *
69
 
 * Pass Criteria:
70
 
 * - pthread_cond_timedwait returns ETIMEDOUT.
71
 
 * - Process returns zero exit status.
72
 
 *
73
 
 * Fail Criteria:
74
 
 * - pthread_cond_timedwait does not return ETIMEDOUT.
75
 
 * - Process returns non-zero exit status.
76
 
 */
77
 
 
78
 
#define _WIN32_WINNT 0x400
79
 
 
80
 
#include "test.h"
81
 
#include <sys/timeb.h>
82
 
 
83
 
static pthread_cond_t cv;
84
 
static pthread_cond_t cv1;
85
 
static pthread_mutex_t mutex;
86
 
static pthread_mutex_t mutex1;
87
 
static struct timespec abstime = { 0, 0 };
88
 
static int timedout = 0;
89
 
static int signaled = 0;
90
 
static int awoken = 0;
91
 
static int waiting = 0;
92
 
 
93
 
enum {
94
 
  NUMTHREADS = 30
95
 
};
96
 
 
97
 
void *
98
 
mythread(void * arg)
99
 
{
100
 
  int result;
101
 
 
102
 
  assert(pthread_mutex_lock(&mutex1) == 0);
103
 
  ++waiting;
104
 
  assert(pthread_mutex_unlock(&mutex1) == 0);
105
 
  assert(pthread_cond_signal(&cv1) == 0);
106
 
 
107
 
  assert(pthread_mutex_lock(&mutex) == 0);
108
 
  result = pthread_cond_timedwait(&cv, &mutex, &abstime);
109
 
  if (result == ETIMEDOUT)
110
 
    {
111
 
      timedout++;
112
 
    }
113
 
  else
114
 
    {
115
 
      awoken++;
116
 
    }
117
 
  assert(pthread_mutex_unlock(&mutex) == 0);
118
 
 
119
 
  return arg;
120
 
}
121
 
 
122
 
#include "../implement.h"
123
 
 
124
 
int
125
 
main()
126
 
{
127
 
  int i;
128
 
  pthread_t t[NUMTHREADS + 1];
129
 
  int result = 0;
130
 
  struct _timeb currSysTime;
131
 
  const DWORD NANOSEC_PER_MILLISEC = 1000000;
132
 
 
133
 
  assert(pthread_cond_init(&cv, NULL) == 0);
134
 
  assert(pthread_cond_init(&cv1, NULL) == 0);
135
 
 
136
 
  assert(pthread_mutex_init(&mutex, NULL) == 0);
137
 
  assert(pthread_mutex_init(&mutex1, NULL) == 0);
138
 
 
139
 
  /* get current system time */
140
 
  _ftime(&currSysTime);
141
 
 
142
 
  abstime.tv_sec = currSysTime.time;
143
 
  abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
144
 
 
145
 
  abstime.tv_sec += 5;
146
 
 
147
 
  assert(pthread_mutex_lock(&mutex1) == 0);
148
 
 
149
 
  for (i = 1; i <= NUMTHREADS; i++)
150
 
    {
151
 
      assert(pthread_create(&t[i], NULL, mythread, (void *) i) == 0);
152
 
    }
153
 
 
154
 
  do {
155
 
    assert(pthread_cond_wait(&cv1,&mutex1) == 0);
156
 
  } while ( NUMTHREADS > waiting );
157
 
 
158
 
  assert(pthread_mutex_unlock(&mutex1) == 0);
159
 
 
160
 
  for (i = NUMTHREADS/3; i <= 2*NUMTHREADS/3; i++)
161
 
    {
162
 
//      assert(pthread_mutex_lock(&mutex) == 0);
163
 
      assert(pthread_cond_signal(&cv) == 0);
164
 
//      assert(pthread_mutex_unlock(&mutex) == 0);
165
 
 
166
 
      signaled++;
167
 
    }
168
 
 
169
 
  for (i = 1; i <= NUMTHREADS; i++)
170
 
    {
171
 
      assert(pthread_join(t[i], (void **) &result) == 0);
172
 
        assert(result == i);
173
 
    }
174
 
 
175
 
      fprintf(stderr, "awk = %d\n", awoken);
176
 
      fprintf(stderr, "sig = %d\n", signaled);
177
 
      fprintf(stderr, "tot = %d\n", timedout);
178
 
 
179
 
  assert(signaled == awoken);
180
 
  assert(timedout == NUMTHREADS - signaled);
181
 
 
182
 
  assert(pthread_cond_destroy(&cv1) == 0);
183
 
 
184
 
  {
185
 
  int result = pthread_cond_destroy(&cv);
186
 
  if (result != 0)
187
 
    {
188
 
      fprintf(stderr, "Result = %s\n", error_string[result]);
189
 
        fprintf(stderr, "\tWaitersBlocked = %ld\n", cv->nWaitersBlocked);
190
 
        fprintf(stderr, "\tWaitersGone = %ld\n", cv->nWaitersGone);
191
 
        fprintf(stderr, "\tWaitersToUnblock = %ld\n", cv->nWaitersToUnblock);
192
 
        fflush(stderr);
193
 
    }
194
 
  assert(result == 0);
195
 
  }
196
 
 
197
 
  assert(pthread_mutex_destroy(&mutex1) == 0);
198
 
  assert(pthread_mutex_destroy(&mutex) == 0);
199
 
 
200
 
  return 0;
201
 
}