PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
thread-thread.c
Go to the documentation of this file.
1 /* Processed by ecpg (regression mode) */
2 /* These include files are added by the preprocessor */
3 #include <ecpglib.h>
4 #include <ecpgerrno.h>
5 #include <sqlca.h>
6 /* End of automatic include section */
7 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
8 
9 #line 1 "thread.pgc"
10 /*
11  * Thread test program
12  * by Philip Yarra & Lee Kindness.
13  */
14 #include <stdlib.h>
15 #include "ecpg_config.h"
16 
17 #ifndef ENABLE_THREAD_SAFETY
18 int
19 main(void)
20 {
21  printf("No threading enabled.\n");
22  return 0;
23 }
24 #else
25 #ifndef WIN32
26 #include <pthread.h>
27 #else
28 #include <windows.h>
29 #endif
30 
31 
32 #line 1 "regression.h"
33 
34 
35 
36 
37 
38 
39 #line 22 "thread.pgc"
40 
41 
42 void *test_thread(void *arg);
43 
44 int nthreads = 10;
45 int iterations = 20;
46 
47 int main()
48 {
49 #ifndef WIN32
50  pthread_t *threads;
51 #else
52  HANDLE *threads;
53 #endif
54  int n;
55  /* exec sql begin declare section */
56 
57 
58 #line 38 "thread.pgc"
59  int l_rows ;
60 /* exec sql end declare section */
61 #line 39 "thread.pgc"
62 
63 
64  /* Do not switch on debug output for regression tests. The threads get executed in
65  * more or less random order */
66  /* ECPGdebug(1, stderr); */
67 
68  /* setup test_thread table */
69  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
70 #line 46 "thread.pgc"
71 
72  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
73 #line 47 "thread.pgc"
74  /* DROP might fail */
75  { ECPGtrans(__LINE__, NULL, "commit");}
76 #line 48 "thread.pgc"
77 
78  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test_thread ( tstamp timestamp not null default cast ( timeofday ( ) as timestamp ) , thread text not null , iteration integer not null , primary key ( thread , iteration ) )", ECPGt_EOIT, ECPGt_EORT);}
79 #line 53 "thread.pgc"
80 
81  { ECPGtrans(__LINE__, NULL, "commit");}
82 #line 54 "thread.pgc"
83 
84  { ECPGdisconnect(__LINE__, "CURRENT");}
85 #line 55 "thread.pgc"
86 
87 
88  /* create, and start, threads */
89  threads = calloc(nthreads, sizeof(threads[0]));
90  if( threads == NULL )
91  {
92  fprintf(stderr, "Cannot alloc memory\n");
93  return( 1 );
94  }
95  for( n = 0; n < nthreads; n++ )
96  {
97 #ifndef WIN32
98  pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
99 #else
100  threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_thread, (void *) (n + 1), 0, NULL);
101 #endif
102  }
103 
104  /* wait for thread completion */
105 #ifndef WIN32
106  for( n = 0; n < nthreads; n++ )
107  {
108  pthread_join(threads[n], NULL);
109  }
110 #else
111  WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
112 #endif
113  free(threads);
114 
115  /* and check results */
116  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
117 #line 85 "thread.pgc"
118 
119  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT,
120  ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int),
121  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
122 #line 86 "thread.pgc"
123 
124  { ECPGtrans(__LINE__, NULL, "commit");}
125 #line 87 "thread.pgc"
126 
127  { ECPGdisconnect(__LINE__, "CURRENT");}
128 #line 88 "thread.pgc"
129 
130  if( l_rows == (nthreads * iterations) )
131  printf("Success.\n");
132  else
133  printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
134 
135  return( 0 );
136 }
137 
138 void *test_thread(void *arg)
139 {
140  long threadnum = (long)arg;
141  /* exec sql begin declare section */
142 
143 
144 
145 #line 101 "thread.pgc"
146  int l_i ;
147 
148 #line 102 "thread.pgc"
149  char l_connection [ 128 ] ;
150 /* exec sql end declare section */
151 #line 103 "thread.pgc"
152 
153 
154  /* build up connection name, and connect to database */
155 #ifndef WIN32_ONLY_COMPILER
156  snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
157 #else
158  _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
159 #endif
160  /* exec sql whenever sqlerror sqlprint ; */
161 #line 111 "thread.pgc"
162 
163  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , l_connection, 0);
164 #line 112 "thread.pgc"
165 
166 if (sqlca.sqlcode < 0) sqlprint();}
167 #line 112 "thread.pgc"
168 
169  if( sqlca.sqlcode != 0 )
170  {
171  printf("%s: ERROR: cannot connect to database!\n", l_connection);
172  return( NULL );
173  }
174  { ECPGtrans(__LINE__, l_connection, "begin");
175 #line 118 "thread.pgc"
176 
177 if (sqlca.sqlcode < 0) sqlprint();}
178 #line 118 "thread.pgc"
179 
180 
181  /* insert into test_thread table */
182  for( l_i = 1; l_i <= iterations; l_i++ )
183  {
184  { ECPGdo(__LINE__, 0, 1, l_connection, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1 , $2 )",
185  ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char),
186  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
187  ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int),
188  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
189 #line 123 "thread.pgc"
190 
191 if (sqlca.sqlcode < 0) sqlprint();}
192 #line 123 "thread.pgc"
193 
194  if( sqlca.sqlcode != 0 )
195  printf("%s: ERROR: insert failed!\n", l_connection);
196  }
197 
198  /* all done */
199  { ECPGtrans(__LINE__, l_connection, "commit");
200 #line 129 "thread.pgc"
201 
202 if (sqlca.sqlcode < 0) sqlprint();}
203 #line 129 "thread.pgc"
204 
205  { ECPGdisconnect(__LINE__, l_connection);
206 #line 130 "thread.pgc"
207 
208 if (sqlca.sqlcode < 0) sqlprint();}
209 #line 130 "thread.pgc"
210 
211  return( NULL );
212 }
213 #endif /* ENABLE_THREAD_SAFETY */
#define calloc(a, b)
Definition: header.h:50
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:669
int main(void)
Definition: thread-thread.c:19
#define sqlca
Definition: sqlca.h:59
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2030
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:271
int nthreads
Definition: pgbench.c:175
void sqlprint(void)
Definition: error.c:336
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:199
void * arg
#define TRUE
Definition: c.h:214
#define pthread_t
Definition: pgbench.c:79