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

« back to all changes in this revision

Viewing changes to win32/3rdparty/pthreads/tests/condvar3.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.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 basic function of a CV
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
 
 * - The primary thread takes the lock before creating any threads.
53
 
 *   The secondary thread blocks on the lock allowing the primary
54
 
 *   thread to enter the cv wait state which releases the lock.
55
 
 *   The secondary thread then takes the lock and signals the waiting
56
 
 *   primary thread.
57
 
 *
58
 
 * Environment:
59
 
 * - 
60
 
 *
61
 
 * Input:
62
 
 * - None.
63
 
 *
64
 
 * Output:
65
 
 * - File name, Line number, and failed expression on failure.
66
 
 * - No output on success.
67
 
 *
68
 
 * Assumptions:
69
 
 * - 
70
 
 *
71
 
 * Pass Criteria:
72
 
 * - pthread_cond_timedwait returns 0.
73
 
 * - Process returns zero exit status.
74
 
 *
75
 
 * Fail Criteria:
76
 
 * - pthread_cond_timedwait returns ETIMEDOUT.
77
 
 * - Process returns non-zero exit status.
78
 
 */
79
 
 
80
 
#include "test.h"
81
 
#include <sys/timeb.h>
82
 
 
83
 
static pthread_cond_t cv;
84
 
static pthread_mutex_t mutex;
85
 
static int shared = 0;
86
 
 
87
 
enum {
88
 
  NUMTHREADS = 2         /* Including the primary thread. */
89
 
};
90
 
 
91
 
void *
92
 
mythread(void * arg)
93
 
{
94
 
  int result = 0;
95
 
 
96
 
  assert(pthread_mutex_lock(&mutex) == 0);
97
 
  shared++;
98
 
  assert(pthread_mutex_unlock(&mutex) == 0);
99
 
 
100
 
  if ((result = pthread_cond_signal(&cv)) != 0)
101
 
    {
102
 
      printf("Error = %s\n", error_string[result]);
103
 
    }
104
 
  assert(result == 0);
105
 
 
106
 
 
107
 
  return (void *) 0;
108
 
}
109
 
 
110
 
int
111
 
main()
112
 
{
113
 
  pthread_t t[NUMTHREADS];
114
 
  struct timespec abstime = { 0, 0 };
115
 
  struct _timeb currSysTime;
116
 
  const DWORD NANOSEC_PER_MILLISEC = 1000000;
117
 
 
118
 
  assert((t[0] = pthread_self()).p != NULL);
119
 
 
120
 
  assert(pthread_cond_init(&cv, NULL) == 0);
121
 
 
122
 
  assert(pthread_mutex_init(&mutex, NULL) == 0);
123
 
 
124
 
  assert(pthread_mutex_lock(&mutex) == 0);
125
 
 
126
 
  /* get current system time */
127
 
  _ftime(&currSysTime);
128
 
 
129
 
  abstime.tv_sec = currSysTime.time;
130
 
  abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
131
 
 
132
 
  assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0);
133
 
 
134
 
  abstime.tv_sec += 5;
135
 
 
136
 
  while (! (shared > 0))
137
 
    assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == 0);
138
 
 
139
 
  assert(shared > 0);
140
 
 
141
 
  assert(pthread_mutex_unlock(&mutex) == 0);
142
 
 
143
 
  assert(pthread_join(t[1], NULL) == 0);
144
 
 
145
 
  assert(pthread_cond_destroy(&cv) == 0);
146
 
 
147
 
  return 0;
148
 
}