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