File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Tue Jul 24 14:06:10 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: sched2_7, sched2_6, SCHED2_6, SCHED2_5, HEAD
version 2.5

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

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