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

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");
1.8.2.1   misho      19:        taskExit(arg, NULL);
1.2       misho      20: }
                     21: 
1.6       misho      22: void *regular(sched_task_t *arg)
1.2       misho      23: {
1.7       misho      24:        printf("Task(%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
1.6       misho      25:        fflush(stdout);
1.8.2.1   misho      26:        taskExit(arg, NULL);
1.2       misho      27: }
                     28: 
                     29: void *timer(sched_task_t *arg)
                     30: {
1.4       misho      31:        printf("Timer %p sec::\n", TASK_ARG(arg));
1.8.2.1   misho      32:        taskExit(arg, NULL);
1.2       misho      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.8.2.1   misho      38:        taskExit(arg, NULL);
1.3       misho      39: }
                     40: 
1.4       misho      41: void *node(sched_task_t *arg)
                     42: {
1.8       misho      43:        printf("Node %s data %d fflags 0x%X\n", (char*) TASK_ARG(arg), TASK_RET(arg), TASK_FLAG(arg));
1.8.2.1   misho      44:        taskExit(arg, NULL);
1.4       misho      45: }
                     46: 
                     47: void *proc(sched_task_t *arg)
                     48: {
1.8       misho      49:        printf("Proc pid=%ld data %d fflags 0x%X\n", TASK_VAL(arg), TASK_RET(arg), TASK_FLAG(arg));
1.8.2.1   misho      50:        taskExit(arg, NULL);
1.4       misho      51: }
                     52: 
                     53: void *sigz(sched_task_t *arg)
                     54: {
1.8       misho      55:        printf("Signal signal=%ld how many times %d\n", TASK_VAL(arg), TASK_RET(arg));
1.8.2.1   misho      56:        taskExit(arg, NULL);
1.4       misho      57: }
                     58: 
                     59: #ifdef EVFILT_USER
                     60: void *user(sched_task_t *arg)
                     61: {
1.8       misho      62:        printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
1.8.2.1   misho      63:        taskExit(arg, NULL);
1.4       misho      64: }
                     65: #endif
                     66: 
1.5       misho      67: void *susp1(sched_task_t *arg)
                     68: {
                     69:        printf("Suspend 1 =%ld\n", TASK_VAL(arg));
1.8.2.1   misho      70:        taskExit(arg, NULL);
1.5       misho      71: }
                     72: void *susp2(sched_task_t *arg)
                     73: {
                     74:        printf("Suspend 2 =%ld\n", TASK_VAL(arg));
1.8.2.1   misho      75:        taskExit(arg, NULL);
1.5       misho      76: }
                     77: void *susp3(sched_task_t *arg)
                     78: {
                     79:        printf("Suspend 3 =%ld\n", TASK_VAL(arg));
1.8.2.1   misho      80:        taskExit(arg, NULL);
1.5       misho      81: }
                     82: 
1.2       misho      83: void *once(sched_task_t *arg)
                     84: {
                     85:        printf("once::\n");
1.8.2.1   misho      86:        taskExit(arg, NULL);
1.2       misho      87: }
                     88: 
1.8.2.2 ! misho      89: void *thr(sched_task_t *arg)
        !            90: {
        !            91:        printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
        !            92:        printf("tid (%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
        !            93:        taskExit(arg, 42);
        !            94: }
        !            95: 
        !            96: void *thr4kill(sched_task_t *arg)
        !            97: {
        !            98:        char blah[BUFSIZ];
        !            99: 
        !           100:        printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
        !           101: 
        !           102:        read(0, blah, sizeof blah);
        !           103:        printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
        !           104:        taskExit(arg, 0);
        !           105: }
        !           106: 
1.2       misho     107: void sig(int s)
                    108: {
                    109:        switch (s) {
                    110:                case SIGTERM:
                    111:                        Kill++;
                    112:                        break;
1.4       misho     113:                case SIGUSR1:
1.5       misho     114:                        schedResumeby(root, CRITERIA_ID, (void*) 0);
                    115:                        schedResumeby(root, CRITERIA_ID, (void*) 7);
1.4       misho     116:                        break;
1.2       misho     117:        }
                    118: }
                    119: 
                    120: int
                    121: main(int argc, char **argv)
                    122: {
                    123:        struct timespec ts = { 20, 0 };
                    124: //     struct timespec p = { 0, 10000000 };
1.4       misho     125:        int f = 0;
                    126:        struct sigaction sa;
1.8.2.2 ! misho     127:        sched_task_t *t;
1.4       misho     128: #ifdef EVFILT_USER
1.8.2.2 ! misho     129:        sched_task_t *tt[4];
1.4       misho     130: #endif
1.5       misho     131:        sched_task_t *task;
1.4       misho     132: 
                    133:        sa.sa_handler = sig;
                    134:        sigemptyset(&sa.sa_mask);
                    135:        sigaction(SIGTERM, &sa, NULL);
                    136:        sigaction(SIGUSR1, &sa, NULL);
1.2       misho     137: 
                    138:        root = schedBegin();
                    139:        if (!root) {
                    140:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    141:                return 1;
                    142:        }
                    143: 
1.4       misho     144:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     145:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    146:                return 4;
                    147:        } else
                    148:                ts.tv_sec = 15;
1.4       misho     149:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     150:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    151:                return 4;
                    152:        } else
                    153:                ts.tv_sec = 10;
                    154: 
                    155:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
                    156:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    157:                return 2;
                    158:        }
                    159: 
