File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_basic.c
Revision 1.2.2.1: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 16:48:14 2014 UTC (10 years, 5 months ago) by misho
Branches: sched5_0
Diff to: branchpoint 1.2: preferred, unified
add new things for ut

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

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