File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.8.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Aug 23 02:17:48 2012 UTC (11 years, 10 months ago) by misho
Branches: sched3_3
Diff to: branchpoint 1.8: preferred, unified
add new UT and check with memory leaks

    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 SIGTERM:
  111: 			Kill++;
  112: 			break;
  113: 		case SIGUSR1:
  114: 			schedResumeby(root, CRITERIA_ID, (void*) 0);
  115: 			schedResumeby(root, CRITERIA_ID, (void*) 7);
  116: 			break;
  117: 	}
  118: }
  119: 
  120: int
  121: main(int argc, char **argv)
  122: {
  123: 	struct timespec ts = { 20, 0 };
  124: //	struct timespec p = { 0, 10000000 };
  125: 	int f = 0;
  126: 	struct sigaction sa;
  127: 	sched_task_t *t;
  128: #ifdef EVFILT_USER
  129: 	sched_task_t *tt[4];
  130: #endif
  131: 	sched_task_t *task;
  132: 
  133: 	sa.sa_handler = sig;
  134: 	sigemptyset(&sa.sa_mask);
  135: 	sigaction(SIGTERM, &sa, NULL);
  136: 	sigaction(SIGUSR1, &sa, NULL);
  137: 
  138: 	root = schedBegin();
  139: 	if (!root) {
  140: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  141: 		return 1;
  142: 	}
  143: 
  144: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  145: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  146: 		return 4;
  147: 	} else
  148: 		ts.tv_sec = 15;
  149: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  150: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  151: 		return 4;
  152: 	} else
  153: 		ts.tv_sec = 10;
  154: 
  155: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  156: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  157: 		return 2;
  158: 	}
  159: 
  160: 	if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
  161: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  162: 		return 3;
  163: 	}
  164: 	if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
  165: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  166: 		return 3;
  167: 	}
  168: 	if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
  169: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  170: 		return 3;
  171: 	}
  172: 	if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
  173: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  174: 		return 3;
  175: 	}
  176: 
  177: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  178: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  179: 		return 4;
  180: 	}
  181: 
  182: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  183: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  184: 		return 5;
  185: 	} else {
  186: 		ts.tv_sec = 3;
  187: 		ts.tv_nsec = 500000000;
  188: 	}
  189: 
  190: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  191: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  192: 		return 5;
  193: 	}
  194: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  195: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  196: 		return 5;
  197: 	} else {
  198: 		ts.tv_sec = 0;
  199: 		ts.tv_nsec = 700000000;
  200: 	}
  201: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  202: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  203: 		return 5;
  204: 	}
  205: 
  206: #ifdef EVFILT_USER
  207: 	if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  208: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  209: 		return 6;
  210: 	}
  211: 	if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  212: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  213: 		return 6;
  214: 	}
  215: 	if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  216: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  217: 		return 6;
  218: 	}
  219: 	if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  220: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  221: 		return 6;
  222: 	}
  223: #endif
  224: 	if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
  225: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  226: 		return 6;
  227: 	}
  228: 	if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
  229: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  230: 		return 6;
  231: 	}
  232: 	if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
  233: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  234: 		return 6;
  235: 	}
  236: 
  237: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  238: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  239: 		return 7;
  240: 	}
  241: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 0, NULL, 0)) {
  242: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  243: 		return 7;
  244: 	}
  245: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, NULL, 0)) {
  246: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  247: 		return 7;
  248: 	}
  249: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
  250: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  251: 		return 7;
  252: 	}
  253: 	printf("try to cancel tid = %lx\n", TASK_VAL(t));
  254: 	schedCancel(t);
  255: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
  256: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  257: 		return 7;
  258: 	}
  259: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 42, NULL, 0))) {
  260: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  261: 		return 7;
  262: 	}
  263: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, NULL, 0)) {
  264: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  265: 		return 7;
  266: 	}
  267: 	sleep(1);
  268: 	schedCancel(t);
  269: 
  270: 	if (argc > 1)
  271: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  272: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  273: 			return 7;
  274: 		}
  275: 	if (argc > 2) {
  276: 		f = open(argv[2], O_RDWR);
  277: 		if (f == -1) {
  278: 			perror("open()");
  279: 			return 8;
  280: 		}
  281: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  282: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  283: 			close(f);
  284: 			return 8;
  285: 		}
  286: 	}
  287: 
  288: #ifdef EVFILT_USER
  289: 	schedTrigger(tt[3]);
  290: 	schedTrigger(tt[1]);
  291: #endif
  292: 	schedResumeby(root, CRITERIA_DATA, task);
  293: 
  294: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  295: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  296: 		close(f);
  297: 		return 9;
  298: 	}
  299: 
  300: #ifdef EVFILT_USER
  301: 	schedTrigger(tt[2]);
  302: 	schedTrigger(tt[0]);
  303: #endif
  304: 
  305: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  306: 
  307: 	printf("read_queue=%d timer_queue=%d\n", 
  308: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  309: 
  310: //	schedPolling(root, &p, NULL);
  311: 	schedRun(root, &Kill);
  312: 	schedEnd(&root);
  313: 
  314: 	if (f > 2)
  315: 		close(f);
  316: 	return 0;
  317: }

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