Annotation of libaitsched/example/test.c, revision 1.14.30.2

1.2       misho       1: #include <stdio.h>
1.7       misho       2: #include <stdlib.h>
                      3: #include <string.h>
1.2       misho       4: #include <unistd.h>
                      5: #include <fcntl.h>
1.5       misho       6: #include <signal.h>
1.2       misho       7: #include <sys/types.h>
                      8: #include <sys/stat.h>
                      9: #include <sys/socket.h>
                     10: #include <netinet/in.h>
1.7       misho      11: #include "../inc/config.h"
1.2       misho      12: #include <aitsched.h>
                     13: 
1.14      misho      14: intptr_t Kill[1];
1.7       misho      15: #ifdef AIO_SUPPORT
                     16: struct iovec iv[3], wiv[3], riv[3];
                     17: #endif
1.2       misho      18: 
                     19: void *event(sched_task_t *arg)
                     20: {
                     21:        printf("Event::\n");
1.10      misho      22:        taskExit(arg, NULL);
1.2       misho      23: }
                     24: 
1.8       misho      25: void *regular(sched_task_t *arg)
1.2       misho      26: {
1.8       misho      27:        printf("Task::\n");
1.10      misho      28:        taskExit(arg, NULL);
1.2       misho      29: }
                     30: 
                     31: void *timer(sched_task_t *arg)
                     32: {
1.6       misho      33:        printf("Timer %p sec::\n", TASK_ARG(arg));
1.10      misho      34:        taskExit(arg, NULL);
1.2       misho      35: }
                     36: 
                     37: void *r(sched_task_t *arg)
                     38: {
1.12      misho      39:        printf("read:: %ld bytes wait\n", (long) TASK_RET(arg));
1.14      misho      40:        Kill[0]++;
1.10      misho      41:        taskExit(arg, NULL);
1.2       misho      42: }
                     43: 
                     44: void *w(sched_task_t *arg)
                     45: {
1.12      misho      46:        printf("write:: %ld bytes wait\n", (long) TASK_RET(arg));
1.10      misho      47:        taskExit(arg, NULL);
1.2       misho      48: }
                     49: 
                     50: void *once(sched_task_t *arg)
                     51: {
                     52:        printf("once::\n");
1.10      misho      53:        taskExit(arg, NULL);
1.2       misho      54: }
                     55: 
1.7       misho      56: #ifdef AIO_SUPPORT
                     57: void *aioread(sched_task_t *arg);
                     58: void *aiowrite(sched_task_t *arg)
                     59: {
                     60:        char *ole = malloc(BUFSIZ);
                     61: 
                     62:        printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
                     63:                        TASK_DATA(arg));
                     64:        free(TASK_DATA(arg));
                     65: 
                     66:        memset(ole, 0, BUFSIZ);
                     67:        schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
1.10      misho      68:        taskExit(arg, NULL);
1.7       misho      69: }
                     70: 
                     71: void *aioread(sched_task_t *arg)
                     72: {
                     73:        char *ole = malloc(BUFSIZ);
                     74:        int len;
                     75: 
                     76:        printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
                     77:                        (char*) TASK_DATA(arg));
                     78: 
                     79:        if (TASK_ARG(arg)) {
                     80:                len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
                     81:                printf("sched Write len=%d %p\n", len, ole);
                     82:                schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole,  len, -1);
                     83:                                
                     84:        }
                     85:        free(TASK_DATA(arg));
1.10      misho      86:        taskExit(arg, NULL);
1.7       misho      87: }
                     88: 
                     89: void *aiobulkread(sched_task_t *arg)
                     90: {
                     91:        struct iovec *iv = TASK_DATA(arg);
                     92:        register int i;
                     93: 
                     94:        printf("aioBULKread::\n");
                     95:        for (i = 0; i < 3; i++) {
                     96:                printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
                     97:                free(iv[i].iov_base);
                     98:        }
                     99: 
1.10      misho     100:        taskExit(arg, NULL);
1.7       misho     101: }
                    102: 
                    103: void *aiobulkwrite(sched_task_t *arg)
                    104: {
                    105:        struct iovec *iv = TASK_DATA(arg);
                    106:        register int i;
                    107: 
                    108:        printf("aioBULKwrite::\n");
                    109:        for (i = 0; i < 3; i++) {
                    110:                printf("%d) wlen=%d\n", i, iv[i].iov_len);
                    111:                free(iv[i].iov_base);
                    112:        }
                    113: 
1.10      misho     114:        taskExit(arg, NULL);
1.7       misho     115: }
                    116: #endif
                    117: 
