~ubuntu-branches/ubuntu/natty/clamav/natty-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-02-19 09:51:33 UTC
  • mfrom: (0.35.19 sid)
  • Revision ID: james.westby@ubuntu.com-20110219095133-sde2dyj8a6bjbkdh
Tags: 0.97+dfsg-0ubuntu1
* Merge from debian unstable (0ubuntu1 because the Debian upload was
  inadvertently left marked UNRELEASED).  Remaining changes:
  - Drop initial signature definitions from clamav-base
  - 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
 * File: condvar5.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 pthread_cond_broadcast.
 
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
 * - Test broadcast with one waiting CV.
 
53
 *
 
54
 * Environment:
 
55
 * - 
 
56
 *
 
57
 * Input:
 
58
 * - None.
 
59
 *
 
60
 * Output:
 
61
 * - File name, Line number, and failed expression on failure.
 
62
 * - No output on success.
 
63
 *
 
64
 * Assumptions:
 
65
 * - 
 
66
 *
 
67
 * Pass Criteria:
 
68
 * - pthread_cond_timedwait returns 0.
 
69
 * - Process returns zero exit status.
 
70
 *
 
71
 * Fail Criteria:
 
72
 * - pthread_cond_timedwait returns ETIMEDOUT.
 
73
 * - Process returns non-zero exit status.
 
74
 */
 
75
 
 
76
#include "test.h"
 
77
#include <sys/timeb.h>
 
78
 
 
79
typedef struct cvthing_t_ cvthing_t;
 
80
 
 
81
struct cvthing_t_ {
 
82
  pthread_cond_t notbusy;
 
83
  pthread_mutex_t lock;
 
84
  int shared;
 
85
};
 
86
 
 
87
static cvthing_t cvthing = {
 
88
  PTHREAD_COND_INITIALIZER,
 
89
  PTHREAD_MUTEX_INITIALIZER,
 
90
  0
 
91
};
 
92
 
 
93
enum {
 
94
  NUMTHREADS = 2
 
95
};
 
96
 
 
97
void *
 
98
mythread(void * arg)
 
99
{
 
100
  assert(pthread_mutex_lock(&cvthing.lock) == 0);
 
101
  cvthing.shared++;
 
102
  assert(pthread_mutex_unlock(&cvthing.lock) == 0);
 
103
 
 
104
  assert(pthread_cond_broadcast(&cvthing.notbusy) == 0);
 
105
 
 
106
  return (void *) 0;
 
107
}
 
108
 
 
109
int
 
110
main()
 
111
{
 
112
  pthread_t t[NUMTHREADS];
 
113
  struct timespec abstime = { 0, 0 };
 
114
  struct _timeb currSysTime;
 
115
  const DWORD NANOSEC_PER_MILLISEC = 1000000;
 
116
 
 
117
  cvthing.shared = 0;
 
118
 
 
119
  assert((t[0] = pthread_self()).p != NULL);
 
120
 
 
121
  assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
 
122
 
 
123
  assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
 
124
 
 
125
  assert(pthread_mutex_lock(&cvthing.lock) == 0);
 
126
 
 
127
  assert(cvthing.lock != PTHREAD_MUTEX_INITIALIZER);
 
128
 
 
129
  /* get current system time */
 
130
  _ftime(&currSysTime);
 
131
 
 
132
  abstime.tv_sec = currSysTime.time;
 
133
  abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
 
134
 
 
135
  abstime.tv_sec += 5;
 
136
 
 
137
  assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == ETIMEDOUT);
 
138
  
 
139
  assert(cvthing.notbusy != PTHREAD_COND_INITIALIZER);
 
140
 
 
141
  assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0);
 
142
 
 
143
  _ftime(&currSysTime);
 
144
 
 
145
  abstime.tv_sec = currSysTime.time;
 
146
  abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
 
147
 
 
148
  abstime.tv_sec += 5;
 
149
 
 
150
  while (! (cvthing.shared > 0))
 
151
    assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
 
152
 
 
153
  assert(cvthing.shared > 0);
 
154
 
 
155
  assert(pthread_mutex_unlock(&cvthing.lock) == 0);
 
156
 
 
157
  assert(pthread_join(t[1], NULL) == 0);
 
158
 
 
159
  assert(pthread_mutex_destroy(&cvthing.lock) == 0);
 
160
 
 
161
  assert(cvthing.lock == NULL);
 
162
 
 
163
  assert(pthread_cond_destroy(&cvthing.notbusy) == 0);
 
164
 
 
165
  assert(cvthing.notbusy == NULL);
 
166
 
 
167
  return 0;
 
168
}