Annotation of libaitsched/example/test_brut.c, revision 1.1.2.3

1.1.2.1   misho       1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <unistd.h>
                      4: #include <fcntl.h>
                      5: #include <signal.h>
                      6: #include <sys/types.h>
                      7: #include <sys/event.h>
                      8: #include <sys/stat.h>
                      9: #include <sys/signal.h>
                     10: #include <netinet/in.h>
                     11: #include <aitsched.h>
                     12: 
                     13: intptr_t Kill;
                     14: sched_root_task_t *root;
                     15: 
                     16: void *event(sched_task_t *arg)
                     17: {
                     18:        printf("Event::\n");
                     19:        taskExit(arg, NULL);
                     20: }
                     21: 
                     22: void *regular(sched_task_t *arg)
                     23: {
                     24:        printf("Task(%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
                     25:        fflush(stdout);
                     26:        taskExit(arg, NULL);
                     27: }
                     28: 
                     29: void *timer(sched_task_t *arg)
                     30: {
                     31:        printf("Timer %p sec::\n", TASK_ARG(arg));
                     32:        taskExit(arg, NULL);
                     33: }
                     34: 
                     35: void *rtc(sched_task_t *arg)
                     36: {
                     37:        printf("RTC %p sec:: rtc id %lx signo=%lu\n", TASK_ARG(arg), (long) TASK_DATA(arg), TASK_VAL(arg));
                     38:        taskExit(arg, NULL);
                     39: }
                     40: 
                     41: void *rtcinf(sched_task_t *arg)
                     42: {
                     43:        printf("RTC(inf) sec:: rtc id %lx signo=%lu\n", (long) TASK_DATA(arg), TASK_VAL(arg));
                     44:        schedRTCSelf(arg);
                     45:        taskExit(arg, NULL);
                     46: }
                     47: 
                     48: void *alarmz(sched_task_t *arg)
                     49: {
                     50:        printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
                     51:        taskExit(arg, NULL);
                     52: }
                     53: 
                     54: void *node(sched_task_t *arg)
                     55: {
                     56:        printf("Node %s data %ld fflags 0x%lX\n", (char*) TASK_ARG(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
                     57:        taskExit(arg, NULL);
                     58: }
                     59: 
                     60: void *proc(sched_task_t *arg)
                     61: {
                     62:        printf("Proc pid=%ld data %ld fflags 0x%lX\n", TASK_VAL(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
                     63:        taskExit(arg, NULL);
                     64: }
                     65: 
                     66: void *sigz(sched_task_t *arg)
                     67: {
                     68:        printf("Signal signal=%ld how many times %ld\n", TASK_VAL(arg), (long) TASK_RET(arg));
                     69:        taskExit(arg, NULL);
                     70: }
                     71: 
                     72: #ifdef EVFILT_USER
                     73: void *user(sched_task_t *arg)
                     74: {
                     75:        printf("User trigger id %ld fflags %ld\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
                     76:        taskExit(arg, NULL);
                     77: }
                     78: #endif
                     79: 
                     80: void *susp1(sched_task_t *arg)
                     81: {
                     82:        printf("Suspend 1 =%ld\n", TASK_VAL(arg));
                     83:        taskExit(arg, NULL);
                     84: }
                     85: void *susp2(sched_task_t *arg)
                     86: {
                     87:        printf("Suspend 2 =%ld\n", TASK_VAL(arg));
                     88:        taskExit(arg, NULL);
                     89: }
                     90: void *susp3(sched_task_t *arg)
                     91: {
                     92:        printf("Suspend 3 =%ld\n", TASK_VAL(arg));
                     93:        taskExit(arg, NULL);
                     94: }
                     95: 
                     96: void *once(sched_task_t *arg)
                     97: {
                     98:        printf("once::\n");
                     99:        taskExit(arg, NULL);
                    100: }
                    101: 
                    102: void *thr(sched_task_t *arg)
                    103: {
                    104:        printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
                    105:        printf("tid (%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
1.1.2.3 ! misho     106:        fflush(stdout);
        !           107:        taskExit(arg, 42);
        !           108: }
        !           109: 
        !           110: void *thrinf(sched_task_t *arg)
        !           111: {
        !           112:        printf("tid INF (%lu):: %s %s\n", TASK_VAL(arg), __func__, (char*) TASK_ARG(arg));
        !           113:        fflush(stdout);
        !           114:        schedThreadSelf(arg);
1.1.2.1   misho     115:        taskExit(arg, 42);
                    116: }
                    117: 
                    118: void *thr4kill(sched_task_t *arg)
                    119: {
                    120:        char blah[BUFSIZ];
                    121: 
                    122:        printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
                    123: 
                    124:        read(0, blah, sizeof blah);
1.1.2.3 ! misho     125:        if (arg)
        !           126:                printf("never see!!! (%lx):: %s (%d == %d)\n", arg ? TASK_VAL(arg) : 0, 
        !           127:                                (char*) arg ? TASK_ARG(arg) : NULL, arg ? TASK_TYPE(arg) : -1, taskTHREAD);
1.1.2.1   misho     128:        taskExit(arg, 0);
                    129: }
                    130: 
                    131: void sig(int s)
                    132: {
                    133:        switch (s) {
                    134:                case SIGINT:
                    135:                case SIGTERM:
                    136:                        Kill++;
                    137:                        break;
                    138:                case SIGUSR1:
                    139:                        schedResumeby(root, CRITERIA_ID, (void*) 0);
                    140:                        schedResumeby(root, CRITERIA_ID, (void*) 7);
                    141:                        break;
                    142:        }
                    143: }
                    144: 
                    145: int
                    146: main(int argc, char **argv)
                    147: {
                    148:        struct timespec ts = { 20, 0 };
                    149: //     struct timespec p = { 0, 10000000 };
                    150:        int f = 0;
                    151:        struct sigaction sa;
                    152:        sched_task_t *t;
                    153: #ifdef EVFILT_USER
                    154:        sched_task_t *tt[4];
                    155: #endif
                    156:        sched_task_t *task;
                    157: 
                    158:        sa.sa_handler = sig;
                    159:        sigemptyset(&sa.sa_mask);
                    160:        sigaction(SIGTERM, &sa, NULL);
                    161:        sigaction(SIGINT, &sa, NULL);
                    162:        sigaction(SIGUSR1, &sa, NULL);
                    163: 
                    164:        root = schedBegin();
                    165:        if (!root) {
                    166:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    167:                return 1;
                    168:        }
                    169: 
                    170:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
                    171:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    172:                return 4;
                    173:        }
                    174:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
                    175:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    176:                return 4;
                    177:        } else
                    178:                ts.tv_sec = 15;
                    179:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
                    180:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    181:                return 4;
                    182:        }
                    183:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
                    184:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    185:                return 4;
                    186:        } else
                    187:                ts.tv_sec = 10;
                    188: 
                    189:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
                    190:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    191:                return 2;
                    192:        }
                    193: 
                    194:        if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
                    195:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    196:                return 3;
                    197:        }
                    198:        if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
                    199:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    200:                return 3;
                    201:        }
                    202:        if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
                    203:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    204:                return 3;
                    205:        }
                    206:        if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
                    207:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    208:                return 3;
                    209:        }
                    210: 
                    211:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
                    212:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    213:                return 4;
                    214:        }
                    215: 
                    216:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
                    217:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    218:                return 4;
                    219:        }
                    220:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
                    221:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    222:                return 4;
                    223:        }
                    224: 
                    225:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
                    226:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    227:                return 5;
                    228:        } else {
                    229:                ts.tv_sec = 3;
                    230:                ts.tv_nsec = 500000000;
                    231:        }
                    232: 
                    233:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
                    234:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    235:                return 5;
                    236:        }
                    237:        if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
                    238:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    239:                return 5;
                    240:        } else {
1.1.2.3 ! misho     241:                ts.tv_sec = 1;
        !           242:                ts.tv_nsec = 0;
1.1.2.1   misho     243:        }
                    244: 
                    245:        if (!schedRTC(root, rtcinf, (void*) (intptr_t) ts.tv_sec, ts, (void*) 12, 0)) {
                    246:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    247:                return 5;
                    248:        }
                    249: 
                    250:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
                    251:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    252:                return 5;
                    253:        }
                    254:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
                    255:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    256:                return 5;
                    257:        } else {
                    258:                ts.tv_sec = 0;
                    259:                ts.tv_nsec = 700000000;
                    260:        }
                    261:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
                    262:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    263:                return 5;
                    264:        }
                    265: 
                    266: #ifdef EVFILT_USER
                    267:        if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
                    268:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    269:                return 6;
                    270:        }
                    271:        if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
                    272:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    273:                return 6;
                    274:        }
                    275:        if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
                    276:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    277:                return 6;
                    278:        }
                    279:        if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
                    280:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    281:                return 6;
                    282:        }
                    283: #endif
                    284:        if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
                    285:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    286:                return 6;
                    287:        }
                    288:        if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
                    289:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    290:                return 6;
                    291:        }
                    292:        if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
                    293:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    294:                return 6;
                    295:        }
                    296: 
