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