~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to exp-drd/tests/pth_cond_race.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrés Roldán
  • Date: 2008-06-13 02:31:40 UTC
  • mto: (1.4.1 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20080613023140-iwk33rz9rhvfkr96
Import upstream version 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Unit test for drd that triggers a race on the use of a POSIX condition
 
2
   variable. By Bart Van Assche.
 
3
*/
 
4
 
 
5
#include <assert.h>
 
6
#include <stdio.h>      // printf()
 
7
#include <pthread.h>
 
8
#include <unistd.h>    // usleep()
 
9
#include "../drd_clientreq.h"
 
10
 
 
11
 
 
12
// Local functions declarations.
 
13
 
 
14
static void* thread_func(void* thread_arg);
 
15
 
 
16
 
 
17
// Local variables.
 
18
 
 
19
static pthread_mutex_t s_mutex;
 
20
static pthread_cond_t  s_cond;
 
21
static int             s_use_mutex = 0;
 
22
 
 
23
 
 
24
// Function definitions.
 
25
 
 
26
static void set_thread_name(const char* const name)
 
27
{
 
28
  int res;
 
29
  VALGRIND_DO_CLIENT_REQUEST(res, 0, VG_USERREQ__SET_THREAD_NAME,
 
30
                             "%s", name, 0, 0, 0);
 
31
}
 
32
 
 
33
int main(int argc, char** argv)
 
34
{
 
35
  int optchar;
 
36
  pthread_t threadid;
 
37
 
 
38
  set_thread_name("main");
 
39
 
 
40
  while ((optchar = getopt(argc, argv, "m")) != EOF)
 
41
  {
 
42
    switch (optchar)
 
43
    {
 
44
    case 'm':
 
45
      s_use_mutex = 1;
 
46
      break;
 
47
    default:
 
48
      assert(0);
 
49
    }
 
50
  }
 
51
 
 
52
  pthread_cond_init(&s_cond, 0);
 
53
  pthread_mutex_init(&s_mutex, 0);
 
54
  pthread_mutex_lock(&s_mutex);
 
55
 
 
56
  pthread_create(&threadid, 0, thread_func, 0);
 
57
 
 
58
  pthread_cond_wait(&s_cond, &s_mutex);
 
59
  pthread_mutex_unlock(&s_mutex);
 
60
 
 
61
  pthread_join(threadid, 0);
 
62
 
 
63
  pthread_mutex_destroy(&s_mutex);
 
64
  pthread_cond_destroy(&s_cond);
 
65
 
 
66
  return 0;
 
67
}
 
68
 
 
69
static void* thread_func(void* thread_arg)
 
70
{
 
71
  set_thread_name("thread_func");
 
72
 
 
73
  // Wait until the main thread has entered pthread_cond_wait().
 
74
  pthread_mutex_lock(&s_mutex);
 
75
  pthread_mutex_unlock(&s_mutex);
 
76
 
 
77
  // Signal the condition variable.
 
78
  if (s_use_mutex) pthread_mutex_lock(&s_mutex);
 
79
  pthread_cond_signal(&s_cond);
 
80
  if (s_use_mutex) pthread_mutex_unlock(&s_mutex);
 
81
 
 
82
  return 0;
 
83
}