File: thread-test.c

package info (click to toggle)
duma 2.5.21-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,460 kB
  • sloc: ansic: 5,512; cpp: 2,205; makefile: 441; javascript: 191; sh: 129
file content (138 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * source URL:
 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=67838
 */

/*
 * Author:  Petter Reinholdtsen <pere@hungry.com>
 * Date:    2002-02-13
 * Lisence: GPL
 *
 * Small test program to demonstrate efence crashing on multithreaded
 * programs.

 * Link it like this, and it crashes with segfault.
 *  cc -o efence-thread-test efence-thread-test.c  -lpthread -lefence
 * Remove the -lefence, and it work as it should.
 * The output from gdb when it fails:
 *
 * Program received signal SIGSEGV, Segmentation fault.
 * [Switching to Thread 1026 (LWP 1189)]
 * 0x400382c7 in memalign () from /usr/lib/libefence.so.0
 * (gdb) bt
 * #0  0x400382c7 in memalign () from /usr/lib/libefence.so.0
 * #1  0x4003873c in malloc () from /usr/lib/libefence.so.0
 * #2  0x400a0811 in fopen () from /lib/libc.so.6
 * #3  0x080488e8 in thread_func ()
 * #4  0x40027f0a in pthread_start_thread () from /lib/libpthread.so.0
 * #5  0x40027f51 in pthread_start_thread_event () from /lib/libpthread.so.0
 */

/* check for pthread library */
#if (!defined(WIN32) || defined(__CYGWIN__))
#define HAVE_PTHREADS 1
#else
#define HAVE_PTHREADS 0
#endif


#include <stdlib.h>
#include <stdio.h>

#if HAVE_PTHREADS

#include <pthread.h>
#include "../duma.h"

static pthread_mutex_t mutex;
static volatile int threads_left = 2;


static void* thread_func(void *arg)
{
  int i = 2000;
  char *name = (char*)arg;

  while (--i)
  {
	if (pthread_mutex_lock(&mutex))
	{
	  fprintf(stderr, "error: %s failed to lock mutex.\n", name);
	  exit(1);
	}
	printf ("%s : %d\n", name, i);
	if (pthread_mutex_unlock(&mutex))
	{
	  fprintf(stderr, "error: %s failed to unlock mutex.\n", name);
	  exit(1);
	}

	{
	  /* Try to trigger efence error */
	  FILE *fp = fopen("/etc/resolv.conf", "r");
	  if (NULL != fp)
	  {
		char buf[1024];
		fread(buf, sizeof(buf), 1, fp);
		fclose(fp);
	  }
	}
  }
  --threads_left;
  return NULL;
}


static void*
idle_func(void* arg)
{
  (void)arg;

  while (threads_left) ;

  /* NOTREACHED */
  return NULL;
}


int main(int argc, char **argv)
{
  pthread_t hello_thread, goodbye_thread;

  (void)argc;
  (void)argv;

  pthread_mutex_init(&mutex, NULL);

  if (pthread_create(&hello_thread, NULL, thread_func, (void*)"hello"))
  {
	fprintf(stderr, "Failed to create hello thread\n");
	exit(1);
  }

  if (pthread_create(&goodbye_thread, NULL, thread_func, (void*)"goodbye"))
  {
	fprintf(stderr, "Failed to create goodbye thread\n");
	exit(1);
  }

  idle_func(NULL);

  pthread_mutex_destroy(&mutex);

  /* NOTREACHED */
  return 0;
}

#else

int main(int argc, char **argv)
{
  (void)argc;
  (void)argv;

  printf("Test not implemented for Win32 and Mingw\n");
  return 0;
}

#endif