Turn virKeepAlive into a virObject

Make virKeepAlive use the virObject APIs for reference counting

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-07-11 14:35:50 +01:00
parent 0b4d3fe556
commit 2303e92086
5 changed files with 35 additions and 54 deletions

View File

@ -77,9 +77,7 @@ provider libvirt {
# file: src/rpc/virkeepalive.c # file: src/rpc/virkeepalive.c
# prefix: rpc # prefix: rpc
probe rpc_keepalive_new(void *ka, void *client, int refs); probe rpc_keepalive_new(void *ka, void *client);
probe rpc_keepalive_ref(void *ka, void *client, int refs);
probe rpc_keepalive_free(void *ka, void *client, int refs);
probe rpc_keepalive_start(void *ka, void *client, int interval, int count); probe rpc_keepalive_start(void *ka, void *client, int interval, int count);
probe rpc_keepalive_stop(void *ka, void *client); probe rpc_keepalive_stop(void *ka, void *client);
probe rpc_keepalive_send(void *ka, void *client, int prog, int vers, int proc); probe rpc_keepalive_send(void *ka, void *client, int prog, int vers, int proc);

View File

@ -35,7 +35,8 @@
#define VIR_FROM_THIS VIR_FROM_RPC #define VIR_FROM_THIS VIR_FROM_RPC
struct _virKeepAlive { struct _virKeepAlive {
int refs; virObject object;
virMutex lock; virMutex lock;
int interval; int interval;
@ -52,6 +53,21 @@ struct _virKeepAlive {
}; };
static virClassPtr virKeepAliveClass;
static void virKeepAliveDispose(void *obj);
static int virKeepAliveOnceInit(void)
{
if (!(virKeepAliveClass = virClassNew("virKeepAlive",
sizeof(virKeepAlive),
virKeepAliveDispose)))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virKeepAlive)
static void static void
virKeepAliveLock(virKeepAlivePtr ka) virKeepAliveLock(virKeepAlivePtr ka)
{ {
@ -165,7 +181,7 @@ virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
if (!dead && !msg) if (!dead && !msg)
goto cleanup; goto cleanup;
ka->refs++; virObjectRef(ka);
virKeepAliveUnlock(ka); virKeepAliveUnlock(ka);
if (dead) { if (dead) {
@ -176,20 +192,13 @@ virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
} }
virKeepAliveLock(ka); virKeepAliveLock(ka);
ka->refs--; virObjectUnref(ka);
cleanup: cleanup:
virKeepAliveUnlock(ka); virKeepAliveUnlock(ka);
} }
static void
virKeepAliveTimerFree(void *opaque)
{
virKeepAliveFree(opaque);
}
virKeepAlivePtr virKeepAlivePtr
virKeepAliveNew(int interval, virKeepAliveNew(int interval,
unsigned int count, unsigned int count,
@ -202,17 +211,17 @@ virKeepAliveNew(int interval,
VIR_DEBUG("client=%p, interval=%d, count=%u", client, interval, count); VIR_DEBUG("client=%p, interval=%d, count=%u", client, interval, count);
if (VIR_ALLOC(ka) < 0) { if (virKeepAliveInitialize() < 0)
virReportOOMError(); return NULL;
if (!(ka = virObjectNew(virKeepAliveClass)))
return NULL; return NULL;
}
if (virMutexInit(&ka->lock) < 0) { if (virMutexInit(&ka->lock) < 0) {
VIR_FREE(ka); VIR_FREE(ka);
return NULL; return NULL;
} }
ka->refs = 1;
ka->interval = interval; ka->interval = interval;
ka->count = count; ka->count = count;
ka->countToDeath = count; ka->countToDeath = count;
@ -223,44 +232,20 @@ virKeepAliveNew(int interval,
ka->freeCB = freeCB; ka->freeCB = freeCB;
PROBE(RPC_KEEPALIVE_NEW, PROBE(RPC_KEEPALIVE_NEW,
"ka=%p client=%p refs=%d", "ka=%p client=%p",
ka, ka->client, ka->refs); ka, ka->client);
return ka; return ka;
} }
void void
virKeepAliveRef(virKeepAlivePtr ka) virKeepAliveDispose(void *obj)
{ {
virKeepAliveLock(ka); virKeepAlivePtr ka = obj;
ka->refs++;
PROBE(RPC_KEEPALIVE_REF,
"ka=%p client=%p refs=%d",
ka, ka->client, ka->refs);
virKeepAliveUnlock(ka);
}
void
virKeepAliveFree(virKeepAlivePtr ka)
{
if (!ka)
return;
virKeepAliveLock(ka);
PROBE(RPC_KEEPALIVE_FREE,
"ka=%p client=%p refs=%d",
ka, ka->client, ka->refs);
if (--ka->refs > 0) {
virKeepAliveUnlock(ka);
return;
}
virMutexDestroy(&ka->lock); virMutexDestroy(&ka->lock);
ka->freeCB(ka->client); ka->freeCB(ka->client);
VIR_FREE(ka);
} }
@ -311,12 +296,12 @@ virKeepAliveStart(virKeepAlivePtr ka,
timeout = ka->interval - delay; timeout = ka->interval - delay;
ka->intervalStart = now - (ka->interval - timeout); ka->intervalStart = now - (ka->interval - timeout);
ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer, ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer,
ka, virKeepAliveTimerFree); ka, virObjectFreeCallback);
if (ka->timer < 0) if (ka->timer < 0)
goto cleanup; goto cleanup;
/* the timer now has another reference to this object */ /* the timer now has another reference to this object */
ka->refs++; virObjectRef(ka);
ret = 0; ret = 0;
cleanup: cleanup:

View File

@ -24,6 +24,7 @@
# define __VIR_KEEPALIVE_H__ # define __VIR_KEEPALIVE_H__
# include "virnetmessage.h" # include "virnetmessage.h"
# include "virobject.h"
typedef int (*virKeepAliveSendFunc)(void *client, virNetMessagePtr msg); typedef int (*virKeepAliveSendFunc)(void *client, virNetMessagePtr msg);
typedef void (*virKeepAliveDeadFunc)(void *client); typedef void (*virKeepAliveDeadFunc)(void *client);
@ -42,9 +43,6 @@ virKeepAlivePtr virKeepAliveNew(int interval,
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6); ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6);
void virKeepAliveRef(virKeepAlivePtr ka);
void virKeepAliveFree(virKeepAlivePtr ka);
int virKeepAliveStart(virKeepAlivePtr ka, int virKeepAliveStart(virKeepAlivePtr ka,
int interval, int interval,
unsigned int count); unsigned int count);

View File

@ -361,7 +361,7 @@ error:
VIR_FORCE_CLOSE(wakeupFD[1]); VIR_FORCE_CLOSE(wakeupFD[1]);
if (ka) { if (ka) {
virKeepAliveStop(ka); virKeepAliveStop(ka);
virKeepAliveFree(ka); virObjectUnref(ka);
} }
virNetClientFree(client); virNetClientFree(client);
return NULL; return NULL;
@ -551,7 +551,7 @@ virNetClientCloseLocked(virNetClientPtr client)
if (ka) { if (ka) {
virKeepAliveStop(ka); virKeepAliveStop(ka);
virKeepAliveFree(ka); virObjectUnref(ka);
} }
if (closeCb) if (closeCb)
closeCb(client, closeReason, closeOpaque); closeCb(client, closeReason, closeOpaque);

View File

@ -629,7 +629,7 @@ void virNetServerClientClose(virNetServerClientPtr client)
client->keepalive = NULL; client->keepalive = NULL;
client->refs++; client->refs++;
virNetServerClientUnlock(client); virNetServerClientUnlock(client);
virKeepAliveFree(ka); virObjectUnref(ka);
virNetServerClientLock(client); virNetServerClientLock(client);
client->refs--; client->refs--;
} }
@ -1199,7 +1199,7 @@ cleanup:
virNetServerClientUnlock(client); virNetServerClientUnlock(client);
if (ka) if (ka)
virKeepAliveStop(ka); virKeepAliveStop(ka);
virKeepAliveFree(ka); virObjectUnref(ka);
return ret; return ret;
} }