]> granicus.if.org Git - postgresql/blob - src/interfaces/ecpg/test/thread/thread_implicit.pgc
More variables gcc moans about
[postgresql] / src / interfaces / ecpg / test / thread / thread_implicit.pgc
1 /*
2  *      Thread test program
3  *      by Lee Kindness.
4  */
5
6 #include <stdlib.h>
7 #include "ecpg_config.h"
8
9 #ifndef ENABLE_THREAD_SAFETY
10 int
11 main(void)
12 {
13         printf("No threading enabled.\n");
14         return 0;
15 }
16 #else
17 #ifndef WIN32
18 #include <pthread.h>
19 #else
20 #include <windows.h>
21 #endif
22
23 exec sql include ../regression;
24
25 void *test_thread(void *arg);
26
27 int nthreads   = 10;
28 int iterations = 20;
29
30 int main()
31 {
32 #ifndef WIN32
33   pthread_t *threads;
34 #else
35   HANDLE *threads;
36 #endif
37   int n;
38   EXEC SQL BEGIN DECLARE SECTION;
39   int l_rows;
40   EXEC SQL END DECLARE SECTION;
41
42  /* Do not switch on debug output for regression tests. The threads get executed in
43   * more or less random order */
44  /* ECPGdebug(1, stderr); */
45
46   /* setup test_thread table */
47   EXEC SQL CONNECT TO REGRESSDB1;
48   EXEC SQL DROP TABLE test_thread; /* DROP might fail */
49   EXEC SQL COMMIT;
50   EXEC SQL CREATE TABLE
51     test_thread(tstamp    TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
52                 thread    TEXT      NOT NULL,
53                 iteration INTEGER   NOT NULL,
54                 PRIMARY KEY(thread, iteration));
55   EXEC SQL COMMIT;
56   EXEC SQL DISCONNECT;
57
58   /* create, and start, threads */
59   threads = calloc(nthreads, sizeof(threads[0]));
60   if( threads == NULL )
61     {
62       fprintf(stderr, "Cannot alloc memory\n");
63       return( 1 );
64     }
65   for( n = 0; n < nthreads; n++ )
66     {
67 #ifndef WIN32
68       pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
69 #else
70       threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
71 #endif
72     }
73
74   /* wait for thread completion */
75 #ifndef WIN32
76   for( n = 0; n < nthreads; n++ )
77     {
78       pthread_join(threads[n], NULL);
79     }
80 #else
81   WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
82 #endif
83   free(threads);
84
85   /* and check results */
86   EXEC SQL CONNECT TO REGRESSDB1;
87   EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
88   EXEC SQL COMMIT;
89   EXEC SQL DISCONNECT;
90   if( l_rows == (nthreads * iterations) )
91     printf("Success.\n");
92   else
93     printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
94
95   return( 0 );
96 }
97
98 void *test_thread(void *arg)
99 {
100   long threadnum = (long)arg;
101   EXEC SQL BEGIN DECLARE SECTION;
102   int  l_i;
103   char l_connection[128];
104   EXEC SQL END DECLARE SECTION;
105
106   /* build up connection name, and connect to database */
107 #ifndef WIN32_ONLY_COMPILER
108   snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
109 #else
110   _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
111 #endif
112   EXEC SQL WHENEVER sqlerror sqlprint;
113   EXEC SQL CONNECT TO REGRESSDB1 AS :l_connection;
114   if( sqlca.sqlcode != 0 )
115     {
116       printf("%s: ERROR: cannot connect to database!\n", l_connection);
117       return( NULL );
118     }
119   EXEC SQL BEGIN;
120
121   /* insert into test_thread table */
122   for( l_i = 1; l_i <= iterations; l_i++ )
123     {
124       EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
125       if( sqlca.sqlcode != 0 )
126         printf("%s: ERROR: insert failed!\n", l_connection);
127     }
128
129   /* all done */
130   EXEC SQL COMMIT;
131   EXEC SQL DISCONNECT :l_connection;
132   return( NULL );
133 }
134 #endif /* ENABLE_THREAD_SAFETY */