Skip to content

Commit

Permalink
db_postgres: init libssl in a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
space88man committed Jan 29, 2024
1 parent 5dffb93 commit 3426b15
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/modules/db_postgres/km_dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "../../core/dprint.h"
#include "../../core/mem/mem.h"
#include "../../lib/srdb1/db.h"
Expand Down Expand Up @@ -108,24 +109,57 @@ static void db_postgres_free_query(const db1_con_t *_con);
* \param _url URL of the database that should be opened
* \return database connection on success, NULL on error
* \note this function must be called prior to any database functions
*
* Init libssl in a thread
*/
db1_con_t *db_postgres_init(const str *_url)
static db1_con_t *db_postgres_init0(const str *_url)
{
return db_do_init(_url, (void *)db_postgres_new_connection);
}

db1_con_t *db_postgres_init(const str *_url)
{
pthread_t tid;
db1_con_t *ret;

pthread_create(
&tid, NULL, (void *(*)(void *))db_postgres_init0, (void *)_url);
pthread_join(tid, (void **)&ret);

return ret;
}
/*!
* \brief Initialize database for future queries, specify pooling
* \param _url URL of the database that should be opened
* \param pooling whether or not to use a pooled connection
* \return database connection on success, NULL on error
* \note this function must be called prior to any database functions
*
* Init libssl in thread
*/
db1_con_t *db_postgres_init2(const str *_url, db_pooling_t pooling)
struct _thread_args
{
const str *_url;
db_pooling_t pooling;
};

static db1_con_t *_db_postgres_init2(struct _thread_args *args)
{
return db_do_init2(_url, (void *)db_postgres_new_connection, pooling);
return db_do_init2(
args->_url, (void *)db_postgres_new_connection, args->pooling);
}

db1_con_t *db_postgres_init2(const str *_url, db_pooling_t pooling)
{
pthread_t tid;
db1_con_t *ret;

pthread_create(&tid, NULL, (void *(*)(void *))_db_postgres_init2,
(void *)&(struct _thread_args){_url, pooling});
pthread_join(tid, (void **)&ret);

return ret;
}
/*!
* \brief Close database when the database is no longer needed
* \param _h closed connection, as returned from db_postgres_init
Expand Down

0 comments on commit 3426b15

Please sign in to comment.