File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_basic.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Jun 5 22:37:29 2014 UTC (9 years, 11 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, sched7_4, sched7_3, sched7_2, sched7_1, sched7_0, sched6_9, sched6_8, sched6_7, sched6_6, sched6_5, sched6_4, sched6_3, sched6_2, sched6_1, sched6_0, SCHED8_2, SCHED8_1, SCHED8_0, SCHED7_9, SCHED7_8, SCHED7_7, SCHED7_6, SCHED7_5, SCHED7_4, SCHED7_3, SCHED7_2, SCHED7_1, SCHED7_0, SCHED6_9, SCHED6_8, SCHED6_7, SCHED6_6, SCHED6_5, SCHED6_4, SCHED6_3, SCHED6_2, SCHED6_1, SCHED6_0, SCHED5_2, HEAD
version 5.2

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

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