LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 34 40 85.0 %
Date: 2019-08-24 15:07:19 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13