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

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.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.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.9       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.9       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.9       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.9       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.9       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.9       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.9       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.9       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.9       misho      86:        taskExit(arg, NULL);
                     87: }
                     88: 
                     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);
1.2       misho     105: }
                    106: 
                    107: void sig(int s)
                    108: {
                    109:        switch (s) {
1.9       misho     110:                case SIGINT:
1.2       misho     111:                case SIGTERM:
                    112:                        Kill++;
                    113:                        break;
1.4       misho     114:                case SIGUSR1:
1.5       misho     115:                        schedResumeby(root, CRITERIA_ID, (void*) 0);
                    116:                        schedResumeby(root, CRITERIA_ID, (void*) 7);
1.4       misho     117:                        break;
1.2       misho     118:        }
                    119: }
                    120: 
                    121: int
                    122: main(int argc, char **argv)
                    123: {
                    124:        struct timespec ts = { 20, 0 };
                    125: //     struct timespec p = { 0, 10000000 };
1.4       misho     126:        int f = 0;
                    127:        struct sigaction sa;
1.9       misho     128:        sched_task_t *t;
1.4       misho     129: #ifdef EVFILT_USER
1.9       misho     130:        sched_task_t *tt[4];
1.4       misho     131: #endif
1.5       misho     132:        sched_task_t *task;
1.4       misho     133: 
                    134:        sa.sa_handler = sig;
                    135:        sigemptyset(&sa.sa_mask);
                    136:        sigaction(SIGTERM, &sa, NULL);
1.9       misho     137:        sigaction(SIGINT, &sa, NULL);
1.4       misho     138:        sigaction(SIGUSR1, &sa, NULL);
1.2       misho     139: 
                    140:        root = schedBegin();
                    141:        if (!root) {
                    142:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    143:                return 1;
                    144:        }
                    145: 
1.4       misho     146:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     147:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    148:                return 4;
                    149:        } else
                    150:                ts.tv_sec = 15;
1.4       misho     151:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     152:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    153:                return 4;
                    154:        } else
                    155:                ts.tv_sec = 10;
                    156: 
                    157:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
                    158:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    159:                return 2;
                    160:        }
                    161: 
1.7       misho     162:        if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
                    163:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    164:                return 3;
                    165:        }
                    166:        if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
                    167:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    168:                return 3;
                    169:        }
                    170:        if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
                    171:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    172:                return 3;
                    173:        }
                    174:        if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
1.2       misho     175:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    176:                return 3;
                    177:        }
                    178: 
1.4       misho     179:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     180:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    181:                return 4;
                    182:        }
                    183: 
1.4       misho     184:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.3       misho     185:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    186:                return 5;
                    187:        } else {
                    188:                ts.tv_sec = 3;
                    189:                ts.tv_nsec = 500000000;
                    190:        }
                    191: 
1.4       misho     192:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
1.3       misho     193:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    194:                return 5;
                    195:        }
1.4       misho     196:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
1.3       misho     197:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    198:                return 5;
                    199:        } else {
                    200:                ts.tv_sec = 0;
                    201:                ts.tv_nsec = 700000000;
                    202:        }
1.4       misho     203:        if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
1.3       misho     204:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    205:                return 5;
                    206:        }
                    207: 
1.4       misho     208: #ifdef EVFILT_USER
1.9       misho     209:        if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
1.4       misho     210:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    211:                return 6;
                    212:        }
1.9       misho     213:        if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
1.4       misho     214:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    215:                return 6;
                    216:        }
1.9       misho     217:        if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
1.4       misho     218:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    219:                return 6;
                    220:        }
1.9       misho     221:        if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
1.4       misho     222:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    223:                return 6;
                    224:        }
                    225: #endif
1.5       misho     226:        if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
                    227:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    228:                return 6;
                    229:        }
                    230:        if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
                    231:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    232:                return 6;
                    233:        }
                    234:        if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
                    235:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    236:                return 6;
                    237:        }
1.4       misho     238: 
1.10    ! misho     239:        if (!(t = schedThread(root, thr4kill, "0aaaa", 0, 0, NULL, 0))) {
1.9       misho     240:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    241:                return 7;
                    242:        }
1.10    ! misho     243:        if (!schedThread(root, thr, "mdaaaa this is thread task", 0, 131072, NULL, 0)) {
1.9       misho     244:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    245:                return 7;
                    246:        }
1.10    ! misho     247:        if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, 0, NULL, 0)) {
1.9       misho     248:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    249:                return 7;
                    250:        }
1.10    ! misho     251:        if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, 131072, NULL, 0)) {
1.9       misho     252:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    253:                return 7;
                    254:        }
                    255:        printf("try to cancel tid = %lx\n", TASK_VAL(t));
                    256:        schedCancel(t);
1.10    ! misho     257:        if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, 131072 * 2, NULL, 0)) {
1.9       misho     258:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    259:                return 7;
                    260:        }
1.10    ! misho     261:        if (!(t = schedThread(root, thr4kill, "0aaaa", 42, /*4096*/0, NULL, 0))) {
1.9       misho     262:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    263:                return 7;
                    264:        }
1.10    ! misho     265:        if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, 0, NULL, 0)) {
1.9       misho     266:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    267:                return 7;
                    268:        }
                    269:        sleep(1);
                    270:        schedCancel(t);
                    271: 
1.4       misho     272:        if (argc > 1)
                    273:                if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
                    274:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    275:                        return 7;
                    276:                }
                    277:        if (argc > 2) {
                    278:                f = open(argv[2], O_RDWR);
                    279:                if (f == -1) {
                    280:                        perror("open()");
                    281:                        return 8;
                    282:                }
                    283:                if (!schedNode(root, node, argv[2], f, 0, 0)) {
                    284:                        printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    285:                        close(f);
                    286:                        return 8;
                    287:                }
                    288:        }
                    289: 
                    290: #ifdef EVFILT_USER
1.9       misho     291:        schedTrigger(tt[3]);
                    292:        schedTrigger(tt[1]);
1.4       misho     293: #endif
1.5       misho     294:        schedResumeby(root, CRITERIA_DATA, task);
1.4       misho     295: 
                    296:        if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
                    297:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    298:                close(f);
                    299:                return 9;
                    300:        }
                    301: 
                    302: #ifdef EVFILT_USER
1.9       misho     303:        schedTrigger(tt[2]);
                    304:        schedTrigger(tt[0]);
1.4       misho     305: #endif
                    306: 
1.2       misho     307:        schedCallOnce(root, once, "000000", 42, NULL, 0);
                    308: 
                    309:        printf("read_queue=%d timer_queue=%d\n", 
                    310:                        ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
                    311: 
                    312: //     schedPolling(root, &p, NULL);
                    313:        schedRun(root, &Kill);
                    314:        schedEnd(&root);
1.4       misho     315: 
                    316:        if (f > 2)
                    317:                close(f);
1.2       misho     318:        return 0;
                    319: }

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