catch the inevitable disaster early and define our own mutex types. sigh. and there i was, thinking that using the glib types would make things easier.

remotes/origin/2.0
Richard Fuchs 13 years ago
parent e11d55b3fb
commit 540d847d08

@ -146,4 +146,22 @@ static inline int smart_pton(int af, char *src, void *dst) {
#if !GLIB_CHECK_VERSION(2,32,0)
typedef GStaticMutex mutex_t;
#define mutex_init(m) g_static_mutex_init(m)
#define mutex_lock(m) g_static_mutex_lock(m)
#define mutex_unlock(m) g_static_mutex_unlock(m)
#else
typedef GMutex mutex_t;
#define mutex_init(m) g_mutex_init(m)
#define mutex_lock(m) g_mutex_lock(m)
#define mutex_unlock(m) g_mutex_unlock(m)
#endif
#endif #endif

@ -33,7 +33,7 @@ struct poller_item_int {
struct poller { struct poller {
int fd; int fd;
GStaticMutex lock; mutex_t lock;
struct poller_item_int **items; struct poller_item_int **items;
unsigned int items_size; unsigned int items_size;
GList *timers; GList *timers;
@ -56,7 +56,7 @@ struct poller *poller_new(void) {
p->fd = epoll_create1(0); p->fd = epoll_create1(0);
if (p->fd == -1) if (p->fd == -1)
abort(); abort();
g_static_mutex_init(&p->lock); mutex_init(&p->lock);
return p; return p;
} }
@ -101,7 +101,7 @@ static int __poller_add_item(struct poller *p, struct poller_item *i, int has_lo
goto fail_lock; goto fail_lock;
if (!has_lock) if (!has_lock)
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (i->fd < p->items_size && p->items[i->fd]) if (i->fd < p->items_size && p->items[i->fd])
goto fail; goto fail;
@ -124,7 +124,7 @@ static int __poller_add_item(struct poller *p, struct poller_item *i, int has_lo
obj_hold(ip->item.obj); /* new ref in *ip */ obj_hold(ip->item.obj); /* new ref in *ip */
p->items[i->fd] = obj_get(&ip->obj); p->items[i->fd] = obj_get(&ip->obj);
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
if (i->timer) if (i->timer)
poller_timer(p, poller_fd_timer, &ip->obj); poller_timer(p, poller_fd_timer, &ip->obj);
@ -134,11 +134,11 @@ static int __poller_add_item(struct poller *p, struct poller_item *i, int has_lo
return 0; return 0;
fail: fail:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return -1; return -1;
fail_lock: fail_lock:
if (has_lock) if (has_lock)
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return -1; return -1;
} }
@ -166,7 +166,7 @@ int poller_del_item(struct poller *p, int fd) {
if (!p || fd < 0) if (!p || fd < 0)
return -1; return -1;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail; goto fail;
@ -178,7 +178,7 @@ int poller_del_item(struct poller *p, int fd) {
p->items[fd] = NULL; /* stealing the ref */ p->items[fd] = NULL; /* stealing the ref */
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
if (it->item.timer) { if (it->item.timer) {
while (1) { while (1) {
@ -198,7 +198,7 @@ int poller_del_item(struct poller *p, int fd) {
return 0; return 0;
fail: fail:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return -1; return -1;
} }
@ -215,7 +215,7 @@ int poller_update_item(struct poller *p, struct poller_item *i) {
if (!i->closed) if (!i->closed)
return -1; return -1;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (i->fd >= p->items_size || !(np = p->items[i->fd])) if (i->fd >= p->items_size || !(np = p->items[i->fd]))
return __poller_add_item(p, i, 1); return __poller_add_item(p, i, 1);
@ -229,7 +229,7 @@ int poller_update_item(struct poller *p, struct poller_item *i) {
np->item.closed = i->closed; np->item.closed = i->closed;
/* updating timer is not supported */ /* updating timer is not supported */
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return 0; return 0;
} }
@ -246,7 +246,7 @@ int poller_poll(struct poller *p, int timeout) {
if (!p) if (!p)
return -1; return -1;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
ret = -1; ret = -1;
if (!p->items || !p->items_size) if (!p->items || !p->items_size)
@ -259,18 +259,18 @@ int poller_poll(struct poller *p, int timeout) {
ne = li->next; ne = li->next;
ti = li->data; ti = li->data;
/* XXX not safe */ /* XXX not safe */
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
ti->func(ti->obj_ptr); ti->func(ti->obj_ptr);
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
} }
ret = p->items_size; ret = p->items_size;
goto out; goto out;
} }
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
errno = 0; errno = 0;
ret = epoll_wait(p->fd, evs, sizeof(evs) / sizeof(*evs), timeout); ret = epoll_wait(p->fd, evs, sizeof(evs) / sizeof(*evs), timeout);
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (errno == EINTR) if (errno == EINTR)
ret = 0; ret = 0;
@ -288,7 +288,7 @@ int poller_poll(struct poller *p, int timeout) {
continue; continue;
obj_hold(&it->obj); obj_hold(&it->obj);
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
if (it->error) { if (it->error) {
it->item.closed(it->item.fd, it->item.obj, it->item.uintp); it->item.closed(it->item.fd, it->item.obj, it->item.uintp);
@ -298,7 +298,7 @@ int poller_poll(struct poller *p, int timeout) {
if ((ev->events & (POLLERR | POLLHUP))) if ((ev->events & (POLLERR | POLLHUP)))
it->item.closed(it->item.fd, it->item.obj, it->item.uintp); it->item.closed(it->item.fd, it->item.obj, it->item.uintp);
else if ((ev->events & POLLOUT)) { else if ((ev->events & POLLOUT)) {
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
it->blocked = 0; it->blocked = 0;
ZERO(e); ZERO(e);
@ -307,7 +307,7 @@ int poller_poll(struct poller *p, int timeout) {
if (epoll_ctl(p->fd, EPOLL_CTL_MOD, it->item.fd, &e)) if (epoll_ctl(p->fd, EPOLL_CTL_MOD, it->item.fd, &e))
abort(); abort();
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
it->item.writeable(it->item.fd, it->item.obj, it->item.uintp); it->item.writeable(it->item.fd, it->item.obj, it->item.uintp);
} }
else if ((ev->events & POLLIN)) else if ((ev->events & POLLIN))
@ -319,12 +319,12 @@ int poller_poll(struct poller *p, int timeout) {
next: next:
obj_put(&it->obj); obj_put(&it->obj);
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
} }
out: out:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return ret; return ret;
} }
@ -335,7 +335,7 @@ void poller_blocked(struct poller *p, int fd) {
if (!p || fd < 0) if (!p || fd < 0)
return; return;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail; goto fail;
@ -353,14 +353,14 @@ void poller_blocked(struct poller *p, int fd) {
abort(); abort();
fail: fail:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
} }
void poller_error(struct poller *p, int fd) { void poller_error(struct poller *p, int fd) {
if (!p || fd < 0) if (!p || fd < 0)
return; return;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail; goto fail;
@ -373,7 +373,7 @@ void poller_error(struct poller *p, int fd) {
p->items[fd]->blocked = 1; p->items[fd]->blocked = 1;
fail: fail:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
} }
int poller_isblocked(struct poller *p, int fd) { int poller_isblocked(struct poller *p, int fd) {
@ -382,7 +382,7 @@ int poller_isblocked(struct poller *p, int fd) {
if (!p || fd < 0) if (!p || fd < 0)
return -1; return -1;
g_static_mutex_lock(&p->lock); mutex_lock(&p->lock);
ret = -1; ret = -1;
if (fd >= p->items_size) if (fd >= p->items_size)
@ -395,7 +395,7 @@ int poller_isblocked(struct poller *p, int fd) {
ret = p->items[fd]->blocked ? 1 : 0; ret = p->items[fd]->blocked ? 1 : 0;
out: out:
g_static_mutex_unlock(&p->lock); mutex_unlock(&p->lock);
return ret; return ret;
} }

Loading…
Cancel
Save