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