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); }