Beispiel #1
0
void *MyThreadCreate (void (*start_funct)(void *), void *args) {

	Thread *thread = initializeThread(start_funct, args);
	insertIntoQueue(readyQueue, thread);
	
	insertIntoQueue(currentThread->children, thread);
	thread->parent = currentThread;

	return (void *)thread;
}		
static void test_remove_from_queue() {
    Queue *queue = (Queue *)malloc(sizeof(Queue));
    initializeQueue(queue);
    Thread *thread1 = malloc(sizeof(Thread));
    Thread *thread2 = malloc(sizeof(Thread));
    Thread *thread3 = malloc(sizeof(Thread));
    insertIntoQueue(queue, thread1);
    insertIntoQueue(queue, thread2);
    insertIntoQueue(queue, thread3);
    assert(sizeOfQueue(queue) == 3);
    removeFromQueue(queue, thread3);
    printQueue(queue);
    assert(sizeOfQueue(queue) == 2);
}
static void test_insert_and_deque_for_empty_queue() {
    Queue *queue = (Queue *)malloc(sizeof(Queue));
    initializeQueue(queue);
    Thread thread2;
    Thread *thread1 = malloc(sizeof(Thread));
    ucontext_t ctxt;
    getcontext(&ctxt);
    thread1->uctxt = ctxt;
    insertIntoQueue(queue, thread1);
    assert(queue->front == queue->rear);
    insertIntoQueue(queue, &thread2);
    assert(queue->front->next == queue->rear);
    dequeue(queue);
    dequeue(queue);
    assert(queue->front == NULL && queue->rear == NULL);
}
Beispiel #4
0
static CacheData *getCacheData(int device, int block) {
	CacheData *cacheData = findCacheData(device, block);
	if (cacheData==NULL) {
		CacheData *temp = startFreeEntry;	
		while (temp!=NULL && temp->nextFree!=startFreeEntry) {
			if (temp->count==0) {
				if (cacheData==NULL || BADNESS(temp)<BADNESS(cacheData)) {
					cacheData = temp;
					if (BADNESS(temp)==0) {
						break;
					}				
				}				
			}
			temp = temp->nextFree;
		}		
		cacheData->count = 1;
		cacheData->isDirty = false;
		cacheData->isUptoDate = false;
		removeFromQueue(cacheData);
		cacheData->device = device;
		cacheData->block = block;
		insertIntoQueue(cacheData);
	}
	return cacheData;	
}
Beispiel #5
0
void MyThreadYield(void) {

	Thread *current = currentThread;
	
	insertIntoQueue(readyQueue, currentThread);
	currentThread = getNextThread();

	swapcontext(&(current->uctxt), &(currentThread->uctxt));
}
Beispiel #6
0
void MySemaphoreSignal(MySemaphore semaphore) {
	Semaphore *sem = semaphore;
	
	(sem->value)++;
	if(sem->value <= 0) {
		Thread *thread = dequeue(sem->blockedQueue);
		insertIntoQueue(readyQueue, thread);
	}	
}	
Beispiel #7
0
void MyThreadJoinAll(void) {

	if(!isQueueEmpty(currentThread->children)) {
		Thread *this = currentThread;
		insertIntoQueue(blockedQueue, currentThread);
		currentThread = getNextThread();
		swapcontext(&(this->uctxt), &(currentThread->uctxt));
	}	
}	
Beispiel #8
0
void MySemaphoreWait(MySemaphore semaphore) {
	Semaphore *sem = semaphore;
	
	(sem->value)--;
	
	if(sem->value < 0) {
		insertIntoQueue(sem->blockedQueue, currentThread);
		Thread *this = currentThread;
		currentThread = getNextThread();
		swapcontext(&(this->uctxt), &(currentThread->uctxt));	
	}	
}	
Beispiel #9
0
void updateParentFieldForChildren(Thread *thread) {
	QueueNode *p = thread->children->front;
	while(p != NULL) {
		if(thread == initThread)
			p->thread->parent = NULL;
		else {	
			p->thread->parent = initThread;
			insertIntoQueue(initThread->children, p->thread);
		}	
		p = p->next;
	}		
}	
Beispiel #10
0
void removeParentFromBlockedQueue(Thread *thread) {
	Thread *parent = thread->parent;
	
	if(parent != NULL)
		removeFromQueue(parent->children, thread);

	if(!isQueueEmpty(blockedQueue) && isPresent(blockedQueue, parent)) {
		
		if(isQueueEmpty(parent->children) || (parent->waitingFor == thread)) {
			removeFromQueue(blockedQueue, parent);
			parent->waitingFor = NULL;
			insertIntoQueue(readyQueue, parent);
		}	
	}
}	
Beispiel #11
0
int MyThreadJoin(void *thread) {

	Thread *child = (Thread *)thread;
	
	if(child->parent != currentThread)
		return -1;
	
	if(isPresent(currentThread->children, child)) {
		currentThread->waitingFor = child;
		insertIntoQueue(blockedQueue, currentThread);
		currentThread = getNextThread();
		swapcontext(&(child->parent->uctxt), &(currentThread->uctxt));
	}
	return 0;		
}