#include <isc/once.h>
#include <isc/platform.h>
#include <isc/print.h>
+#include <isc/refcount.h>
#include <isc/region.h>
#include <isc/resource.h>
#include <isc/socket.h>
isc_mutex_t lock;
isc_sockettype_t type;
const isc_statscounter_t *statsindex;
+ isc_refcount_t references;
/* Locked by socket lock. */
ISC_LINK(isc__socket_t) link;
- unsigned int references;
int fd;
int pf;
int threadid;
#define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
#define SELECT_POKE_CLOSE (-5)
-#define SOCK_DEAD(s) ((s)->references == 0)
+#define SOCK_DEAD(s) (isc_refcount_current(&((s)->references)) == 0)
/*%
* Shortcut index arrays to get access to statistics counters.
sock->common.magic = 0;
sock->common.impmagic = 0;
- sock->references = 0;
+ isc_refcount_init(&sock->references, 0);
sock->manager = manager;
sock->type = type;
isc__socket_t *sock = *socketp;
INSIST(VALID_SOCKET(sock));
- INSIST(sock->references == 0);
+ INSIST(isc_refcount_current(&sock->references) == 0);
INSIST(!sock->connecting);
INSIST(ISC_LIST_EMPTY(sock->recv_list));
INSIST(ISC_LIST_EMPTY(sock->send_list));
abort();
}
sock->threadid = gen_threadid(sock);
- sock->references = 1;
+ isc_refcount_init(&sock->references, 1);
thread = &manager->threads[sock->threadid];
*socketp = (isc_socket_t *)sock;
REQUIRE(VALID_SOCKET(sock));
- LOCK(&sock->lock);
- REQUIRE(sock->references == 1);
- UNLOCK(&sock->lock);
+ REQUIRE(isc_refcount_current(&sock->references) == 1);
/*
* We don't need to retain the lock hereafter, since no one else has
* this socket.
REQUIRE(VALID_SOCKET(sock));
REQUIRE(socketp != NULL && *socketp == NULL);
- LOCK(&sock->lock);
- REQUIRE(sock->references > 0);
- sock->references++;
- UNLOCK(&sock->lock);
+ int old_refs = isc_refcount_increment(&sock->references);
+ REQUIRE(old_refs > 0);
*socketp = (isc_socket_t *)sock;
}
void
isc_socket_detach(isc_socket_t **socketp) {
isc__socket_t *sock;
- bool kill_socket = false;
REQUIRE(socketp != NULL);
sock = (isc__socket_t *)*socketp;
REQUIRE(VALID_SOCKET(sock));
- LOCK(&sock->lock);
- REQUIRE(sock->references > 0);
- sock->references--;
- if (sock->references == 0)
- kill_socket = true;
- UNLOCK(&sock->lock);
-
- if (kill_socket)
+ if (isc_refcount_decrement(&sock->references) == 1) {
destroy(&sock);
+ }
*socketp = NULL;
}
goto unlock_fd;
}
if (SOCK_DEAD(sock)) { /* Sock is being closed, bail */
- UNLOCK(&sock->lock);
- UNLOCK(&thread->fdlock[lockid]);
- return;
+ goto unlock_fd;
}
- LOCK(&sock->lock);
- sock->references++;
- UNLOCK(&sock->lock);
+ isc_refcount_increment(&sock->references);
if (readable) {
if (sock->listener)
if (unwatch_write)
(void)unwatch_fd(thread, fd, SELECT_POKE_WRITE);
if (sock != NULL) {
- LOCK(&sock->lock);
- sock->references--;
- UNLOCK(&sock->lock);
+ if (isc_refcount_decrement(&sock->references) == 1) {
+ destroy(&sock);
+ }
}
}
UNLOCK(&sock->lock);
return (ISC_R_SHUTTINGDOWN);
}
- nsock->references++;
+ isc_refcount_increment(&nsock->references);
nsock->statsindex = sock->statsindex;
dev->ev_sender = ntask;
TRY0(xmlTextWriterStartElement(writer,
ISC_XMLCHAR "references"));
TRY0(xmlTextWriterWriteFormatString(writer, "%d",
- sock->references));
+ isc_refcount_current(&sock->references)));
TRY0(xmlTextWriterEndElement(writer));
TRY0(xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
json_object_object_add(entry, "name", obj);
}
- obj = json_object_new_int(sock->references);
+ obj = json_object_new_int(isc_refcount_current(&sock->references));
CHECKMEM(obj);
json_object_object_add(entry, "references", obj);