LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread_implicit.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 34 40 85.0 %
Date: 2024-11-21 08:14:44 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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           4 : 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           4 :   EXEC SQL CONNECT TO REGRESSDB1;
      41           4 :   EXEC SQL DROP TABLE test_thread; /* DROP might fail */
      42           4 :   EXEC SQL COMMIT;
      43           4 :   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           4 :   EXEC SQL COMMIT;
      49           4 :   EXEC SQL DISCONNECT;
      50             : 
      51             :   /* create, and start, threads */
      52           4 :   threads = calloc(nthreads, sizeof(threads[0]));
      53           4 :   if( threads == NULL )
      54             :     {
      55           0 :       fprintf(stderr, "Cannot alloc memory\n");
      56           0 :       return 1;
      57             :     }
      58          44 :   for( n = 0; n < nthreads; n++ )
      59             :     {
      60             : #ifndef WIN32
      61          40 :       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          44 :   for( n = 0; n < nthreads; n++ )
      70             :     {
      71          40 :       pthread_join(threads[n], NULL);
      72             :     }
      73             : #else
      74             :   WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
      75             : #endif
      76           4 :   free(threads);
      77             : 
      78             :   /* and check results */
      79           4 :   EXEC SQL CONNECT TO REGRESSDB1;
      80           4 :   EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
      81           4 :   EXEC SQL COMMIT;
      82           4 :   EXEC SQL DISCONNECT;
      83           4 :   if( l_rows == (nthreads * iterations) )
      84           4 :     printf("Success.\n");
      85             :   else
      86           0 :     printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
      87             : 
      88           4 :   return 0;
      89             : }
      90             : 
      91          40 : void *test_thread(void *arg)
      92             : {
      93          40 :   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          40 :   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          40 :   EXEC SQL CONNECT TO REGRESSDB1 AS :l_connection;
     108          40 :   if( sqlca.sqlcode != 0 )
     109             :     {
     110           0 :       printf("%s: ERROR: cannot connect to database!\n", l_connection);
     111           0 :       return NULL;
     112             :     }
     113          40 :   EXEC SQL BEGIN;
     114          40 : 
     115             :   /* insert into test_thread table */
     116         840 :   for( l_i = 1; l_i <= iterations; l_i++ )
     117             :     {
     118         800 :       EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
     119         800 :       if( sqlca.sqlcode != 0 )
     120           0 :     printf("%s: ERROR: insert failed!\n", l_connection);
     121             :     }
     122             : 
     123             :   /* all done */
     124          40 :   EXEC SQL COMMIT;
     125          40 :   EXEC SQL DISCONNECT :l_connection;
     126          40 :   return NULL;
     127             : }

Generated by: LCOV version 1.14