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