1.9       misho     118: void *thr(sched_task_t *arg)
                    119: {
1.10      misho     120:        printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
1.9       misho     121:        taskExit(arg, 42);
                    122: }
                    123: 
1.10      misho     124: void *thr4kill(sched_task_t *arg)
                    125: {
                    126:        char blah[BUFSIZ];
                    127: 
1.14      misho     128:        printf("THREAD!!! tid (%lx):: %s\n", TASK_VAL(arg), __func__);
1.10      misho     129: 
                    130:        read(0, blah, sizeof blah);
                    131:        printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
                    132:        taskExit(arg, 0);
                    133: }
                    134: 
1.5       misho     135: void sig(int s)
                    136: {
                    137:        switch (s) {
                    138:                case SIGTERM:
1.14      misho     139:                        Kill[0]++;
1.5       misho     140:                        break;
1.14.30.1  misho     141:                case SIGHUP:
                    142:                        printf("Test SIGHUP\n");
                    143:                        break;
1.5       misho     144:        }
                    145: }
                    146: 
1.14.30.1  misho     147: void *sigt(sched_task_t *arg)
                    148: {
                    149:        int s = TASK_VAL(arg);
                    150: 
                    151:        printf("Received signal #%d\n", s);
                    152: 
                    153:        sig(s);
                    154: 
                    155:        schedSignalSelf(arg);
                    156:        taskExit(arg, NULL);
                    157: }
                    158: 
1.2       misho     159: int
                    160: main(int argc, char **argv)
                    161: {
                    162:        sched_root_task_t *root;
1.7       misho     163:        int f, fd;
1.2       misho     164:        struct sockaddr_in sin;
1.5       misho     165:        struct timespec ts = { 20, 0 };
                    166: //     struct timespec p = { 0, 10000000 };
1.14.30.2! misho     167: //     struct sigaction sa;
1.10      misho     168:        sched_task_t *t;
1.7       misho     169: #ifdef AIO_SUPPORT
                    170:        char *ole = malloc(BUFSIZ);
                    171:        register int i;
                    172: #endif
1.5       misho     173: 
1.14.30.2! misho     174:        /*
1.5       misho     175:        sa.sa_handler = sig;
                    176:        sigemptyset(&sa.sa_mask);
                    177:        sigaction(SIGTERM, &sa, NULL);
1.14.30.2! misho     178:        */
1.2       misho     179: 
                    180:        f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                    181:        if (f == -1)
                    182:                return 1;
1.14      misho     183: #ifndef __linux__
1.2       misho     184:        sin.sin_len = sizeof sin;
1.14      misho     185: #endif
1.2       misho     186:        sin.sin_family = AF_INET;
                    187:        sin.sin_port = htons(2345);
                    188:        sin.sin_addr.s_addr = INADDR_ANY;
                    189:        if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
                    190:                return 1;
                    191: 
1.7       misho     192:        fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
                    193:        if (fd == -1)
                    194:                return 1;
                    195:        printf("fd=%d\n", fd);
                    196: 
1.2       misho     197:        root = schedBegin();
                    198:        if (!root) {
                    199:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    200:                return 1;
                    201:        }
                    202: 
1.6       misho     203:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     204:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    205:                return 4;
                    206:        } else
                    207:                ts.tv_sec = 15;
1.6       misho     208:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     209:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    210:                return 4;
                    211:        } else
                    212:                ts.tv_sec = 10;
                    213: 
1.4       misho     214:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
1.2       misho     215:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    216:                return 2;
                    217:        }
                    218: 
1.8       misho     219:        if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
1.2       misho     220:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    221:                return 3;
                    222:        }
                    223: 
