예제 #1
0
	tree *findNode(tree **root,int data)
	{
		queue *Q;	
		tree *temp;

		if(!(*root)){
		return NULL;}
	
	   	Q=create();
	   	Enqueue(Q,*root);
	   	while(!(IsEmptyQueue(Q)))
	   	{
	   		temp=Dequeue(Q);
	   		if(temp->data == data)
	   		{deleteQueue(Q);
	   		return temp;
	   		}
	   		
	   		if(temp->left)
	   			Enqueue(Q,temp->left);

	   		if(temp->right)
	   			Enqueue(Q,temp->right);
	   	}
	   deleteQueue(Q);
	 return NULL;  	
	}
예제 #2
0
파일: linkqueue-link.c 프로젝트: wugsh/wgs
int main(int argc, const char * argv[])
{
    LinkQueue queue;
    puts("初始化队列 queue");
    initQueue(&queue);
    traversal(queue);
    
    puts("队尾依次插入0 1 2 3");
    insertQueue(&queue, 0);
    insertQueue(&queue, 1);
    insertQueue(&queue, 2);
    insertQueue(&queue, 3);
    traversal(queue);
    
    puts("先进先出,删除队列从头开始, 0 ");
    deleteQueue(&queue);
    traversal(queue);
    
    puts("先进先出,删除队列从头开始, 1 ");
    deleteQueue(&queue);
    traversal(queue);
    
    puts("先进先出,删除队列从头开始, 2 ");
    deleteQueue(&queue);
    traversal(queue);
    
    puts("先进先出,删除队列从头开始, 3");
    deleteQueue(&queue);
    traversal(queue);
    
    destoryQueue(&queue);
    return 0;
}
예제 #3
0
int main()
{
  int choice;

  while(1)
  {
    printf("\nChoose an option \n1. Add Element \n2. Delete Element \n3. View Queue \n4. Exit \n");

    printf("Enter your choice: ");
    scanf("%d", &choice); // get choice from user

    if(choice==1)
    {
      addQueue(); // if choice 1 then call addQueue function
    }
    else if(choice==2)
    {
      deleteQueue(); // if choice 2 then call deleteQueue function
    }
    else if(choice==3)
    {
      viewQueue(); // if choice 3 then call viewQueue function
    }
    else if(choice==4)
    {
      return 0; // if choice 4 then exit
    }
    else
    {
      printf("\nEnter valid choice \n"); // invalid choice
    }
  }
}
예제 #4
0
void linkedQueueType<Type>::copyQueue(const linkedQueueType<Type>& otherQueue){
    nodeType<Type> *newNode;
    nodeType<Type> *current;
    
    if(!otherQueue.front()){
        std::cout << "Cannot copy an empty queue" << std::endl;
        initializeQueue();
        return;
    }
    
    if (queueFront != NULL){
        deleteQueue();
    }
    initializeQueue();
    current = otherQueue.queueFront;
    queueFront = new nodeType<Type>;
    queueFront->info = current->info;
    queueFront->link = NULL;
    queueRear = queueFront;
    current = current->link;
    
    while(current != NULL){
        newNode = new nodeType<Type>;
        newNode->info = current->info;
        newNode->link = NULL;
        queueRear->link = newNode;
        queueRear = newNode;
        current = current->link;
    }
}
예제 #5
0
파일: test_queue.c 프로젝트: magic003/notes
int main(int argc, char* argv[]) {
    Queue* q = newQueue(5);

    enqueue(q, 1);
    enqueue(q, 2);
    enqueue(q, 3);

    printf("%d\n", dequeue(q));
    printf("%d\n", dequeue(q));

    enqueue(q, 4);
    enqueue(q, 5);
    enqueue(q, 6);

    printf("%d\n", dequeue(q));
    printf("%d\n", dequeue(q));
    printf("%d\n", dequeue(q));
    printf("%d\n", dequeue(q));

    printf("Empty: %d\n", emptyQueue(q));

    deleteQueue(q);

    return 0;
}
int isPathBfs(Graph g, Vertex v, Vertex w)
{
	int isPath = FALSE;
	Queue q = newQueue(); // create a new queue
	Vertex curV = 0;
	int *visited = calloc(g->nV, sizeof(int)); // allocate + init to 0
	int i = 0;

	assert(visited != NULL);
	enQueue(q, v);
	visited[v] = TRUE;

	while ( !isEmptyQueue(q) ){ // still have vertices to traverse
		curV = deQueue(q); // get a vertex from the queue
		printf("Visiting: %d.\n", curV);
		visited[curV] = TRUE; // mark it as visited
		if (curV == w) { isPath = TRUE; } 
		for (i = 0; i < g->nV; i++){ // find vertices to add to queue
			if (g->edges[curV][i] && !visited[i]){ // found a vertex to add
				enQueue(q, i); // add it to queue
				visited[i] = TRUE; // mark it as visited
			}
		}
	}	
	free(visited);
	deleteQueue(q);
	return isPath;
}
예제 #7
0
int main(int argc, char* argv[])
{
	int width, height;
	int** board;
	FILE* file;
	struct Queue* changeQueue;

	if (argc!=2)
	{
		printf("the right usage is: life <board file>\n");
		return WRONG_ARGUMENTS;
	}
	/*open board file*/
	file = fopen(argv[1], "r");
	if (file==NULL)
	{
		printf("error in openning the board file\n");
		return FILE_OPEN_ERROR;
	}

	board = readBoard(file, &width, &height);

	/*close board file*/
	if (fclose(file) != 0)
	{
		free(board);
		return FILE_CLOSE_ERROR;
		printf("error in closing the board file\n");
	}
	if (board==NULL)
	{
		return FILE_FORMAT_ERROR;
	}

	changeQueue = newQueue();

	/*print initial state*/
	system("cls");/*clear the screen*/
	printBoard(board, width, height);
	Sleep(2000);

	/*game math*/
	while (1)
	{
		calcChange(board, width, height, changeQueue);
		flip(board,changeQueue);
		system("cls");/*clear the screen*/
		printBoard(board, width, height);
		Sleep(1500);
	}

	/*doesn't really get here because it's an infinte loop*/
	deleteQueue(changeQueue);
	freeBoard(board,height);
	return 0;
}
예제 #8
0
// set a new size for the queue (recopy the values)
// s > 0
Queue setSize(Queue q, int s){
	Queue nq = newQueue(s);
	setQHead(nq, getQHead(q));
	setQTail(nq, getQTail(q));
	setQSize(nq, s);
	copyValues(q, nq);
	deleteQueue(q);
	free(q);
	return nq;
}
예제 #9
0
	void Insert(tree **root,int data)
	{
		queue *Q;	
		tree *temp,*Tnode;
		Tnode=(tree*)malloc(sizeof(tree));
		Tnode->data=data;
		Tnode->left=Tnode->right=NULL;
		
		
		if(!(*root)){
		*root=Tnode;
		return;}
	
	   	Q=create();
	   	Enqueue(Q,*root);
	   	while(!(IsEmptyQueue(Q)))
	   	{
	   		temp=Dequeue(Q);
	   		if(temp->left)
	   		{
	   			Enqueue(Q,temp->left);
	   		}
	   		else {
	   		temp->left=Tnode;
	   		deleteQueue(Q);
	   		return ;
	   		}
	   		
	   		if(temp->right){
	   			Enqueue(Q,temp->right);
	   			}
	   		else{
	   		temp->right=Tnode;
	   		deleteQueue(Q);
	   		return;
	   		}
	   	}
	   	deleteQueue(Q);
	 return;  	
	}