1.7       misho     160:        if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
                    161:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    162:                return 3;
                    163:        }
                    164:        if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
                    165:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    166:                return 3;
                    167:        }
                    168:        if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
                    169:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    170:                return 3;
                    171:        }
                    172:        if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
1.2       misho     173:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    174:                return 3;
                    175:        }
                    176: 
1.4       misho     177:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     178:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    179:                return 4;
                    180:        }
                    181: 
1.4       misho     182:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.3       misho     183:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    184:                return 5;
                    185:        } else {
                    186:                ts.tv_sec = 3;
                    187:                ts.tv_nsec = 500000000;
                    188:        }
                    189: 
1.4       misho     190:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
1.3       misho     191:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    192:                return 5;
                    193:        }
1.4       misho     194:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
1.3       misho     195:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    196:                return 5;
                    197:        } else {
                    198:                ts.tv_sec = 0;
                    199:                ts.tv_nsec = 700000000;
                    200:        }
1.4       misho     201:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
1.3       misho     202:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    203:                return 5;
                    204:        }
                    205: 
1.4       misho     206: #ifdef EVFILT_USER
1.8.2.2 ! misho     207:        if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
1.4       misho     208:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    209:                return 6;
                    210:        }
1.8.2.2 ! misho     211:        if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
1.4       misho     212:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    213:                return 6;
                    214:        }
1.8.2.2 ! misho     215:        if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
1.4       misho     216:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    217:                return 6;
                    218:        }
1.8.2.2 ! misho     219:        if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
1.4       misho     220:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    221:                return 6;
                    222:        }
                    223: #endif
1.5       misho     224:        if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
                    225:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    226:                return 6;
                    227:        }
                    228:        if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
                    229:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    230:                return 6;
                    231:        }
                    232:        if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
                    233:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    234:                return 6;
                    235:        }
1.4       misho     236: 
1.8.2.2 ! misho     237:        if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
        !           238:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           239:                return 7;
        !           240:        }
        !           241:        if (!schedThread(root, thr, "mdaaaa this is thread task", 0, NULL, 0)) {
        !           242:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           243:                return 7;
        !           244:        }
        !           245:        if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, NULL, 0)) {
        !           246:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           247:                return 7;
        !           248:        }
        !           249:        if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
        !           250:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           251:                return 7;
        !           252:        }
        !           253:        printf("try to cancel tid = %lx\n", TASK_VAL(t));
        !           254:        schedCancel(t);
        !           255:        if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
        !           256:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           257:                return 7;
        !           258:        }
        !           259:        if (!(t = schedThread(root, thr4kill, "0aaaa", 42, NULL, 0))) {
        !           260:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           261:                return 7;
        !           262:        }
        !           263:        if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, NULL, 0)) {
        !           264:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           265:                return 7;
        !           266:        }
        !           267:        sleep(1);
        !           268:        schedCancel(t);
        !           269: 
1.4       misho     270:        if (argc > 1)
                    271:                if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
                    272:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    273:                        return 7;
                    274:                }
                    275:        if (argc > 2) {
                    276:                f = open(argv[2], O_RDWR);
                    277:                if (f == -1) {
                    278:                        perror("open()");
                    279:                        return 8;
                    280:                }
                    281:                if (!schedNode(root, node, argv[2], f, 0, 0)) {
                    282:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    283:                        close(f);
                    284:                        return 8;
                    285:                }
                    286:        }
                    287: 
                    288: #ifdef EVFILT_USER
1.8.2.2 ! misho     289:        schedTrigger(tt[3]);
        !           290:        schedTrigger(tt[1]);
1.4       misho     291: #endif
1.5       misho     292:        schedResumeby(root, CRITERIA_DATA, task);
1.4       misho     293: 
                    294:        if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
                    295:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    296:                close(f);
                    297:                return 9;
                    298:        }
                    299: 
                    300: #ifdef EVFILT_USER
1.8.2.2 ! misho     301:        schedTrigger(tt[2]);
        !           302:        schedTrigger(tt[0]);
1.4       misho     303: #endif
                    304: 
1.2       misho     305:        schedCallOnce(root, once, "000000", 42, NULL, 0);
                    306: 
                    307:        printf("read_queue=%d timer_queue=%d\n", 
                    308:                        ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
                    309: 
                    310: //     schedPolling(root, &p, NULL);
                    311:        schedRun(root, &Kill);
                    312:        schedEnd(&root);
1.4       misho     313: 
                    314:        if (f > 2)
                    315:                close(f);
1.2       misho     316:        return 0;
                    317: }

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