Commit 0c2eb200 authored by Michael Meskes's avatar Michael Meskes

ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp> added thread-safe

descriptor handling
parent f1d37a99
...@@ -2247,5 +2247,10 @@ Sun, 30 Sep 2007 13:37:31 +0200 ...@@ -2247,5 +2247,10 @@ Sun, 30 Sep 2007 13:37:31 +0200
- Applied another patch by ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp> - Applied another patch by ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp>
to get memory allocation thread-safe. He also did some cleaning up. to get memory allocation thread-safe. He also did some cleaning up.
Tue, 02 Oct 2007 11:32:25 +0200
- ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp> added thread-safe
descriptor handling
- Set ecpg library version to 6.0. - Set ecpg library version to 6.0.
- Set ecpg version to 4.4. - Set ecpg version to 4.4.
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.44 2007/09/30 11:38:48 meskes Exp $ */ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.45 2007/10/02 09:49:59 meskes Exp $ */
#define POSTGRES_ECPG_INTERNAL #define POSTGRES_ECPG_INTERNAL
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -447,6 +447,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p ...@@ -447,6 +447,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
this->cache_head = NULL; this->cache_head = NULL;
this->prep_stmts = NULL; this->prep_stmts = NULL;
this->descriptors = NULL;
if (all_connections == NULL) if (all_connections == NULL)
this->next = NULL; this->next = NULL;
......
/* dynamic SQL support routines /* dynamic SQL support routines
* *
* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.23 2007/08/14 10:01:52 meskes Exp $ * $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.24 2007/10/02 09:49:59 meskes Exp $
*/ */
#define POSTGRES_ECPG_INTERNAL #define POSTGRES_ECPG_INTERNAL
#include "postgres_fe.h" #include "postgres_fe.h"
#include "pg_type.h" #include "pg_type.h"
#include "ecpg-pthread-win32.h"
#include "ecpgtype.h" #include "ecpgtype.h"
#include "ecpglib.h" #include "ecpglib.h"
#include "ecpgerrno.h" #include "ecpgerrno.h"
...@@ -14,18 +15,55 @@ ...@@ -14,18 +15,55 @@
#include "sqlca.h" #include "sqlca.h"
#include "sql3types.h" #include "sql3types.h"
struct descriptor *all_descriptors = NULL; static void descriptor_free(struct descriptor *desc);
static void descriptor_deallocate_all(struct descriptor *list);
/* We manage descriptors separately for each thread. */
#ifdef ENABLE_THREAD_SAFETY
static pthread_key_t descriptor_key;
#ifndef WIN32
static pthread_once_t descriptor_once = PTHREAD_ONCE_INIT;
#endif
static void
descriptor_destructor(void *arg)
{
descriptor_deallocate_all(arg);
}
NON_EXEC_STATIC void
descriptor_key_init(void)
{
pthread_key_create(&descriptor_key, descriptor_destructor);
}
static struct descriptor *
get_descriptors(void)
{
pthread_once(&descriptor_once, descriptor_key_init);
return (struct descriptor *) pthread_getspecific(descriptor_key);
}
static void
set_descriptors(struct descriptor *value)
{
pthread_setspecific(descriptor_key, value);
}
#else
static struct descriptor *all_descriptors = NULL;
#define get_descriptors() (all_descriptors)
#define set_descriptors(value) do { all_descriptors = (value); } while(0)
#endif
/* old internal convenience function that might go away later */ /* old internal convenience function that might go away later */
static PGresult static PGresult *
*
ECPGresultByDescriptor(int line, const char *name) ECPGresultByDescriptor(int line, const char *name)
{ {
PGresult **resultpp = ECPGdescriptor_lvalue(line, name); struct descriptor *desc = ECPGfind_desc(line, name);
if (desc == NULL)
if (resultpp)
return *resultpp;
return NULL; return NULL;
return desc->result;
} }
static unsigned int static unsigned int
...@@ -445,20 +483,9 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...) ...@@ -445,20 +483,9 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
bool bool
ECPGset_desc_header(int lineno, const char *desc_name, int count) ECPGset_desc_header(int lineno, const char *desc_name, int count)
{ {
struct descriptor *desc; struct descriptor *desc = ECPGfind_desc(lineno, desc_name);
for (desc = all_descriptors; desc; desc = desc->next)
{
if (strcmp(desc_name, desc->name) == 0)
break;
}
if (desc == NULL) if (desc == NULL)
{
ECPGraise(lineno, ECPG_UNKNOWN_DESCRIPTOR, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, desc_name);
return false; return false;
}
desc->count = count; desc->count = count;
return true; return true;
} }
...@@ -471,17 +498,9 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...) ...@@ -471,17 +498,9 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...)
struct descriptor_item *desc_item; struct descriptor_item *desc_item;
struct variable *var; struct variable *var;
for (desc = all_descriptors; desc; desc = desc->next) desc = ECPGfind_desc(lineno, desc_name);
{
if (strcmp(desc_name, desc->name) == 0)
break;
}
if (desc == NULL) if (desc == NULL)
{
ECPGraise(lineno, ECPG_UNKNOWN_DESCRIPTOR, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, desc_name);
return false; return false;
}
for (desc_item = desc->items; desc_item; desc_item = desc_item->next) for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
{ {
...@@ -506,7 +525,7 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...) ...@@ -506,7 +525,7 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...)
va_start(args, index); va_start(args, index);
do for (;;)
{ {
enum ECPGdtype itemtype; enum ECPGdtype itemtype;
const char *tobeinserted = NULL; const char *tobeinserted = NULL;
...@@ -585,24 +604,16 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...) ...@@ -585,24 +604,16 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...)
return false; return false;
} }
} }
} while (true); }
ECPGfree(var); ECPGfree(var);
return true; return true;
} }
bool /* Free the descriptor and items in it. */
ECPGdeallocate_desc(int line, const char *name) static void
descriptor_free(struct descriptor *desc)
{ {
struct descriptor *desc;
struct descriptor **lastptr = &all_descriptors;
struct sqlca_t *sqlca = ECPGget_sqlca();
ECPGinit_sqlca(sqlca);
for (desc = all_descriptors; desc; lastptr = &desc->next, desc = desc->next)
{
if (!strcmp(name, desc->name))
{
struct descriptor_item *desc_item; struct descriptor_item *desc_item;
for (desc_item = desc->items; desc_item;) for (desc_item = desc->items; desc_item;)
...@@ -615,10 +626,28 @@ ECPGdeallocate_desc(int line, const char *name) ...@@ -615,10 +626,28 @@ ECPGdeallocate_desc(int line, const char *name)
ECPGfree(di); ECPGfree(di);
} }
*lastptr = desc->next;
ECPGfree(desc->name); ECPGfree(desc->name);
PQclear(desc->result); PQclear(desc->result);
ECPGfree(desc); ECPGfree(desc);
}
bool
ECPGdeallocate_desc(int line, const char *name)
{
struct descriptor *desc;
struct descriptor *prev;
struct sqlca_t *sqlca = ECPGget_sqlca();
ECPGinit_sqlca(sqlca);
for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
{
if (!strcmp(name, desc->name))
{
if (prev)
prev->next = desc->next;
else
set_descriptors(desc->next);
descriptor_free(desc);
return true; return true;
} }
} }
...@@ -626,6 +655,18 @@ ECPGdeallocate_desc(int line, const char *name) ...@@ -626,6 +655,18 @@ ECPGdeallocate_desc(int line, const char *name)
return false; return false;
} }
/* Deallocate all descriptors in the list */
static void
descriptor_deallocate_all(struct descriptor *list)
{
while (list)
{
struct descriptor *next = list->next;
descriptor_free(list);
list = next;
}
}
bool bool
ECPGallocate_desc(int line, const char *name) ECPGallocate_desc(int line, const char *name)
{ {
...@@ -636,7 +677,7 @@ ECPGallocate_desc(int line, const char *name) ...@@ -636,7 +677,7 @@ ECPGallocate_desc(int line, const char *name)
new = (struct descriptor *) ECPGalloc(sizeof(struct descriptor), line); new = (struct descriptor *) ECPGalloc(sizeof(struct descriptor), line);
if (!new) if (!new)
return false; return false;
new->next = all_descriptors; new->next = get_descriptors();
new->name = ECPGalloc(strlen(name) + 1, line); new->name = ECPGalloc(strlen(name) + 1, line);
if (!new->name) if (!new->name)
{ {
...@@ -654,23 +695,24 @@ ECPGallocate_desc(int line, const char *name) ...@@ -654,23 +695,24 @@ ECPGallocate_desc(int line, const char *name)
return false; return false;
} }
strcpy(new->name, name); strcpy(new->name, name);
all_descriptors = new; set_descriptors(new);
return true; return true;
} }
PGresult ** /* Find descriptor with name in the connection. */
ECPGdescriptor_lvalue(int line, const char *descriptor) struct descriptor *
ECPGfind_desc(int line, const char *name)
{ {
struct descriptor *i; struct descriptor *desc;
for (i = all_descriptors; i != NULL; i = i->next) for (desc = get_descriptors(); desc; desc = desc->next)
{ {
if (!strcmp(descriptor, i->name)) if (strcmp(name, desc->name) == 0)
return &i->result; return desc;
} }
ECPGraise(line, ECPG_UNKNOWN_DESCRIPTOR, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, (char *) descriptor); ECPGraise(line, ECPG_UNKNOWN_DESCRIPTOR, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, name);
return NULL; return NULL; /* not found */
} }
bool bool
......
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.70 2007/09/26 10:57:00 meskes Exp $ */ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.71 2007/10/02 09:49:59 meskes Exp $ */
/* /*
* The aim is to get a simpler inteface to the database routines. * The aim is to get a simpler inteface to the database routines.
...@@ -1088,17 +1088,9 @@ ECPGexecute(struct statement * stmt) ...@@ -1088,17 +1088,9 @@ ECPGexecute(struct statement * stmt)
struct descriptor *desc; struct descriptor *desc;
struct descriptor_item *desc_item; struct descriptor_item *desc_item;
for (desc = all_descriptors; desc; desc = desc->next) desc = ECPGfind_desc(stmt->lineno, var->pointer);
{
if (strcmp(var->pointer, desc->name) == 0)
break;
}
if (desc == NULL) if (desc == NULL)
{
ECPGraise(stmt->lineno, ECPG_UNKNOWN_DESCRIPTOR, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, var->pointer);
return false; return false;
}
desc_counter++; desc_counter++;
for (desc_item = desc->items; desc_item; desc_item = desc_item->next) for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
...@@ -1334,16 +1326,15 @@ ECPGexecute(struct statement * stmt) ...@@ -1334,16 +1326,15 @@ ECPGexecute(struct statement * stmt)
if (var != NULL && var->type == ECPGt_descriptor) if (var != NULL && var->type == ECPGt_descriptor)
{ {
PGresult **resultpp = ECPGdescriptor_lvalue(stmt->lineno, (const char *) var->pointer); struct descriptor *desc = ECPGfind_desc(stmt->lineno, var->pointer);
if (desc == NULL)
if (resultpp == NULL)
status = false; status = false;
else else
{ {
if (*resultpp) if (desc->result)
PQclear(*resultpp); PQclear(desc->result);
*resultpp = results; desc->result = results;
clear_result = FALSE; clear_result = false;
ECPGlog("ECPGexecute putting result (%d tuples) into descriptor '%s'\n", PQntuples(results), (const char *) var->pointer); ECPGlog("ECPGexecute putting result (%d tuples) into descriptor '%s'\n", PQntuples(results), (const char *) var->pointer);
} }
var = var->next; var = var->next;
......
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.28 2007/09/30 11:38:48 meskes Exp $ */ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.29 2007/10/02 09:49:59 meskes Exp $ */
#ifndef _ECPG_LIB_EXTERN_H #ifndef _ECPG_LIB_EXTERN_H
#define _ECPG_LIB_EXTERN_H #define _ECPG_LIB_EXTERN_H
...@@ -36,6 +36,8 @@ bool ECPGget_data(const PGresult *, int, int, int, enum ECPGttype type, ...@@ -36,6 +36,8 @@ bool ECPGget_data(const PGresult *, int, int, int, enum ECPGttype type,
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
void ecpg_pthreads_init(void); void ecpg_pthreads_init(void);
#else
#define ecpg_pthreads_init() ((void)0)
#endif #endif
struct connection *ECPGget_connection(const char *); struct connection *ECPGget_connection(const char *);
char *ECPGalloc(long, int); char *ECPGalloc(long, int);
...@@ -92,6 +94,7 @@ struct connection ...@@ -92,6 +94,7 @@ struct connection
int autocommit; int autocommit;
struct ECPGtype_information_cache *cache_head; struct ECPGtype_information_cache *cache_head;
struct prepared_statement *prep_stmts; struct prepared_statement *prep_stmts;
struct descriptor *descriptors;
struct connection *next; struct connection *next;
}; };
...@@ -105,8 +108,6 @@ struct descriptor ...@@ -105,8 +108,6 @@ struct descriptor
struct descriptor_item *items; struct descriptor_item *items;
}; };
extern struct descriptor *all_descriptors;
struct descriptor_item struct descriptor_item
{ {
int num; int num;
...@@ -136,7 +137,7 @@ struct variable ...@@ -136,7 +137,7 @@ struct variable
struct variable *next; struct variable *next;
}; };
PGresult **ECPGdescriptor_lvalue(int line, const char *descriptor); struct descriptor *ECPGfind_desc(int line, const char *name);
bool ECPGstore_result(const PGresult *results, int act_field, bool ECPGstore_result(const PGresult *results, int act_field,
const struct statement * stmt, struct variable * var); const struct statement * stmt, struct variable * var);
......
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.37 2007/09/30 11:38:48 meskes Exp $ */ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.38 2007/10/02 09:49:59 meskes Exp $ */
#define POSTGRES_ECPG_INTERNAL #define POSTGRES_ECPG_INTERNAL
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -431,6 +431,7 @@ DllMain(HANDLE module, DWORD reason, LPVOID reserved) ...@@ -431,6 +431,7 @@ DllMain(HANDLE module, DWORD reason, LPVOID reserved)
auto_mem_key_init(); auto_mem_key_init();
ecpg_actual_connection_init(); ecpg_actual_connection_init();
ecpg_sqlca_key_init(); ecpg_sqlca_key_init();
descriptor_key_init();
} }
return TRUE; return TRUE;
} }
......
/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/ecpg-pthread-win32.h,v 1.2 2007/09/30 11:38:48 meskes Exp $ */ /* $PostgreSQL: pgsql/src/interfaces/ecpg/include/ecpg-pthread-win32.h,v 1.3 2007/10/02 09:49:59 meskes Exp $ */
/* /*
* pthread mapping macros for win32 native thread implementation * pthread mapping macros for win32 native thread implementation
*/ */
...@@ -47,6 +47,7 @@ extern pthread_mutex_t debug_init_mutex; ...@@ -47,6 +47,7 @@ extern pthread_mutex_t debug_init_mutex;
extern void auto_mem_key_init(void); extern void auto_mem_key_init(void);
extern void ecpg_actual_connection_init(void); extern void ecpg_actual_connection_init(void);
extern void ecpg_sqlca_key_init(void); extern void ecpg_sqlca_key_init(void);
extern void descriptor_key_init(void);
extern BOOL WINAPI DllMain(HANDLE module, DWORD reason, LPVOID reserved); extern BOOL WINAPI DllMain(HANDLE module, DWORD reason, LPVOID reserved);
#endif /* WIN32 */ #endif /* WIN32 */
......
...@@ -43,3 +43,4 @@ test: thread/thread ...@@ -43,3 +43,4 @@ test: thread/thread
test: thread/thread_implicit test: thread/thread_implicit
test: thread/prep test: thread/prep
test: thread/alloc test: thread/alloc
test: thread/descriptor
...@@ -43,5 +43,6 @@ test: thread/thread ...@@ -43,5 +43,6 @@ test: thread/thread
test: thread/thread_implicit test: thread/thread_implicit
test: thread/prep test: thread/prep
test: thread/alloc test: thread/alloc
test: thread/descriptor
test: connect/test1 test: connect/test1
/* Processed by ecpg (regression mode) */
/* These include files are added by the preprocessor */
#include <ecpgtype.h>
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */
#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
#line 1 "descriptor.pgc"
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#include <stdio.h>
#define THREADS 16
#define REPEATS 50000
#line 1 "sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H
#ifndef PGDLLIMPORT
#if defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif /* __CYGWIN__ */
#endif /* PGDLLIMPORT */
#define SQLERRMC_LEN 150
#ifdef __cplusplus
extern "C"
{
#endif
struct sqlca_t
{
char sqlcaid[8];
long sqlabc;
long sqlcode;
struct
{
int sqlerrml;
char sqlerrmc[SQLERRMC_LEN];
} sqlerrm;
char sqlerrp[8];
long sqlerrd[6];
/* Element 0: empty */
/* 1: OID of processed tuple if applicable */
/* 2: number of rows processed */
/* after an INSERT, UPDATE or */
/* DELETE statement */
/* 3: empty */
/* 4: empty */
/* 5: empty */
char sqlwarn[8];
/* Element 0: set to 'W' if at least one other is 'W' */
/* 1: if 'W' at least one character string */
/* value was truncated when it was */
/* stored into a host variable. */
/*
* 2: if 'W' a (hopefully) non-fatal notice occurred
*/ /* 3: empty */
/* 4: empty */
/* 5: empty */
/* 6: empty */
/* 7: empty */
char sqlstate[5];
};
struct sqlca_t *ECPGget_sqlca(void);
#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif
#ifdef __cplusplus
}
#endif
#endif
#line 13 "descriptor.pgc"
/* exec sql whenever sqlerror sqlprint ; */
#line 14 "descriptor.pgc"
/* exec sql whenever not found sqlprint ; */
#line 15 "descriptor.pgc"
#ifdef WIN32
static unsigned STDCALL fn(void* arg)
#else
void* fn(void* arg)
#endif
{
int i;
for (i = 1; i <= REPEATS; ++i)
{
ECPGallocate_desc(__LINE__, "mydesc");
#line 27 "descriptor.pgc"
if (sqlca.sqlcode < 0) sqlprint();
#line 27 "descriptor.pgc"
ECPGdeallocate_desc(__LINE__, "mydesc");
#line 28 "descriptor.pgc"
if (sqlca.sqlcode < 0) sqlprint();
#line 28 "descriptor.pgc"
}
return 0;
}
int main (int argc, char** argv)
{
int 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, NULL, 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, NULL);
for (i = 0; i < THREADS; ++i)
pthread_join(threads[i], NULL);
#endif
return 0;
}
...@@ -7,6 +7,7 @@ include $(top_srcdir)/$(subdir)/../Makefile.regress ...@@ -7,6 +7,7 @@ include $(top_srcdir)/$(subdir)/../Makefile.regress
TESTS = thread_implicit thread_implicit.c \ TESTS = thread_implicit thread_implicit.c \
thread thread.c \ thread thread.c \
prep prep.c \ prep prep.c \
descriptor descriptor.c \
alloc alloc.c alloc alloc.c
all: $(TESTS) all: $(TESTS)
......
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#include <stdio.h>
#define THREADS 16
#define REPEATS 50000
EXEC SQL include sqlca;
EXEC SQL whenever sqlerror sqlprint;
EXEC SQL whenever not found sqlprint;
#ifdef WIN32
static unsigned STDCALL fn(void* arg)
#else
void* fn(void* arg)
#endif
{
int i;
for (i = 1; i <= REPEATS; ++i)
{
EXEC SQL ALLOCATE DESCRIPTOR mydesc;
EXEC SQL DEALLOCATE DESCRIPTOR mydesc;
}
return 0;
}
int main (int argc, char** argv)
{
int 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, NULL, 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, NULL);
for (i = 0; i < THREADS; ++i)
pthread_join(threads[i], NULL);
#endif
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment