Annotation of libaitsched/example/test_time.c, revision 1.5.4.1

1.2       misho       1: #include <stdio.h>
1.4       misho       2: #include <stdlib.h>
1.2       misho       3: #include <unistd.h>
                      4: #include <fcntl.h>
1.4       misho       5: #include <signal.h>
1.2       misho       6: #include <sys/types.h>
1.4       misho       7: #include <sys/event.h>
1.2       misho       8: #include <sys/stat.h>
                      9: #include <sys/signal.h>
                     10: #include <netinet/in.h>
                     11: #include <aitsched.h>
                     12: 
                     13: intptr_t Kill;
1.5       misho      14: sched_root_task_t *root;
1.2       misho      15: 
                     16: void *event(sched_task_t *arg)
                     17: {
                     18:        printf("Event::\n");
                     19:        return NULL;
                     20: }
                     21: 
1.5.4.1 ! misho      22: void *regular(sched_task_t *arg)
1.2       misho      23: {
1.5.4.1 ! misho      24:        printf("Task::\n");
        !            25:        fflush(stdout);
1.2       misho      26:        return NULL;
                     27: }
                     28: 
                     29: void *timer(sched_task_t *arg)
                     30: {
1.4       misho      31:        printf("Timer %p sec::\n", TASK_ARG(arg));
1.2       misho      32:        return NULL;
                     33: }
                     34: 
1.3       misho      35: void *alarmz(sched_task_t *arg)
                     36: {
1.4       misho      37:        printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
1.3       misho      38:        return NULL;
                     39: }
                     40: 
1.4       misho      41: void *node(sched_task_t *arg)
                     42: {
                     43:        printf("Node %s fflags 0x%X\n", (char*) TASK_ARG(arg), (u_int) TASK_DATLEN(arg));
                     44:        return NULL;
                     45: }
                     46: 
                     47: void *proc(sched_task_t *arg)
                     48: {
                     49:        printf("Proc pid=%ld fflags 0x%X data %p\n", TASK_VAL(arg), (u_int) TASK_DATLEN(arg), 
                     50:                        TASK_DATA(arg));
                     51:        return NULL;
                     52: }
                     53: 
                     54: void *sigz(sched_task_t *arg)
                     55: {
                     56:        printf("Signal signal=%ld\n", TASK_VAL(arg));
                     57:        return NULL;
                     58: }
                     59: 
                     60: #ifdef EVFILT_USER
                     61: void *user(sched_task_t *arg)
                     62: {
                     63:        printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_DATLEN(arg) & NOTE_FFLAGSMASK);
                     64:        return NULL;
                     65: }
                     66: #endif
                     67: 
1.5       misho      68: void *susp1(sched_task_t *arg)
                     69: {
                     70:        printf("Suspend 1 =%ld\n", TASK_VAL(arg));
                     71:        return NULL;
                     72: }
                     73: void *susp2(sched_task_t *arg)
                     74: {
                     75:        printf("Suspend 2 =%ld\n", TASK_VAL(arg));
                     76:        return NULL;
                     77: }
                     78: void *susp3(sched_task_t *arg)
                     79: {
                     80:        printf("Suspend 3 =%ld\n", TASK_VAL(arg));
                     81:        return NULL;
                     82: }
                     83: 
1.2       misho      84: void *once(sched_task_t *arg)
                     85: {
                     86:        printf("once::\n");
                     87:        return NULL;
                     88: }
                     89: 
                     90: void sig(int s)
                     91: {
                     92:        switch (s) {
                     93:                case SIGTERM:
                     94:                        Kill++;
                     95:                        break;
1.4       misho      96:                case SIGUSR1:
1.5       misho      97:                        schedResumeby(root, CRITERIA_ID, (void*) 0);
                     98:                        schedResumeby(root, CRITERIA_ID, (void*) 7);
1.4       misho      99:                        break;
1.2       misho     100:        }
                    101: }
                    102: 
                    103: int
                    104: main(int argc, char **argv)
                    105: {
                    106:        struct timespec ts = { 20, 0 };
                    107: //     struct timespec p = { 0, 10000000 };
1.4       misho     108:        int f = 0;
                    109:        struct sigaction sa;
                    110: #ifdef EVFILT_USER
                    111:        sched_task_t *t[4];
                    112: #endif
1.5       misho     113:        sched_task_t *task;
1.4       misho     114: 
                    115:        sa.sa_handler = sig;
                    116:        sigemptyset(&sa.sa_mask);
                    117:        sigaction(SIGTERM, &sa, NULL);
                    118:        sigaction(SIGUSR1, &sa, NULL);
1.2       misho     119: 
                    120:        root = schedBegin();
                    121:        if (!root) {
                    122:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    123:                return 1;
                    124:        }
                    125: 
1.4       misho     126:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     127:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    128:                return 4;
                    129:        } else
                    130:                ts.tv_sec = 15;
