~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-security

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/thread/alloc.pgc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include "ecpg_config.h"
 
3
 
 
4
#ifndef ENABLE_THREAD_SAFETY
 
5
int
 
6
main(void)
 
7
{
 
8
        printf("No threading enabled.\n");
 
9
        return 0;
 
10
}
 
11
#else
 
12
#ifdef WIN32
 
13
#define WIN32_LEAN_AND_MEAN
 
14
#include <windows.h>
 
15
#include <process.h>
 
16
#else
 
17
#include <pthread.h>
 
18
#endif
 
19
#include <stdio.h>
 
20
 
 
21
#define THREADS         16
 
22
#define REPEATS         50
 
23
 
 
24
exec sql include sqlca;
 
25
exec sql include ../regression;
 
26
 
 
27
exec sql whenever sqlerror sqlprint;
 
28
exec sql whenever not found sqlprint;
 
29
 
 
30
#ifdef WIN32
 
31
static unsigned __stdcall fn(void* arg)
 
32
#else
 
33
static void* fn(void* arg)
 
34
#endif
 
35
{
 
36
        int i;
 
37
 
 
38
        EXEC SQL BEGIN DECLARE SECTION;
 
39
        int  value;
 
40
        char name[100];
 
41
        char **r = NULL;
 
42
        EXEC SQL END DECLARE SECTION;
 
43
 
 
44
        value = (long)arg;
 
45
        sprintf(name, "Connection: %d", value);
 
46
 
 
47
        EXEC SQL CONNECT TO REGRESSDB1 AS :name;
 
48
        EXEC SQL SET AUTOCOMMIT TO ON;
 
49
        for (i = 1; i <= REPEATS; ++i)
 
50
        {
 
51
                EXEC SQL SELECT relname INTO :r FROM pg_class WHERE relname = 'pg_class';
 
52
                free(r);
 
53
                r = NULL;
 
54
        }
 
55
        EXEC SQL DISCONNECT :name;
 
56
 
 
57
        return 0;
 
58
}
 
59
 
 
60
int main (int argc, char** argv)
 
61
{
 
62
        int i;
 
63
#ifdef WIN32
 
64
        HANDLE threads[THREADS];
 
65
#else
 
66
        pthread_t threads[THREADS];
 
67
#endif
 
68
 
 
69
#ifdef WIN32
 
70
        for (i = 0; i < THREADS; ++i)
 
71
        {
 
72
                unsigned id;
 
73
                threads[i] = (HANDLE)_beginthreadex(NULL, 0, fn, (void*)i, 0, &id);
 
74
        }
 
75
 
 
76
        WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE);
 
77
        for (i = 0; i < THREADS; ++i)
 
78
                CloseHandle(threads[i]);
 
79
#else
 
80
        for (i = 0; i < THREADS; ++i)
 
81
                pthread_create(&threads[i], NULL, fn, (void *) (long) i);
 
82
        for (i = 0; i < THREADS; ++i)
 
83
                pthread_join(threads[i], NULL);
 
84
#endif
 
85
 
 
86
        return 0;
 
87
}
 
88
#endif
 
89