File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_basic.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 16:58:33 2014 UTC (10 years, 5 months ago) by misho
Branches: MAIN
CVS tags: sched5_2, sched5_1, SCHED5_1, SCHED5_0, HEAD
version 5.0

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

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