File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_basic.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 13:17:33 2014 UTC (10 years, 5 months ago) by misho
Branches: MAIN
CVS tags: sched5_0, SCHED4_7, HEAD
version 4.7

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

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