diff --git a/include/Callbackable.h b/include/Callbackable.h index 58501c455b..c22d95a40a 100644 --- a/include/Callbackable.h +++ b/include/Callbackable.h @@ -17,6 +17,8 @@ #ifndef CALLBACKABLE_H_ #define CALLBACKABLE_H_ +#include + 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_*/ diff --git a/src/host/Host.cc b/src/host/Host.cc index 9309869286..bd3db54333 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -110,6 +110,8 @@ int Host::select(SqlDB *db) rc = db->exec(oss, this); + unset_callback(); + if ((rc != 0) || (oid != boid )) { return -1; diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 10497204ec..ad1997c085 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -84,6 +84,8 @@ int HostPool::discover(map * discovered_hosts) rc = db->exec(sql,this); + unset_callback(); + return rc; } @@ -123,5 +125,7 @@ int HostPool::dump(ostringstream& oss, const string& where) oss << ""; + unset_callback(); + return rc; } diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index eb1cbf9e51..b9b92a0247 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -184,6 +184,8 @@ int HostShare::select(SqlDB * db) rc = db->exec(oss,this); + unset_callback(); + if (hsid != bhsid ) { rc = -1; diff --git a/src/pool/PoolSQL.cc b/src/pool/PoolSQL.cc index 6615d0c23d..f904a293cd 100644 --- a/src/pool/PoolSQL.cc +++ b/src/pool/PoolSQL.cc @@ -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; } diff --git a/src/pool/test/TestPoolSQL.cc b/src/pool/test/TestPoolSQL.cc index 8e6fe4ed3f..dc0f63f410 100644 --- a/src/pool/test/TestPoolSQL.cc +++ b/src/pool/test/TestPoolSQL.cc @@ -73,6 +73,8 @@ int TestObjectSQL::select(SqlDB *db) rc = db->exec(oss, this); + unset_callback(); + if ((rc != 0) || (oid != boid )) { return -1; diff --git a/src/template/TemplateSQL.cc b/src/template/TemplateSQL.cc index 701cc7e0c1..760fee7738 100644 --- a/src/template/TemplateSQL.cc +++ b/src/template/TemplateSQL.cc @@ -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; } diff --git a/src/um/User.cc b/src/um/User.cc index fbf9768dbf..abcf2f4ade 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -98,6 +98,8 @@ int User::select(SqlDB *db) rc = db->exec(oss, this); + unset_callback(); + if ((rc != 0) || (oid != boid )) { return -1; diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 42b46dc6a6..f7293ada1b 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -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 << ""; return rc; -} \ No newline at end of file +} diff --git a/src/vm/History.cc b/src/vm/History.cc index d73ab00927..9a74b9d813 100644 --- a/src/vm/History.cc +++ b/src/vm/History.cc @@ -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(); diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 97089edb89..c26dfa3b9a 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -165,6 +165,8 @@ int VirtualMachine::select(SqlDB * db) rc = db->exec(oss,this); + unset_callback(); + if ((rc != 0) || (oid != boid )) { goto error_id; diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index 47e785f39b..7e2e20bb60 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -455,5 +455,7 @@ int VirtualMachinePool::dump(ostringstream& oss, const string& where) oss << ""; + unset_callback(); + return rc; } diff --git a/src/vnm/Leases.cc b/src/vnm/Leases.cc index d2478b3ecf..8813e7d2aa 100644 --- a/src/vnm/Leases.cc +++ b/src/vnm/Leases.cc @@ -314,6 +314,8 @@ int Leases::select(SqlDB * db) rc = db->exec(oss,this); + unset_callback(); + if (rc != 0) { goto error_id; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 416f685b90..7a051ef5ed 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -111,6 +111,8 @@ int VirtualNetwork::select(SqlDB * db) rc = db->exec(oss, this); + unset_callback(); + if ((rc != 0) || (oid != boid )) { goto error_id; diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index a52e3b6b0e..28e1b01789 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -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 << ""; + unset_callback(); + return rc; -} \ No newline at end of file +}