1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-23 22:50:09 +03:00

bug #259: Callback functions are locked until the DB query answer is processed.

This commit is contained in:
Carlos Martín 2010-06-11 16:40:29 +02:00 committed by Ruben S. Montero
parent 83c1b9d993
commit 6d60b1c0b2
15 changed files with 72 additions and 9 deletions

View File

@ -17,6 +17,8 @@
#ifndef CALLBACKABLE_H_
#define CALLBACKABLE_H_
#include <pthread.h>
using namespace std;
/**
@ -27,9 +29,15 @@ class Callbackable
{
public:
Callbackable():cb(0),arg(0){};
Callbackable():cb(0),arg(0)
{
pthread_mutex_init(&mutex,0);
};
virtual ~Callbackable(){};
virtual ~Callbackable()
{
pthread_mutex_destroy(&mutex);
};
/**
* Datatype for call back pointers
@ -38,12 +46,14 @@ public:
/**
* Set the callback function and custom arguments to be executed by the
* next SQL command
* next SQL command, and locks the mutex until unset_callback is called.
* @param ptr to the callback function
* @param arg custom arguments for the callback function
*/
void set_callback(Callback _cb, void * _arg = 0)
{
pthread_mutex_lock(&mutex);
cb = _cb;
arg = _arg;
};
@ -58,16 +68,26 @@ public:
};
/**
* Set the callback function and custom arguments to be executed by the
* next SQL command
* @param ptr to the callback function
* @param arg custom arguments for the callback function
* Call the callback funcion set. This method must be called only if
* isCallBackSet returns true.
* @return the callback function return value.
*/
int do_callback(int num, char **values, char **names)
{
return (this->*cb)(arg, num, values, names);
};
/**
* Unset the callback function.
*/
void unset_callback()
{
cb = 0;
arg = 0;
pthread_mutex_unlock(&mutex);
}
private:
/**
* SQL callback to be executed for each row result of an SQL statement
@ -78,6 +98,11 @@ private:
* Custom arguments for the callback
*/
void * arg;
/**
* Mutex for locking the callback function.
*/
pthread_mutex_t mutex;
};
#endif /*CALLBACKABLE_H_*/

View File

@ -110,6 +110,8 @@ int Host::select(SqlDB *db)
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;

View File

@ -84,6 +84,8 @@ int HostPool::discover(map<int, string> * discovered_hosts)
rc = db->exec(sql,this);
unset_callback();
return rc;
}
@ -123,5 +125,7 @@ int HostPool::dump(ostringstream& oss, const string& where)
oss << "</HOST_POOL>";
unset_callback();
return rc;
}

View File

@ -184,6 +184,8 @@ int HostShare::select(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
if (hsid != bhsid )
{
rc = -1;

View File

@ -61,6 +61,8 @@ PoolSQL::PoolSQL(SqlDB * _db, const char * table): db(_db), lastOID(-1)
oss << "SELECT MAX(oid) FROM " << table;
db->exec(oss,this);
unset_callback();
};
/* -------------------------------------------------------------------------- */
@ -302,5 +304,7 @@ int PoolSQL::search(
rc = db->exec(sql, this);
unset_callback();
return rc;
}

View File

@ -73,6 +73,8 @@ int TestObjectSQL::select(SqlDB *db)
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;

View File

@ -61,6 +61,8 @@ int TemplateSQL::insert(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
if ( rc != 0 )
{
return -1;
@ -231,6 +233,8 @@ int TemplateSQL::select(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
return rc;
}

View File

@ -98,6 +98,8 @@ int User::select(SqlDB *db)
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;

View File

@ -53,6 +53,8 @@ UserPool::UserPool(SqlDB * db):PoolSQL(db,User::table)
db->exec(sql, this);
unset_callback();
if ((int) known_users.size() == 0)
{
// User oneadmin needs to be added in the bootstrap
@ -217,7 +219,9 @@ int UserPool::dump(ostringstream& oss, const string& where)
rc = db->exec(cmd, this);
unset_callback();
oss << "</USER_POOL>";
return rc;
}
}

View File

@ -371,6 +371,8 @@ int History::select(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
if ( rc == 0 ) // Regenerate non-persistent data
{
non_persistent_data();

View File

@ -165,6 +165,8 @@ int VirtualMachine::select(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
goto error_id;

View File

@ -455,5 +455,7 @@ int VirtualMachinePool::dump(ostringstream& oss, const string& where)
oss << "</VM_POOL>";
unset_callback();
return rc;
}

View File

@ -314,6 +314,8 @@ int Leases::select(SqlDB * db)
rc = db->exec(oss,this);
unset_callback();
if (rc != 0)
{
goto error_id;

View File

@ -111,6 +111,8 @@ int VirtualNetwork::select(SqlDB * db)
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
goto error_id;

View File

@ -173,6 +173,8 @@ VirtualNetwork * VirtualNetworkPool::get(const string& name, bool lock)
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if (rc != 0 || oid == -1)
@ -232,5 +234,7 @@ int VirtualNetworkPool::dump(ostringstream& oss, const string& where)
oss << "</VNET_POOL>";
unset_callback();
return rc;
}
}