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 *rtc(sched_task_t *arg)
36: {
37: printf("RTC %p sec:: rtc id %lx\n", TASK_ARG(arg), (long) TASK_DATA(arg));
38: taskExit(arg, NULL);
39: }
40:
41: void *alarmz(sched_task_t *arg)
42: {
43: printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
44: taskExit(arg, NULL);
45: }
46:
47: void *node(sched_task_t *arg)
48: {
49: printf("Node %s data %ld fflags 0x%X\n", (char*) TASK_ARG(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
50: taskExit(arg, NULL);
51: }
52:
53: void *proc(sched_task_t *arg)
54: {
55: printf("Proc pid=%ld data %ld fflags 0x%X\n", TASK_VAL(arg), (long) TASK_RET(arg), TASK_FLAG(arg));
56: taskExit(arg, NULL);
57: }
58:
59: void *sigz(sched_task_t *arg)
60: {
61: printf("Signal signal=%ld how many times %ld\n", TASK_VAL(arg), (long) TASK_RET(arg));
62: taskExit(arg, NULL);
63: }
64:
65: #ifdef EVFILT_USER
66: void *user(sched_task_t *arg)
67: {
68: printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
69: taskExit(arg, NULL);
70: }
71: #endif
72:
73: void *susp1(sched_task_t *arg)
74: {
75: printf("Suspend 1 =%ld\n", TASK_VAL(arg));
76: taskExit(arg, NULL);
77: }
78: void *susp2(sched_task_t *arg)
79: {
80: printf("Suspend 2 =%ld\n", TASK_VAL(arg));
81: taskExit(arg, NULL);
82: }
83: void *susp3(sched_task_t *arg)
84: {
85: printf("Suspend 3 =%ld\n", TASK_VAL(arg));
86: taskExit(arg, NULL);
87: }
88:
89: void *once(sched_task_t *arg)
90: {
91: printf("once::\n");
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);
111: }
112:
113: void sig(int s)
114: {
115: switch (s) {
116: case SIGINT:
117: case SIGTERM:
118: Kill++;
119: break;
120: case SIGUSR1:
121: schedResumeby(root, CRITERIA_ID, (void*) 0);
122: schedResumeby(root, CRITERIA_ID, (void*) 7);
123: break;
124: }
125: }
126:
127: int
128: main(int argc, char **argv)
129: {
130: struct timespec ts = { 20, 0 };
131: // struct timespec p = { 0, 10000000 };
132: int f = 0;
133: struct sigaction sa;
134: sched_task_t *t;
135: #ifdef EVFILT_USER
136: sched_task_t *tt[4];
137: #endif
138: sched_task_t *task;
139:
140: sa.sa_handler = sig;
141: sigemptyset(&sa.sa_mask);
142: sigaction(SIGTERM, &sa, NULL);
143: sigaction(SIGINT, &sa, NULL);
144: sigaction(SIGUSR1, &sa, NULL);
145:
146: root = schedBegin();
147: if (!root) {
148: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
149: return 1;
150: }
151:
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: }
156: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
157: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
158: return 4;
159: } else
160: ts.tv_sec = 15;
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: }
165: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
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:
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)) {
189: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
190: return 3;
191: }
192:
193: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
194: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
195: return 4;
196: }
197:
198: /*
199: if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
200: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
201: return 4;
202: }
203: if (!schedRTC(root, rtc, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
204: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
205: return 4;
206: }
207: */
208:
209: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
210: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
211: return 5;
212: } else {
213: ts.tv_sec = 3;
214: ts.tv_nsec = 500000000;
215: }
216:
217: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
218: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
219: return 5;
220: }
221: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
222: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
223: return 5;
224: } else {
225: ts.tv_sec = 0;
226: ts.tv_nsec = 700000000;
227: }
228: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
229: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
230: return 5;
231: }
232:
233: #ifdef EVFILT_USER
234: if (!(tt[0] = schedUser(root, user, NULL, 42, 0, 0))) {
235: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
236: return 6;
237: }
238: if (!(tt[1] = schedUser(root, user, NULL, 1, 0, 73))) {
239: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
240: return 6;
241: }
242: if (!(tt[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
243: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
244: return 6;
245: }
246: if (!(tt[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
247: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
248: return 6;
249: }
250: #endif
251: if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
252: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
253: return 6;
254: }
255: if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
256: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
257: return 6;
258: }
259: if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
260: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
261: return 6;
262: }
263:
264: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, 0, NULL, 0))) {
265: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
266: return 7;
267: }
268: if (!schedThread(root, thr, "mdaaaa this is thread task", 0, 131072, NULL, 0)) {
269: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
270: return 7;
271: }
272: if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, 0, NULL, 0)) {
273: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
274: return 7;
275: }
276: if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, 131072, NULL, 0)) {
277: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
278: return 7;
279: }
280: printf("try to cancel tid = %lx\n", TASK_VAL(t));
281: schedCancel(t);
282: if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, 131072 * 2, NULL, 0)) {
283: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
284: return 7;
285: }
286: if (!(t = schedThread(root, thr4kill, "0aaaa", 42, /*4096*/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 -j3", 0, 0, NULL, 0)) {
291: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
292: return 7;
293: }
294: sleep(1);
295: schedCancel(t);
296:
297: if (argc > 1)
298: if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
299: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
300: return 7;
301: }
302: if (argc > 2) {
303: f = open(argv[2], O_RDWR);
304: if (f == -1) {
305: perror("open()");
306: return 8;
307: }
308: if (!schedNode(root, node, argv[2], f, 0, 0)) {
309: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
310: close(f);
311: return 8;
312: }
313: }
314:
315: #ifdef EVFILT_USER
316: schedTrigger(tt[3]);
317: schedTrigger(tt[1]);
318: #endif
319: schedResumeby(root, CRITERIA_DATA, task);
320:
321: if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
322: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
323: close(f);
324: return 9;
325: }
326:
327: #ifdef EVFILT_USER
328: schedTrigger(tt[2]);
329: schedTrigger(tt[0]);
330: #endif
331:
332: schedCallOnce(root, once, "000000", 42, NULL, 0);
333:
334: printf("read_queue=%d timer_queue=%d\n",
335: ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
336:
337: // schedPolling(root, &p, NULL);
338: schedRun(root, &Kill);
339: schedEnd(&root);
340:
341: if (f > 2)
342: close(f);
343: return 0;
344: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>