Removed pthread_mutex stuff, as the sem_trywait(), etc. semaphores
authoraland <aland>
Fri, 4 May 2001 19:14:11 +0000 (19:14 +0000)
committeraland <aland>
Fri, 4 May 2001 19:14:11 +0000 (19:14 +0000)
will properly handle contention among threads.

Also, if we run out of SQL sockets, it's an ERROR.  We log an error
message describing what the problem is, how to fix it.  We do NOT
wait forever, trying to see if an SQL socket maybe will free up.

src/modules/rlm_sql/rlm_sql.c
src/modules/rlm_sql/rlm_sql.h
src/modules/rlm_sql/sql.c

index ee61f34..c64d063 100644 (file)
@@ -116,14 +116,6 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance) {
        inst->config = rad_malloc(sizeof(SQL_CONFIG));
        memset(inst->config, 0, sizeof(SQL_CONFIG));
 
-#if HAVE_PTHREAD_H
-       inst->lock = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
-       pthread_mutex_init(inst->lock, NULL);
-
-       inst->notfull = (pthread_cond_t *) rad_malloc(sizeof(pthread_cond_t));
-       pthread_cond_init(inst->notfull, NULL);
-#endif
-
        /*
         * If the configuration parameters can't be parsed, then
         * fail.
index d16c1be..218a24e 100644 (file)
@@ -10,7 +10,7 @@
 #include        <semaphore.h>
 #endif
 
-#include <ltdl.h>
+#include       <ltdl.h>
 
 #include "conf.h"
 #include "conffile.h"
@@ -68,10 +68,6 @@ typedef struct sql_inst {
        time_t connect_after;
        SQLSOCK         *sqlpool;
        SQL_CONFIG      *config;
-#if HAVE_PTHREAD_H
-       pthread_mutex_t *lock;
-       pthread_cond_t  *notfull;
-#endif
 
        rlm_sql_module_t *module;
 } SQL_INST;
index a502aed..fe99fa1 100644 (file)
@@ -100,10 +100,13 @@ int sql_init_socketpool(SQL_INST * inst) {
                sqlsocket->state = sockunconnected;
 
 #if HAVE_PTHREAD_H
+               /*
+                *  FIXME! Check return codes!
+                */
                sqlsocket->semaphore = (sem_t *) rad_malloc(sizeof(sem_t));
                sem_init(sqlsocket->semaphore, 0, SQLSOCK_UNLOCKED);
 #else
-               sqlsocket->in_use = 0;
+               sqlsocket->in_use = SQLSOCK_UNLOCKED;
 #endif
 
                if (time(NULL) > inst->connect_after) {
@@ -133,10 +136,6 @@ void sql_poolfree(SQL_INST * inst) {
        for (cur = inst->sqlpool; cur; cur = cur->next) {
                sql_close_socket(inst, cur);
        }
-#if HAVE_PTHREAD_H
-       pthread_mutex_destroy(inst->lock);
-       pthread_cond_destroy(inst->notfull);
-#endif
 }
 
 
@@ -171,19 +170,9 @@ SQLSOCK * sql_get_socket(SQL_INST * inst) {
        struct timeval timeout;
        int tried_to_connect = 0;
 
-#if HAVE_PTHREAD_H
-       pthread_mutex_lock(inst->lock);
-#endif
        while (inst->used == inst->config->num_sql_socks) {
-               radlog(L_DBG, "rlm_sql: Waiting for open sql socket");
-#if HAVE_PTHREAD_H
-               pthread_cond_wait(inst->notfull, inst->lock);
-#else
-               /* this should be portable... */
-               timeout.tv_sec = 0;
-               timeout.tv_usec = 200;
-               select(0, NULL, NULL, NULL, &timeout)
-#endif
+               radlog(L_ERR, "rlm_sql: All sockets are being used! Please increase the maximum number of sockets!");
+         return NULL;
        }
 
        for (cur = inst->sqlpool; cur; cur = cur->next) {
@@ -210,9 +199,7 @@ SQLSOCK * sql_get_socket(SQL_INST * inst) {
                if (cur->in_use == SQLSOCK_UNLOCKED) {
 #endif
                        (inst->used)++;
-#if HAVE_PTHREAD_H
-                       pthread_mutex_unlock(inst->lock);
-#else
+#ifne HAVE_PTHREAD_H
                        cur->in_use = SQLSOCK_LOCKED;
 #endif
                        radlog(L_DBG, "rlm_sql: Reserving sql socket id: %d", cur->id);
@@ -220,10 +207,6 @@ SQLSOCK * sql_get_socket(SQL_INST * inst) {
                }
        }
 
-#if HAVE_PTHREAD_H
-       pthread_mutex_unlock(inst->lock);
-#endif
-
        /* We get here if every DB handle is unconnected and unconnectABLE */
        radlog((tried_to_connect = 0) ? (L_DBG) : (L_CONS | L_ERR), "rlm_sql:  There are no DB handles to use!");
        return NULL;
@@ -238,23 +221,15 @@ SQLSOCK * sql_get_socket(SQL_INST * inst) {
  *************************************************************************/
 int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket) {
 
-#if HAVE_PTHREAD_H
-       pthread_mutex_lock(inst->lock);
-#endif
        (inst->used)--;
 #if HAVE_PTHREAD_H
        sem_post(sqlsocket->semaphore);
 #else
-       sqlsocket->in_use = 0;
+       sqlsocket->in_use = SQLSOCK_UNLOCKED;
 #endif
 
        radlog(L_DBG, "rlm_sql: Released sql socket id: %d", sqlsocket->id);
 
-#if HAVE_PTHREAD_H
-       pthread_mutex_unlock(inst->lock);
-       pthread_cond_signal(inst->notfull);
-#endif
-
        return 1;
 }