File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Wed Aug 8 23:04:41 2012 UTC (11 years, 10 months ago) by misho
Branches: MAIN
CVS tags: sched3_2, SCHED3_1, HEAD
version 3.1

    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: 	return 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: 	return NULL;
   27: }
   28: 
   29: void *timer(sched_task_t *arg)
   30: {
   31: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   32: 	return NULL;
   33: }
   34: 
   35: void *alarmz(sched_task_t *arg)
   36: {
   37: 	printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
   38: 	return NULL;
   39: }
   40: 
   41: void *node(sched_task_t *arg)
   42: {
   43: 	printf("Node %s fflags 0x%X\n", (char*) TASK_ARG(arg), (u_int) TASK_DATLEN(arg));
   44: 	return NULL;
   45: }
   46: 
   47: void *proc(sched_task_t *arg)
   48: {
   49: 	printf("Proc pid=%ld fflags 0x%X data %p\n", TASK_VAL(arg), (u_int) TASK_DATLEN(arg), 
   50: 			TASK_DATA(arg));
   51: 	return NULL;
   52: }
   53: 
   54: void *sigz(sched_task_t *arg)
   55: {
   56: 	printf("Signal signal=%ld\n", TASK_VAL(arg));
   57: 	return NULL;
   58: }
   59: 
   60: #ifdef EVFILT_USER
   61: void *user(sched_task_t *arg)
   62: {
   63: 	printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_DATLEN(arg) & NOTE_FFLAGSMASK);
   64: 	return NULL;
   65: }
   66: #endif
   67: 
   68: void *susp1(sched_task_t *arg)
   69: {
   70: 	printf("Suspend 1 =%ld\n", TASK_VAL(arg));
   71: 	return NULL;
   72: }
   73: void *susp2(sched_task_t *arg)
   74: {
   75: 	printf("Suspend 2 =%ld\n", TASK_VAL(arg));
   76: 	return NULL;
   77: }
   78: void *susp3(sched_task_t *arg)
   79: {
   80: 	printf("Suspend 3 =%ld\n", TASK_VAL(arg));
   81: 	return NULL;
   82: }
   83: 
   84: void *once(sched_task_t *arg)
   85: {
   86: 	printf("once::\n");
   87: 	return NULL;
   88: }
   89: 
   90: void sig(int s)
   91: {
   92: 	switch (s) {
   93: 		case SIGTERM:
   94: 			Kill++;
   95: 			break;
   96: 		case SIGUSR1:
   97: 			schedResumeby(root, CRITERIA_ID, (void*) 0);
   98: 			schedResumeby(root, CRITERIA_ID, (void*) 7);
   99: 			break;
  100: 	}
  101: }
  102: 
  103: int
  104: main(int argc, char **argv)
  105: {
  106: 	struct timespec ts = { 20, 0 };
  107: //	struct timespec p = { 0, 10000000 };
  108: 	int f = 0;
  109: 	struct sigaction sa;
  110: #ifdef EVFILT_USER
  111: 	sched_task_t *t[4];
  112: #endif
  113: 	sched_task_t *task;
  114: 
  115: 	sa.sa_handler = sig;
  116: 	sigemptyset(&sa.sa_mask);
  117: 	sigaction(SIGTERM, &sa, NULL);
  118: 	sigaction(SIGUSR1, &sa, NULL);
  119: 
  120: 	root = schedBegin();
  121: 	if (!root) {
  122: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  123: 		return 1;
  124: 	}
  125: 
  126: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  127: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  128: 		return 4;
  129: 	} else
  130: 		ts.tv_sec = 15;
  131: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  132: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  133: 		return 4;
  134: 	} else
  135: 		ts.tv_sec = 10;
  136: 
  137: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  138: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  139: 		return 2;
  140: 	}
  141: 
  142: 	if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
  143: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  144: 		return 3;
  145: 	}
  146: 	if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
  147: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  148: 		return 3;
  149: 	}
  150: 	if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
  151: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  152: 		return 3;
  153: 	}
  154: 	if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
  155: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  156: 		return 3;
  157: 	}
  158: 
  159: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  160: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  161: 		return 4;
  162: 	}
  163: 
  164: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  165: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  166: 		return 5;
  167: 	} else {
  168: 		ts.tv_sec = 3;
  169: 		ts.tv_nsec = 500000000;
  170: 	}
  171: 
  172: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  173: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  174: 		return 5;
  175: 	}
  176: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  177: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  178: 		return 5;
  179: 	} else {
  180: 		ts.tv_sec = 0;
  181: 		ts.tv_nsec = 700000000;
  182: 	}
  183: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  184: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  185: 		return 5;
  186: 	}
  187: 
  188: #ifdef EVFILT_USER
  189: 	if (!(t[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  190: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  191: 		return 6;
  192: 	}
  193: 	if (!(t[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  194: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  195: 		return 6;
  196: 	}
  197: 	if (!(t[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  198: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  199: 		return 6;
  200: 	}
  201: 	if (!(t[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  202: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  203: 		return 6;
  204: 	}
  205: #endif
  206: 	if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
  207: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  208: 		return 6;
  209: 	}
  210: 	if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
  211: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  212: 		return 6;
  213: 	}
  214: 	if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
  215: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  216: 		return 6;
  217: 	}
  218: 
  219: 	if (argc > 1)
  220: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  221: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  222: 			return 7;
  223: 		}
  224: 	if (argc > 2) {
  225: 		f = open(argv[2], O_RDWR);
  226: 		if (f == -1) {
  227: 			perror("open()");
  228: 			return 8;
  229: 		}
  230: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  231: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  232: 			close(f);
  233: 			return 8;
  234: 		}
  235: 	}
  236: 
  237: #ifdef EVFILT_USER
  238: 	schedTrigger(t[3]);
  239: 	schedTrigger(t[1]);
  240: #endif
  241: 	schedResumeby(root, CRITERIA_DATA, task);
  242: 
  243: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  244: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  245: 		close(f);
  246: 		return 9;
  247: 	}
  248: 
  249: #ifdef EVFILT_USER
  250: 	schedTrigger(t[2]);
  251: 	schedTrigger(t[0]);
  252: #endif
  253: 
  254: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  255: 
  256: 	printf("read_queue=%d timer_queue=%d\n", 
  257: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  258: 
  259: //	schedPolling(root, &p, NULL);
  260: 	schedRun(root, &Kill);
  261: 	schedEnd(&root);
  262: 
  263: 	if (f > 2)
  264: 		close(f);
  265: 	return 0;
  266: }

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