LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread.pgc (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 50 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 2 0
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.3.2-1