File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.10: download - view: text, annotated - select for diffs - revision graph
Mon Sep 10 15:07:52 2012 UTC (11 years, 9 months ago) by misho
Branches: MAIN
CVS tags: sched3_8, sched3_7, sched3_6, sched3_5, SCHED3_7, SCHED3_6, SCHED3_5, SCHED3_4, HEAD
version 3.4

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

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