File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_basic.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 12:57:52 2014 UTC (10 years, 5 months ago) by misho
Branches: sched4_7
add fds

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

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