Added locking to dynamic socket list

This commit is contained in:
Martin Willi
2010-02-26 11:44:34 +01:00
parent af2c43fdc7
commit 9cb2360e4f
@@ -38,6 +38,7 @@
#include <daemon.h> #include <daemon.h>
#include <threading/thread.h> #include <threading/thread.h>
#include <threading/rwlock.h>
#include <utils/hashtable.h> #include <utils/hashtable.h>
/* Maximum size of a packet */ /* Maximum size of a packet */
@@ -90,6 +91,11 @@ struct private_socket_dynamic_socket_t {
*/ */
hashtable_t *sockets; hashtable_t *sockets;
/**
* Lock for sockets hashtable
*/
rwlock_t *lock;
/** /**
* Notification pipe to signal receiver * Notification pipe to signal receiver
*/ */
@@ -146,6 +152,7 @@ static int build_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
FD_SET(this->notify[0], fds); FD_SET(this->notify[0], fds);
maxfd = this->notify[0]; maxfd = this->notify[0];
this->lock->read_lock(this->lock);
enumerator = this->sockets->create_enumerator(this->sockets); enumerator = this->sockets->create_enumerator(this->sockets);
while (enumerator->enumerate(enumerator, &key, &value)) while (enumerator->enumerate(enumerator, &key, &value))
{ {
@@ -153,6 +160,7 @@ static int build_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
maxfd = max(maxfd, value->fd); maxfd = max(maxfd, value->fd);
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return maxfd + 1; return maxfd + 1;
} }
@@ -165,6 +173,7 @@ static dynsock_t* scan_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
enumerator_t *enumerator; enumerator_t *enumerator;
dynsock_t *key, *value, *selected = NULL; dynsock_t *key, *value, *selected = NULL;
this->lock->read_lock(this->lock);
enumerator = this->sockets->create_enumerator(this->sockets); enumerator = this->sockets->create_enumerator(this->sockets);
while (enumerator->enumerate(enumerator, &key, &value)) while (enumerator->enumerate(enumerator, &key, &value))
{ {
@@ -175,6 +184,8 @@ static dynsock_t* scan_fds(private_socket_dynamic_socket_t *this, fd_set *fds)
} }
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return selected; return selected;
} }
@@ -428,7 +439,9 @@ static dynsock_t *find_socket(private_socket_dynamic_socket_t *this,
char buf[] = {0x01}; char buf[] = {0x01};
int fd; int fd;
this->lock->read_lock(this->lock);
skt = this->sockets->get(this->sockets, &lookup); skt = this->sockets->get(this->sockets, &lookup);
this->lock->unlock(this->lock);
if (skt) if (skt)
{ {
return skt; return skt;
@@ -444,7 +457,9 @@ static dynsock_t *find_socket(private_socket_dynamic_socket_t *this,
.port = port, .port = port,
.fd = fd, .fd = fd,
); );
this->lock->write_lock(this->lock);
this->sockets->put(this->sockets, skt, skt); this->sockets->put(this->sockets, skt, skt);
this->lock->unlock(this->lock);
/* notify receiver thread to reread socket list */ /* notify receiver thread to reread socket list */
ignore_result(write(this->notify[1], buf, sizeof(buf))); ignore_result(write(this->notify[1], buf, sizeof(buf)));
@@ -567,6 +582,7 @@ METHOD(socket_dynamic_socket_t, destroy, void,
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->sockets->destroy(this->sockets); this->sockets->destroy(this->sockets);
this->lock->destroy(this->lock);
close(this->notify[0]); close(this->notify[0]);
close(this->notify[1]); close(this->notify[1]);
@@ -588,6 +604,7 @@ socket_dynamic_socket_t *socket_dynamic_socket_create()
}, },
.destroy = _destroy, .destroy = _destroy,
}, },
.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
); );
if (pipe(this->notify) != 0) if (pipe(this->notify) != 0)