Browse Source

MT#56374 use LOCK macro in poller

Change-Id: I779b894ce7f445ddd1731c4409a0393b524a9e1e
pull/1701/head
Richard Fuchs 2 years ago
parent
commit
baf12d25e5
1 changed files with 26 additions and 44 deletions
  1. +26
    -44
      lib/poller.c

+ 26
- 44
lib/poller.c View File

@ -58,10 +58,9 @@ void poller_map_add(struct poller_map *map) {
return; return;
tid = pthread_self(); tid = pthread_self();
mutex_lock(&map->lock);
LOCK(&map->lock);
p = poller_new(); p = poller_new();
g_hash_table_insert(map->table, (gpointer)tid, p); g_hash_table_insert(map->table, (gpointer)tid, p);
mutex_unlock(&map->lock);
} }
struct poller *poller_map_get(struct poller_map *map) { struct poller *poller_map_get(struct poller_map *map) {
@ -70,14 +69,13 @@ struct poller *poller_map_get(struct poller_map *map) {
struct poller *p = NULL; struct poller *p = NULL;
pthread_t tid = pthread_self(); pthread_t tid = pthread_self();
mutex_lock(&map->lock);
LOCK(&map->lock);
p = g_hash_table_lookup(map->table, (gpointer)tid); p = g_hash_table_lookup(map->table, (gpointer)tid);
if (!p) { if (!p) {
gpointer *arr = g_hash_table_get_keys_as_array(map->table, NULL); gpointer *arr = g_hash_table_get_keys_as_array(map->table, NULL);
p = g_hash_table_lookup(map->table, arr[ssl_random() % g_hash_table_size(map->table)]); p = g_hash_table_lookup(map->table, arr[ssl_random() % g_hash_table_size(map->table)]);
g_free(arr); g_free(arr);
} }
mutex_unlock(&map->lock);
return p; return p;
} }
@ -90,10 +88,8 @@ void poller_map_free(struct poller_map **map) {
struct poller_map *m = *map; struct poller_map *m = *map;
if (!m) if (!m)
return; return;
mutex_lock(&m->lock);
g_hash_table_foreach(m->table, poller_map_free_poller, NULL); g_hash_table_foreach(m->table, poller_map_free_poller, NULL);
g_hash_table_destroy(m->table); g_hash_table_destroy(m->table);
mutex_unlock(&m->lock);
mutex_destroy(&m->lock); mutex_destroy(&m->lock);
g_slice_free1(sizeof(*m), m); g_slice_free1(sizeof(*m), m);
*map = NULL; *map = NULL;
@ -154,18 +150,20 @@ int poller_add_item(struct poller *p, struct poller_item *i) {
if (!p) if (!p)
return -1; return -1;
if (!i) if (!i)
goto fail_lock;
return -1;
if (i->fd < 0) if (i->fd < 0)
goto fail_lock;
return -1;
if (!i->readable && !i->writeable) if (!i->readable && !i->writeable)
goto fail_lock;
return -1;
if (!i->closed) if (!i->closed)
goto fail_lock;
return -1;
mutex_lock(&p->lock);
{
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;
return -1;
ZERO(e); ZERO(e);
e.events = epoll_events(i, NULL); e.events = epoll_events(i, NULL);
@ -185,18 +183,11 @@ int poller_add_item(struct poller *p, struct poller_item *i) {
obj_hold_o(ip->item.obj); /* new ref in *ip */ obj_hold_o(ip->item.obj); /* new ref in *ip */
p->items[i->fd] = obj_get(ip); p->items[i->fd] = obj_get(ip);
mutex_unlock(&p->lock);
} // unlock
obj_put(ip); obj_put(ip);
return 0; return 0;
fail:
mutex_unlock(&p->lock);
return -1;
fail_lock:
mutex_unlock(&p->lock);
return -1;
} }
@ -206,27 +197,25 @@ int poller_del_item(struct poller *p, int fd) {
if (!p || fd < 0) if (!p || fd < 0)
return -1; return -1;
mutex_lock(&p->lock);
{
LOCK(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail;
return -1;
if (!p->items || !(it = p->items[fd])) if (!p->items || !(it = p->items[fd]))
goto fail;
return -1;
if (epoll_ctl(p->fd, EPOLL_CTL_DEL, fd, NULL)) if (epoll_ctl(p->fd, EPOLL_CTL_DEL, fd, NULL))
abort(); abort();
p->items[fd] = NULL; /* stealing the ref */ p->items[fd] = NULL; /* stealing the ref */
mutex_unlock(&p->lock);
} // unlock
obj_put(it); obj_put(it);
return 0; return 0;
fail:
mutex_unlock(&p->lock);
return -1;
} }
@ -315,14 +304,14 @@ void poller_blocked(struct poller *p, void *fdp) {
if (!p || fd < 0) if (!p || fd < 0)
return; return;
mutex_lock(&p->lock);
LOCK(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail;
return;
if (!p->items || !p->items[fd]) if (!p->items || !p->items[fd])
goto fail;
return;
if (!p->items[fd]->item.writeable) if (!p->items[fd]->item.writeable)
goto fail;
return;
p->items[fd]->blocked = 1; p->items[fd]->blocked = 1;
@ -330,9 +319,6 @@ void poller_blocked(struct poller *p, void *fdp) {
e.events = epoll_events(NULL, p->items[fd]); e.events = epoll_events(NULL, p->items[fd]);
e.data.fd = fd; e.data.fd = fd;
epoll_ctl(p->fd, EPOLL_CTL_MOD, fd, &e); epoll_ctl(p->fd, EPOLL_CTL_MOD, fd, &e);
fail:
mutex_unlock(&p->lock);
} }
void poller_error(struct poller *p, void *fdp) { void poller_error(struct poller *p, void *fdp) {
@ -340,20 +326,17 @@ void poller_error(struct poller *p, void *fdp) {
if (!p || fd < 0) if (!p || fd < 0)
return; return;
mutex_lock(&p->lock);
LOCK(&p->lock);
if (fd >= p->items_size) if (fd >= p->items_size)
goto fail;
return;
if (!p->items || !p->items[fd]) if (!p->items || !p->items[fd])
goto fail;
return;
if (!p->items[fd]->item.writeable) if (!p->items[fd]->item.writeable)
goto fail;
return;
p->items[fd]->error = 1; p->items[fd]->error = 1;
p->items[fd]->blocked = 1; p->items[fd]->blocked = 1;
fail:
mutex_unlock(&p->lock);
} }
int poller_isblocked(struct poller *p, void *fdp) { int poller_isblocked(struct poller *p, void *fdp) {
@ -363,7 +346,7 @@ int poller_isblocked(struct poller *p, void *fdp) {
if (!p || fd < 0) if (!p || fd < 0)
return -1; return -1;
mutex_lock(&p->lock);
LOCK(&p->lock);
ret = -1; ret = -1;
if (fd >= p->items_size) if (fd >= p->items_size)
@ -376,7 +359,6 @@ int poller_isblocked(struct poller *p, void *fdp) {
ret = p->items[fd]->blocked ? 1 : 0; ret = p->items[fd]->blocked ? 1 : 0;
out: out:
mutex_unlock(&p->lock);
return ret; return ret;
} }


Loading…
Cancel
Save