예제 #10
0
void levelOrderIter (TNODE_p_t root) {
	if (root == NULL)
		return;
	QUEUE_p_t queue = initQueue(root);
	while (!isEmptyQueue(queue)) {
		TNODE_p_t temp = deleteQueue(&queue);
		printf(" %d", temp->data);
		if (temp->left != NULL)
			insertQueue(&queue, temp->left);
		if (temp->right != NULL)
			insertQueue(&queue, temp->right);
	}
}
예제 #11
0
/* free queue memory after dispatcher quits*/
void freeQueues() {
	deleteQueue(dispatchQ);
  	deleteQueue(realtimeQ);
 	deleteQueue(userQ);
 	deleteQueue(p1Q);
	deleteQueue(p2Q);
 	deleteQueue(p3Q);
}
Graph shortestPath(Graph g, Vertex v)
{
	Graph mst = newGraph(g->nV); // create a new mst graph
	Queue q = newQueue(); // create a new queue
	int *visitedVertices = malloc(sizeof(Vertex) * g->nV);
	int *dist = malloc(sizeof(int) * g->nV); // create the distance array
	int *pred = malloc(sizeof(int) * g->nV); // create the predecessor array, stores vertices passed through
	int total = 0, i = 0;
	Vertex curV = 0, w = 0;

	assert(visitedVertices != NULL && dist != NULL && pred != NULL);
	
	// clear all the memory blocks
	setArray(visitedVertices, UNVISITED, g->nV);
	setArray(dist, INF, g->nV);
	setArray(pred, -1, g->nV);

	visitedVertices[v] = VISITED; // mark the starting vertex as visited
	dist[v] = 0;
	
	enQueue(q, v); // add the starting vertex to the queue

	while ( !isEmptyQueue(q) ){
		curV = deQueue(q); // remvoe first element from queue
		for (w = 0; w < getnV(g); w++){
			if (g->wt[curV][w] == NO_WEIGHT) continue;
			if (dist[curV] + g->wt[curV][w] < dist[w]){ // edge relaxation
				dist[w] = dist[curV] + g->wt[curV][w];
				pred[w] = curV;
				enQueue(q,w);
			}
		}
	}
	

	// add the appropriate edges
	for (i = 0; i < g->nV; i++){
		if (pred[i] != NOT_ASSIGNED){
			addEdge(mst, pred[i], i);
			total += dist[i];
		}
	}

	deleteQueue(q);
	free(dist);
	free(pred);
	printf("Total = %d.\n", total);

	return mst;
}
예제 #13
0
void waitingCustomerQueueType<customerType>::updateWaitingQueue() {
    customerType cust;
    cust.setWaitingTime(-1);
    int wTime = 0;
    addQueue(cust);
    while (wTime != -1){
        cust = front();
        deleteQueue();
        wTime = cust.getWaitingTime();
        if (wTime == -1){
            break;
        }
        cust.incrementWaitingTime();
        addQueue(cust);
    }
}
예제 #14
0
파일: dcap_close.c 프로젝트: dCache/dcap
int
dc_close2(int fd)
{
	int              res = 0;
	struct vsp_node *node;
	int32_t          size;


#ifdef DC_CALL_TRACE
	showTraceBack();
#endif

	/* nothing wrong ... yet */
	dc_errno = DEOK;

	node = delete_vsp_node(fd);
	if (node == NULL) {
		/* we have not such file descriptor, so lets give a try to system */
		return system_close(fd);
	}


	dc_real_fsync( node );


	if(node->unsafeWrite) {

		size = htonl(-1); /* send end of data */
		writen(node->dataFd, (char *) &size, sizeof(size), NULL);
		/* FIXME: error detection missing */

		if (get_fin(node) < 0) {
			dc_debug(DC_ERROR, "dc_close: mover did not FIN the data blocks.");
			res = -1;
		}
	}

	close_data_socket(node->dataFd);

	deleteQueue(node->queueID);

	m_unlock(&node->mux);

	node_destroy(node);

	return res;
}
예제 #15
0
파일: dcap_chown.c 프로젝트: ic-hep/emi3
int dc_chown( const char *path, uid_t uid, gid_t gid)
 {


	dcap_url *url;
	struct vsp_node *node;
	int rc;

	url = (dcap_url *)dc_getURL(path);
 	if( url == NULL ) {
		dc_debug(DC_INFO, "Using system native chown for %s.", path);
		return system_chown(path, uid, gid);

	}

 	node = new_vsp_node( path );
	if( node == NULL ) {
		free(url->file);
		free(url->host);
		if( url->prefix != NULL ) free(url->prefix);
		free(url);
		return 1;
	}

	node->url = url;
	if (url->type == URL_PNFS) {
		node->pnfsId = (char *)strdup(url->file);
	}else{
		node->pnfsId = (char *)strdup(path);
	}
	node->asciiCommand = DCAP_CMD_CHOWN;

	node->uid = uid;
	node->gid = gid;
	rc = cache_open(node);

	/* node cleanup procedure */
	node_unplug( node );

	deleteQueue(node->queueID);
	node_destroy(node);

	return rc;

}
예제 #16
0
파일: queue1.c 프로젝트: sachinDcoder/DS
int main()
{
    int choice,cap,data;
    printf("1. create Queue(a queue is required before any other operation to be done on the queue\n");
    printf("2. insert an element in the queue Queue\n");
    printf("3. delete an element from the Queue\n");
    printf("4. delete the whole Queue\n");
    printf("5. view Queue\n");
    printf("6. exit\n");
    while(1)
    {
        printf("\nEnter your choice : ");
        fflush(stdin);
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("\nEnter the capacity of the Queue : ");
                scanf("%d",&cap);
                if(createQueue(cap)!=NULL)
                     printf("\nQueue has been created successfully");
                break;
            case 2:
                printf("\nEnter the data to be inserted : ");
                scanf("%d",&data);
                enQueue(createQueue(cap),data);
                break;
            case 3:
                deQueue(createQueue(cap));
                break;
            case 4:
                deleteQueue(createQueue(cap));
                break;
            case 5:
                viewQueue(createQueue(cap));
                break;
            case 6:
                exit(0);
            default:
                printf("\nInvalid choice");
        }
    }
    return 0;
}
// Note: Level order cannot be done recursively
void levelOrder(Link n)
{
	Queue q = newQueue();
	Link curNode = NULL;
	enQueue(q, n);
	while ( !emptyQueue(q) ){
		curNode = deQueue(q);
		if (curNode->left != NULL){
			enQueue(q, curNode->left);
		}

		if (curNode->right != NULL){
			enQueue(q, curNode->right);
		}
		printf("%d ", curNode->value);
	} 
	printf("\n");
	deleteQueue(q);
}
예제 #18
0
파일: p5.c 프로젝트: jsungholee/dataStruct
int main(int argc, char* argv[]){
  FILE *ifp;
  ifp = fopen(argv[1], "r");
  char input[10]; // array to store line
  char cmd;
  char *cmdPtr;
  int element = 1;

  circularQueue q;
  
  while (fgets(input, 10, ifp)){
    cmdPtr = strtok(input, " ");
    cmd = cmdPtr[0];
    if (cmd == 'n' || cmd == 'e')
      element = atoi(strtok(NULL, " "));
    switch(cmd){
      case 'e':
        enqueue(q, element);
        break;
      case 'd':
        dequeue(q);
        break;
      case 'f':
        printFirst(q);
        break;
      case 'r':
        printRear(q);
        break;
      case 'n':
        q = makeEmpty(element);
        break;
      case 'p': // printing off the array to test if circular array is working
        for(int i=0; i<q->max_queue_size; i++)
          printf("[%i],", q->key[i]);
        printf("\n");
    }
  }
  deleteQueue(q); // free the memory
  fclose(ifp);

  return 0;
}
예제 #19
0
	int LevelOrderTraverse(tree *root)
	{ int count=0;
		queue *Q;	
		tree *temp;

		if(!root){
		return 0;}
	
	   	Q=create();
	   	Enqueue(Q,root);
		Enqueue(Q,NULL);

	   	while(!(IsEmptyQueue(Q)))
	   	{
	   		temp=Dequeue(Q);
			if(temp){
			printf("%d\t",temp->data);
			count++;
			}
			
	   		//completion of current Level
	   		if(temp == NULL)
	   		{
	   			if(!IsEmptyQueue(Q)){
	   			Enqueue(Q,NULL);
	   			printf("\n");
	   			}
	   		}
	   		else
	   		{
	   		if(temp->left)
	   			Enqueue(Q,temp->left);

	   		if(temp->right)
	   			Enqueue(Q,temp->right);
	   		}
	   	}
	   deleteQueue(Q);
	 return count;  	
	}
