int priorityEnqueue(Queue* queue, void* element,compare comp){ int temp = (queue->rear)-1; void* elementToCompare; int comparisonResult; if(queueIsFull(queue)) return 0; if(queue->rear >= queue->length) queue->rear = 0; if(queue->front == -1 && queue->rear == -1){ queue->front++; queue->rear++; } else{ queue->rear++; for (temp = queue->rear-1; temp >= queue->front; temp--) { elementToCompare = queue->base + temp * queue->elementSize; comparisonResult = comp(element, elementToCompare); if (comparisonResult >= 0) { break; } memmove(elementToCompare + queue->elementSize, elementToCompare, queue->elementSize); } memmove(queue->base+((temp+1)*queue->elementSize), element, queue->elementSize); return 1; } memmove(queue->base+(queue->rear*queue->elementSize), element, queue->elementSize); return 1; }
void CommunicationLayer::unblockUpperQueues() { t_uint i = 0; while(!queueIsFull() && i < m_upperLayers.size()) { m_upperLayers[i]->unblockQueue(); ++i; } }
int enqueue(Queue* queue, void* element){ void *copyTo; if(queueIsFull(queue)) return 0; copyTo = queue->base+(queue->rear++)*queue->elementSize; memmove(copyTo , element, queue->elementSize); return 1; }
int enqueue(queue *q, btree_node *e) { int r = 0; if (!queueIsFull(q)) { q->last = (q->last + 1) % q->size; q->els[q->last] = e; q->lastAdded = 1; r = 1; } return r; }
bool queuePut(Queue queue, void* data) { if (!queueIsFull(queue)) { listPush(queue->queue, data); queue->end = (queue->end + 1) % queue->max; if (queue->start == queue->end) queue->full = true; } }
int queueInsert(Queue *queue, int item) { if(queueIsFull(queue)) return -1; queue->items[queue->back] = item; if(queue->back + 1 < MAX_QUEUE_MEMBERS) queue->back++; else queue->back = 0; return queue->count++; }
bool CommunicationLayer::sendToQueue(PacketPtr packet, t_uint lowerLayerIdx) { bool wasSuccessful = false; pair<PacketPtr,t_uint> queueElement = make_pair(packet, lowerLayerIdx); if(!queueIsFull()) { wasSuccessful = true; m_packetQueue.push_back(queueElement); sendFromQueue(); } else { // The packet is dropped. } if(queueIsFull()) blockUpperQueues(); return wasSuccessful; }
void pqPush(PayQueue pq, Payload pld) { Payload pay; if (queueIsFull(pq)) { pay = (Payload)queuePop(pq); payDelete(pay); } queueAppend(pq, pld); }
int enqueue(queueT *queue, queueElementT element) { if(queueIsFull(queue)) { printf("queue is full\n"); return -1; } queue->contents[queue->rear % queue->size] = element; queue->rear = (queue->rear + 1) % queue->size; return 0; }
void CommunicationLayer::sendFromQueue() { while(!m_lowerLayerRecvEventPending && !m_queueIsBlocked && !m_packetQueue.empty()) { pair<PacketPtr,t_uint> queueElement = m_packetQueue.front(); m_packetQueue.pop_front(); sendToLayer(CommunicationLayer::Directions_Lower, queueElement.first, queueElement.second); } if(!queueIsFull()) unblockUpperQueues(); }
int ThreadPool::threadReaper () { TRACE_CALL; VxCritSec cs (m_threadCountLock); int unusedThreads = threadCount() - queueSize (); if ((threadCount () - unusedThreads) < minThreads ()) unusedThreads -= minThreads (); if (queueIsFull () || unusedThreads <= 0) return 0; while (unusedThreads-- > 0) threadRemove (); return 0; }
/** * Add a new task to the pool. * The new task will be executed as soon as * there is at least one free thread. * * NOTE: This method MAY be blocked. */ void DWThreadPool::run(const Task &task) { if (_threads.empty()) // if there is no threads in the pool, // we run it on the main thread. task(); else { // add the new task to the queue // and notify one free thread std::unique_lock<std::mutex> ulock(_queue_mutex); while (queueIsFull()) { // one may be blocked here _not_full.wait(ulock); } _task_queue.push_back(task); _not_empty.notify_one(); } }
int main(void){ int i=0; queueCreate (12); //create queue for(;i<13;i++){ if(!queueIsFull ()) //check whether queue is full or not if not data enter otherwise discard enqueue(15+i); //enter data to queue } //queueDestroy (); //destroy the queue for(i=0;i<12;i++) printf("%d..asasadasj\n",StArray[i]); //print the data in the queue from the begining to show it has implemented correctly //printf("%d..\n",dequeue()); //dequeue the que and print //printf("%d..\n",queuePeek ());//peek the que and print return 0; }
void enqueue(struct node *root, struct Queue *queue) { if (queueIsFull(queue)) { SIZE *= 2; struct Queue *newQueue = createQueue(SIZE); newQueue->front = queue->front; newQueue->rear = queue->rear; for (int i = 0; i <= newQueue->size; ++i) { newQueue->array[i] = queue->array[i]; } printf("Created new queue of size %d.\n", SIZE); //printf("newQueue-> front = %d, queue->front = %d\n", newQueue->front, queue->front); //printf("newQueue-> rear = %d, queue->rear = %d\n", newQueue->rear, queue->rear); return; } //printf("Adding item %d to existing queue.\n", root->data); queue->array[++queue->rear] = root; if (queueIsEmpty(queue)) ++queue->front; }
void queueAddLast(Queue_t * queue, void * elt) { ASSERT(!queueIsFull(queue)); queue->head[queue->tail++] = elt; }
/* * This function assumes the header has been tested to be valid but is still encrypted*/ void* decryptQueue(void* parameters) { pdebug("decryptQueue()\n"); bool decrypted = false; bool header = true; chunk* decrypt_chunk = NULL; cryptParams* params = parameters; uint64_t* chain_even = NULL; uint64_t* chain_odd = NULL; uint64_t decrypt_count = 0; uint64_t crypto_progress = 0; chain_even = calloc((params->tf_key->stateSize/8), sizeof(uint64_t)); chain_odd = calloc((params->tf_key->stateSize/8), sizeof(uint64_t)); if(chain_even == NULL || chain_odd == NULL) //check that calloc succeeded { pdebug("$$$ Error allocating memory for chain buffer $$$\n"); *(params->error) = MEMORY_ALLOCATION_FAIL; return NULL; } while(*(params->running) && *(params->error) == 0) { bool even = decrypt_count % 2 == 0 ? true : false; //in keep track if this is an even or odd loop //get the next chunk if(decrypt_chunk == NULL) { pthread_mutex_lock(params->in_mutex); if(front(params->in) != NULL) { decrypt_chunk = front(params->in); if(decrypt_chunk != NULL) { deque(params->in); } decrypted = false; pdebug("$$$ Dequing chunk of size %lu $$$\n", decrypt_chunk->data_size); } pthread_mutex_unlock(params->in_mutex); } //check for termination conditions if(decrypt_chunk != NULL && decrypt_chunk->action == DONE) { pdebug("$$$ decryptQueue() loop terminating $$$\n"); destroyChunk(decrypt_chunk); break; } /* * Get the chain block from the encrypted header then strip it out */ if(header && decrypt_chunk != NULL) { getChainInBuffer(decrypt_chunk->data, chain_even, 2, params->tf_key->stateSize); destroyChunk(decrypt_chunk); decrypt_chunk = NULL; header = false; pdebug("$$$ Stripping out header $$$\n"); } //in order to save the last block of cipher text and not overrite the previous one before it is needed we need to store chains for even and odd blocks separately else if(decrypt_chunk != NULL && !decrypted) //decrypt the chunk { const uint64_t num_blocks = getNumBlocks(decrypt_chunk->data_size, (uint32_t)params->tf_key->stateSize); if(even) { getChainInBuffer(decrypt_chunk->data, chain_odd, num_blocks, params->tf_key->stateSize); decryptInPlace(params->tf_key, chain_even, decrypt_chunk->data, num_blocks); } else { getChainInBuffer(decrypt_chunk->data, chain_even, num_blocks, params->tf_key->stateSize); decryptInPlace(params->tf_key, chain_odd, decrypt_chunk->data, num_blocks); } decrypted = true; crypto_progress += decrypt_chunk->data_size; pdebug("$$$ Decrypting chunk of size %lu $$$\n", decrypt_chunk->data_size); ++decrypt_count; } if(decrypt_chunk != NULL && decrypted) //attempt to queue the chunk { pthread_mutex_lock(params->out_mutex); if(!queueIsFull(params->out)) { if(enque(decrypt_chunk, params->out)) { pdebug("$$$ Queueing chunk of size %lu $$$\n", decrypt_chunk->data_size); decrypt_chunk = NULL; decrypted = false; } } pthread_mutex_unlock(params->out_mutex); } //end queue operation if(crypto_progress > 0) //update the progress bar { if(pthread_mutex_trylock(params->progress->progress_mutex) == 0) { params->progress->progress += crypto_progress; crypto_progress = 0; pthread_mutex_unlock(params->progress->progress_mutex); } } } //end while loop //queue Done flag while(queueIsFull(params->out)) { nanosleep(&wait_interval, NULL); } pthread_mutex_lock(params->out_mutex); if(!queueDone(params->out)) { pdebug("$$$ Error queueing done $$$\n"); *(params->error) = QUEUE_OPERATION_FAIL; return NULL; } pthread_mutex_unlock(params->out_mutex); pdebug("$$$ Done queued $$$ \n"); pdebug("$$$ Decryption complete $$$\n"); if(chain_even != NULL) { free(chain_even); } //free allocated resources if(chain_odd != NULL) { free(chain_odd); } //free allocated resources return NULL; } //end decryptQueue
void* encryptQueue(void* parameters) { pdebug("encryptQueue()\n"); bool first_chunk = true; bool encrypted = false; //a flag to protect a chunk from getting encrpted multiple times cryptParams* params = parameters; chunk* encrypt_chunk = NULL; uint64_t* chain = NULL; chain = calloc(params->tf_key->stateSize/64, sizeof(uint64_t)); if (chain == NULL) //check that calloc succeeded { *(params->error) = MEMORY_ALLOCATION_FAIL; return NULL; } uint64_t crypto_progress = 0; while (*(params->running) && *(params->error) == 0) { if (encrypt_chunk == NULL) { pthread_mutex_lock(params->in_mutex); if (front(params->in) != NULL) { encrypt_chunk = front(params->in); if (encrypt_chunk != NULL) { encrypted = false; //set the encrypted flag deque(params->in); } } pthread_mutex_unlock(params->in_mutex); } if (encrypt_chunk != NULL && encrypt_chunk->action == DONE) { pdebug("$$$ encryptQueue() terminating loop $$$\n"); destroyChunk(encrypt_chunk); break; } if (first_chunk && encrypt_chunk != NULL) //assume the first chunk is the header { pdebug("$$$ Encrypting header of size %lu $$$\n", encrypt_chunk->data_size); if (!encryptHeader(params->tf_key, encrypt_chunk->data)) { //if we failed to encrypt the header pdebug("$$$ Failed to encrypt header $$$\n"); destroyChunk(encrypt_chunk); *(params->error) = CIPHER_OPERATION_FAIL; //set the error flag break; //break out } //save the chain of cipher text in the next chunk getChainInBuffer(encrypt_chunk->data, chain, 2, params->tf_key->stateSize); crypto_progress += encrypt_chunk->data_size; first_chunk = false; } else if (!first_chunk && encrypt_chunk != NULL && !encrypted) { uint64_t num_blocks = getNumBlocks(encrypt_chunk->data_size, (uint32_t)params->tf_key->stateSize); encryptInPlace(params->tf_key, chain, encrypt_chunk->data, num_blocks); getChainInBuffer(encrypt_chunk->data, chain, num_blocks, params->tf_key->stateSize); crypto_progress += encrypt_chunk->data_size; encrypted = true; } if (encrypt_chunk != NULL && !queueIsFull(params->out)) //attempt to queue the last encrypted chunk { encrypt_chunk->action = GEN_MAC; //change the next queued action //on success clear the chunk ptr so the next operation can happen pthread_mutex_lock(params->out_mutex); if(enque(encrypt_chunk, params->out)) { pdebug("$$$ Queing encrypted chunk of size %lu $$$\n", encrypt_chunk->data_size); encrypt_chunk = NULL; } pthread_mutex_unlock(params->out_mutex); } //end queue operation if (crypto_progress > 0) //update the progress bar { if (pthread_mutex_trylock(params->progress->progress_mutex) == 0) { params->progress->progress += crypto_progress; crypto_progress = 0; pthread_mutex_unlock(params->progress->progress_mutex); } } } //end while loop //queue Done flag while (queueIsFull(params->out)) { nanosleep(&wait_interval, NULL); } pthread_mutex_lock(params->out_mutex); if (!queueDone(params->out)) { pdebug("Error queueing done\n"); *(params->error) = QUEUE_OPERATION_FAIL; if (chain != NULL) { free(chain); } return NULL; } pthread_mutex_unlock(params->out_mutex); pdebug("$$$ Done queued $$$ \n"); if (chain != NULL) { free(chain); } return NULL; } //end encryptQueue()
int pqIsFull(PayQueue queue) { return queueIsFull(queue); }