Server* schedule_weighted_round_robin(Service* service, Endpoint* client_endpoint) { uint32_t count = list_size(service->active_servers); RoundRobin* roundrobin = service->priv; if(count == 0) return NULL; uint32_t whole_weight = 0; ListIterator iter; list_iterator_init(&iter, service->active_servers); while(list_iterator_has_next(&iter)) { Server* server = list_iterator_next(&iter); whole_weight += server->weight; } uint32_t _index = (roundrobin->robin++) & whole_weight; list_iterator_init(&iter, service->active_servers); while(list_iterator_has_next(&iter)) { Server* server = list_iterator_next(&iter); if(_index < server->weight) return server; else _index -= server->weight; } return NULL; }
static void stdio_callback(uint32_t vmid, int thread_id, int fd, char* buffer, volatile size_t* head, volatile size_t* tail, size_t size) { size_t len0, len1, len2; bool wrapped; if(*head <= *tail) { wrapped = false; len0 = *tail - *head; } else { wrapped = true; len1 = size - *head; len2 = *tail; } if(!manager_core) return; ListIterator iter; list_iterator_init(&iter, manager_core->clients); while(list_iterator_has_next(&iter)) { RPC* rpc = list_iterator_next(&iter); if(wrapped) { rpc_stdio(rpc, vmid, thread_id, fd, buffer + *head, len1, NULL, NULL); rpc_stdio(rpc, vmid, thread_id, fd, buffer, len2, NULL, NULL); } else { rpc_stdio(rpc, vmid, thread_id, fd, buffer + *head, len0, NULL, NULL); } rpc_loop(rpc); } if(wrapped) *head = (*head + len1 + len2) % size; else *head += len0; }
static void rpc_free(RPC* rpc) { RPCData* data = (RPCData*)rpc->data; ListIterator iter; list_iterator_init(&iter, data->pbufs); while(list_iterator_has_next(&iter)) { struct pbuf* pbuf = list_iterator_next(&iter); list_iterator_remove(&iter); pbuf_free(pbuf); } list_destroy(data->pbufs); list_remove_data(manager.actives, rpc); free(rpc); list_remove_data(manager_core->clients, rpc); }
static void destroy(Map* map) { for(size_t i = 0; i < map->capacity; i++) { List* list = map->table[i]; if(!list) continue; ListIterator iter; list_iterator_init(&iter, list); while(list_iterator_has_next(&iter)) { MapEntry* entry = list_iterator_next(&iter); __free(entry, map->pool); } list_destroy(list); } __free(map->table, map->pool); }
static inline void aggregate_seed(int agg_type, void *acc, void *start, int count, size_t size) { switch(agg_type) { case AGG_FIRST: memcpy(acc, start, size); return; case AGG_MIN_INT: case AGG_MAX_INT: MELD_INT(acc) = MELD_INT(start); return; case AGG_SUM_INT: MELD_INT(acc) = MELD_INT(start) * count; return; case AGG_MIN_FLOAT: case AGG_MAX_FLOAT: MELD_FLOAT(acc) = MELD_FLOAT(start); return; case AGG_SUM_FLOAT: MELD_FLOAT(acc) = MELD_FLOAT(start) * count; return; case AGG_SET_UNION_INT: { Set *set = set_int_create(); set_int_insert(set, MELD_INT(start)); set_print(set); MELD_SET(acc) = set; return; } case AGG_SET_UNION_FLOAT: { Set *set = set_float_create(); set_float_insert(set, MELD_FLOAT(start)); set_print(set); MELD_SET(acc) = set; return; } case AGG_SUM_LIST_INT: { List *result_list = list_int_create(); List *start_list = MELD_LIST(start); /* add values to result_list */ list_iterator it; for(it = list_get_iterator(start_list); list_iterator_has_next(it); it = list_iterator_next(it)) { meld_int total = list_iterator_int(it) * (meld_int)count; list_int_push_tail(result_list, total); } MELD_LIST(acc) = result_list; return; } case AGG_SUM_LIST_FLOAT: { List *result_list = list_float_create(); List *start_list = MELD_LIST(start); /* add values to result_list */ list_iterator it; for(it = list_get_iterator(start_list); list_iterator_has_next(it); it = list_iterator_next(it)) { meld_float total = list_iterator_float(it) * (meld_float)count; list_float_push_tail(result_list, total); } MELD_LIST(acc) = result_list; return; } } assert(0); while(1); }
static inline bool aggregate_accumulate(int agg_type, void *acc, void *obj, int count) { switch (agg_type) { case AGG_SET_UNION_INT: { Set *set = MELD_SET(acc); set_int_insert(set, MELD_INT(obj)); set_print(set); return false; } case AGG_SET_UNION_FLOAT: { Set *set = MELD_SET(acc); set_float_insert(set, MELD_FLOAT(obj)); set_print(set); return false; } case AGG_FIRST: return false; case AGG_MAX_INT: if (MELD_INT(obj) > MELD_INT(acc)) { MELD_INT(acc) = MELD_INT(obj); return true; } else return false; case AGG_MIN_INT: if (MELD_INT(obj) < MELD_INT(acc)) { MELD_INT(acc) = MELD_INT(obj); return true; } else return false; case AGG_SUM_INT: MELD_INT(acc) += MELD_INT(obj) * count; return false; case AGG_MAX_FLOAT: if(MELD_FLOAT(obj) > MELD_FLOAT(acc)) { MELD_FLOAT(acc) = MELD_FLOAT(obj); return true; } else return false; case AGG_MIN_FLOAT: if(MELD_FLOAT(obj) < MELD_FLOAT(acc)) { MELD_FLOAT(acc) = MELD_FLOAT(obj); return true; } else return false; case AGG_SUM_FLOAT: MELD_FLOAT(acc) += MELD_FLOAT(obj) * (meld_float)count; return false; case AGG_SUM_LIST_INT: { List *result_list = MELD_LIST(acc); List *other_list = MELD_LIST(obj); if(list_total(result_list) != list_total(other_list)) { fprintf(stderr, "lists differ in size for accumulator AGG_SUM_LIST_INT:" " %d vs %d\n", list_total(result_list), list_total(other_list)); exit(1); } list_iterator it_result = list_get_iterator(result_list); list_iterator it_other = list_get_iterator(other_list); while(list_iterator_has_next(it_result)) { list_iterator_int(it_result) += list_iterator_int(it_other) * (meld_int)count; it_other = list_iterator_next(it_other); it_result = list_iterator_next(it_result); } return false; } case AGG_SUM_LIST_FLOAT: { List *result_list = MELD_LIST(acc); List *other_list = MELD_LIST(obj); if(list_total(result_list) != list_total(other_list)) { fprintf(stderr, "lists differ in size for accumulator AGG_SUM_LIST_FLOAT: " "%d vs %d\n", list_total(result_list), list_total(other_list)); exit(1); } list_iterator it_result = list_get_iterator(result_list); list_iterator it_other = list_get_iterator(other_list); while(list_iterator_has_next(it_result)) { list_iterator_float(it_result) += list_iterator_float(it_other) * (meld_float)count; it_result = list_iterator_next(it_result); it_other = list_iterator_next(it_other); } return false; } } assert(0); while(1); }