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

1.1     ! root        1: /*     HEADER FILE FOR THE TASK SYSTEM         */
        !             2: 
        !             3: printf(const 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: friend timer;
        !           122: friend task;
        !           123: friend object;
        !           124:        void    schedule();     /* sched clock_task or front of run_chain */
        !           125:        void    insert(int,object*); /* sched for d time units, ?t_alert=obj */
        !           126:        void    remove();       /* remove from run_chain & make IDLE */
        !           127: 
        !           128:        long    s_time;         /* time to sched; result after cancel() */
        !           129:        int     s_state;        /* IDLE, RUNNING, TERMINATED */
        !           130: public:
        !           131:        void    print(int);
        !           132: 
        !           133:        long    rdtime()        { return s_time; };
        !           134:        int     rdstate()       { return s_state; };
        !           135: 
        !           136:        void    cancel(int);
        !           137:        int     result();
        !           138: };
        !           139: 
        !           140: struct timer : sched {
        !           141:                timer(int);
        !           142:                ~timer();
        !           143:        void    reset(int);
        !           144:        void    print(int);
        !           145: };
        !           146: 
        !           147: extern _hwm;
        !           148: class task : public sched {
        !           149: friend sched;
        !           150:        void    save();
        !           151:        void    restore();      /* swap in new task */
        !           152:        int     curr_hwm();     /* "high water mark" */
        !           153:                                /*     (how high stack has risen) */
        !           154:        int*    t_framep;       /* WARNING: t_framep
        !           155:                                   is manipulated as an offset
        !           156:                                   by restore()
        !           157:                                */
        !           158:        void*   th; /* fudge return from swap */
        !           159:        int*    t_basep;
        !           160:        int     t_size;         /* holds hwm after cancel() */
        !           161:        int*    t_savearea;     /* for saving stack */
        !           162:        int     t_trap;
        !           163:        team*   t_team;         /* stack and info for sharing */
        !           164: 
        !           165:        int     t_mode;         /* DEDICATED/SHARED stack */
        !           166:        int     t_stacksize;
        !           167: 
        !           168:        object* t_alert;        /* object that inserted you */
        !           169: public:
        !           170:                task(char* =0, int =0, int =0);
        !           171:                ~task();
        !           172: 
        !           173:        task*   t_next;         /* insertion in "task_chain" */
        !           174:        char*   t_name;
        !           175: 
        !           176:        int     waitvec(object**);
        !           177:        int     waitlist(object* ...);
        !           178:        void    wait(object* ob)        { (void) waitlist(ob,0); };
        !           179: 
        !           180:        void    delay(int);
        !           181:        int     preempt();
        !           182:        void    sleep();
        !           183:        void    resultis(int);
        !           184:        void    cancel(int);
        !           185:        void    swap_stack(int*,int*,int*,int*,int*); /* set fram and restore */
        !           186: 
        !           187:        void    print(int);
        !           188: };
        !           189: 
        !           190: 
        !           191: /* QUEUE MANIPULATION (see queue.c) */
        !           192: /*
        !           193:        qhead <--> oqueue <--> qtail   (qhead, qtail independent)
        !           194:        oqueue ->> circular queue of objects
        !           195: */
        !           196: 
        !           197: /* qh_modes */
        !           198: #define EMODE          1
        !           199: #define WMODE          2
        !           200: #define ZMODE          3
        !           201: 
        !           202: class oqueue
        !           203: {
        !           204: friend qhead;
        !           205: friend qtail;
        !           206:        int     q_max;
        !           207:        int     q_count;
        !           208:        object* q_ptr;
        !           209:        qhead*  q_head;
        !           210:        qtail*  q_tail;
        !           211: 
        !           212:                oqueue(int m)   { q_max=m; q_count=0; q_head=0; q_tail=0; };
        !           213:                ~oqueue()       { (q_count)?task_error(E_QDEL,0):0; };
        !           214:        void    print(int);
        !           215: };
        !           216: 
        !           217: class qhead : public object
        !           218: {
        !           219: friend qtail;
        !           220:        int     qh_mode;        /* EMODE,WMODE,ZMODE */
        !           221:        oqueue* qh_queue;
        !           222: public:
        !           223:                qhead(int = WMODE, int = 10000);
        !           224:                ~qhead();
        !           225: 
        !           226:        object* get();
        !           227:        int     putback(object*);
        !           228: 
        !           229:        int     rdcount()       { return qh_queue->q_count; }
        !           230:        int     rdmax()         { return qh_queue->q_max; }
        !           231:        int     rdmode()        { return qh_mode; }
        !           232:        qtail*  tail();
        !           233: 
        !           234:        qhead*  cut();
        !           235:        void    splice(qtail*);
        !           236: 
        !           237:        void    setmode(int m)  { qh_mode = m; };
        !           238:        void    setmax(int m)   { qh_queue->q_max = m; };
        !           239: 
        !           240:        void    print(int);
        !           241: };
        !           242: 
        !           243: class qtail : public object
        !           244: {
        !           245: friend qhead;
        !           246:        int     qt_mode;
        !           247:        oqueue* qt_queue;
        !           248: public:
        !           249:                qtail(int = WMODE, int = 10000);
        !           250:                ~qtail();
        !           251: 
        !           252:        int     put(object*);
        !           253: 
        !           254:        int     rdspace()       { return qt_queue->q_max - qt_queue->q_count; };
        !           255:        int     rdmax()         { return qt_queue->q_max; };
        !           256:        int     rdmode()        { return qt_mode; };
        !           257: 
        !           258:        qtail*  cut();
        !           259:        void    splice(qhead*);
        !           260: 
        !           261:        qhead*  head();
        !           262: 
        !           263:        void    setmode(int m)  { qt_mode = m; };
        !           264:        void    setmax(int m)   { qt_queue->q_max = m; };
        !           265: 
        !           266:        void    print(int);
        !           267: };
        !           268: 
        !           269: 
        !           270: struct histogram
        !           271: /*
        !           272:        "nbin" bins covering the range [l:r[ uniformly
        !           273:        nbin*binsize == r-l
        !           274: */
        !           275: {
        !           276:        int     l, r;
        !           277:        int     binsize;
        !           278:        int     nbin;
        !           279:        int*    h;
        !           280:        long    sum;
        !           281:        long    sqsum;
        !           282:                histogram(int=16, int=0, int=16);
        !           283: 
        !           284:        void    add(int);
        !           285:        void    print();
        !           286: };
        !           287: 
        !           288: /*     the result of randint() is always >= 0  */
        !           289: 
        !           290: #define DRAW (randx = randx*1103515245 + 12345)
        !           291: #define ABS(x) (x&0x7fffffff)
        !           292: 
        !           293: #ifdef pdp11
        !           294: #define MASK(x)        ((x>>16)&077777)
        !           295: #define MAX 32768.0
        !           296: #endif
        !           297: 
        !           298: #ifdef vax
        !           299: #define MASK(x) ABS(x)
        !           300: #define MAX 2147483648.0
        !           301: #endif
        !           302: 
        !           303: class randint
        !           304: /*     uniform distribution in the interval [0,MAX] */
        !           305: {
        !           306:        long    randx;
        !           307: public:
        !           308:                randint(long s = 0)     { randx=s; }
        !           309:        void    seed(long s)    { randx=s; }
        !           310:        int     draw()          { return MASK(DRAW); }
        !           311:        float   fdraw()         { return ABS(DRAW)/MAX; };
        !           312: };
        !           313: 
        !           314: class urand : public randint
        !           315: /*     uniform distribution in the interval [low,high] */
        !           316: {
        !           317: public:
        !           318:        int     low, high;
        !           319:                urand(int l, int h)     { low=l; high=h; }
        !           320:        int     draw() { return int(low + (high-low) * (0+randint::draw()/MAX)); }
        !           321: };
        !           322: 
        !           323: extern double log(double);
        !           324: 
        !           325: class erand : public randint
        !           326: /*     exponential distribution random number generator */
        !           327: {
        !           328: public:
        !           329:        int     mean;
        !           330:                erand(int m) { mean=m; };
        !           331:        int     draw() { return (int)(-mean * log( (double)(MAX-randint::draw())
        !           332:                                                / MAX) + .5); };
        !           333: };

unix.superglobalmegacorp.com

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