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

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