void llPairDelete(queue *sl, queue *bl){ int vol1 = sl->item[sl->head].vol, vol2 = bl->item[bl->head].vol; int pvol = 0; int id1 = sl->item[sl->head].id, id2 = bl->item[bl->head].id; order ord; if (vol1 < vol2) { currentPriceX10 = ( sl->item[sl->head].price1 + bl->item[bl->head].price1)/2; vol2 = vol2 - vol1; pvol = vol1; queueDel(sl,&ord); bl->item[bl->head].vol = vol2; pthread_cond_broadcast(sl->notFull); } else if (vol1 > vol2) { currentPriceX10 = ( sl->item[sl->head].price1 + bl->item[bl->head].price1)/2; vol1 = vol1 - vol2; pvol = vol2; queueDel(bl,&ord); sl->item[sl->head].vol = vol1; pthread_cond_broadcast(bl->notFull); } else { currentPriceX10 = ( sl->item[sl->head].price1 + bl->item[bl->head].price1)/2; queueDel(sl,&ord); queueDel(bl,&ord); pvol = vol1; pthread_cond_broadcast(sl->notFull); pthread_cond_broadcast(bl->notFull); } fprintf(log_file,"%08ld %08ld %5.1f %05d %08d %08d\n", ord.timestamp, getTimestamp(), (float)currentPriceX10/10, pvol, id1, id2); }
void *consumer (void *q) { queue *fifo; int i, d; fifo = (queue *)q; for (i = 0; i < LOOP; i++) { pthread_mutex_lock (fifo->mut); while (fifo->empty) { printf ("consumer: queue EMPTY.\n"); pthread_cond_wait (fifo->notEmpty, fifo->mut); } queueDel (fifo, &d); pthread_mutex_unlock (fifo->mut); pthread_cond_signal (fifo->notFull); printf ("consumer: recieved %d.\n", d); usleep(200000); } for (i = 0; i < LOOP; i++) { pthread_mutex_lock (fifo->mut); while (fifo->empty) { printf ("consumer: queue EMPTY.\n"); pthread_cond_wait (fifo->notEmpty, fifo->mut); } queueDel (fifo, &d); pthread_mutex_unlock (fifo->mut); pthread_cond_signal (fifo->notFull); printf ("consumer: recieved %d.\n", d); usleep (50000); } return (NULL); }
void *ThreeDeeApp (void *param) { queue *fifo; int msg, buff = 0; fifo = (queue *)param; float Yrotation = 0; assets = new Obj3d(true); EGLinit(eglDisplay, eglSurface); if (1==preRender()) { EGLdeinit(eglDisplay); exit(1); } assets->start(g_hTXShaderProgram, "resources/models/tumbler/BatMobil.obj", *assets); if(!assets->getScene()) { printf("scene could not be loaded\n"); exit(1); } printf("scene loaded\n"); for (;;) { if(!fifo->empty) { pthread_mutex_lock(fifo->mut); queueDel(fifo, &msg); pthread_mutex_unlock(fifo->mut); pthread_cond_signal(fifo->notFull); //printf("3D app: received %i\n", msg); } if (msg != buff) { Yrotation = msg - buff; buff = msg; Render(assets, 0, Yrotation*3, 0, 0); } else { Render(assets, 0, 0, 0, 0); } Yrotation = 0; //msg=0; eglSwapBuffers(eglDisplay, eglSurface); } RenderCleanup(assets); // cleanup DestroyShaders(); EGLdeinit(eglDisplay); return (NULL); }
/* The same as queueDel but thread safe */ void qSafeDelete(queue *q,order *arg) { pthread_mutex_lock(q->mut); while (q->empty){ pthread_cond_wait(q->notEmpty, q->mut); } queueDel(q, arg); pthread_cond_broadcast(q->notFull); pthread_mutex_unlock(q->mut); }
void *consumer( void *args ) { queue *fifo; int d; fifo = static_cast<queue*>(args); for( int i = 0; i < LOOP; ++i ) { pthread_mutex_lock(fifo->mut); while( fifo->empty ) { RPMS_INFO(LOGNAME, "consumer: queue EMPTY"); pthread_cond_wait(fifo->notEmpty, fifo->mut); } queueDel(fifo, &d); pthread_mutex_unlock(fifo->mut); pthread_cond_signal(fifo->notFull); RPMS_INFO(LOGNAME, "consumer: RECEIVED "); usleep(200000); } for( int i = 0; i < LOOP; ++i ) { pthread_mutex_lock(fifo->mut); while( fifo->empty ) { RPMS_INFO(LOGNAME, "consumer: queue EMPTY"); pthread_cond_wait(fifo->notEmpty, fifo->mut); } queueDel(fifo, &d); pthread_mutex_unlock(fifo->mut); pthread_cond_signal(fifo->notFull); RPMS_INFO(LOGNAME, "consumer: RECEIVED "); usleep(500000); } return 0; }
void cleaner_thread(void) { THREAD_DATA td; dbgprintf(KMAG "cleaner: Gonna get the the mutex\n" KNRM); pthread_mutex_lock(cleaner_fifo->mut); dbgprintf(KMAG "cleaner: Got the mutex\n" KNRM); if (cleaner_fifo->empty) { dbgprintf(KMAG "cleaner: queue EMPTY.\n" KNRM); pthread_mutex_unlock(cleaner_fifo->mut); } else { while (!cleaner_fifo->empty) { queueDel(cleaner_fifo, &td); dbgprintf(KMAG "cleaner: cleaning %d\n" KNRM, td.fd); clear_connection_baggage(td.fdDataList, td.fd, td.pMaster); } dbgprintf(KMAG "cleaner: NOW queue EMPTY.\n" KNRM); pthread_mutex_unlock(cleaner_fifo->mut); pthread_cond_signal(cleaner_fifo->notFull); } }
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); */ } }
/* * Removes orders from the order buffer. And process them according to their type by * adding them to the correct queue for transaction. */ void *Cons (void *arg) { queue *q = (queue *) arg; order ord; while (1) { pthread_mutex_lock (q->mut); while (q->empty) { pthread_cond_wait (q->notEmpty, q->mut); } queueDel (q, &ord); pthread_cond_broadcast(q->notFull); pthread_mutex_unlock (q->mut); /* * Here we save the type of the order processed at thje archive so that later if we have to, * we can cancel them. Afterwards we */ pthread_mutex_lock(saves.mut); if (ord.type == 'M') { if(ord.action == 'S'){ saves.archive[ord.id] = 'M'; pthread_mutex_unlock(saves.mut); qSafeAdd(msq,ord); } else { saves.archive[ord.id] = 'N'; pthread_mutex_unlock(saves.mut); qSafeAdd(mbq,ord); } } else if (ord.type == 'L') { if(ord.action == 'S'){ saves.archive[ord.id] = 'L'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(lsq,ord); }else{ saves.archive[ord.id] = 'K'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(lbq,ord); } } else if (ord.type == 'S') { if(ord.action == 'S'){ saves.archive[ord.id] = 'S'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(ssq,ord); }else{ saves.archive[ord.id] = 'P'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(sbq,ord); } } else if (ord.type == 'T') { if(ord.action == 'S'){ saves.archive[ord.id] = 'T'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(tsq,ord); }else{ saves.archive[ord.id] = 'W'; pthread_mutex_unlock(saves.mut); qSafeSortAdd(tbq,ord); } } else { saves.archive[ord.id] = 'C'; pthread_mutex_unlock(saves.mut); qSafeAdd(cq,ord); } } return NULL; }