File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_brut.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Mon Sep 2 11:21:22 2013 UTC (10 years, 9 months ago) by misho
Branches: MAIN
CVS tags: sched5_2, sched5_1, sched5_0, sched4_7, sched4_6, sched4_5, sched4_4, SCHED5_1, SCHED5_0, SCHED4_7, SCHED4_6, SCHED4_5, SCHED4_4, SCHED4_3, HEAD
version 4.3

    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: 				arg ? (char*) TASK_ARG(arg) : NULL, 
  128: 				arg ? TASK_TYPE(arg) : -1, taskTHREAD);
  129: 	taskExit(arg, 0);
  130: }
  131: 
  132: void sig(int s)
  133: {
  134: 	switch (s) {
  135: 		case SIGINT:
  136: 		case SIGTERM:
  137: 			Kill++;
  138: 			break;
  139: 		case SIGUSR1:
  140: 			schedResumeby(root, CRITERIA_ID, (void*) 0);
  141: 			schedResumeby(root, CRITERIA_ID, (void*) 7);
  142: 			break;
  143: 	}
  144: }
  145: 
  146: int
  147: main(int argc, char **argv)
  148: {
  149: 	struct timespec ts = { 20, 0 };
  150: //	struct timespec p = { 0, 10000000 };
  151: 	int f = 0;
  152: 	struct sigaction sa;
  153: 	sched_task_t *t;
  154: #ifdef EVFILT_USER
  155: 	sched_task_t *tt[4];
  156: #endif
  157: 	sched_task_t *task;
  158: 
  159: 	sa.sa_handler = sig;
  160: 	sigemptyset(&sa.sa_mask);
  161: 	sigaction(SIGTERM, &sa, NULL);
  162: 	sigaction(SIGINT, &sa, NULL);
  163: 	sigaction(SIGUSR1, &sa, NULL);
  164: 
  165: 	root = schedBegin();
  166: 	if (!root) {
  167: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  168: 		return 1;
  169: 	}
  170: 
  171: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  172: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  173: 		return 4;
  174: 	}
  175: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  176: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  177: 		return 4;
  178: 	} else
  179: 		ts.tv_sec = 15;
  180: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  181: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  182: 		return 4;
  183: 	}
  184: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  185: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  186: 		return 4;
  187: 	} else
  188: 		ts.tv_sec = 10;
  189: 
  190: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  191: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  192: 		return 2;
  193: 	}
  194: 
  195: 	if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
  196: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  197: 		return 3;
  198: 	}
  199: 	if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
  200: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  201: 		return 3;
  202: 	}
  203: 	if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
  204: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  205: 		return 3;
  206: 	}
  207: 	if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
  208: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  209: 		return 3;
  210: 	}
  211: 
  212: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  213: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  214: 		return 4;
  215: 	}
  216: 
  217: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  218: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  219: 		return 4;
  220: 	}
  221: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  222: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  223: 		return 4;
  224: 	}
  225: 
  226: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  227: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  228: 		return 5;
  229: 	} else {
  230: 		ts.tv_sec = 3;
  231: 		ts.tv_nsec = 500000000;
  232: 	}
  233: 
  234: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  235: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  236: 		return 5;
  237: 	}
  238: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  239: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  240: 		return 5;
  241: 	} else {
  242: 		ts.tv_sec = 1;
  243: 		ts.tv_nsec = 0;
  244: 	}
  245: 
  246: 	if (!schedRTC(root, rtcinf, (void*) (intptr_t) ts.tv_sec, ts, (void*) 12, 0)) {
  247: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  248: 		return 5;
  249: 	}
  250: 
  251: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  252: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  253: 		return 5;
  254: 	}
  255: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  256: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  257: 		return 5;
  258: 	} else {
  259: 		ts.tv_sec = 0;
  260: 		ts.tv_nsec = 700000000;
  261: 	}
  262: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  263: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  264: 		return 5;
  265: 	}
  266: 
  267: #ifdef EVFILT_USER
  268: 	if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  269: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  270: 		return 6;
  271: 	}
  272: 	if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  273: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  274: 		return 6;
  275: 	}
  276: 	if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  277: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  278: 		return 6;
  279: 	}
  280: 	if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  281: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  282: 		return 6;
  283: 	}
  284: #endif
  285: 	if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
  286: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  287: 		return 6;
  288: 	}
  289: 	if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
  290: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  291: 		return 6;
  292: 	}
  293: 	if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
  294: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  295: 		return 6;
  296: 	}
  297: 
  298: 	if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA", 0, NULL, 0)) {
  299: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  300: 		return 7;
  301: 	}
  302: 	if (!schedThread(root, thrinf, "OHOBOHO_i_BOZA+1",  0, NULL, 0)) {
  303: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  304: 		return 7;
  305: 	}
  306: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  307: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  308: 		return 7;
  309: 	}
  310: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 131072, NULL, 0)) {
  311: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  312: 		return 7;
  313: 	}
  314: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 0, NULL, 0)) {
  315: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  316: 		return 7;
  317: 	}
  318: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 131072, NULL, 0)) {
  319: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  320: 		return 7;
  321: 	}
  322: 	printf("try to cancel tid = %lx\n", TASK_VAL(t));
  323: 	schedCancel(t);
  324: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 131072 * 2, NULL, 0)) {
  325: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  326: 		return 7;
  327: 	}
  328: 	if (!(t = schedThread(root, thr4kill, "0aaaa", /*4096*/0, NULL, 0))) {
  329: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  330: 		return 7;
  331: 	}
  332: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, NULL, 0)) {
  333: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  334: 		return 7;
  335: 	}
  336: 	sleep(1);
  337: 	schedCancel(t);
  338: 
  339: 	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 2, NULL);
  340: //	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 10, NULL);
  341: 
  342: 	if (argc > 1)
  343: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  344: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  345: 			return 7;
  346: 		}
  347: 	if (argc > 2) {
  348: 		f = open(argv[2], O_RDWR);
  349: 		if (f == -1) {
  350: 			perror("open()");
  351: 			return 8;
  352: 		}
  353: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  354: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  355: 			close(f);
  356: 			return 8;
  357: 		}
  358: 	}
  359: 
  360: #ifdef EVFILT_USER
  361: 	schedTrigger(tt[3]);
  362: 	schedTrigger(tt[1]);
  363: #endif
  364: 	schedResumeby(root, CRITERIA_DATA, task);
  365: 
  366: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  367: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  368: 		close(f);
  369: 		return 9;
  370: 	}
  371: 
  372: #ifdef EVFILT_USER
  373: 	schedTrigger(tt[2]);
  374: 	schedTrigger(tt[0]);
  375: #endif
  376: 
  377: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  378: 
  379: 	printf("read_queue=%d timer_queue=%d\n", 
  380: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  381: 
  382: //	schedPolling(root, &p, NULL);
  383: 	schedRun(root, &Kill);
  384: 	schedEnd(&root);
  385: 
  386: 	if (f > 2)
  387: 		close(f);
  388: 	return 0;
  389: }

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