LCOV - differential code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread_implicit.pgc (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 85.0 % 40 34 6 34
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 2 2 2
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a