Annotation of researchv8dc/cmd/cfront/libC/task/task.h, revision 1.1

1.1     ! root        1: /*     HEADER FILE FOR THE TASK SYSTEM         */
        !             2: 
        !             3: printf(char* ...);
        !             4: void exit(int);
        !             5: 
        !             6: #define SIZE           750
        !             7: #define MODE           DEDICATED
        !             8: 
        !             9: class object;
        !            10: class sched;   /* : public object */
        !            11: class task;    /* : public sched  */
        !            12: class qhead;   /* : public object */
        !            13: class qtail;   /* : public object */
        !            14: class team;
        !            15: 
        !            16: extern sched* run_chain;   /* list of ready-to-run scheds (ordered by s_time) */
        !            17: extern task* task_chain;   /* list of tasks */
        !            18: 
        !            19: extern int task_error(int, object*);
        !            20: extern void print_error(int);
        !            21: 
        !            22: /* object.o_type */
        !            23: #define TIMER          1
        !            24: #define TASK           2
        !            25: #define QHEAD          4
        !            26: #define QTAIL          5
        !            27: 
        !            28: /* sched.s_state */
        !            29: #define IDLE           1
        !            30: #define RUNNING                2
        !            31: #define TERMINATED     4
        !            32: 
        !            33: /* type of stack */
        !            34: #define DEDICATED      1
        !            35: #define SHARED         2
        !            36: /* loc on stack */
        !            37: #define UNTOUCHED      052525
        !            38: 
        !            39: extern task* clock_task;
        !            40: extern task* thistask;
        !            41: extern long clock;
        !            42: void setclock(long);
        !            43: 
        !            44: /* error codes */
        !            45: #define E_OLINK                1
        !            46: #define E_ONEXT                2
        !            47: #define E_GETEMPTY     3
        !            48: #define E_PUTOBJ       4
        !            49: #define E_PUTFULL      5
        !            50: #define E_BACKOBJ      6
        !            51: #define E_BACKFULL     7
        !            52: #define E_SETCLOCK     8
        !            53: #define E_CLOCKIDLE    9
        !            54: #define E_RESTERM      10
        !            55: #define E_RESRUN       11
        !            56: #define E_NEGTIME      12
        !            57: #define E_RESOBJ       13
        !            58: #define E_HISTO                14
        !            59: #define E_STACK        15
        !            60: #define E_STORE                16
        !            61: #define E_TASKMODE     17
        !            62: #define E_TASKDEL      18
        !            63: #define E_TASKPRE      19
        !            64: #define E_TIMERDEL     20
        !            65: #define E_SCHTIME      21
        !            66: #define E_SCHOBJ       22
        !            67: #define E_QDEL         23
        !            68: #define E_RESULT       24
        !            69: #define E_WAIT         25
        !            70: #define MAXERR E_WAIT
        !            71: 
        !            72: typedef int (*PFIO)(int,object*);
        !            73: typedef void (*PFV)();
        !            74: 
        !            75: extern PFIO error_fct;
        !            76: extern PFV exit_fct;
        !            77: 
        !            78: /* print flags */
        !            79: #define CHAIN          1
        !            80: #define VERBOSE                2
        !            81: #define STACK          4
        !            82: 
        !            83: 
        !            84: /* DATA STRUCTURES */
        !            85: /*
        !            86:        object --> olink --> olink ...
        !            87:           |         |         |
        !            88:          ...        V         V
        !            89:           |        task      task
        !            90:           V
        !            91:        object --> ...
        !            92: */
        !            93: 
        !            94: class olink
        !            95: /*     the building block for chains of task pointers */
        !            96: {
        !            97: friend object;
        !            98:        olink*  l_next;
        !            99:        task*   l_task;
        !           100:                olink(task* t, olink* l) { l_task=t; l_next=l; };
        !           101: };
        !           102: 
        !           103: class object
        !           104: {
        !           105:        olink*  o_link;
        !           106: public:
        !           107:        object* o_next;
        !           108:        int     o_type;         /* TASK,TIMER,QHEAD/QTAIL */
        !           109: 
        !           110:                object(int t = 0)       { o_type=t; o_link=0; o_next=0; }
        !           111:                ~object();
        !           112: 
        !           113:        void    remember(task* t)       { o_link = new olink(t,o_link); }
        !           114:        void    forget(task*);  /* remove all occurrences of task from chain */
        !           115:        void    alert();        /* prepare IDLE tasks for scheduling */
        !           116: 
        !           117:        void    print(int);
        !           118: };
        !           119: 
        !           120: class sched : public object
        !           121: {
        !           122: friend timer;
        !           123: friend task;
        !           124: friend object;
        !           125:        void    schedule();     /* sched clock_task or front of run_chain */
        !           126:        void    insert(int,object*); /* sched for d time units, ?t_alert=obj */
        !           127:        void    remove();       /* remove from run_chain & make IDLE */
        !           128: 
        !           129:        long    s_time;         /* time to sched; result after cancel() */
        !           130:        int     s_state;        /* IDLE, RUNNING, TERMINATED */
        !           131: public:
        !           132:        void    print(int);
        !           133: 
        !           134:        long    rdtime()        { return s_time; };
        !           135:        int     rdstate()       { return s_state; };
        !           136: 
        !           137:        void    cancel(int);
        !           138:        int     result();
        !           139: };
        !           140: 
        !           141: struct timer : public sched
        !           142: {
        !           143:                timer(int);
        !           144:                ~timer();
        !           145:        void    reset(int);
        !           146:        void    print(int);
        !           147: };
        !           148: 
        !           149: extern _hwm;
        !           150: class task : public sched
        !           151: {
        !           152: friend sched;
        !           153:                task(char* =0, int =0, int =0);
        !           154:                ~task();
        !           155: 
        !           156:        void    save();
        !           157:        void    restore();      /* swap in new task */
        !           158:        int     curr_hwm();     /* "high water mark" */
        !           159:                                /*     (how high stack has risen) */
        !           160:        int*    t_framep;       /* WARNING: t_framep
        !           161:                                   is manipulated as an offset
        !           162:                                   by restore()
        !           163:                                */
        !           164:        void*   th; /* fudge return from swap */
        !           165:        int*    t_basep;
        !           166:        int     t_size;         /* holds hwm after cancel() */
        !           167:        int*    t_savearea;     /* for saving stack */
        !           168:        int     t_trap;
        !           169:        team*   t_team;         /* stack and info for sharing */
        !           170: 
        !           171:        int     t_mode;         /* DEDICATED/SHARED stack */
        !           172:        int     t_stacksize;
        !           173: 
        !           174:        object* t_alert;        /* object that inserted you */
        !           175: public:
        !           176:        task*   t_next;         /* insertion in "task_chain" */
        !           177:        char*   t_name;
        !           178: 
        !           179:        int     waitvec(object**);
        !           180:        int     waitlist(object* ...);
        !           181:        void    wait(object* ob)        { (void) waitlist(ob,0); };
        !           182: 
        !           183:        void    delay(int);
        !           184:        int     preempt();
        !           185:        void    sleep();
        !           186:        void    resultis(int);
        !           187:        void    cancel(int);
        !           188:        void    swap_stack(int*,int*,int*,int*,int*); /* set fram and restore */
        !           189: 
        !           190:        void    print(int);
        !           191: };
        !           192: 
        !           193: 
        !           194: /* QUEUE MANIPULATION (see queue.c) */
        !           195: /*
        !           196:        qhead <--> oqueue <--> qtail   (qhead, qtail independent)
        !           197:        oqueue ->> circular queue of objects
        !           198: */
        !           199: 
        !           200: /* qh_modes */
        !           201: #define EMODE          1
        !           202: #define WMODE          2
        !           203: #define ZMODE          3
        !           204: 
        !           205: class oqueue
        !           206: {
        !           207: friend qhead;
        !           208: friend qtail;
        !           209:        int     q_max;
        !           210:        int     q_count;
        !           211:        object* q_ptr;
        !           212:        qhead*  q_head;
        !           213:        qtail*  q_tail;
        !           214: 
        !           215:                oqueue(int m)   { q_max=m; q_count=0; q_head=0; q_tail=0; };
        !           216:                ~oqueue()       { (q_count)?task_error(E_QDEL,0):0; };
        !           217:        void    print(int);
        !           218: };
        !           219: 
        !           220: class qhead : public object
        !           221: {
        !           222: friend qtail;
        !           223:                qhead(int = WMODE, int = 10000);
        !           224:                ~qhead();
        !           225: 
        !           226:        int     qh_mode;        /* EMODE,WMODE,ZMODE */
        !           227:        oqueue* qh_queue;
        !           228: public:
        !           229:        object* get();
        !           230:        int     putback(object*);
        !           231: 
        !           232:        int     rdcount()       { return qh_queue->q_count; }
        !           233:        int     rdmax()         { return qh_queue->q_max; }
        !           234:        int     rdmode()        { return qh_mode; }
        !           235:        qtail*  tail();
        !           236: 
        !           237:        qhead*  cut();
        !           238:        void    splice(qtail*);
        !           239: 
        !           240:        void    setmode(int m)  { qh_mode = m; };
        !           241:        void    setmax(int m)   { qh_queue->q_max = m; };
        !           242: 
        !           243:        void    print(int);
        !           244: };
        !           245: 
        !           246: class qtail : public object
        !           247: {
        !           248: friend qhead;
        !           249:                qtail(int = WMODE, int = 10000);
        !           250:                ~qtail();
        !           251: 
        !           252:        int     qt_mode;
        !           253:        oqueue* qt_queue;
        !           254: public:
        !           255:        int     put(object*);
        !           256: 
        !           257:        int     rdspace()       { return qt_queue->q_max - qt_queue->q_count; };
        !           258:        int     rdmax()         { return qt_queue->q_max; };
        !           259:        int     rdmode()        { return qt_mode; };
        !           260: 
        !           261:        qtail*  cut();
        !           262:        void    splice(qhead*);
        !           263: 
        !           264:        qhead*  head();
        !           265: 
        !           266:        void    setmode(int m)  { qt_mode = m; };
        !           267:        void    setmax(int m)   { qt_queue->q_max = m; };
        !           268: 
        !           269:        void    print(int);
        !           270: };
        !           271: 
        !           272: 
        !           273: struct histogram
        !           274: /*
        !           275:        "nbin" bins covering the range [l:r[ uniformly
        !           276:        nbin*binsize == r-l
        !           277: */
        !           278: {
        !           279:        int     l, r;
        !           280:        int     binsize;
        !           281:        int     nbin;
        !           282:        int*    h;
        !           283:        long    sum;
        !           284:        long    sqsum;
        !           285:                histogram(int=16, int=0, int=16);
        !           286: 
        !           287:        void    add(int);
        !           288:        void    print();
        !           289: };
        !           290: 
        !           291: /*     the result of randint() is always >= 0  */
        !           292: 
        !           293: #define DRAW (randx = randx*1103515245 + 12345)
        !           294: #define ABS(x) (x&0x7fffffff)
        !           295: 
        !           296: #ifdef pdp11
        !           297: #define MASK(x)        ((x>>16)&077777)
        !           298: #define MAX 32768.0
        !           299: #endif
        !           300: 
        !           301: #ifdef vax
        !           302: #define MASK(x) ABS(x)
        !           303: #define MAX 2147483648.0
        !           304: #endif
        !           305: 
        !           306: class randint
        !           307: /*     uniform distribution in the interval [0,MAX] */
        !           308: {
        !           309:        long    randx;
        !           310: public:
        !           311:                randint(long s = 0)     { randx=s; }
        !           312:        void    seed(long s)    { randx=s; }
        !           313:        int     draw()          { return MASK(DRAW); }
        !           314:        float   fdraw()         { return ABS(DRAW)/MAX; };
        !           315: };
        !           316: 
        !           317: class urand : public randint
        !           318: /*     uniform distribution in the interval [low,high] */
        !           319: {
        !           320: public:
        !           321:        int     low, high;
        !           322:                urand(int l, int h)     { low=l; high=h; }
        !           323:        int     draw() { return int(low + (high-low) * (0+randint::draw()/MAX)); }
        !           324: };
        !           325: 
        !           326: extern double log(double);
        !           327: 
        !           328: class erand : public randint
        !           329: /*     exponential distribution random number generator */
        !           330: {
        !           331: public:
        !           332:        int     mean;
        !           333:                erand(int m) { mean=m; };
        !           334:        int     draw() { return (int)(-mean * log( (double)(MAX-randint::draw())
        !           335:                                                / MAX) + .5); };
        !           336: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.