update controller code
This commit is contained in:
parent
2836d196da
commit
41f56fa6ed
54 changed files with 9688 additions and 214 deletions
209
src/controller/resmgr.c
Normal file
209
src/controller/resmgr.c
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cluster.h"
|
||||
#include "htab.h"
|
||||
#include "utils.h"
|
||||
#include "xce/cluster_def.h"
|
||||
#include "xce/errnox.h"
|
||||
#include "xstring.h"
|
||||
|
||||
extern void res_obj_killer(xce_resource_t*);
|
||||
extern void resval_obj_killer(xce_resval_t*);
|
||||
|
||||
int resmgr_init(resmgr_t* mgr,
|
||||
int nslots,
|
||||
int delta,
|
||||
int inst_delta)
|
||||
{
|
||||
TRACE("%s nslots %d delta %d inst_delta %d",
|
||||
__func__, nslots, delta,
|
||||
inst_delta);
|
||||
|
||||
mylock_init(&(mgr->lock));
|
||||
|
||||
ht_init(&(mgr->htab), nslots,
|
||||
string_hash_key);
|
||||
zarray_init(&(mgr->defs), delta,
|
||||
(zarray_item_killer_t)res_obj_killer);
|
||||
zarray_init(&(mgr->sharedinst), inst_delta,
|
||||
(zarray_item_killer_t)resval_obj_killer);
|
||||
mgr->nshared = 0;
|
||||
mgr->nunshared = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void resmgr_fini(resmgr_t* mgr)
|
||||
{
|
||||
|
||||
zarray_fini(&(mgr->sharedinst));
|
||||
zarray_fini(&(mgr->defs));
|
||||
ht_fini(&(mgr->htab));
|
||||
mylock_fini(&(mgr->lock));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int32_t resmgr_add_res_(resmgr_t* mgr,
|
||||
xce_resource_t* res)
|
||||
{
|
||||
hent_t* e = NULL;
|
||||
int idx;
|
||||
|
||||
e = ht_get(&(mgr->htab), (void*)res->name);
|
||||
if (e != NULL) {
|
||||
return -EEXIST;
|
||||
}
|
||||
|
||||
idx = zarray_store(&(mgr->defs), res);
|
||||
if (idx < 0) {
|
||||
return idx;
|
||||
}
|
||||
|
||||
e = ht_add(&(mgr->htab),
|
||||
(void*)res->name);
|
||||
if (e == NULL) {
|
||||
zarray_del(&(mgr->defs), idx);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
e->data = idx;
|
||||
if (res->flags & XCE_RESF_SHARED) {
|
||||
res->idx = mgr->nshared;
|
||||
mgr->nshared++;
|
||||
} else {
|
||||
res->idx = mgr->nunshared;
|
||||
mgr->nunshared++;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
int32_t resmgr_add_res(resmgr_t* mgr,
|
||||
xce_resource_t* res)
|
||||
{
|
||||
int32_t idx;
|
||||
|
||||
if ((mgr == NULL) || (res == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
idx = resmgr_add_res_(mgr, res);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
static xce_resource_t* resmgr_lockres_(resmgr_t* mgr,
|
||||
char* resname,
|
||||
int op)
|
||||
{
|
||||
hent_t* e = NULL;
|
||||
int idx;
|
||||
xce_resource_t* r = NULL;
|
||||
|
||||
e = ht_get(&(mgr->htab), (void*)resname);
|
||||
if (e == NULL) {
|
||||
SET_ERROR(E_XCE_BAD_RES);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
idx = e->data;
|
||||
if (idx < 0) {
|
||||
SET_ERROR(E_XCE_INTERNAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = (xce_resource_t*)zarray_get(&(mgr->defs), idx);
|
||||
if (r == NULL) {
|
||||
SET_ERROR(E_XCE_INTERNAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return obj_ref(r, op);
|
||||
}
|
||||
xce_resource_t* resmgr_lockres(resmgr_t* mgr,
|
||||
char* resname,
|
||||
int op)
|
||||
{
|
||||
xce_resource_t* r = NULL;
|
||||
|
||||
if (mgr == NULL) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (resname == NULL) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* clang-format off */
|
||||
if ((op != OBJREF_NOP) &&
|
||||
(op != OBJREF_READ) &&
|
||||
(op != OBJREF_UPDATE)) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
/* clang-format on */
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
r = resmgr_lockres_(mgr, resname, op);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void resmgr_del_res_(resmgr_t* mgr,
|
||||
xce_resource_t* r)
|
||||
{
|
||||
hent_t* e = NULL;
|
||||
int idx;
|
||||
|
||||
e = ht_get(&(mgr->htab),
|
||||
(void*)r->name);
|
||||
if (e == NULL) {
|
||||
SET_ERROR(E_XCE_BAD_RES);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
idx = e->data;
|
||||
if (idx < 0) {
|
||||
SET_ERROR(E_XCE_INTERNAL);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
zarray_del(&(mgr->defs), idx);
|
||||
}
|
||||
|
||||
void resmgr_del_res(resmgr_t* mgr,
|
||||
xce_resource_t* r)
|
||||
{
|
||||
if ((mgr == NULL) || (r == NULL)) {
|
||||
SET_ERROR(EINVAL);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
resmgr_del_res_(mgr, r);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
}
|
||||
|
||||
int resmgr_iter(resmgr_t* mgr,
|
||||
xlist_t* l,
|
||||
void* arg)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
rc = zarray_iter_ex(&(mgr->defs), l, arg);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
return rc;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue