void Semaphore::wait () throw (thrcancel) { threadle self(lisle::self); self->testcancel(); // Never reached if pending cancellation prioqueue waitqueue(&sem.waiting); { Acquirer device(sem.guard); if (sem.value > 0) { sem.value--; return; // no wait since semaphore was > 0 } else waitqueue.push(self); } try { self->waitrestartcancel(sem.waiting, sem.guard); // Never reached if canceled // If we were restarted (signaled/broadcasted) then we were poped from the waiting queue. // No need to remove self from the waiting queue here. } catch (thrcancel&) { waitqueue.remove(self); throw; } }
void Barrier::wait () throw () { thread* waiting = NULL; threadle self(lisle::self); bool wait = false; { Acquirer device(barrier.guard); barrier.waiting.count++; if (barrier.waiting.count == barrier.height) { barrier.waiting.count = 0; waiting = barrier.waiting.queue; barrier.waiting.queue = NULL; } else { prioqueue waitqueue(&barrier.waiting.queue); waitqueue.push(self); wait = true; } } if (wait) { Acquirer thread(self->guard); self->waitrestart(); } if (waiting != NULL) { prioqueue waitqueue(&waiting); while (!waitqueue.empty()) { waitqueue.top()->restart(); waitqueue.pop(); } } }
void Semaphore::post () throw (overflow) { thread* waiting = NULL; { Acquirer device(sem.guard); prioqueue waitqueue(&sem.waiting); if (!waitqueue.empty()) { waiting = waitqueue.top(); waitqueue.pop(); } else { threadle self(lisle::self); assert(sem.value < max(), overflow()); sem.value++; } } if (waiting != NULL) waiting->restart(); }
void time_tick(int signo) { int a; if (global_time_tick > 100) { fclose(fp); pid_t temp; while (run_q->total_count) { temp = dequeue(run_q); kill(temp, SIGKILL); } while (wait_q->total_count) { temp = dequeue(run_q); kill(temp, SIGKILL); } kill(getpid(), SIGKILL); return; } printf("run_q : "); fprintf(fp,"run_q : "); if (run_q->total_count>0) { node* temp = run_q->front; for (a = 1; a <= run_q->total_count; temp=temp->next, a++) { printf("<%d>", temp->pid); fprintf(fp,"<%d>", temp->pid); } } printf("\n"); fprintf(fp,"\n"); printf("wait_q : "); fprintf(fp,"wait_q: "); if (wait_q->total_count>0) { node* temp = wait_q->front; for (a = 1; a <= wait_q->total_count; temp = temp->next, a++) { printf("<%d>", temp->pid); fprintf(fp,"<%d>", temp->pid); } } printf("\n"); fprintf(fp,"\n"); waitqueue(); if (time_quantum>0) { time_quantum--; kill(run_q->front->pid, SIGUSR1); printf("send signal to %d, remain time quantum: %d\n", run_q->front->pid, time_quantum); fprintf(fp,"send signal to %d, remain time quantum : %d\n", run_q->front->pid, time_quantum); } else { schedule(); time_quantum = 10; printf("time_quantum is reseted as 10\n"); //fprintf("time_quantum is rested as 10\n"); } global_time_tick++; fprintf(fp,"Total Time : %d\n\n",global_time_tick/10); //fclose(fp); }