Debian Codegraph @

src/interfaces/ecpg/test/thread/thread_implicit.pgc

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