28.08.2015 Views

The Design and Implementation of the Anykernel and Rump Kernels

1F3KDce

1F3KDce

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

87<br />

void<br />

schedule_cpu()<br />

{<br />

struct lwp *lwp = curlwp;<br />

/* 1: fastpath */<br />

cpu = lwp->prevcpu;<br />

if (atomic_cas(cpu->prevlwp, lwp, CPU_BUSY) == lwp)<br />

return;<br />

/* 2: slowpath */<br />

mutex_enter(cpu->mutex);<br />

for (;;) {<br />

/* 3: signal we want <strong>the</strong> CPU */<br />

old = atomic_swap(cpu->prevlwp, CPU_WANTED);<br />

if (old != CPU_BUSY && old != CPU_WANTED) {<br />

membar();<br />

if (atomic_cas(cpu->prevlwp, CPU_WANTED, CPU_BUSY) == CPU_WANTED) {<br />

break;<br />

}<br />

}<br />

newcpu = migrate(lwp, cpu);<br />

if (newcpu != cpu) {<br />

continue;<br />

}<br />

}<br />

/* 4: wait for CPU */<br />

cpu->wanted++;<br />

cv_wait(cpu->cv, cpu->mutex);<br />

cpu->wanted--;<br />

}<br />

mutex_exit(cpu->mutex);<br />

return;<br />

Figure 3.6: CPU scheduling algorithm in pseudocode. See <strong>the</strong> text for a<br />

detailed description.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!