File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_brut.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon Aug 26 18:48:23 2013 UTC (10 years, 10 months ago) by misho
Branches: MAIN
CVS tags: sched4_3, SCHED4_2, HEAD
version 4.2

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <unistd.h>
    4: #include <fcntl.h>
    5: #include <signal.h>
    6: #include <sys/types.h>
    7: #include <sys/event.h>
    8: #include <sys/stat.h>
    9: #include <sys/signal.h>
   10: #include <netinet/in.h>
   11: #include <aitsched.h>
   12: 
   13: intptr_t Kill;
   14: sched_root_task_t *root;
   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(%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
   25: 	fflush(stdout);
   26: 	taskExit(arg, NULL);
   27: }
   28: 
   29: void *timer(sched_task_t *arg)
   30: {
   31: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   32: 	taskExit(arg, NULL);
   33: }
   34: 
   35: void *rtc(sched_task_t *arg)
   36: {
   37: 	printf("RTC %p sec:: rtc id %lx signo=%lu\n", TASK_ARG(arg), (long) TASK_DATA(arg), TASK_VAL(arg));
   38: 	taskExit(arg, NULL);
   39: }
   40: 
   41: void *rtcinf(sched_task_t *arg)
   42: {
   43: 	printf("RTC(inf) sec:: rtc id %lx signo=%lu\n", (long) TASK_DATA(arg), TASK_VAL(arg));
   44: 	schedRTCSelf(arg);
   45: 	taskExit(arg, NULL);
   46: }
   47: 
   48: void *alarmz(sched_task_t *arg)
   49: {
   50: 	printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
   51: 	taskExit(arg, NULL);
   52: }
   53: 
   54: void *node(sched_task_t *arg)
   55: {
   56: 	printf("Node %s data %ld fflags 0x%lX\n", (char*) TASK_ARG(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
   57: 	taskExit(arg, NULL);
   58: }
   59: 
   60: void *proc(sched_task_t *arg)
   61: {
   62: 	printf("Proc pid=%ld data %ld fflags 0x%lX\n", TASK_VAL(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
   63: 	taskExit(arg, NULL);
   64: }
   65: 
   66: void *sigz(sched_task_t *arg)
   67: {
   68: 	printf("Signal signal=%ld how many times %ld\n", TASK_VAL(arg), (long) TASK_RET(arg));
   69: 	taskExit(arg, NULL);
   70: }
   71: 
   72: #ifdef EVFILT_USER
   73: void *user(sched_task_t *arg)
   74: {
   75: 	printf("User trigger id %ld fflags %ld\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
   76: 	taskExit(arg, NULL);
   77: }
   78: #endif
   79: 
   80: void *susp1(sched_task_t *arg)
   81: {
   82: 	printf("Suspend 1 =%ld\n", TASK_VAL(arg));
   83: 	taskExit(arg, NULL);
   84: }
   85: void *susp2(sched_task_t *arg)
   86: {
   87: 	printf("Suspend 2 =%ld\n", TASK_VAL(arg));
   88: 	taskExit(arg, NULL);
   89: }
   90: void *susp3(sched_task_t *arg)
   91: {
   92: 	printf("Suspend 3 =%ld\n", TASK_VAL(arg));
   93: 	taskExit(arg, NULL);
   94: }
   95: 
   96: void *once(sched_task_t *arg)
   97: {
   98: 	printf("once::\n");
   99: 	taskExit(arg, NULL);
  100: }
  101: 
  102: void *thr(sched_task_t *arg)
  103: {
  104: 	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  105: 	printf("tid (%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
  106: 	fflush(stdout);
  107: 	taskExit(arg, 42);
  108: }
  109: 
  110: void *thrinf(sched_task_t *arg)
  111: {
  112: 	printf("tid INF (%lu):: %s %s\n", TASK_VAL(arg), __func__, (char*) TASK_ARG(arg));
  113: 	fflush(stdout);
  114: 	schedThreadSelf(arg);
  115: 	taskExit(arg, 42);
  116: }
  117: 
  118: void *thr4kill(sched_task_t *arg)
  119: {
  120: 	char blah[BUFSIZ];
  121: 
  122: 	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  123: 
  124: 	read(0, blah, sizeof blah);
  125: 	if (arg)
  126: 		printf("never see!!! (%lx):: %s (%d == %d)\n", arg ? TASK_VAL(arg) : 0, 
  127: 				(char*) arg ? TASK_ARG(arg) : NULL, arg ? TASK_TYPE(arg) : -1, taskTHREAD);
  128: 	taskExit(arg, 0);
  129: }
  130: 
  131: void sig(int s)
  132: {
  133: 	switch (s) {
  134: 		case SIGINT:
  135: 		case SIGTERM:
  136: 			Kill++;
  137: 			break;
  138: 		case SIGUSR1:
  139: 			schedResumeby(root, CRITERIA_ID, (void*) 0);
  140: 			schedResumeby(root, CRITERIA_ID, (void*) 7);
  141: 			break;
  142: 	}
  143: }
  144: 
  145: int
  146: main(int argc, char **argv)
  147: {
  148: 	struct timespec ts = { 20, 0 };
  149: //	struct timespec p = { 0, 10000000 };
  150: 	int f = 0;
  151: 	struct sigaction sa;
  152: 	sched_task_t *t;
  153: #ifdef EVFILT_USER
  154: 	sched_task_t *tt[4];
  155: #endif
  156: 	sched_task_t *task;
  157: 
  158: 	sa.sa_handler = sig;
  159: 	sigemptyset(&sa.sa_mask);
  160: 	sigaction(SIGTERM, &sa, NULL);
  161: 	sigaction(SIGINT, &sa, NULL);
  162: 	sigaction(SIGUSR1, &sa, NULL);
  163: 
  164: 	root = schedBegin();
  165: 	if (!root) {
  166: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  167: 		return 1;
  168: 	}
  169: 
  170: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  171: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  172: 		return 4;
  173: 	}
  174: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  175: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  176: 		return 4;
  177: 	} else
  178: 		ts.tv_sec = 15;
  179: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  180: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  181: 		return 4;
  182: 	}
  183: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  184: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  185: 		return 4;
  186: 	} else
  187: 		ts.tv_sec = 10;
  188: 
  189: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  190: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  191: 		return 2;
  192: 	}
  193: 
  194: 	if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
  195: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  196: 		return 3;
  197: 	}
  198: 	if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
  199: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  200: 		return 3;
  201: 	}
  202: 	if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
  203: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  204: 		return 3;
  205: 	}
  206: 	if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
  207: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  208: 		return 3;
  209: 	}
  210: 
  211: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  212: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  213: 		return 4;
  214: 	}
  215: 
  216: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  217: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  218: 		return 4;
  219: 	}
  220: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  221: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  222: 		return 4;
  223: 	}
  224: 
  225: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  226: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  227: 		return 5;
  228: 	} else {
  229: 		ts.tv_sec = 3;
  230: 		ts.tv_nsec = 500000000;
  231: 	}
  232: 
  233: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  234: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  235: 		return 5;
  236: 	}
  237: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  238: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  239: 		return 5;
  240: 	} else {
  241: 		ts.tv_sec = 1;
  242: 		ts.tv_nsec = 0;
  243: 	}
  244: 
  245: 	if (!schedRTC(root, rtcinf, (void*) (intptr_t) ts.tv_sec, ts, (void*) 12, 0)) {
  246: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  247: 		return 5;
  248: 	}
  249: 
  250: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  251: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  252: 		return 5;
  253: 	}
  254: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  255: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  256: 		return 5;
  257: 	} else {
  258: 		ts.tv_sec = 0;
  259: 		ts.tv_nsec = 700000000;
  260: 	}
  261: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  262: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  263: 		return 5;
  264: 	}
  265: 
  266: #ifdef EVFILT_USER
  267: 	if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  268: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  269: 		return 6;
  270: 	}
  271: 	if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  272: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  273: 		return 6;
  274: 	}
  275: 	if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  276: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  277: 		return 6;
  278: 	}
  279: 	if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  280: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  281: 		return 6;
  282: 	}
  283: #endif
  284: 	if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
  285: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  286: 		return 6;
  287: 	}
  288: 	if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
  289: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  290: 		return 6;
  291: 	}
  292: 	if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
  293: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  294: 		return 6;
  295: 	}
  296: 
  297: 	if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA", 0, NULL, 0)) {
  298: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  299: 		return 7;
  300: 	}
  301: 	if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA+1",  0, NULL, 0)) {
  302: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  303: 		return 7;
  304: 	}
  305: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  306: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  307: 		return 7;
  308: 	}
  309: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 131072, NULL, 0)) {
  310: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  311: 		return 7;
  312: 	}
  313: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 0, NULL, 0)) {
  314: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  315: 		return 7;
  316: 	}
  317: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 131072, NULL, 0)) {
  318: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  319: 		return 7;
  320: 	}
  321: 	printf("try to cancel tid = %lx\n", TASK_VAL(t));
  322: 	schedCancel(t);
  323: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 131072 * 2, NULL, 0)) {
  324: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  325: 		return 7;
  326: 	}
  327: 	if (!(t = schedThread(root, thr4kill, "0aaaa", /*4096*/0, NULL, 0))) {
  328: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  329: 		return 7;
  330: 	}
  331: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, NULL, 0)) {
  332: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  333: 		return 7;
  334: 	}
  335: 	sleep(1);
  336: 	schedCancel(t);
  337: 
  338: 	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 2, NULL);
  339: //	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 10, NULL);
  340: 
  341: 	if (argc > 1)
  342: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  343: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  344: 			return 7;
  345: 		}
  346: 	if (argc > 2) {
  347: 		f = open(argv[2], O_RDWR);
  348: 		if (f == -1) {
  349: 			perror("open()");
  350: 			return 8;
  351: 		}
  352: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  353: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  354: 			close(f);
  355: 			return 8;
  356: 		}
  357: 	}
  358: 
  359: #ifdef EVFILT_USER
  360: 	schedTrigger(tt[3]);
  361: 	schedTrigger(tt[1]);
  362: #endif
  363: 	schedResumeby(root, CRITERIA_DATA, task);
  364: 
  365: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  366: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  367: 		close(f);
  368: 		return 9;
  369: 	}
  370: 
  371: #ifdef EVFILT_USER
  372: 	schedTrigger(tt[2]);
  373: 	schedTrigger(tt[0]);
  374: #endif
  375: 
  376: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  377: 
  378: 	printf("read_queue=%d timer_queue=%d\n", 
  379: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  380: 
  381: //	schedPolling(root, &p, NULL);
  382: 	schedRun(root, &Kill);
  383: 	schedEnd(&root);
  384: 
  385: 	if (f > 2)
  386: 		close(f);
  387: 	return 0;
  388: }

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