예제 #20
0
/*		Deepest Node in Binary Tree		*/
	tree *depthOfTree(tree **root)
	{
		queue *Q;	
		tree *temp,*temp1;

		if(!(*root)){
		return NULL;}
	
	   	Q=create();
	   	Enqueue(Q,*root);
	   	while(!(IsEmptyQueue(Q)))
	   	{
	   		temp=Dequeue(Q);
	   		if(temp->left)
	   			Enqueue(Q,temp->left);

	   		if(temp->right)
	   			Enqueue(Q,temp->right);
	   	}
	   deleteQueue(Q);
	 return temp;  	
	}
예제 #21
0
	int heightOfBinary(tree *root)
	{ 	
		int level=1;
		queue *Q;	
		
		if(!root){
		return 0;}
	
	   	Q=create();
	   	Enqueue(Q,root);
	   	//End Of first Level
	   	Enqueue(Q,NULL);
	   	
	   	while(!(IsEmptyQueue(Q)))
	   	{
	   		root=Dequeue(Q);
			
	   		//completion of current Level
	   		if(root == NULL)
	   		{
	   			if(!IsEmptyQueue(Q)){
	   			Enqueue(Q,NULL);
	   			level++;
	   			}
	   		}
	   		else
	   		{
	   		if(root->left)
	   			Enqueue(Q,root->left);
			
	   		if(root->right){
	   			Enqueue(Q,root->right);
		   		}
	   		}
	   	}
	   	deleteQueue(Q);
	 return level;  	
	}
