File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test_time.c
Revision 1.13.12.1: download - view: text, annotated - select for diffs - revision graph
Wed May 21 21:35:07 2014 UTC (10 years, 1 month ago) by misho
Branches: sched5_2
Diff to: branchpoint 1.13: preferred, unified
fix unit tests for linux

    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/stat.h>
    8: #include <sys/signal.h>
    9: #include <netinet/in.h>
   10: #include <aitsched.h>
   11: 
   12: intptr_t Kill;
   13: sched_root_task_t *root;
   14: 
   15: void *event(sched_task_t *arg)
   16: {
   17: 	printf("Event::\n");
   18: 	taskExit(arg, NULL);
   19: }
   20: 
   21: void *regular(sched_task_t *arg)
   22: {
   23: 	printf("Task(%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
   24: 	fflush(stdout);
   25: 	taskExit(arg, NULL);
   26: }
   27: 
   28: void *timer(sched_task_t *arg)
   29: {
   30: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   31: 	taskExit(arg, NULL);
   32: }
   33: 
   34: void *rtc(sched_task_t *arg)
   35: {
   36: 	printf("RTC %p sec:: rtc id %lx signo=%lu\n", TASK_ARG(arg), (long) TASK_DATA(arg), TASK_VAL(arg));
   37: 	taskExit(arg, NULL);
   38: }
   39: 
   40: void *alarmz(sched_task_t *arg)
   41: {
   42: 	printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
   43: 	taskExit(arg, NULL);
   44: }
   45: 
   46: void *node(sched_task_t *arg)
   47: {
   48: 	printf("Node %s data %ld fflags 0x%lX\n", (char*) TASK_ARG(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
   49: 	taskExit(arg, NULL);
   50: }
   51: 
   52: void *proc(sched_task_t *arg)
   53: {
   54: 	printf("Proc pid=%ld data %ld fflags 0x%lX\n", TASK_VAL(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
   55: 	taskExit(arg, NULL);
   56: }
   57: 
   58: void *sigz(sched_task_t *arg)
   59: {
   60: 	printf("Signal signal=%ld how many times %ld\n", TASK_VAL(arg), (long) TASK_RET(arg));
   61: 	taskExit(arg, NULL);
   62: }
   63: 
   64: #ifdef EVFILT_USER
   65: void *user(sched_task_t *arg)
   66: {
   67: 	printf("User trigger id %ld fflags %ld\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
   68: 	taskExit(arg, NULL);
   69: }
   70: #endif
   71: 
   72: void *susp1(sched_task_t *arg)
   73: {
   74: 	printf("Suspend 1 =%ld\n", TASK_VAL(arg));
   75: 	taskExit(arg, NULL);
   76: }
   77: void *susp2(sched_task_t *arg)
   78: {
   79: 	printf("Suspend 2 =%ld\n", TASK_VAL(arg));
   80: 	taskExit(arg, NULL);
   81: }
   82: void *susp3(sched_task_t *arg)
   83: {
   84: 	printf("Suspend 3 =%ld\n", TASK_VAL(arg));
   85: 	taskExit(arg, NULL);
   86: }
   87: 
   88: void *once(sched_task_t *arg)
   89: {
   90: 	printf("once::\n");
   91: 	taskExit(arg, NULL);
   92: }
   93: 
   94: void *thr(sched_task_t *arg)
   95: {
   96: 	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
   97: 	printf("tid (%lu):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
   98: 	taskExit(arg, 42);
   99: }
  100: 
  101: void *thr4kill(sched_task_t *arg)
  102: {
  103: 	char blah[BUFSIZ];
  104: 
  105: 	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  106: 
  107: 	read(0, blah, sizeof blah);
  108: 	printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
  109: 	taskExit(arg, 0);
  110: }
  111: 
  112: void sig(int s)
  113: {
  114: 	switch (s) {
  115: 		case SIGINT:
  116: 		case SIGTERM:
  117: 			Kill++;
  118: 			break;
  119: 		case SIGUSR1:
  120: 			schedResumeby(root, CRITERIA_ID, (void*) 0);
  121: 			schedResumeby(root, CRITERIA_ID, (void*) 7);
  122: 			break;
  123: 	}
  124: }
  125: 
  126: int
  127: main(int argc, char **argv)
  128: {
  129: 	struct timespec ts = { 20, 0 };
  130: //	struct timespec p = { 0, 10000000 };
  131: 	int f = 0;
  132: 	struct sigaction sa;
  133: 	sched_task_t *t;
  134: #ifdef EVFILT_USER
  135: 	sched_task_t *tt[4];
  136: #endif
  137: 	sched_task_t *task;
  138: 
  139: 	sa.sa_handler = sig;
  140: 	sigemptyset(&sa.sa_mask);
  141: 	sigaction(SIGTERM, &sa, NULL);
  142: 	sigaction(SIGINT, &sa, NULL);
  143: 	sigaction(SIGUSR1, &sa, NULL);
  144: 
  145: 	root = schedBegin();
  146: 	if (!root) {
  147: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  148: 		return 1;
  149: 	}
  150: 
  151: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  152: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  153: 		return 4;
  154: 	}
  155: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  156: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  157: 		return 4;
  158: 	} else
  159: 		ts.tv_sec = 15;
  160: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  161: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  162: 		return 4;
  163: 	}
  164: 	printf("query by call %d\n", schedQueryby(root, taskRTC, CRITERIA_CALL, rtc));
  165: 	printf("query by any %d\n", schedQueryby(root, taskRTC, CRITERIA_ANY, NULL));
  166: 	printf("query by call in all queues %d\n", schedQueryby(root, taskMAX, 
  167: 				CRITERIA_ANY, NULL));
  168: 	printf("query by call in wrong queue %d\n", schedQueryby(root, taskTHREAD, 
  169: 				CRITERIA_CALL, rtc));
  170: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  171: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  172: 		return 4;
  173: 	} else
  174: 		ts.tv_sec = 10;
  175: 
  176: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  177: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  178: 		return 2;
  179: 	}
  180: 
  181: 	if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
  182: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  183: 		return 3;
  184: 	}
  185: 	if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
  186: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  187: 		return 3;
  188: 	}
  189: 	if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
  190: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  191: 		return 3;
  192: 	}
  193: 	if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
  194: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  195: 		return 3;
  196: 	}
  197: 
  198: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  199: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  200: 		return 4;
  201: 	}
  202: 
  203: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  204: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  205: 		return 4;
  206: 	}
  207: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  208: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  209: 		return 4;
  210: 	}
  211: 
  212: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  213: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  214: 		return 5;
  215: 	} else {
  216: 		ts.tv_sec = 3;
  217: 		ts.tv_nsec = 500000000;
  218: 	}
  219: 
  220: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  221: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  222: 		return 5;
  223: 	}
  224: 	if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 10, 0)) {
  225: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  226: 		return 5;
  227: 	}
  228: 
  229: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
  230: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  231: 		return 5;
  232: 	}
  233: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
  234: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  235: 		return 5;
  236: 	} else {
  237: 		ts.tv_sec = 0;
  238: 		ts.tv_nsec = 700000000;
  239: 	}
  240: 	if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
  241: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  242: 		return 5;
  243: 	}
  244: 
  245: #ifdef EVFILT_USER
  246: 	if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
  247: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  248: 		return 6;
  249: 	}
  250: 	if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
  251: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  252: 		return 6;
  253: 	}
  254: 	if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
  255: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  256: 		return 6;
  257: 	}
  258: 	if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
  259: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  260: 		return 6;
  261: 	}
  262: #endif
  263: 	if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
  264: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  265: 		return 6;
  266: 	}
  267: 	if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
  268: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  269: 		return 6;
  270: 	}
  271: 	printf("query by task %d\n", schedQuery(task));
  272: 	if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
  273: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  274: 		return 6;
  275: 	}
  276: 
  277: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  278: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  279: 		return 7;
  280: 	}
  281: 	printf("query by task %d\n", schedQuery(t));
  282: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 131072, NULL, 0)) {
  283: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  284: 		return 7;
  285: 	}
  286: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 0, NULL, 0)) {
  287: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  288: 		return 7;
  289: 	}
  290: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 131072, NULL, 0)) {
  291: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  292: 		return 7;
  293: 	}
  294: 	printf("try to cancel tid = %lx\n", TASK_VAL(t));
  295: 	schedCancel(t);
  296: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 131072 * 2, NULL, 0)) {
  297: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  298: 		return 7;
  299: 	}
  300: 	if (!(t = schedThread(root, thr4kill, "0aaaa", /*4096*/0, NULL, 0))) {
  301: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  302: 		return 7;
  303: 	}
  304: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 0, NULL, 0)) {
  305: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  306: 		return 7;
  307: 	}
  308: 	sleep(1);
  309: 	schedCancel(t);
  310: 
  311: 	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 2, NULL);
  312: //	schedCancelby(root, taskRTC, CRITERIA_DATA, (void*) 10, NULL);
  313: 
  314: 	if (argc > 1)
  315: 		if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
  316: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  317: 			return 7;
  318: 		}
  319: 	if (argc > 2) {
  320: 		f = open(argv[2], O_RDWR);
  321: 		if (f == -1) {
  322: 			perror("open()");
  323: 			return 8;
  324: 		}
  325: 		if (!schedNode(root, node, argv[2], f, 0, 0)) {
  326: 			printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  327: 			close(f);
  328: 			return 8;
  329: 		}
  330: 	}
  331: 
  332: #ifdef EVFILT_USER
  333: 	schedTrigger(tt[3]);
  334: 	schedTrigger(tt[1]);
  335: #endif
  336: 	schedResumeby(root, CRITERIA_DATA, task);
  337: 
  338: 	if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
  339: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  340: 		close(f);
  341: 		return 9;
  342: 	}
  343: 
  344: #ifdef EVFILT_USER
  345: 	schedTrigger(tt[2]);
  346: 	schedTrigger(tt[0]);
  347: #endif
  348: 
  349: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  350: 
  351: 	printf("read_queue=%d timer_queue=%d\n", 
  352: 			ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
  353: 
  354: //	schedPolling(root, &p, NULL);
  355: 	schedRun(root, &Kill);
  356: 	schedEnd(&root);
  357: 
  358: 	if (f > 2)
  359: 		close(f);
  360: 	return 0;
  361: }

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