1.4       misho     131:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     132:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    133:                return 4;
                    134:        } else
                    135:                ts.tv_sec = 10;
                    136: 
                    137:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
                    138:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    139:                return 2;
                    140:        }
                    141: 
1.5.4.1 ! misho     142:        if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
1.2       misho     143:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    144:                return 3;
                    145:        }
                    146: 
1.4       misho     147:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     148:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    149:                return 4;
                    150:        }
                    151: 
1.4       misho     152:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.3       misho     153:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    154:                return 5;
                    155:        } else {
                    156:                ts.tv_sec = 3;
                    157:                ts.tv_nsec = 500000000;
                    158:        }
                    159: 
1.4       misho     160:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
1.3       misho     161:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    162:                return 5;
                    163:        }
1.4       misho     164:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
1.3       misho     165:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    166:                return 5;
                    167:        } else {
                    168:                ts.tv_sec = 0;
                    169:                ts.tv_nsec = 700000000;
                    170:        }
1.4       misho     171:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
1.3       misho     172:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    173:                return 5;
                    174:        }
                    175: 
1.4       misho     176: #ifdef EVFILT_USER
                    177:        if (!(t[0] = schedUser(root, user, NULL, 42, 0, 0))) {
                    178:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    179:                return 6;
                    180:        }
                    181:        if (!(t[1] = schedUser(root, user, NULL, 1, 0, 73))) {
                    182:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    183:                return 6;
                    184:        }
                    185:        if (!(t[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
                    186:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    187:                return 6;
                    188:        }
                    189:        if (!(t[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
                    190:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    191:                return 6;
                    192:        }
                    193: #endif
1.5       misho     194:        if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
                    195:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    196:                return 6;
                    197:        }
                    198:        if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
                    199:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    200:                return 6;
                    201:        }
                    202:        if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
                    203:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    204:                return 6;
                    205:        }
1.4       misho     206: 
                    207:        if (argc > 1)
                    208:                if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
                    209:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    210:                        return 7;
                    211:                }
                    212:        if (argc > 2) {
                    213:                f = open(argv[2], O_RDWR);
                    214:                if (f == -1) {
                    215:                        perror("open()");
                    216:                        return 8;
                    217:                }
                    218:                if (!schedNode(root, node, argv[2], f, 0, 0)) {
                    219:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    220:                        close(f);
                    221:                        return 8;
                    222:                }
                    223:        }
                    224: 
                    225: #ifdef EVFILT_USER
                    226:        schedTrigger(t[3]);
                    227:        schedTrigger(t[1]);
                    228: #endif
1.5       misho     229:        schedResumeby(root, CRITERIA_DATA, task);
1.4       misho     230: 
                    231:        if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
                    232:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    233:                close(f);
                    234:                return 9;
                    235:        }
                    236: 
                    237: #ifdef EVFILT_USER
                    238:        schedTrigger(t[2]);
                    239:        schedTrigger(t[0]);
                    240: #endif
                    241: 
1.2       misho     242:        schedCallOnce(root, once, "000000", 42, NULL, 0);
                    243: 
                    244:        printf("read_queue=%d timer_queue=%d\n", 
                    245:                        ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
                    246: 
                    247: //     schedPolling(root, &p, NULL);
                    248:        schedRun(root, &Kill);
                    249:        schedEnd(&root);
1.4       misho     250: 
                    251:        if (f > 2)
                    252:                close(f);
1.2       misho     253:        return 0;
                    254: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>