Header And Logo

PostgreSQL
| The world's most advanced open source database.

thread-thread_implicit.c

Go to the documentation of this file.
00001 /* Processed by ecpg (regression mode) */
00002 /* These include files are added by the preprocessor */
00003 #include <ecpglib.h>
00004 #include <ecpgerrno.h>
00005 #include <sqlca.h>
00006 /* End of automatic include section */
00007 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
00008 
00009 #line 1 "thread_implicit.pgc"
00010 /*
00011  *  Thread test program
00012  *  by Lee Kindness.
00013  */
00014 
00015 #include <stdlib.h>
00016 #include "ecpg_config.h"
00017 
00018 #ifndef ENABLE_THREAD_SAFETY
00019 int
00020 main(void)
00021 {
00022     printf("No threading enabled.\n");
00023     return 0;
00024 }
00025 #else
00026 #ifndef WIN32
00027 #include <pthread.h>
00028 #else
00029 #include <windows.h>
00030 #endif
00031 
00032 
00033 #line 1 "regression.h"
00034 
00035 
00036 
00037 
00038 
00039 
00040 #line 23 "thread_implicit.pgc"
00041 
00042 
00043 void *test_thread(void *arg);
00044 
00045 int nthreads   = 10;
00046 int iterations = 20;
00047 
00048 int main()
00049 {
00050 #ifndef WIN32
00051   pthread_t *threads;
00052 #else
00053   HANDLE *threads;
00054 #endif
00055   int n;
00056   /* exec sql begin declare section */
00057    
00058   
00059 #line 39 "thread_implicit.pgc"
00060  int l_rows ;
00061 /* exec sql end declare section */
00062 #line 40 "thread_implicit.pgc"
00063 
00064 
00065  /* Do not switch on debug output for regression tests. The threads get executed in
00066   * more or less random order */
00067  /* ECPGdebug(1, stderr); */
00068 
00069   /* setup test_thread table */
00070   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
00071 #line 47 "thread_implicit.pgc"
00072 
00073   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
00074 #line 48 "thread_implicit.pgc"
00075  /* DROP might fail */
00076   { ECPGtrans(__LINE__, NULL, "commit");}
00077 #line 49 "thread_implicit.pgc"
00078 
00079   { 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);}
00080 #line 54 "thread_implicit.pgc"
00081 
00082   { ECPGtrans(__LINE__, NULL, "commit");}
00083 #line 55 "thread_implicit.pgc"
00084 
00085   { ECPGdisconnect(__LINE__, "CURRENT");}
00086 #line 56 "thread_implicit.pgc"
00087 
00088 
00089   /* create, and start, threads */
00090   threads = calloc(nthreads, sizeof(threads[0]));
00091   if( threads == NULL )
00092     {
00093       fprintf(stderr, "Cannot alloc memory\n");
00094       return( 1 );
00095     }
00096   for( n = 0; n < nthreads; n++ )
00097     {
00098 #ifndef WIN32
00099       pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
00100 #else
00101       threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
00102 #endif
00103     }
00104 
00105   /* wait for thread completion */
00106 #ifndef WIN32
00107   for( n = 0; n < nthreads; n++ )
00108     {
00109       pthread_join(threads[n], NULL);
00110     }
00111 #else
00112   WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
00113 #endif
00114   free(threads);
00115 
00116   /* and check results */
00117   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
00118 #line 86 "thread_implicit.pgc"
00119 
00120   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT, 
00121     ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int), 
00122     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00123 #line 87 "thread_implicit.pgc"
00124 
00125   { ECPGtrans(__LINE__, NULL, "commit");}
00126 #line 88 "thread_implicit.pgc"
00127 
00128   { ECPGdisconnect(__LINE__, "CURRENT");}
00129 #line 89 "thread_implicit.pgc"
00130 
00131   if( l_rows == (nthreads * iterations) )
00132     printf("Success.\n");
00133   else
00134     printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
00135 
00136   return( 0 );
00137 }
00138 
00139 void *test_thread(void *arg)
00140 {
00141   long threadnum = (long)arg;
00142   /* exec sql begin declare section */
00143     
00144    
00145   
00146 #line 102 "thread_implicit.pgc"
00147  int l_i ;
00148  
00149 #line 103 "thread_implicit.pgc"
00150  char l_connection [ 128 ] ;
00151 /* exec sql end declare section */
00152 #line 104 "thread_implicit.pgc"
00153 
00154 
00155   /* build up connection name, and connect to database */
00156 #ifndef WIN32_ONLY_COMPILER
00157   snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
00158 #else
00159   _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
00160 #endif
00161   /* exec sql whenever sqlerror  sqlprint ; */
00162 #line 112 "thread_implicit.pgc"
00163 
00164   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , l_connection, 0); 
00165 #line 113 "thread_implicit.pgc"
00166 
00167 if (sqlca.sqlcode < 0) sqlprint();}
00168 #line 113 "thread_implicit.pgc"
00169 
00170   if( sqlca.sqlcode != 0 )
00171     {
00172       printf("%s: ERROR: cannot connect to database!\n", l_connection);
00173       return( NULL );
00174     }
00175   { ECPGtrans(__LINE__, NULL, "begin");
00176 #line 119 "thread_implicit.pgc"
00177 
00178 if (sqlca.sqlcode < 0) sqlprint();}
00179 #line 119 "thread_implicit.pgc"
00180 
00181 
00182   /* insert into test_thread table */
00183   for( l_i = 1; l_i <= iterations; l_i++ )
00184     {
00185       { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1  , $2  )", 
00186     ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char), 
00187     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
00188     ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int), 
00189     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
00190 #line 124 "thread_implicit.pgc"
00191 
00192 if (sqlca.sqlcode < 0) sqlprint();}
00193 #line 124 "thread_implicit.pgc"
00194 
00195       if( sqlca.sqlcode != 0 )
00196     printf("%s: ERROR: insert failed!\n", l_connection);
00197     }
00198 
00199   /* all done */
00200   { ECPGtrans(__LINE__, NULL, "commit");
00201 #line 130 "thread_implicit.pgc"
00202 
00203 if (sqlca.sqlcode < 0) sqlprint();}
00204 #line 130 "thread_implicit.pgc"
00205 
00206   { ECPGdisconnect(__LINE__, l_connection);
00207 #line 131 "thread_implicit.pgc"
00208 
00209 if (sqlca.sqlcode < 0) sqlprint();}
00210 #line 131 "thread_implicit.pgc"
00211 
00212   return( NULL );
00213 }
00214 #endif /* ENABLE_THREAD_SAFETY */