LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 85.0 % 40 34
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 62.5 % 24 15

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

Generated by: LCOV version 2.0-1