예제 #22
0
파일: dcap_stat.c 프로젝트: ic-hep/emi3
int dc_lstat64(const char *path, struct stat64 *buf)
#endif
{
	dcap_url *url;
	struct vsp_node *node;
#ifdef WIN32
	struct _stati64 *s;
#else
	struct stat64 *s;
#endif
	int rc;
	int old_errno;

#ifdef DC_CALL_TRACE
	showTraceBack();
#endif

	/* nothing wrong ... yet */
	dc_errno = DEOK;

	url = (dcap_url *)dc_getURL(path);

	/* let system to do the work where it's possible */
	if(url == NULL) {
		dc_debug(DC_INFO, "Using system native lstat64 for %s.", path);
		rc = system_lstat64(path, buf);
		/* isPnfs overwrite errno */
		old_errno = errno;
		if( (buf->st_size != 1) ||  !isPnfs(path) ) {
			errno = old_errno;
			return rc;
		}
	}

	node = new_vsp_node(path);
	if (node == NULL) {
		dc_debug(DC_ERROR, "dc_stat: Failed to create new node.");
		free(url->file);
		free(url->host);
		free(url);
		return -1;
	}

	node->url = url;
	if (url == NULL ) {
		getPnfsID(node);
	}else{
		if (url->type == URL_PNFS) {
			node->pnfsId = (char *)strdup(url->file);
		}else{
			node->pnfsId = (char *)strdup(path);
		}
	}

	node->asciiCommand = DCAP_CMD_LSTAT;

	rc = cache_open(node);

	if(node->ipc != NULL) {
#ifdef WIN32
		s = (struct _stati64*)node->ipc;
		memcpy(buf, s, sizeof(struct _stati64) );
#else
		s = (struct stat64*)node->ipc;
		memcpy(buf, s, sizeof(struct stat64) );
#endif
		free(node->ipc);
		node->ipc = NULL;
	}

	/* node cleanup procedure */
	node_unplug( node );

	deleteQueue(node->queueID);
	node_destroy(node);

	if( rc != 0 ) {
		errno = ENOENT;
	}
	return rc;
}
예제 #23
0
파일: dcap_close.c 프로젝트: dCache/dcap
int
dc_close(int fd)
{
	int             res = 0;
	int             tmp;
	int32_t         size;
	int32_t         closemsg[6];
	int             msglen;
	struct vsp_node *node;


#ifdef DC_CALL_TRACE
	showTraceBack();
#endif


	/* nothing wrong ... yet */
	dc_errno = DEOK;

	node = delete_vsp_node(fd);
	if (node == NULL) {
		/* we have not such file descriptor, so lets give a try to system */
		dc_debug(DC_INFO, "Using system native close for [%d].", fd);
		return system_close(fd);
	}


	if ( node->lcb != NULL ) {
		dc_lcb_clean( node );
	}

	dc_real_fsync( node );

	if(node->unsafeWrite > 1) {

		size = htonl(-1); /* send end of data */
		writen(node->dataFd, (char *) &size, sizeof(size), NULL);
		/* FIXME: error detection missing */

		if (get_fin(node) < 0) {
			dc_debug(DC_ERROR, "dc_close: mover did not FIN the data blocks.");
			res = -1;
		}
	}

	if(node->reference == 0 ) {

		if( (node->sum != NULL) && ( node->sum->isOk == 1 ) ) {
			closemsg[0] = htonl(20);
			closemsg[2] = htonl(12);
			closemsg[3] = htonl(DCAP_DATA_SUM);
			closemsg[4] = htonl(node->sum->type);
			closemsg[5] = htonl(node->sum->sum);

			msglen = 6;
			dc_debug(DC_INFO, "File checksum is: %u", node->sum->sum);
		}else{
			closemsg[0] = htonl(4);
			msglen = 2;
		}

		closemsg[1] = htonl(IOCMD_CLOSE); /* actual command */

		dc_debug(DC_IO, "Sending CLOSE for fd:%d ID:%d.", node->dataFd, node->queueID);
		check_timeout_envar();
		dcap_set_alarm(closeTimeOut > 0 ? closeTimeOut : DCAP_IO_TIMEOUT/4);
		tmp = sendDataMessage(node, (char *) closemsg, msglen*sizeof(int32_t), ASCII_OK, NULL);
		/* FIXME: error detection missing */
		if( tmp < 0 ) {
			dc_debug(DC_ERROR, "sendDataMessage failed.");
			/* ignore close errors if file was open for read */
			if(node->flags & O_WRONLY) {
				res = -1;
			}

			if(isIOFailed) {
				isIOFailed = 0;
				/* command line dwon */
				if(!ping_pong(node)) {
					/* remove file descriptor from the list of control lines in use */
					lockMember();
					deleteMemberByValue(node->fd);
					unlockMember();
					pollDelete(node->fd);
					close_control_socket(node->fd, node->tunnel);
				}
			}
		}
		dcap_set_alarm(0);
		deleteQueue(node->queueID);

	}

	/*
	 * Even if there is still a reference to the dcap session,
	 * we have to close local socket descriptor.
	 */
	close_data_socket(node->dataFd);
	node_destroy(node);

	return res;
}
예제 #24
0
CSmartPtr<CMsgCommon> CMsgQueues::popMsg()
{
    LOG_INFO_FMT("%s[%p] pop msg ...", m_operator->toString(), m_operator);
    LOG_INFO_FMT("%s[%p] before pop msg, path[%s] has queue count[%lu] ...", 
        m_operator->toString(), m_operator, m_path.c_str(), m_queues.size());

    CSmartPtr<CMsgCommon> msg(NULL);

#if defined(USING_STL_MAP)
    //m_rwlock.rlock();

    UINT4 size = m_queues.size();
    if (size <= 0)
    {
        //m_rwlock.unlock();
        return msg;
    }

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueMap::iterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i < size; ++i)
    {
        if (it->second.isNull() || it->second->empty())
        {
            ++ m_pos;
            if (++it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
        }
        else
        {
            it->second->pop(msg);
#if 0
            if (it->second->empty())
            {
                deleteQueue(it->first);
                if ((--m_count % 5000 == 0) && (m_count > 0))
                    LOG_WARN_FMT("-------- path[%s] has %llu queues --------", m_path.c_str(), m_count);
                break;
            }
#else
            ++ m_pos;
#endif
            break;
        }
    }

    m_mutex.unlock();
    //m_rwlock.unlock();
#elif defined(USING_HASH_MAP)
    //m_rwlock.rlock();

    UINT4 bucketNum = m_queues.bucketNum();
    if (bucketNum <= 0)
    {
        //m_rwlock.unlock();
        return msg;
    }

    m_mutex.lock();

    m_bucket %= bucketNum;
    CMsgQueueHMap::CBucket* map = m_queues.bucket(m_bucket);

    for (UINT4 i = 0; i < bucketNum+1; ++i)
    {
        if (!map->empty() && (m_pos < map->size()))
        {
            CMsgQueueHMap::CBucket::iterator it = map->begin();
            if (m_pos > 0)
                advance(it, m_pos);
            while (it != map->end())
            {
                if (it->second.isNull() || it->second->empty())
                {
                    ++ it;
                    ++ m_pos;
                }
                else
                {
                    it->second->pop(msg);
                    ++ m_pos;
                    break;
                }
            }
        }

        if (msg.isNotNull())
        {
            break;
        }
        else
        {
            m_pos = 0;
            ++ m_bucket;
            m_bucket %= bucketNum;
            map = m_queues.bucket(m_bucket);
        }
    }

    m_mutex.unlock();
    //m_rwlock.unlock();
#elif defined(USING_LF_HASH_MAP)
    UINT4 size = m_queues.size();
    if (size <= 0)
        return msg;

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueLFHMap::CIterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i < size; ++i)
    {
        if (it->second.isNull() || it->second->empty())
        {
            ++ m_pos;
            if (++it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
        }
        else
        {
            it->second->pop(msg);
#if 0
            if (it->second->empty())
            {
                deleteQueue(it->first);
                if ((m_queues.size() > 0) && (m_queues.size() % 5000 == 0))
                    LOG_WARN_FMT("-------- path[%s] has %llu queues --------", m_path.c_str(), m_queues.size());
                break;
            }
#endif
            ++ m_pos;
            break;
        }
    }

    m_mutex.unlock();
#elif defined(USING_LK_HASH_MAP)
    UINT4 size = m_queues.size();
    if (size <= 0)
        return msg;

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueLKHMap::CReferenceIterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i <= size; i++)
    {
        CMsgKey key = it->first;
        CSmartPtr<CMsgQueue> queue = it->second;

#if defined(POP_LOCK_GRANULARITY_LARGE)
        UINT4 index = m_queues.bucketIndex(key);
        //m_queues.bucketLock(index);
        if (m_queues.bucketTrylock(index) != 0)
        {
            ++ it;
            ++ m_pos;
            if (it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
            continue;
        }
#endif

        if (queue.isNull() || queue->empty())
        {
            ++ it;
#if defined(POP_LOCK_GRANULARITY_LARGE)
            m_queues.bucketUnlock(index);
#endif
            ++ m_pos;
            if (it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
        }
        else
        {
            queue->pop(msg);
#if defined(POP_LOCK_GRANULARITY_LARGE)
            if (queue->empty())
            {
                deleteQueue(key);
                m_queues.bucketUnlock(index);
                break;
            }
            m_queues.bucketUnlock(index);
#elif defined(POP_LOCK_GRANULARITY_LITTLE)
            if (queue->empty())
            {
                UINT4 index = m_queues.bucketIndex(key);
                //m_queues.bucketLock(index);
                if (m_queues.bucketTrylock(index) == 0)
                {
                    if (queue->empty())
                    {
                        deleteQueue(key);
                        m_queues.bucketUnlock(index);
                        break;
                    }
                    m_queues.bucketUnlock(index);
                }
            }
#endif
            ++ m_pos;
            break;
        }
    }

    m_mutex.unlock();
#endif
    
    LOG_INFO_FMT("%s[%p] after poped msg, path[%s] has queue count[%lu] ...", 
        m_operator->toString(), m_operator, m_path.c_str(), m_queues.size());
#if 0
    if (!m_queues.empty()&&
        (!strcmp(m_operator->toString(),"CSwitchEventTester")||
        !strcmp(m_operator->toString(),"CLinkEventTester")||
        !strcmp(m_operator->toString(),"CPortEventTester")))
        if (m_queues.size() == 1)
        {
            CMsgKey key;
            CMsgQueue* queue = NULL;
            m_queues.front(key, queue);
            LOG_WARN_FMT("######%s[%p] after poped msg, path[%s] has only key[%s] queue size[%lu] ...", 
                m_operator->toString(), m_operator, m_path.c_str(), key.c_str(), queue->size());
        }
        else
        {
            LOG_WARN_FMT("######%s[%p] after poped msg, path[%s] has queue count[%lu] ...", 
                m_operator->toString(), m_operator, m_path.c_str(), m_queues.size());
        }
#endif

    return msg;
}
예제 #25
0
CSmartPtr<CMsgQueue> CMsgQueues::popMsgQueue()
{
    LOG_INFO_FMT("%s[%p] pop msg queue ...", m_operator->toString(), m_operator);
    LOG_INFO_FMT("%s[%p] before pop msg queue, path[%s] has queue count[%lu] ...", 
        m_operator->toString(), m_operator, m_path.c_str(), m_queues.size());

    CSmartPtr<CMsgQueue> queue(NULL);
    
#if defined(USING_STL_MAP)
    //m_rwlock.rlock();

    UINT4 size = m_queues.size();
    if (size <= 0)
    {
        //m_rwlock.unlock();
        return queue;
    }

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueMap::iterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i < size; i++)
    {
        if (it->second.isNull() || it->second->empty() || it->second->isBusy())
        {
            ++ m_pos;
            if (++it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
        }
        else
        {
            it->second->setBusy(TRUE);
            queue = it->second;
            ++ m_pos;
            break;
        }
    }

    m_mutex.unlock();
    //m_rwlock.unlock();
#elif defined(USING_HASH_MAP)
    //m_rwlock.rlock();

    UINT4 bucketNum = m_queues.bucketNum();
    if (bucketNum <= 0)
    {
        //m_rwlock.unlock();
        return queue;
    }

    m_mutex.lock();

    m_bucket %= bucketNum;
    CMsgQueueHMap::CBucket* map = m_queues.bucket(m_bucket);

    for (UINT4 i = 0; i < bucketNum+1; ++i)
    {
        if (!map->empty() && (m_pos < map->size()))
        {
            CMsgQueueHMap::CBucket::iterator it = map->begin();
            if (m_pos > 0)
                advance(it, m_pos);
            while (it != map->end())
            {
                if (it->second.isNull() || it->second->empty() || it->second->isBusy())
                {
                    ++ it;
                    ++ m_pos;
                }
                else
                {
                    it->second->setBusy(TRUE);
                    queue = it->second;
                    ++ m_pos;
                    break;
                }
            }
        }

        if (queue.isNotNull())
        {
            break;
        }
        else
        {
            m_pos = 0;
            ++ m_bucket;
            m_bucket %= bucketNum;
            map = m_queues.bucket(m_bucket);
        }
    }

    m_mutex.unlock();
    //m_rwlock.unlock();
#elif defined(USING_LF_HASH_MAP)
    UINT4 size = m_queues.size();
    if (size <= 0)
        return queue;

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueLFHMap::CIterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i < size; i++)
    {
        if (it->second.isNull() || it->second->empty() || it->second->isBusy())
        {
            ++ m_pos;
            if (++it == m_queues.end())
            {
                it = m_queues.begin();
                m_pos = 0;
            }
        }
        else
        {
            it->second->setBusy(TRUE);
            queue = it->second;
            ++ m_pos;
            break;
        }
    }

    m_mutex.unlock();
#elif defined(USING_LK_HASH_MAP)
    UINT4 size = m_queues.size();
    if (size <= 0)
        return queue;

    m_mutex.lock();

    m_pos %= size;
    CMsgQueueLKHMap::CReferenceIterator it = m_queues.begin();
    if (m_pos > 0)
        advance(it, m_pos);

    for (UINT4 i = 0; i <= size; i++)
    {
        CMsgKey key = it->first;
        CSmartPtr<CMsgQueue> que = it->second;

#if defined(POP_LOCK_GRANULARITY_LARGE) || defined(POP_LOCK_GRANULARITY_LITTLE)
        UINT4 index = m_queues.bucketIndex(key);
        //m_queues.bucketLock(index);
        if (m_queues.bucketTrylock(index) != 0)
        {
            ++ it;
            ++ m_pos;
            continue;
        }
#endif

        if (que.isNull() || que->empty())
        {
            it++;
#if defined(POP_LOCK_GRANULARITY_LARGE) || defined(POP_LOCK_GRANULARITY_LITTLE)
            deleteQueue(key);
            m_queues.bucketUnlock(index);
#else
            ++ m_pos;
#endif
        }
        else if (que->isBusy())
        {
            ++ it;
#if defined(POP_LOCK_GRANULARITY_LARGE) || defined(POP_LOCK_GRANULARITY_LITTLE)
            m_queues.bucketUnlock(index);
#endif
            ++ m_pos;
        }
        else
        {
            que->setBusy(TRUE);
#if defined(POP_LOCK_GRANULARITY_LARGE) || defined(POP_LOCK_GRANULARITY_LITTLE)
            m_queues.bucketUnlock(index);
#endif
            queue = que;
            ++ m_pos;
            break;
        }

        if (it == m_queues.end())
        {
            it = m_queues.begin();
            m_pos = 0;
        }
    }

    m_mutex.unlock();
#endif

    LOG_INFO_FMT("%s[%p] after pop msg queue, path[%s] has queue count[%lu] ...", 
        m_operator->toString(), m_operator, m_path.c_str(), m_queues.size());

    return queue;
}
예제 #26
0
/**
 * @brief  Deletes the queue specified by the queue URL.
 *
 * @param  queueUrl  URL of the Amazon SQS queue to take action on.
 *
 * @return A pointer to a related response object.
 *
 * @note   The caller is to take responsbility for the resulting pointer.
 *
 * @see    SqsDeleteQueueRequest
 */
SqsDeleteQueueResponse * SqsClient::deleteQueue(const QString &queueUrl)
{
    const SqsDeleteQueueRequest request(queueUrl);
    return deleteQueue(request);
}