Exemple #1
0
extern char* _allocate_node(char* key, char* seen_value, char** seen_value_array,
		int num_node_allocate, int num_node_before, char** query_value)
{
	int i = 0;
	char* nodelist = c_calloc(100 * 100);
	char* new_value = c_calloc(100 * 100);
	int num_node_left = num_node_before - num_node_allocate;
	char *_num_node_left = int_to_str(num_node_left);
	//usleep(1000);
	pthread_mutex_lock(&time_mutex);
	ntime++;
	pthread_mutex_unlock(&time_mutex);
	strcat(new_value, _num_node_left);
	free(_num_node_left);
	strcat(new_value, ",");
	for (i = 0; i < num_node_allocate; i++)
	{
		strcat(nodelist, seen_value_array[i]);
		if (i != num_node_allocate - 1)
		{
			strcat(nodelist, ",");
		}
	}
	for (i = 0; i < num_node_left; i++)
	{
		strcat(new_value, seen_value_array[i + num_node_allocate]);
		if (i != num_node_left - 1)
		{
			strcat(new_value, ",");
		}
	}
	int res = c_zht_compare_and_swap(key, seen_value, new_value, query_value);
	free(new_value);
	if (res)
	{
		return nodelist;
	}
	else
	{
		free(nodelist);
		printf("OK, compare and swap failed!\n");
		return NULL;
	}
}
Exemple #2
0
int compare_swap_internal() {

    int nodes_avail = lookup_nodes();

    int nodes_left = nodes_avail - 100; //allocate 100 nodes

    /*todo: guard the case: nodes_left < 0, means no nodes available*/
    if (nodes_left < 0)
        return RC_NO_NODES_AVAIL; //for example, -1000 means no nodes available

    char p_nodes_avail[20];
    sprintf(p_nodes_avail, "%d", nodes_avail);

    char p_nodes_left[20];
    sprintf(p_nodes_left, "%d", nodes_left);

    int rc = c_zht_compare_and_swap(key, p_nodes_avail, p_nodes_left);

    fprintf(stdout,
            "[%d] c_zht_compare_and_swap, {seen_value}:{new_value} => {%s}:{%s}, rc(%d)\n",
            CUR_PID, p_nodes_avail, p_nodes_left, rc);

    return rc;
}