File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.15: download - view: text, annotated - select for diffs - revision graph
Sat Feb 25 15:55:00 2023 UTC (16 months ago) by misho
Branches: MAIN
CVS tags: sched8_3, sched8_2, sched8_1, sched8_0, sched7_9, sched7_8, sched7_7, sched7_6, sched7_5, SCHED8_2, SCHED8_1, SCHED8_0, SCHED7_9, SCHED7_8, SCHED7_7, SCHED7_6, SCHED7_5, SCHED7_4, HEAD
version 7.4

    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[1];
   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:: %ld bytes wait\n", (long) TASK_RET(arg));
   40: 	Kill[0]++;
   41: 	taskExit(arg, NULL);
   42: }
   43: 
   44: void *w(sched_task_t *arg)
   45: {
   46: 	printf("write:: %ld bytes wait\n", (long) 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("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  121: 	taskExit(arg, 42);
  122: }
  123: 
  124: void *thr4kill(sched_task_t *arg)
  125: {
  126: 	char blah[BUFSIZ];
  127: 
  128: 	printf("THREAD!!! tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  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: 
  135: void sig(int s)
  136: {
  137: 	switch (s) {
  138: 		case SIGTERM:
  139: 		case SIGINT:
  140: 			printf("I'm in switch case %d\n", s);
  141: 			Kill[0]++;
  142: 			break;
  143: 		case SIGHUP:
  144: 			printf("Test SIGHUP\n");
  145: 			break;
  146: 	}
  147: }
  148: 
  149: void *sigt(sched_task_t *arg)
  150: {
  151: 	int s = TASK_VAL(arg);
  152: 
  153: 	printf("Received signal #%d\n", s);
  154: 
  155: 	sig(s);
  156: 
  157: 	schedSignalSelf(arg);
  158: 	taskExit(arg, NULL);
  159: }
  160: 
  161: int
  162: main(int argc, char **argv)
  163: {
  164: 	sched_root_task_t *root;
  165: 	int f, fd;
  166: 	struct sockaddr_in sin;
  167: 	struct timespec ts = { 20, 0 };
  168: 	struct timespec p = { 0, 10000000 };
  169: //	struct sigaction sa;
  170: 	sched_task_t *t;
  171: #ifdef AIO_SUPPORT
  172: 	char *ole = malloc(BUFSIZ);
  173: 	register int i;
  174: #endif
  175: 
  176: 	/*
  177: 	sa.sa_handler = sig;
  178: 	sigemptyset(&sa.sa_mask);
  179: 	sigaction(SIGTERM, &sa, NULL);
  180: 	*/
  181: 
  182: 	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  183: 	if (f == -1)
  184: 		return 1;
  185: #ifndef __linux__
  186: 	sin.sin_len = sizeof sin;
  187: #endif
  188: 	sin.sin_family = AF_INET;
  189: 	sin.sin_port = htons(2345);
  190: 	sin.sin_addr.s_addr = INADDR_ANY;
  191: 	if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
  192: 		return 1;
  193: 
  194: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  195: 	if (fd == -1)
  196: 		return 1;
  197: 	printf("fd=%d\n", fd);
  198: 
  199: 	root = schedBegin();
  200: 	if (!root) {
  201: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  202: 		return 1;
  203: 	}
  204: 
  205: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  206: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  207: 		return 4;
  208: 	} else
  209: 		ts.tv_sec = 15;
  210: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  211: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  212: 		return 4;
  213: 	} else
  214: 		ts.tv_sec = 10;
  215: 
  216: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  217: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  218: 		return 2;
  219: 	}
  220: 
  221: 	if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
  222: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  223: 		return 3;
  224: 	}
  225: 
  226: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  227: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  228: 		return 4;
  229: 	}
  230: 
  231: 	if (!(t = schedRead(root, r, "rrr_test", f, NULL, 0))) {
  232: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  233: 		return 5;
  234: 	}
  235: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  236: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  237: 		return 5;
  238: 	}
  239: 	schedCancel(t);
  240: 
  241: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  242: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  243: 		return 6;
  244: 	}
  245: 
  246: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  247: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  248: 		return 7;
  249: 	}
  250: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
  251: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  252: 		printf("stack is too small\n");
  253: 	}
  254: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
  255: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  256: 		return 7;
  257: 	}
  258: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
  259: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  260: 		return 7;
  261: 	}
  262: 	printf("~~~try to cancel tid = %lx\n", TASK_VAL(t));
  263: 	schedCancel(t);
  264: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
  265: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  266: 		return 7;
  267: 	}
  268: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  269: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  270: 		return 7;
  271: 	}
  272: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
  273: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  274: 		printf("stack is too small\n");
  275: 	}
  276: 	sleep(1);
  277: 	schedCancel(t);
  278: 
  279: #ifdef AIO_SUPPORT
  280: 	memset(ole, 0, BUFSIZ);
  281: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
  282: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  283: 
  284: 
  285: 	iv[0].iov_len = 5;
  286: 	iv[1].iov_len = 2;
  287: 	iv[2].iov_len = 50;
  288: 	for (i = 0; i < 3; i++)
  289: 		iv[i].iov_base = malloc(iv[i].iov_len);
  290: 	if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
  291: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  292: 	fsync(fd);
  293: 	for (i = 0; i < 3; i++) {
  294: 		wiv[i].iov_len = 100;
  295: 		wiv[i].iov_base = malloc(wiv[i].iov_len);
  296: 	}
  297: 	strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
  298: 	wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
  299: 	strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
  300: 	wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
  301: 	strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
  302: 	wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
  303: 	if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
  304: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  305: 
  306: 	for (i = 0; i < 3; i++) {
  307: 		riv[i].iov_len = 5;
  308: 		riv[i].iov_base = malloc(riv[i].iov_len + 1);
  309: 		memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
  310: 	}
  311: 	if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
  312: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  313: #endif
  314: 
  315: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  316: 
  317: 	printf("add signals\n");
  318: 	schedSignal(root, sigt, NULL, SIGHUP, NULL, 0);
  319: 	schedSignal(root, sigt, NULL, SIGTERM, NULL, 0);
  320: 	schedSignal(root, sigt, NULL, SIGINT, NULL, 0);
  321: 
  322: 	schedPolling(root, &p, NULL);
  323: 	schedSignalDispatch(root, 42);
  324: 	schedRun(root, Kill);
  325: 	schedSignalDispatch(root, 0);
  326: 	schedEnd(&root);
  327: 	sleep(1);
  328: 
  329: #ifdef AIO_SUPPORT
  330: 	for (i = 0; i < 3; i++)
  331: 		free(iv[i].iov_base);
  332: #endif
  333: 
  334: 	close(fd);
  335: 	close(f);
  336: 	return 0;
  337: }

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