Annotation of libaitsched/example/test_basic.c, revision 1.1.2.1
1.1.2.1 ! misho 1: #include <stdio.h>
! 2: #include <stdlib.h>
! 3: #include <string.h>
! 4: #include <unistd.h>
! 5: #include <fcntl.h>
! 6: #include <signal.h>
! 7: #include <sys/types.h>
! 8: #include <sys/stat.h>
! 9: #include <sys/socket.h>
! 10: #include <netinet/in.h>
! 11: #include "../inc/config.h"
! 12: #include <aitsched.h>
! 13:
! 14: intptr_t Kill;
! 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::\n");
! 25: taskExit(arg, NULL);
! 26: }
! 27:
! 28: void *timer(sched_task_t *arg)
! 29: {
! 30: printf("Timer %p sec::\n", TASK_ARG(arg));
! 31: taskExit(arg, NULL);
! 32: }
! 33:
! 34: void *r(sched_task_t *arg)
! 35: {
! 36: printf("read:: %ld bytes wait\n", (long) TASK_RET(arg));
! 37: Kill++;
! 38: taskExit(arg, NULL);
! 39: }
! 40:
! 41: void *w(sched_task_t *arg)
! 42: {
! 43: printf("write:: %ld bytes wait\n", (long) TASK_RET(arg));
! 44: taskExit(arg, NULL);
! 45: }
! 46:
! 47: void *once(sched_task_t *arg)
! 48: {
! 49: printf("once::\n");
! 50: taskExit(arg, NULL);
! 51: }
! 52:
! 53: void *thr(sched_task_t *arg)
! 54: {
! 55: printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
! 56: taskExit(arg, 42);
! 57: }
! 58:
! 59: void *thr4kill(sched_task_t *arg)
! 60: {
! 61: char blah[BUFSIZ];
! 62:
! 63: printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
! 64:
! 65: read(0, blah, sizeof blah);
! 66: printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
! 67: taskExit(arg, 0);
! 68: }
! 69:
! 70: void sig(int s)
! 71: {
! 72: switch (s) {
! 73: case SIGTERM:
! 74: Kill++;
! 75: break;
! 76: }
! 77: }
! 78:
! 79: int
! 80: main(int argc, char **argv)
! 81: {
! 82: sched_root_task_t *root;
! 83: int f, fd;
! 84: struct sockaddr_in sin;
! 85: struct timespec ts = { 20, 0 };
! 86: // struct timespec p = { 0, 10000000 };
! 87: struct sigaction sa;
! 88: sched_task_t *t;
! 89:
! 90: sa.sa_handler = sig;
! 91: sigemptyset(&sa.sa_mask);
! 92: sigaction(SIGTERM, &sa, NULL);
! 93:
! 94: f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
! 95: if (f == -1)
! 96: return 1;
! 97: sin.sin_len = sizeof sin;
! 98: sin.sin_family = AF_INET;
! 99: sin.sin_port = htons(2345);
! 100: sin.sin_addr.s_addr = INADDR_ANY;
! 101: if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
! 102: return 1;
! 103:
! 104: fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
! 105: if (fd == -1)
! 106: return 1;
! 107: printf("fd=%d\n", fd);
! 108:
! 109: root = schedBegin();
! 110: if (!root) {
! 111: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 112: return 1;
! 113: }
! 114:
! 115: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
! 116: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 117: return 4;
! 118: } else
! 119: ts.tv_sec = 15;
! 120: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
! 121: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 122: return 4;
! 123: } else
! 124: ts.tv_sec = 10;
! 125:
! 126: if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
! 127: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 128: return 2;
! 129: }
! 130:
! 131: if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
! 132: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 133: return 3;
! 134: }
! 135:
! 136: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
! 137: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 138: return 4;
! 139: }
! 140:
! 141: if (!schedRead(root, r, "rrr", f, NULL, 0)) {
! 142: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 143: return 5;
! 144: }
! 145:
! 146: if (!schedWrite(root, w, "www", f, NULL, 0)) {
! 147: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 148: return 6;
! 149: }
! 150:
! 151: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
! 152: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 153: return 7;
! 154: }
! 155: if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
! 156: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 157: return 7;
! 158: }
! 159: if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
! 160: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 161: return 7;
! 162: }
! 163: if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
! 164: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 165: return 7;
! 166: }
! 167: printf("try to cancel tid = %lx\n", TASK_VAL(t));
! 168: schedCancel(t);
! 169: if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
! 170: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 171: return 7;
! 172: }
! 173: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
! 174: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 175: return 7;
! 176: }
! 177: if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
! 178: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 179: return 7;
! 180: }
! 181: sleep(1);
! 182: schedCancel(t);
! 183:
! 184: schedCallOnce(root, once, "000000", 42, NULL, 0);
! 185:
! 186: // schedPolling(root, &p, NULL);
! 187: schedRun(root, &Kill);
! 188: schedEnd(&root);
! 189:
! 190: close(fd);
! 191: close(f);
! 192: return 0;
! 193: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>