1.1.2.3 ! misho     297:        if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA", 0, 0, NULL, 0)) {
        !           298:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           299:                return 7;
        !           300:        }
        !           301:        /*
        !           302:        if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA+detach", 42, 0, NULL, 0)) {
        !           303:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           304:                return 7;
        !           305:        }
        !           306:        */
1.1.2.1   misho     307:        if (!(t = schedThread(root, thr4kill, "0aaaa", 0, 0, NULL, 0))) {
                    308:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    309:                return 7;
                    310:        }
                    311:        if (!schedThread(root, thr, "mdaaaa this is thread task", 0, 131072, NULL, 0)) {
                    312:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    313:                return 7;
                    314:        }
                    315:        if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, 0, NULL, 0)) {
                    316:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    317:                return 7;
                    318:        }
                    319:        if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, 131072, NULL, 0)) {
                    320:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    321:                return 7;
                    322:        }
                    323:        printf("try to cancel tid = %lx\n", TASK_VAL(t));
                    324:        schedCancel(t);
                    325:        if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, 131072 * 2, NULL, 0)) {
                    326:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    327:                return 7;
                    328:        }
                    329:        if (!(t = schedThread(root, thr4kill, "0aaaa", 42, /*4096*/0, NULL, 0))) {
                    330:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    331:                return 7;
                    332:        }
                    333:        if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, 0, NULL, 0)) {
                    334:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    335:                return 7;
                    336:        }
                    337:        sleep(1);
                    338:        schedCancel(t);
                    339: 
                    340:        schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 2, NULL);
                    341: //     schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 10, NULL);
                    342: 
                    343:        if (argc > 1)
                    344:                if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
                    345:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    346:                        return 7;
                    347:                }
                    348:        if (argc > 2) {
                    349:                f = open(argv[2], O_RDWR);
                    350:                if (f == -1) {
                    351:                        perror("open()");
                    352:                        return 8;
                    353:                }
                    354:                if (!schedNode(root, node, argv[2], f, 0, 0)) {
                    355:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    356:                        close(f);
                    357:                        return 8;
                    358:                }
                    359:        }
                    360: 
                    361: #ifdef EVFILT_USER
                    362:        schedTrigger(tt[3]);
                    363:        schedTrigger(tt[1]);
                    364: #endif
                    365:        schedResumeby(root, CRITERIA_DATA, task);
                    366: 
                    367:        if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
                    368:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    369:                close(f);
                    370:                return 9;
                    371:        }
                    372: 
                    373: #ifdef EVFILT_USER
                    374:        schedTrigger(tt[2]);
                    375:        schedTrigger(tt[0]);
                    376: #endif
                    377: 
                    378:        schedCallOnce(root, once, "000000", 42, NULL, 0);
                    379: 
                    380:        printf("read_queue=%d timer_queue=%d\n", 
                    381:                        ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
                    382: 
                    383: //     schedPolling(root, &p, NULL);
                    384:        schedRun(root, &Kill);
                    385:        schedEnd(&root);
                    386: 
                    387:        if (f > 2)
                    388:                close(f);
                    389:        return 0;
                    390: }

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