// Thread-safe and non-blocking. Returns false if the deque is empty. // Complexity: O(Processes) bool pop_right(T& r) { // Loop until we either pop an element or learn that the deque is empty. while (true) { // Load the anchor. anchor_pair lrs = anchor_.lrs(); // Check if the deque is empty. // FIXME: Should we check both pointers here? if (lrs.get_right_ptr() == nullptr) return false; // Check if the deque has 1 element. if (lrs.get_right_ptr() == lrs.get_left_ptr()) { // Try to set both anchor pointer if (anchor_.cas(lrs, anchor_pair(nullptr, nullptr, lrs.get_left_tag(), lrs.get_right_tag() + 1))) { // Set the result, deallocate the popped node, and return. r = lrs.get_right_ptr()->data; dealloc_node(lrs.get_right_ptr()); return true; } } // Check if the deque is stable. else if (lrs.get_left_tag() == stable) { // Make sure the anchor hasn't changed since we loaded it. if (anchor_ != lrs) continue; // Get the rightmost nodes' left node. node_pointer prev = lrs.get_right_ptr()->left.load(); // Try to update the anchor to point to prev as the rightmost // node. if (anchor_.cas(lrs, anchor_pair(lrs.get_left_ptr(), prev.get_ptr(), lrs.get_left_tag(), lrs.get_right_tag() + 1))) { // Set the result, deallocate the popped node, and return. r = lrs.get_right_ptr()->data; dealloc_node(lrs.get_right_ptr()); return true; } } // The deque must be unstable, so we have to stabilize it before // we can continue. else // lrs.s() != stable stabilize(lrs); } }
bool dequeue (T * ret) { for (;;) { atomic_node_ptr head (head_); read_memory_barrier(); atomic_node_ptr tail(tail_); node * next = head->next.get_ptr(); if (likely(head == head_)) { if (head.get_ptr() == tail.get_ptr()) { if (next == 0) return false; tail_.cas(tail, next); } else { *ret = next->data; if (head_.cas(head, next)) { dealloc_node(head.get_ptr()); return true; } } } } }
int control_p_node_register(int fd, const char * body, uint32_t body_len) { int index = 0; uint32_t node_version = 0; taomee::unpack_h(body, node_version, index); #ifdef CHECK_NODE_VERSION if (0 != g_allow_node_version) { if (g_allow_node_version != node_version) { // 关掉连接 net_close_cli(fd); return 0; } } #endif uint32_t node_id = 0; taomee::unpack_h(body, node_id, index); uint32_t node_ip = 0; taomee::unpack_h(body, node_ip, index); if (body_len != (uint32_t)index) { return -1; } c_node * p_node = find_node(node_id); if (NULL != p_node) { dealloc_node(p_node); } p_node = alloc_node(node_id, fd, node_ip); if (NULL == p_node) { net_close_cli(fd); return -1; } STRNCPY(p_node->m_node_ip_str, long2ip(node_ip), sizeof(p_node->m_node_ip_str)); INFO_LOG("new incoming node, id: %u, ip: %s", p_node->m_node_id, p_node->m_node_ip_str); return 0; }
void link_down_cli(int fd) { DEBUG_LOG("client link down, fd: %d", fd); c_node * p_node = find_node_by_fd(fd); if (NULL != p_node) { clear_node_proxy(p_node); dealloc_node(p_node); } }
// Delete Node from any point in DynArray int delete_dyn_array_node(DynArray *a, int index) { LIBPREFIX_ASSERT(a->type != UNINITIALIZED, ARR_NOT_INIT); LIBPREFIX_ASSERT(a->type == NON_CONTINUOUS, INCORRECT_ARR_TYPE); LIBPREFIX_ASSERT(index < a->size || index >= 0, INVALID_INDEX); LIBPREFIX_ASSERT(a->array[index] != NULL, KEY_NOT_FOUND); clear_node(a->array[index]); dealloc_node(a->array[index]); a->array[index] = NULL; a->total -= 1; return 0; }
bool pop(T * ret) { for (;;) { ptr_type old_tos; old_tos.set(tos); if (!old_tos) return false; node * new_tos = old_tos->next.get_ptr(); if (tos.cas(old_tos, new_tos)) { *ret = old_tos->v; dealloc_node(old_tos.get_ptr()); return true; } } }
// Free dynamic array structure void clear_dyn_array(DynArray *a) { int i; if (a->type == CONTINUOUS) { for (i = 0; i < a->size; i++) { free(a->array[i]); } } else { for (i = 0; i < a->size; i++) { if (a->array[i] != NULL) { clear_node(a->array[i]); dealloc_node(a->array[i]); } } } free(a->array); a->size = 0; a->total = 0; a->type = UNINITIALIZED; a->array = NULL; }
// Remove a word in the set // Params: a node, a word int delete_word (Node *graph, wchar_t *word) { Node *lastwordnode = NULL, *searchPointer = graph; while (*word != L'\0' && searchPointer != NULL){ HashTable *word_table = searchPointer->next; if (searchPointer->isword && word_table->array->total == 1){ lastwordnode = searchPointer; } else if (word_table->array->total > 1) { lastwordnode = NULL; } searchPointer = lookup_node_hash_table(word_table, *word); word++; } if (searchPointer == NULL) return -1; searchPointer->isword = 0; if (lastwordnode != NULL){ clear_node(lastwordnode); dealloc_node(lastwordnode); } return 0; }
void free_node(struct mc *m, void *ptr, uint32_t cnt, int level, int use_mm) { if (ptr){ #ifndef USE_MM free(ptr); //if(cnt == 0) // printf("strange\n"); #ifdef DEBUG_MEMORY_ALLOC mem_v6 -= cnt * NODE_SIZE; #endif //node_num -= cnt; #else if(use_mm) dealloc_node(m, cnt, level, ptr); else { free(ptr); #ifdef DEBUG_MEMORY_ALLOC mem_v6 -= cnt * NODE_SIZE; #endif } #endif } }
~fifo(void) { assert(empty()); dealloc_node(head_.get_ptr()); }
void teardown(void) { dealloc_node(graph); }