File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_brut.c
Revision 1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Aug 26 15:01:07 2013 UTC (10 years, 10 months ago) by misho
Branches: sched4_2
add new ut

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

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