int list_add(list_t * list, void *data) { list_node_t *node = NULL; return_val_if_fail(list != NULL, RET_INVALID_PARAMS); node = node_create(data); return_val_if_fail(node != NULL, RET_NOMEM); dlist_lock(list); __list_add(node, &list->head, list->head.next); dlist_unlock(list); return 0; }
Ret dlist_insert(DList* thiz, size_t index, void* data) { Ret ret = RET_OK; DListNode* node = NULL; DListNode* cursor = NULL; return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS); dlist_lock(thiz); do { if((node = dlist_create_node(thiz, data)) == NULL) { ret = RET_OOM; break; } if(thiz->first == NULL) { thiz->first = node; break; } cursor = dlist_get_node(thiz, index, 1); if(index < dlist_length(thiz)) { node->next = cursor; if(cursor->prev != NULL) { cursor->prev->next = node; } cursor->prev = node; if(thiz->first == cursor) { thiz->first = node; } } else { cursor->next = node; node->prev = cursor; } }while(0); dlist_unlock(thiz); return ret; }
size_t dlist_length(DList* thiz) { size_t length = 0; return_val_if_fail(thiz != NULL, 0); dlist_rdlock(thiz); length = dlist_length_nolock(thiz); dlist_unlock(thiz); return length; }
size_t list_length(list_t * thiz) { size_t length = 0; list_node_t *iter = NULL; return_val_if_fail(thiz != NULL, 0); dlist_lock(thiz); iter = &thiz->head; while (iter->next != &thiz->head) { length++; iter = iter->next; } dlist_unlock(thiz); return length; }
Ret dlist_get_by_index(DList* thiz, size_t index, void** data) { DListNode* cursor = NULL; return_val_if_fail(thiz != NULL && data != NULL, RET_INVALID_PARAMS); dlist_lock(thiz); cursor = dlist_get_node(thiz, index, 0); if(cursor != NULL) { *data = cursor->data; } dlist_unlock(thiz); return cursor != NULL ? RET_OK : RET_INVALID_PARAMS; }
Ret dlist_delete(DList* thiz, size_t index) { Ret ret = RET_OK; DListNode* cursor = NULL; return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS); dlist_lock(thiz); cursor = dlist_get_node(thiz, index, 0); do { if(cursor == NULL) { ret = RET_INVALID_PARAMS; break; } if(cursor != NULL) { if(cursor == thiz->first) { thiz->first = cursor->next; } if(cursor->next != NULL) { cursor->next->prev = cursor->prev; } if(cursor->prev != NULL) { cursor->prev->next = cursor->next; } dlist_destroy_node(thiz, cursor); } }while(0); dlist_unlock(thiz); return RET_OK; }
Ret dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx) { Ret ret = RET_OK; DListNode* iter = NULL; return_val_if_fail(thiz != NULL && visit != NULL, RET_INVALID_PARAMS); dlist_lock(thiz); iter = thiz->first; while(iter != NULL && ret != RET_STOP) { ret = visit(ctx, iter->data); iter = iter->next; } dlist_unlock(thiz); return ret; }
size_t dlist_length(DList* thiz) { size_t length = 0; DListNode* iter = NULL; return_val_if_fail(thiz != NULL, 0); dlist_lock(thiz); iter = thiz->first; while(iter != NULL) { length++; iter = iter->next; } dlist_unlock(thiz); return length; }
int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx) { int i = 0; DListNode* iter = NULL; return_val_if_fail(thiz != NULL && cmp != NULL, -1); dlist_lock(thiz); iter = thiz->first; while(iter != NULL) { if(cmp(ctx, iter->data) == 0) { break; } i++; iter = iter->next; } dlist_unlock(thiz); return i; }
static int server_cron(event_loop el, unsigned long id, void *data) { dlist_iter_t iter; dlist_node_t node; NOT_USED(el); NOT_USED(id); NOT_USED(data); if (log_reload) { close_logger(); if (init_logger("/var/log/xcb/xcb-dp2.log", __LOG_DEBUG) == 0) { const char *tmp; pthread_mutex_lock(&cfg_lock); if ((tmp = variable_retrieve(cfg, "general", "log_level"))) { if (!strcasecmp(tmp, "debug")) set_logger_level(__LOG_DEBUG); else if (!strcasecmp(tmp, "info")) set_logger_level(__LOG_INFO); else if (!strcasecmp(tmp, "notice")) set_logger_level(__LOG_NOTICE); else if (!strcasecmp(tmp, "warning")) set_logger_level(__LOG_WARNING); } pthread_mutex_unlock(&cfg_lock); log_reload = 0; } /* FIXME */ if (addms) table_clear(times); } if (shut_down) { if (prepare_for_shutdown() == 0) exit(0); xcb_log(XCB_LOG_WARNING, "SIGTERM received, but errors trying to shutdown the server"); } /* FIXME */ iter = dlist_iter_new(clients_to_close, DLIST_START_HEAD); while ((node = dlist_next(iter))) { client c = (client)dlist_node_value(node); if (c->refcount == 0) client_free(c); } dlist_iter_free(&iter); /* FIXME */ if (cronloops % 200 == 0) { char meme[] = "SU OT GNOLEB ERA ESAB RUOY LLA"; int status; /* heartbeat */ dlist_lock(clients); if (dlist_length(clients) > 0) { dstr res = dstr_new("HEARTBEAT|"); dstr ip = getipv4(); res = dstr_cat(res, ip); res = dstr_cat(res, "\r\n"); iter = dlist_iter_new(clients, DLIST_START_HEAD); while ((node = dlist_next(iter))) { client c = (client)dlist_node_value(node); pthread_spin_lock(&c->lock); if (net_try_write(c->fd, res, dstr_length(res), 100, NET_NONBLOCK) == -1) xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s", c, strerror(errno)); pthread_spin_unlock(&c->lock); } dlist_iter_free(&iter); dstr_free(ip); dstr_free(res); } dlist_unlock(clients); /* FIXME: trying to lower the high CPU load while idle */ if ((status = pgm_send(pgm_sender, meme, sizeof meme, NULL)) != PGM_IO_STATUS_NORMAL) xcb_log(XCB_LOG_WARNING, "Communication test failed"); } ++cronloops; return 100; }