File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.3.2.2: download - view: text, annotated - select for diffs - revision graph
Thu May 31 15:09:18 2012 UTC (12 years, 2 months ago) by misho
Branches: sched2_2
Diff to: branchpoint 1.3: preferred, unified
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: 
   15: void *event(sched_task_t *arg)
   16: {
   17: 	printf("Event::\n");
   18: 	return NULL;
   19: }
   20: 
   21: void *eventlo(sched_task_t *arg)
   22: {
   23: 	printf("EventLOW::\n");
   24: 	return NULL;
   25: }
   26: 
   27: void *timer(sched_task_t *arg)
   28: {
   29: 	printf("Timer %d sec::\n", (int) TASK_ARG(arg));
   30: 	return NULL;
   31: }
   32: 
   33: void *alarmz(sched_task_t *arg)
   34: {
   35: 	printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
   36: 	return NULL;
   37: }
   38: 
   39: void *node(sched_task_t *arg)
   40: {
   41: 	printf("Node %s fflags 0x%X\n", (char*) TASK_ARG(arg), TASK_DATLEN(arg));
   42: 	return NULL;
   43: }
   44: 
   45: void *proc(sched_task_t *arg)
   46: {
   47: 	printf("Proc pid=%ld fflags 0x%X data %x\n", TASK_VAL(arg), TASK_DATLEN(arg), 
   48: 			(uintptr_t) TASK_DATA(arg));
   49: 	return NULL;
   50: }
   51: 
   52: void *sigz(sched_task_t *arg)
   53: {
   54: 	printf("Signal signal=%ld\n", TASK_VAL(arg));
   55: 	return NULL;
   56: }
   57: 
   58: void *user(sched_task_t *arg)
   59: {
   60: 	printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_DATLEN(arg) & NOTE_FFLAGSMASK);
   61: 	return NULL;
   62: }
   63: 
   64: void *once(sched_task_t *arg)
   65: {
   66: 	printf("once::\n");
   67: 	return NULL;
   68: }
   69: 
   70: void sig(int s)
   71: {
   72: 	switch (s) {
   73: 		case SIGTERM:
   74: 			Kill++;
   75: 			break;
   76: 		case SIGUSR1:
   77: 			break;
   78: 	}
   79: }
   80: 
   81: int
   82: main(int argc, char **argv)
   83: {
   84: 	sched_root_task_t *root;
   85: 	struct timespec ts = { 20, 0 };
   86: //	struct timespec p = { 0, 10000000 };
   87: 	int f = 0;
   88: 	struct sigaction sa;
   89: 	sched_task_t *t[4];
   90: 
   91: 	sa.sa_handler = sig;
   92: 	sigemptyset(&sa.sa_mask);
   93: 	sigaction(SIGTERM, &sa, NULL);
   94: 	sigaction(SIGUSR1, &sa, NULL);
   95: 
   96: 	root = schedBegin();
   97: 	if (!root) {
   98: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   99: 		return 1;
  100: 	}
  101: 
  102: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
  103: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  104: 		return 4;
  105: 	} else
  106: 		ts.tv_sec = 15;
  107: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
  108: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  109: 		return 4;
  110: 	} else
  111: 		ts.tv_sec = 10;
  112: 
  113: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  114: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  115: 		return 2;
  116: 	}
  117: 
  118: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  119: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  120: 		return 3;
  121: 	}
  122: 
  123: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
  124: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  125: 		return 4;
  126: 	}
  127: 
  128: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, NULL, 0)) {
  129: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  130: 		return 5;
  131: 	} else {
  132: 		ts.tv_sec = 3;
  133: 		ts.tv_nsec = 500000000;
  134: 	}
  135: 
  136: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 1, 0)) {
  137: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  138: 		return 5;
  139: 	}
  140: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 2, 0)) {
  141: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  142: 		return 5;
  143: 	} else {
  144: 		ts.tv_sec = 0;
  145: 		ts.tv_nsec = 700000000;
  146: 	}
  147: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 3, 0)) {
  148: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  149: 		return 5;
  150: 	}
  151: 
  152: 	if (!(t[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  153: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  154: 		return 6;
  155: 	}
  156: 	if (!(t[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  157: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  158: 		return 6;
  159: 	}
  160: 	if (!(t[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  161: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  162: 		return 6;
  163: 	}
  164: 	if (!(t[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  165: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  166: 		return 6;
  167: 	}
  168: 
  169: 	if (argc > 1)
  170: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  171: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  172: 			return 7;
  173: 		}
  174: 	if (argc > 2) {
  175: 		f = open(argv[2], O_RDWR);
  176: 		if (f == -1) {
  177: 			perror("open()");
  178: 			return 8;
  179: 		}
  180: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  181: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  182: 			close(f);
  183: 			return 8;
  184: 		}
  185: 	}
  186: 
  187: 	schedTrigger(t[3]);
  188: 	schedTrigger(t[1]);
  189: 
  190: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  191: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  192: 		close(f);
  193: 		return 9;
  194: 	}
  195: 
  196: 	schedTrigger(t[2]);
  197: 	schedTrigger(t[0]);
  198: 
  199: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  200: 
  201: 	printf("read_queue=%d timer_queue=%d\n", 
  202: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  203: 
  204: //	schedPolling(root, &p, NULL);
  205: 	schedRun(root, &Kill);
  206: 	schedEnd(&root);
  207: 
  208: 	if (f > 2)
  209: 		close(f);
  210: 	return 0;
  211: }

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