summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/thread/alloc.pgc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:15:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:15:05 +0000
commit46651ce6fe013220ed397add242004d764fc0153 (patch)
tree6e5299f990f88e60174a1d3ae6e48eedd2688b2b /src/interfaces/ecpg/test/thread/alloc.pgc
parentInitial commit. (diff)
downloadpostgresql-14-upstream.tar.xz
postgresql-14-upstream.zip
Adding upstream version 14.5.upstream/14.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/interfaces/ecpg/test/thread/alloc.pgc')
-rw-r--r--src/interfaces/ecpg/test/thread/alloc.pgc90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/interfaces/ecpg/test/thread/alloc.pgc b/src/interfaces/ecpg/test/thread/alloc.pgc
new file mode 100644
index 0000000..c0021a7
--- /dev/null
+++ b/src/interfaces/ecpg/test/thread/alloc.pgc
@@ -0,0 +1,90 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include "ecpg_config.h"
+
+#ifndef ENABLE_THREAD_SAFETY
+int
+main(void)
+{
+ printf("No threading enabled.\n");
+ return 0;
+}
+#else
+#ifdef WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <process.h>
+#include <locale.h>
+#else
+#include <pthread.h>
+#endif
+#include <stdio.h>
+
+#define THREADS 16
+#define REPEATS 50
+
+exec sql include sqlca;
+exec sql include ../regression;
+
+exec sql whenever sqlerror sqlprint;
+exec sql whenever not found sqlprint;
+
+#ifdef WIN32
+static unsigned __stdcall fn(void* arg)
+#else
+static void* fn(void* arg)
+#endif
+{
+ int i;
+
+ EXEC SQL BEGIN DECLARE SECTION;
+ int value;
+ char name[100];
+ char **r = NULL;
+ EXEC SQL END DECLARE SECTION;
+
+ value = (intptr_t) arg;
+ sprintf(name, "Connection: %d", value);
+
+ EXEC SQL CONNECT TO REGRESSDB1 AS :name;
+ EXEC SQL SET AUTOCOMMIT TO ON;
+ for (i = 1; i <= REPEATS; ++i)
+ {
+ EXEC SQL SELECT relname INTO :r FROM pg_class WHERE relname = 'pg_class';
+ free(r);
+ r = NULL;
+ }
+ EXEC SQL DISCONNECT :name;
+
+ return 0;
+}
+
+int main ()
+{
+ intptr_t i;
+#ifdef WIN32
+ HANDLE threads[THREADS];
+#else
+ pthread_t threads[THREADS];
+#endif
+
+#ifdef WIN32
+ for (i = 0; i < THREADS; ++i)
+ {
+ unsigned id;
+ threads[i] = (HANDLE)_beginthreadex(NULL, 0, fn, (void*)i, 0, &id);
+ }
+
+ WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE);
+ for (i = 0; i < THREADS; ++i)
+ CloseHandle(threads[i]);
+#else
+ for (i = 0; i < THREADS; ++i)
+ pthread_create(&threads[i], NULL, fn, (void *) i);
+ for (i = 0; i < THREADS; ++i)
+ pthread_join(threads[i], NULL);
+#endif
+
+ return 0;
+}
+#endif