void *producer( void *args ) { queue *fifo; fifo = static_cast<queue*>(args); for( int i = 0; i < LOOP; ++i ) { pthread_mutex_lock(fifo->mut); while( fifo->full ) { RPMS_WARN(LOGNAME, "producer: queue FULL"); pthread_cond_wait( fifo->notFull, fifo->mut ); } queueAdd( fifo, i ); pthread_mutex_unlock(fifo->mut); pthread_cond_signal (fifo->notEmpty); usleep(100000); } for( int i = 0; i < LOOP; ++i ) { pthread_mutex_lock(fifo->mut); while( fifo->full ) { RPMS_INFO(LOGNAME, "producer: queue FULL"); pthread_cond_wait( fifo->notFull, fifo->mut ); } queueAdd( fifo, i ); pthread_mutex_unlock(fifo->mut); pthread_cond_signal (fifo->notEmpty); usleep(100000); } return 0; }
void *producer (void *q) { queue *fifo; int i; fifo = (queue *)q; for (i = 0; i < LOOP; i++) { pthread_mutex_lock (fifo->mut); while (fifo->full) { printf ("producer: queue FULL.\n"); pthread_cond_wait (fifo->notFull, fifo->mut); } queueAdd (fifo, i); pthread_mutex_unlock (fifo->mut); pthread_cond_signal (fifo->notEmpty); usleep (100000); } for (i = 0; i < LOOP; i++) { pthread_mutex_lock (fifo->mut); while (fifo->full) { printf ("producer: queue FULL.\n"); pthread_cond_wait (fifo->notFull, fifo->mut); } queueAdd (fifo, i); pthread_mutex_unlock (fifo->mut); pthread_cond_signal (fifo->notEmpty); usleep (200000); } return (NULL); }
void rbtreeToDot(RBTree tree, const char* racine, const char* dossier) { assert(!rbtreeEmpty(tree)); static int numerofichier=0; char final[30]; sprintf(final,"%s/%s%d.dot",dossier,racine,numerofichier++); FILE*fd = fopen(final,"wt"); fprintf(fd,"digraph G { \n"); Node node; QUEUE queue; queueCreate(&queue); queueAdd(queue,rbfirst(tree)); do { node = queueRemove(queue); if(node->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->key)); if(node->left != tree->nil) { fprintf(fd,"\t%d -> %d;\n",keyPut(node->key),keyPut(node->left->key)); if(node->left->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->left->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->left->key)); } if(node->right != tree->nil) { fprintf(fd,"\t%d -> %d;\n",keyPut(node->key),keyPut(node->right->key)); if(node->right->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->right->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->right->key)); } if(node->left != tree->nil) queueAdd(queue,node->left); if(node->right != tree->nil) queueAdd(queue,node->right); } while(!queueEmpty(queue)); fprintf(fd,"}\n"); fclose(fd); }
/* The same as queueAdd but thread safe */ void qSafeAdd(queue *q,order arg) { pthread_mutex_lock (q->mut); while (q->full) { pthread_cond_wait (q->notFull, q->mut); } queueAdd(q, arg); pthread_cond_broadcast(q->notEmpty); pthread_mutex_unlock(q->mut); }
void setTaskEnabled(const int taskId, bool enabled) { if (taskId == TASK_SELF || taskId < (int)taskCount) { cfTask_t *task = taskId == TASK_SELF ? currentTask : &cfTasks[taskId]; if (enabled && task->taskFunc) { queueAdd(task); } else { queueRemove(task); } } }
void setTaskEnabled(cfTaskId_e taskId, bool enabled) { if (taskId == TASK_SELF || taskId < TASK_COUNT) { cfTask_t *task = taskId == TASK_SELF ? currentTask : &cfTasks[taskId]; if (enabled && task->taskFunc) { queueAdd(task); } else { queueRemove(task); } } }
void farmer_thread(THREAD_DATA td) { dbgprintf(KYEL "farmer: Gonna get the the mutex for %d\n" KYEL, td.fd); pthread_mutex_lock(fifo->mut); dbgprintf(KYEL "farmer: Got the the mutex %d\n" KYEL, td.fd); while (fifo->full) { printf(KRED "producer: queue FULL.\n" KNRM); pthread_cond_wait(fifo->notFull, fifo->mut); } queueAdd(fifo, td); pthread_mutex_unlock(fifo->mut); pthread_cond_signal(fifo->notEmpty); }
void rbtreeMapDebug(RBTree tree) { assert(!rbtreeEmpty(tree)); Node node; QUEUE queue; queueCreate(&queue); queueAdd(queue,rbfirst(tree)); printf("\033[01;35m==== Début de l'arbre ====\n\033[0m"); do { node = queueRemove(queue); if(rbExists(node->left) || rbExists(node->right) || rbfirst(tree)==node) { if(node->father != tree->root) printf("(pere: %d)",keyPut(node->father->key)); if(node->color==red) printf("\033[01;31m"); if(rbfirst(tree)==node) printf("\033[01;32m"); printf("Noeud %d\033[0m",keyPut(node->key)); if(rbExists(node->left)) { if(node->left->color==red) printf("\033[01;31m"); printf(", "); if(node->left->father != tree->root) printf("(pere: %d) ",keyPut(node->left->father->key)); printf("%d fils gauche\033[0m",keyPut(node->left->key)); } if(rbExists(node->right)) { printf(", "); if(node->right->color==red) printf("\033[01;31m"); if(node->right->father != tree->root) printf("(pere: %d) ",keyPut(node->right->father->key)); printf("%d fils droit\033[0m",keyPut(node->right->key)); } printf("\n"); } if(node->left != tree->nil) queueAdd(queue,node->left); if(node->right != tree->nil) queueAdd(queue,node->right); } while(!queueEmpty(queue)); printf("\n"); }
int bfs(int source, int goal) { int pathFound = FALSE; int destination, element, vertex, i, tmp; for(vertex = 0; vertex < graph.nNodes; vertex++) { parent[vertex] = -1; visited[vertex] = FALSE; } queueAdd(source); parent[source] = -1; visited[source] = TRUE; while (!queueIsEmpty()) { element = queueGet(); destination = 0; while (destination < graph.nNodes) { tmp=-1; for(i=0; i<graph.nNeighbors[element]; i++) { if(graph.neighbors[element][i] == destination) { tmp = i; break; } } if (tmp != -1 && graph.nodesInSearch[element][tmp] > 0 && !visited[destination]) { parent[destination] = element; queueAdd(destination); visited[destination] = TRUE; } destination++; } } if (visited[goal]) { pathFound = TRUE; } return pathFound; }
void *worker_thread(void *arg) { THREAD_DATA td; struct timespec tim, tim2; dbgprintf("Creating consumer\n"); for (;;) { dbgprintf(KBLU "Worker: Gonna get the the mutex\n" KNRM); pthread_mutex_lock(fifo->mut); dbgprintf(KBLU "Worker: Got the mutex\n" KNRM); while (fifo->empty) { dbgprintf("Worker: task queue EMPTY.\n"); pthread_cond_wait(fifo->notEmpty, fifo->mut); } queueDel(fifo, &td); dbgprintf(KGRN "Worker: starting %d\n" KNRM, td.fd); pthread_mutex_unlock(fifo->mut); pthread_cond_signal(fifo->notFull); dbgprintf("td.fdDataList %d, td.fd %d, td.pMaster %d, td.request.headers0 %s\n", td.fdDataList, td.fd, td.pMaster, td.request.headers[0]); server(&td.request,&td.response); dbgprintf(KGRN "Worker: completed %d with response %d\n" KNRM, td.fd,td.response.status); //clear_connection_baggage(td.fdDataList, td.fd, td.pMaster); pthread_mutex_lock(cleaner_fifo->mut); while (cleaner_fifo->full) { printf(KCYN "Worker: clean queue FULL.\n" KNRM); pthread_cond_wait(cleaner_fifo->notFull, cleaner_fifo->mut); } queueAdd(cleaner_fifo, td); pthread_mutex_unlock(cleaner_fifo->mut); pthread_cond_signal(cleaner_fifo->notEmpty); //notify_parent(); /* tim.tv_sec = 0; tim.tv_nsec = 1000; nanosleep(&tim , &tim2); */ } }
int main(int argc, char *argv[]) { int port = 0; if (argc !=2) { printf("Usage: ./rs <port number>\n"); return 1; } port = atoi(argv[1]); if (port<2000 || port>65535) { printf("port must be an integer between 2000 and 65535\n"); printf("invalid value %i", port); return 1; } queue *fifo; pthread_t srv, app; fifo = queueInit(); if (fifo==NULL) { printf("queueInit failed\n"); exit (1); } queueAdd(fifo, port); pthread_create(&srv, NULL, TCPServer, fifo); pthread_create(&app, NULL, ThreeDeeApp, fifo); pthread_join(srv, NULL); pthread_join(app, NULL); queueDelete(fifo); return 0; }
/* * The producer thread that feels the order buffer with orders. * They are left there until a consumer processes them. */ void *Prod (void *arg) { queue *q = (queue *) arg; while (1) { fputs("\033[A\033[2K",stdout); rewind(stdout); ftruncate(1,0); printf("**** %05d **** %05d **** %05d **** %05d **** %05d **** %05d **** %05d **** %05d **** %05d **** %05d ****\n",q->size,msq->size,mbq->size,lsq->size,lbq->size,ssq->size,sbq->size,tsq->size,tbq->size,currentPriceX10); fflush(stdout); pthread_mutex_lock (q->mut); while (q->full) { pthread_cond_wait (q->notFull, q->mut); } queueAdd (q, makeOrder()); pthread_cond_broadcast (q->notEmpty); pthread_mutex_unlock (q->mut); } return NULL; }
void *producer (void *parg) { queue *fifo; int item_produced; pcdata *mydata; int my_tid; int *total_produced; // int producerLoopCondition; mydata = (pcdata *) parg; fifo = mydata->q; total_produced = mydata->count; my_tid = mydata->tid; /* * Continue producing until the total produced reaches the * configured maximum */ while (1) { /* * Do work to produce an item. Tthe get a slot in the queue for * it. Finally, at the end of the loop, outside the critical * section, announce that we produced it. */ do_work(PRODUCER_CPU, PRODUCER_BLOCK); /* * If the queue is full, we have no place to put anything we * produce, so wait until it is not full. */ pthread_mutex_lock(fifo->mutex); while (fifo->full && *total_produced != WORK_MAX) { printf ("prod %d: FULL.\n", my_tid); pthread_cond_wait(fifo->notFull,fifo->mutex); // producerLoopCondition=fifo->full && *total_produced != WORK_MAX; } /* * Check to see if the total produced by all producers has reached * the configured maximum, if so, we can quit. */ if (*total_produced >= WORK_MAX) { pthread_mutex_unlock(fifo->mutex); break; } /* * OK, so we produce an item. Increment the counter of total * widgets produced, and add the new widget ID, its number, to the * queue. */ item_produced = (*total_produced)++; queueAdd (fifo, item_produced); pthread_cond_broadcast(fifo->notEmpty); pthread_mutex_unlock(fifo->mutex); /* * Announce the production outside the critical section */ printf("prod %d: %d.\n", my_tid, item_produced); } printf("prod %d: exited\n", my_tid); return (NULL); }
void schedulerInit(void) { queueClear(); queueAdd(&cfTasks[TASK_SYSTEM]); }
/* Adds the supplied order to the tail of the queue and then sorts it */ void qSortAdd (queue *q,order ord){ queueAdd(q,ord); queueSort(q); }
int main(void) { queueHead* queue; int i1 = 1; int i2 = 2; int i3 = 3; int i4 = 4; /* * Test 1 */ /* Create queue */ assert(NULL != (queue = queueCreate())); /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ /* Check the queue */ assert(1 == (*(int*)queueFirst(queue))); /* Check 1 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueFirst(queue))); /* Check 2 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(3 == (*(int*)queueFirst(queue))); /* Check 3 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueFirst(queue))); /* Check 4 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ assert(NULL == (queueFirst(queue))); /* Check empty */ assert(NULL == (queueRemove(queue))); /* Queue is empty */ /* Clear the queue */ queueFree(queue); printf("Queue: Test1 success!\n"); /* * Test 2 */ /* Create queue */ assert(NULL != (queue = queueCreate())); /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (*(int*)queueFirst(queue))); /* Check 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ /* Check the queue */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueFirst(queue))); /* Check 2 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ assert(NULL == (queueFirst(queue))); /* Check empty */ assert(NULL == (queueRemove(queue))); /* Queue is empty */ /* Clear the queue */ queueFree(queue); printf("Queue: Test2 success!\n"); return 0; }