1.6       misho     224:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     225:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    226:                return 4;
                    227:        }
                    228: 
1.14      misho     229:        if (!(t = schedRead(root, r, "rrr_test", f, NULL, 0))) {
                    230:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    231:                return 5;
                    232:        }
1.4       misho     233:        if (!schedRead(root, r, "rrr", f, NULL, 0)) {
1.2       misho     234:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    235:                return 5;
                    236:        }
1.14      misho     237:        schedCancel(t);
1.2       misho     238: 
1.4       misho     239:        if (!schedWrite(root, w, "www", f, NULL, 0)) {
1.2       misho     240:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    241:                return 6;
                    242:        }
                    243: 
1.13      misho     244:        if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
1.10      misho     245:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    246:                return 7;
                    247:        }
1.13      misho     248:        if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
1.9       misho     249:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
1.14      misho     250:                printf("stack is too small\n");
1.9       misho     251:        }
1.13      misho     252:        if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
1.9       misho     253:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    254:                return 7;
                    255:        }
1.13      misho     256:        if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
1.9       misho     257:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    258:                return 7;
                    259:        }
1.14      misho     260:        printf("~~~try to cancel tid = %lx\n", TASK_VAL(t));
1.10      misho     261:        schedCancel(t);
1.13      misho     262:        if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
1.10      misho     263:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    264:                return 7;
                    265:        }
1.13      misho     266:        if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
1.10      misho     267:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    268:                return 7;
                    269:        }
1.13      misho     270:        if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
1.10      misho     271:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
1.14      misho     272:                printf("stack is too small\n");
1.10      misho     273:        }
                    274:        sleep(1);
                    275:        schedCancel(t);
1.9       misho     276: 
1.7       misho     277: #ifdef AIO_SUPPORT
                    278:        memset(ole, 0, BUFSIZ);
                    279:        if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
                    280:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    281: 
                    282: 
                    283:        iv[0].iov_len = 5;
                    284:        iv[1].iov_len = 2;
                    285:        iv[2].iov_len = 50;
                    286:        for (i = 0; i < 3; i++)
                    287:                iv[i].iov_base = malloc(iv[i].iov_len);
                    288:        if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
                    289:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    290:        fsync(fd);
                    291:        for (i = 0; i < 3; i++) {
                    292:                wiv[i].iov_len = 100;
                    293:                wiv[i].iov_base = malloc(wiv[i].iov_len);
                    294:        }
                    295:        strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
                    296:        wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
                    297:        strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
                    298:        wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
                    299:        strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
                    300:        wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
                    301:        if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
                    302:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    303: 
                    304:        for (i = 0; i < 3; i++) {
                    305:                riv[i].iov_len = 5;
                    306:                riv[i].iov_base = malloc(riv[i].iov_len + 1);
                    307:                memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
                    308:        }
                    309:        if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
                    310:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    311: #endif
                    312: 
1.4       misho     313:        schedCallOnce(root, once, "000000", 42, NULL, 0);
1.2       misho     314: 
1.14.30.1  misho     315:        printf("add signals\n");
                    316:        schedSignal(root, sigt, NULL, SIGHUP, NULL, 0);
                    317:        schedSignal(root, sigt, NULL, SIGTERM, NULL, 0);
                    318:        schedSignal(root, sigt, NULL, SIGINT, NULL, 0);
                    319: 
1.5       misho     320: //     schedPolling(root, &p, NULL);
1.14.30.1  misho     321:        schedSignalDispatch(root, 42);
1.14      misho     322:        schedRun(root, Kill);
1.14.30.1  misho     323:        schedSignalDispatch(root, 0);
1.2       misho     324:        schedEnd(&root);
1.14      misho     325:        sleep(1);
1.2       misho     326: 
1.7       misho     327: #ifdef AIO_SUPPORT
                    328:        for (i = 0; i < 3; i++)
                    329:                free(iv[i].iov_base);
                    330: #endif
                    331: 
                    332:        close(fd);
1.2       misho     333:        close(f);
                    334:        return 0;
                    335: }

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