File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.3.2.1: download - view: text, annotated - select for diffs - revision graph
Thu May 31 14:45:10 2012 UTC (12 years, 1 month ago) by misho
Branches: sched2_2
Diff to: branchpoint 1.3: preferred, unified
add schedUser features

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <unistd.h>
    4: #include <fcntl.h>
    5: #include <sys/types.h>
    6: #include <sys/stat.h>
    7: #include <sys/signal.h>
    8: #include <netinet/in.h>
    9: #include <aitsched.h>
   10: 
   11: intptr_t Kill;
   12: 
   13: void *event(sched_task_t *arg)
   14: {
   15: 	printf("Event::\n");
   16: 	return NULL;
   17: }
   18: 
   19: void *eventlo(sched_task_t *arg)
   20: {
   21: 	printf("EventLOW::\n");
   22: 	return NULL;
   23: }
   24: 
   25: void *timer(sched_task_t *arg)
   26: {
   27: 	printf("Timer %d sec::\n", (int) TASK_ARG(arg));
   28: 	return NULL;
   29: }
   30: 
   31: void *alarmz(sched_task_t *arg)
   32: {
   33: 	printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
   34: 	return NULL;
   35: }
   36: 
   37: void *node(sched_task_t *arg)
   38: {
   39: 	printf("Node %s\n", (char*) TASK_ARG(arg));
   40: 	return NULL;
   41: }
   42: 
   43: void *proc(sched_task_t *arg)
   44: {
   45: 	printf("Proc pid=%ld\n", TASK_VAL(arg));
   46: 	return NULL;
   47: }
   48: 
   49: void *sigz(sched_task_t *arg)
   50: {
   51: 	printf("Signal signal=%ld\n", TASK_VAL(arg));
   52: 	return NULL;
   53: }
   54: 
   55: void *user(sched_task_t *arg)
   56: {
   57: 	printf("User trigger id %ld\n", TASK_VAL(arg));
   58: 	return NULL;
   59: }
   60: 
   61: void *once(sched_task_t *arg)
   62: {
   63: 	printf("once::\n");
   64: 	return NULL;
   65: }
   66: 
   67: void sig(int s)
   68: {
   69: 	switch (s) {
   70: 		case SIGTERM:
   71: 			Kill++;
   72: 			break;
   73: 	}
   74: }
   75: 
   76: int
   77: main(int argc, char **argv)
   78: {
   79: 	sched_root_task_t *root;
   80: 	struct timespec ts = { 20, 0 };
   81: //	struct timespec p = { 0, 10000000 };
   82: 	int f = 0;
   83: 
   84: 	signal(SIGTERM, sig);
   85: 
   86: 	root = schedBegin();
   87: 	if (!root) {
   88: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   89: 		return 1;
   90: 	}
   91: 
   92: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
   93: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   94: 		return 4;
   95: 	} else
   96: 		ts.tv_sec = 15;
   97: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
   98: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   99: 		return 4;
  100: 	} else
  101: 		ts.tv_sec = 10;
  102: 
  103: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  104: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  105: 		return 2;
  106: 	}
  107: 
  108: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  109: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  110: 		return 3;
  111: 	}
  112: 
  113: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
  114: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  115: 		return 4;
  116: 	}
  117: 
  118: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, NULL, 0)) {
  119: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  120: 		return 5;
  121: 	} else {
  122: 		ts.tv_sec = 3;
  123: 		ts.tv_nsec = 500000000;
  124: 	}
  125: 
  126: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 1, 0)) {
  127: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  128: 		return 5;
  129: 	}
  130: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 2, 0)) {
  131: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  132: 		return 5;
  133: 	} else {
  134: 		ts.tv_sec = 0;
  135: 		ts.tv_nsec = 700000000;
  136: 	}
  137: 	if (!schedAlarm(root, alarmz, (void*) ts.tv_sec, ts, (void*) 3, 0)) {
  138: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  139: 		return 5;
  140: 	}
  141: 
  142: 	if (!schedUser(root, user, NULL, 42, 0, 0)) {
  143: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  144: 		return 6;
  145: 	}
  146: 
  147: 	if (argc > 1)
  148: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  149: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  150: 			return 7;
  151: 		}
  152: 	if (argc > 2) {
  153: 		f = open(argv[2], O_RDWR);
  154: 		if (f == -1) {
  155: 			perror("open()");
  156: 			return 8;
  157: 		}
  158: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  159: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  160: 			close(f);
  161: 			return 8;
  162: 		}
  163: 	}
  164: 
  165: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  166: 
  167: 	printf("read_queue=%d timer_queue=%d\n", 
  168: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  169: 
  170: //	schedPolling(root, &p, NULL);
  171: 	schedRun(root, &Kill);
  172: 	schedEnd(&root);
  173: 
  174: 	if (f > 2)
  175: 		close(f);
  176: 	return 0;
  177: }

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