int main() { int i; int ids[BLOB_COUNT]; __dpmi_regs regs; clrscr(); printf("\nLWP: Example 3. Pre-emptive/Cooperative\n"); printf("This program spawns %d threads that each draw\n", BLOB_COUNT); printf("a dot on the screen that moves in a random direction.\n"); printf("Each thread yields after it draws a pixel, to smooth\n"); printf("out the dot motion\n"); printf("Press any key to continue..."); fflush(stdout); getch(); regs.x.ax = 0x13; srandom(time(0)); __dpmi_int(0x10, ®s); lwpInit(RTC128, 1); lwpEnterCriticalSection(); _farsetsel(_dos_ds); lwpLeaveCriticalSection(); for(i=0;(i<BLOB_COUNT) && !kbhit();i++) { ids[i] = lwpSpawn(blobProc, NULL, 4096, 1, TRUE); lwpYield(); /* let the blob move a little */ } while(!kbhit()) { if(plotCount == BLOB_COUNT) { plotCount = 0; waitVBL(); } plotCount++; lwpYield(); } /* key was hit, all other threads start dying here */ while(lwpThreadCount() > 0) { lwpYield(); /* wait for all the other threads to die */ } getch(); lwpEnterCriticalSection(); regs.x.ax = 0x3; __dpmi_int(0x10, ®s); lwpLeaveCriticalSection(); return(0); }
void lwp_rwlock_unlock(struct lwp_rwlock *rwlock) { struct lwpProc *p; int maxpri; lwpStatus(LwpCurrent, "unlocking rwlock %s", rwlock->name); if (CANT_HAPPEN(rwlock->count == 0)) return; if (rwlock->count < 0) rwlock->count = 0; else rwlock->count--; if (rwlock->count == 0 && rwlock->wq.head) { p = lwpGetFirst(&rwlock->wq); lwpStatus(p, "wake up next writer of rwlock %s", rwlock->name); maxpri = p->pri; lwpReady(p); } else if (rwlock->count >= 0 && rwlock->rq.head && !rwlock->wq.head) { maxpri = 0; while ((p = lwpGetFirst(&rwlock->rq))) { lwpStatus(p, "wake up next reader of rwlock %s", rwlock->name); maxpri = MAX(maxpri, p->pri); lwpReady(p); } } else return; if (LwpCurrent->pri < maxpri) { lwpStatus(LwpCurrent, "yielding to thread with higher priority"); lwpYield(); } }
void blobProc(void *p) { int x, y, xspeed,yspeed ; char col; lwpEnterCriticalSection(); x = (int) (319 * (float)((float) random() / (float) RAND_MAX )); y = (int) (199 * (float)((float) random() / (float) RAND_MAX )); col = (int) (255 * (float)((float) random() / (float) RAND_MAX )); xspeed = (int) (3 * (float)((float) random() / (float) RAND_MAX )); yspeed = (int) (3 * (float)((float) random() / (float) RAND_MAX )); _farsetsel(_dos_ds); lwpLeaveCriticalSection(); /* loop until keypress */ while(!kbhit()) { putpixel(x,y,0); plotCount++; x+=xspeed; if(x>=319) { xspeed=-xspeed; x = 319; } if(x<=0) { xspeed=-xspeed; x = 0; } y+=yspeed; if(y>=199) { yspeed=-yspeed; y=199; } if(y<=0) { yspeed=-yspeed; y=0; } putpixel(x,y,col); lwpYield(); } putpixel(x,y,0); /* erase pixel */ /* thread dies here */ }
void empth_yield(void) { ef_make_stale(); lwpYield(); }