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

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

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