File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.9.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Aug 22 10:33:45 2012 UTC (11 years, 10 months ago) by misho
Branches: sched3_3
Diff to: branchpoint 1.9: preferred, unified
do some extension for thread tasks

    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: #ifdef AIO_SUPPORT
   16: struct iovec iv[3], wiv[3], riv[3];
   17: #endif
   18: 
   19: void *event(sched_task_t *arg)
   20: {
   21: 	printf("Event::\n");
   22: 	taskExit(arg, NULL);
   23: }
   24: 
   25: void *regular(sched_task_t *arg)
   26: {
   27: 	printf("Task::\n");
   28: 	taskExit(arg, NULL);
   29: }
   30: 
   31: void *timer(sched_task_t *arg)
   32: {
   33: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   34: 	taskExit(arg, NULL);
   35: }
   36: 
   37: void *r(sched_task_t *arg)
   38: {
   39: 	printf("read:: %d bytes wait\n", TASK_RET(arg));
   40: 	Kill++;
   41: 	taskExit(arg, NULL);
   42: }
   43: 
   44: void *w(sched_task_t *arg)
   45: {
   46: 	printf("write:: %d bytes wait\n", TASK_RET(arg));
   47: 	taskExit(arg, NULL);
   48: }
   49: 
   50: void *once(sched_task_t *arg)
   51: {
   52: 	printf("once::\n");
   53: 	taskExit(arg, NULL);
   54: }
   55: 
   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);
   68: 	taskExit(arg, NULL);
   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));
   86: 	taskExit(arg, NULL);
   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: 
  100: 	taskExit(arg, NULL);
  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: 
  114: 	taskExit(arg, NULL);
  115: }
  116: #endif
  117: 
  118: void *thr(sched_task_t *arg)
  119: {
  120: 	printf("thread(%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
  121: 	taskExit(arg, 42);
  122: }
  123: 
  124: void *thr4kill(sched_task_t *arg)
  125: {
  126: 	sleep(3);
  127: 	printf("never see!!! (%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
  128: 	taskExit(arg, 0);
  129: }
  130: 
  131: void sig(int s)
  132: {
  133: 	switch (s) {
  134: 		case SIGTERM:
  135: 			Kill++;
  136: 			break;
  137: 	}
  138: }
  139: 
  140: int
  141: main(int argc, char **argv)
  142: {
  143: 	sched_root_task_t *root;
  144: 	int f, fd;
  145: 	struct sockaddr_in sin;
  146: 	struct timespec ts = { 20, 0 };
  147: //	struct timespec p = { 0, 10000000 };
  148: 	struct sigaction sa;
  149: 	sched_task_t *t;
  150: #ifdef AIO_SUPPORT
  151: 	char *ole = malloc(BUFSIZ);
  152: 	register int i;
  153: #endif
  154: 
  155: 	sa.sa_handler = sig;
  156: 	sigemptyset(&sa.sa_mask);
  157: 	sigaction(SIGTERM, &sa, NULL);
  158: 
  159: 	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  160: 	if (f == -1)
  161: 		return 1;
  162: 	sin.sin_len = sizeof sin;
  163: 	sin.sin_family = AF_INET;
  164: 	sin.sin_port = htons(2345);
  165: 	sin.sin_addr.s_addr = INADDR_ANY;
  166: 	if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
  167: 		return 1;
  168: 
  169: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  170: 	if (fd == -1)
  171: 		return 1;
  172: 	printf("fd=%d\n", fd);
  173: 
  174: 	root = schedBegin();
  175: 	if (!root) {
  176: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  177: 		return 1;
  178: 	}
  179: 
  180: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  181: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  182: 		return 4;
  183: 	} else
  184: 		ts.tv_sec = 15;
  185: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  186: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  187: 		return 4;
  188: 	} else
  189: 		ts.tv_sec = 10;
  190: 
  191: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  192: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  193: 		return 2;
  194: 	}
  195: 
  196: 	if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
  197: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  198: 		return 3;
  199: 	}
  200: 
  201: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  202: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  203: 		return 4;
  204: 	}
  205: 
  206: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  207: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  208: 		return 5;
  209: 	}
  210: 
  211: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  212: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  213: 		return 6;
  214: 	}
  215: 
  216: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 0, NULL, 0)) {
  217: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  218: 		return 7;
  219: 	}
  220: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  221: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  222: 		return 7;
  223: 	}
  224: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, NULL, 0)) {
  225: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  226: 		return 7;
  227: 	}
  228: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
  229: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  230: 		return 7;
  231: 	}
  232: 	schedCancel(t);
  233: 
  234: #ifdef AIO_SUPPORT
  235: 	memset(ole, 0, BUFSIZ);
  236: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
  237: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  238: 
  239: 
  240: 	iv[0].iov_len = 5;
  241: 	iv[1].iov_len = 2;
  242: 	iv[2].iov_len = 50;
  243: 	for (i = 0; i < 3; i++)
  244: 		iv[i].iov_base = malloc(iv[i].iov_len);
  245: 	if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
  246: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  247: 	fsync(fd);
  248: 	for (i = 0; i < 3; i++) {
  249: 		wiv[i].iov_len = 100;
  250: 		wiv[i].iov_base = malloc(wiv[i].iov_len);
  251: 	}
  252: 	strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
  253: 	wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
  254: 	strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
  255: 	wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
  256: 	strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
  257: 	wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
  258: 	if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
  259: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  260: 
  261: 	for (i = 0; i < 3; i++) {
  262: 		riv[i].iov_len = 5;
  263: 		riv[i].iov_base = malloc(riv[i].iov_len + 1);
  264: 		memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
  265: 	}
  266: 	if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
  267: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  268: #endif
  269: 
  270: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  271: 
  272: //	schedPolling(root, &p, NULL);
  273: 	schedRun(root, &Kill);
  274: 	schedEnd(&root);
  275: 
  276: #ifdef AIO_SUPPORT
  277: 	for (i = 0; i < 3; i++)
  278: 		free(iv[i].iov_base);
  279: #endif
  280: 
  281: 	close(fd);
  282: 	close(f);
  283: 	return 0;
  284: }

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