LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread_implicit.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 34 40 85.0 %
Date: 2019-06-18 07:06:57 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             : 
       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           4 : int main()
      32             : {
      33             : #ifndef WIN32
      34             :   pthread_t *threads;
      35             : #else
      36             :   HANDLE *threads;
      37             : #endif
      38             :   int 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           4 :   EXEC SQL CONNECT TO REGRESSDB1;
      49           4 :   EXEC SQL DROP TABLE test_thread; /* DROP might fail */
      50           4 :   EXEC SQL COMMIT;
      51           4 :   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           4 :   EXEC SQL COMMIT;
      57           4 :   EXEC SQL DISCONNECT;
      58             : 
      59             :   /* create, and start, threads */
      60           4 :   threads = calloc(nthreads, sizeof(threads[0]));
      61           4 :   if( threads == NULL )
      62             :     {
      63           0 :       fprintf(stderr, "Cannot alloc memory\n");
      64           0 :       return 1;
      65             :     }
      66          44 :   for( n = 0; n < nthreads; n++ )
      67             :     {
      68             : #ifndef WIN32
      69          40 :       pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
      70             : #else
      71             :       threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
      72             : #endif
      73             :     }
      74             : 
      75             :   /* wait for thread completion */
      76             : #ifndef WIN32
      77          44 :   for( n = 0; n < nthreads; n++ )
      78             :     {
      79          40 :       pthread_join(threads[n], NULL);
      80             :     }
      81             : #else
      82             :   WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
      83             : #endif
      84           4 :   free(threads);
      85             : 
      86             :   /* and check results */
      87           4 :   EXEC SQL CONNECT TO REGRESSDB1;
      88           4 :   EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
      89           4 :   EXEC SQL COMMIT;
      90           4 :   EXEC SQL DISCONNECT;
      91           4 :   if( l_rows == (nthreads * iterations) )
      92           4 :     printf("Success.\n");
      93             :   else
      94           0 :     printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
      95             : 
      96           4 :   return 0;
      97             : }
      98             : 
      99          40 : void *test_thread(void *arg)
     100             : {
     101          40 :   long threadnum = (long)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          40 :   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          40 :   EXEC SQL CONNECT TO REGRESSDB1 AS :l_connection;
     116          40 :   if( sqlca.sqlcode != 0 )
     117             :     {
     118           0 :       printf("%s: ERROR: cannot connect to database!\n", l_connection);
     119           0 :       return NULL;
     120             :     }
     121          40 :   EXEC SQL BEGIN;
     122          40 : 
     123             :   /* insert into test_thread table */
     124         840 :   for( l_i = 1; l_i <= iterations; l_i++ )
     125             :     {
     126         800 :       EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
     127         800 :       if( sqlca.sqlcode != 0 )
     128           0 :     printf("%s: ERROR: insert failed!\n", l_connection);
     129             :     }
     130             : 
     131             :   /* all done */
     132          40 :   EXEC SQL COMMIT;
     133          40 :   EXEC SQL DISCONNECT :l_connection;
     134          40 :   return NULL;
     135             : }
     136             : #endif /* ENABLE_THREAD_SAFETY */

Generated by: LCOV version 1.13