PostgreSQL Source Code  git master
thread-thread.c File Reference
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
#include <stdint.h>
#include <stdlib.h>
#include "ecpg_config.h"
#include <pthread.h>
Include dependency graph for thread-thread.c:

Go to the source code of this file.

Macros

#define ECPGdebug(X, Y)   ECPGdebug((X)+100,(Y))
 

Functions

void * test_thread (void *arg)
 
int main ()
 

Variables

int nthreads = 10
 
int iterations = 20
 

Macro Definition Documentation

◆ ECPGdebug

#define ECPGdebug (   X,
 
)    ECPGdebug((X)+100,(Y))

Definition at line 7 of file thread-thread.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 41 of file thread-thread.c.

42 {
43 #ifndef WIN32
44  pthread_t *threads;
45 #else
46  HANDLE *threads;
47 #endif
48  intptr_t n;
49  /* exec sql begin declare section */
50 
51 
52 #line 32 "thread.pgc"
53  int l_rows ;
54 /* exec sql end declare section */
55 #line 33 "thread.pgc"
56 
57 
58  /* Do not switch on debug output for regression tests. The threads get executed in
59  * more or less random order */
60  /* ECPGdebug(1, stderr); */
61 
62  /* setup test_thread table */
63  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
64 #line 40 "thread.pgc"
65 
66  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
67 #line 41 "thread.pgc"
68  /* DROP might fail */
69  { ECPGtrans(__LINE__, NULL, "commit");}
70 #line 42 "thread.pgc"
71 
72  { 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);}
73 #line 47 "thread.pgc"
74 
75  { ECPGtrans(__LINE__, NULL, "commit");}
76 #line 48 "thread.pgc"
77 
78  { ECPGdisconnect(__LINE__, "CURRENT");}
79 #line 49 "thread.pgc"
80 
81 
82  /* create, and start, threads */
83  threads = calloc(nthreads, sizeof(threads[0]));
84  if( threads == NULL )
85  {
86  fprintf(stderr, "Cannot alloc memory\n");
87  return 1;
88  }
89  for( n = 0; n < nthreads; n++ )
90  {
91 #ifndef WIN32
92  pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
93 #else
94  threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) (void (*) (void)) test_thread, (void *) (n + 1), 0, NULL);
95 #endif
96  }
97 
98  /* wait for thread completion */
99 #ifndef WIN32
100  for( n = 0; n < nthreads; n++ )
101  {
102  pthread_join(threads[n], NULL);
103  }
104 #else
105  WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
106 #endif
107  free(threads);
108 
109  /* and check results */
110  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
111 #line 79 "thread.pgc"
112 
113  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT,
114  ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int),
115  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
116 #line 80 "thread.pgc"
117 
118  { ECPGtrans(__LINE__, NULL, "commit");}
119 #line 81 "thread.pgc"
120 
121  { ECPGdisconnect(__LINE__, "CURRENT");}
122 #line 82 "thread.pgc"
123 
124  if( l_rows == (nthreads * iterations) )
125  printf("Success.\n");
126  else
127  printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
128 
129  return 0;
130 }
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:673
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:255
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
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:2277
#define calloc(a, b)
Definition: header.h:55
#define free(a)
Definition: header.h:65
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
#define fprintf
Definition: port.h:242
#define printf(...)
Definition: port.h:244
int iterations
Definition: thread-thread.c:39
int nthreads
Definition: thread-thread.c:38
void * test_thread(void *arg)

References calloc, ECPGconnect(), ECPGdisconnect(), ECPGdo(), ECPGst_normal, ECPGt_EOIT, ECPGt_EORT, ECPGt_int, ECPGt_NO_INDICATOR, ECPGtrans(), fprintf, free, iterations, nthreads, printf, and test_thread().

◆ test_thread()

void * test_thread ( void *  arg)

Definition at line 132 of file thread-thread.c.

133 {
134  long threadnum = (intptr_t) arg;
135 
136  /* exec sql begin declare section */
137 
138 
139 
140 #line 96 "thread.pgc"
141  int l_i ;
142 
143 #line 97 "thread.pgc"
144  char l_connection [ 128 ] ;
145 /* exec sql end declare section */
146 #line 98 "thread.pgc"
147 
148 
149  /* build up connection name, and connect to database */
150 #ifndef _MSC_VER
151  snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
152 #else
153  _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
154 #endif
155  /* exec sql whenever sqlerror sqlprint ; */
156 #line 106 "thread.pgc"
157 
158  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , l_connection, 0);
159 #line 107 "thread.pgc"
160 
161 if (sqlca.sqlcode < 0) sqlprint();}
162 #line 107 "thread.pgc"
163 
164  if( sqlca.sqlcode != 0 )
165  {
166  printf("%s: ERROR: cannot connect to database!\n", l_connection);
167  return NULL;
168  }
169  { ECPGtrans(__LINE__, l_connection, "begin");
170 #line 113 "thread.pgc"
171 
172 if (sqlca.sqlcode < 0) sqlprint();}
173 #line 113 "thread.pgc"
174 
175 
176  /* insert into test_thread table */
177  for( l_i = 1; l_i <= iterations; l_i++ )
178  {
179  { ECPGdo(__LINE__, 0, 1, l_connection, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1 , $2 )",
180  ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char),
181  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
182  ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int),
183  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
184 #line 118 "thread.pgc"
185 
186 if (sqlca.sqlcode < 0) sqlprint();}
187 #line 118 "thread.pgc"
188 
189  if( sqlca.sqlcode != 0 )
190  printf("%s: ERROR: insert failed!\n", l_connection);
191  }
192 
193  /* all done */
194  { ECPGtrans(__LINE__, l_connection, "commit");
195 #line 124 "thread.pgc"
196 
197 if (sqlca.sqlcode < 0) sqlprint();}
198 #line 124 "thread.pgc"
199 
200  { ECPGdisconnect(__LINE__, l_connection);
201 #line 125 "thread.pgc"
202 
203 if (sqlca.sqlcode < 0) sqlprint();}
204 #line 125 "thread.pgc"
205 
206  return NULL;
207 }
@ ECPGt_char
Definition: ecpgtype.h:43
void sqlprint(void)
Definition: error.c:334
void * arg
#define snprintf
Definition: port.h:238
#define sqlca
Definition: sqlca.h:59

References arg, ECPGconnect(), ECPGdisconnect(), ECPGdo(), ECPGst_normal, ECPGt_char, ECPGt_EOIT, ECPGt_EORT, ECPGt_int, ECPGt_NO_INDICATOR, ECPGtrans(), iterations, printf, snprintf, sqlca, and sqlprint().

Referenced by main().

Variable Documentation

◆ iterations

◆ nthreads

int nthreads = 10

Definition at line 38 of file thread-thread.c.

Referenced by main().