Annotation of libaitsched/example/test_time.c, revision 1.8.2.1
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.8.2.1 ! 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.8.2.1 ! 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.8.2.1 ! misho 32: taskExit(arg, NULL);
1.2 misho 33: }
34:
1.3 misho 35: void *alarmz(sched_task_t *arg)
36: {
1.4 misho 37: printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
1.8.2.1 ! misho 38: taskExit(arg, NULL);
1.3 misho 39: }
40:
1.4 misho 41: void *node(sched_task_t *arg)
42: {
1.8 misho 43: printf("Node %s data %d fflags 0x%X\n", (char*) TASK_ARG(arg), TASK_RET(arg), TASK_FLAG(arg));
1.8.2.1 ! misho 44: taskExit(arg, NULL);
1.4 misho 45: }
46:
47: void *proc(sched_task_t *arg)
48: {
1.8 misho 49: printf("Proc pid=%ld data %d fflags 0x%X\n", TASK_VAL(arg), TASK_RET(arg), TASK_FLAG(arg));
1.8.2.1 ! misho 50: taskExit(arg, NULL);
1.4 misho 51: }
52:
53: void *sigz(sched_task_t *arg)
54: {
1.8 misho 55: printf("Signal signal=%ld how many times %d\n", TASK_VAL(arg), TASK_RET(arg));
1.8.2.1 ! misho 56: taskExit(arg, NULL);
1.4 misho 57: }
58:
59: #ifdef EVFILT_USER
60: void *user(sched_task_t *arg)
61: {
1.8 misho 62: printf("User trigger id %ld fflags %d\n", TASK_VAL(arg), TASK_FLAG(arg) & NOTE_FFLAGSMASK);
1.8.2.1 ! misho 63: taskExit(arg, NULL);
1.4 misho 64: }
65: #endif
66:
1.5 misho 67: void *susp1(sched_task_t *arg)
68: {
69: printf("Suspend 1 =%ld\n", TASK_VAL(arg));
1.8.2.1 ! misho 70: taskExit(arg, NULL);
1.5 misho 71: }
72: void *susp2(sched_task_t *arg)
73: {
74: printf("Suspend 2 =%ld\n", TASK_VAL(arg));
1.8.2.1 ! misho 75: taskExit(arg, NULL);
1.5 misho 76: }
77: void *susp3(sched_task_t *arg)
78: {
79: printf("Suspend 3 =%ld\n", TASK_VAL(arg));
1.8.2.1 ! misho 80: taskExit(arg, NULL);
1.5 misho 81: }
82:
1.2 misho 83: void *once(sched_task_t *arg)
84: {
85: printf("once::\n");
1.8.2.1 ! misho 86: taskExit(arg, NULL);
1.2 misho 87: }
88:
89: void sig(int s)
90: {
91: switch (s) {
92: case SIGTERM:
93: Kill++;
94: break;
1.4 misho 95: case SIGUSR1:
1.5 misho 96: schedResumeby(root, CRITERIA_ID, (void*) 0);
97: schedResumeby(root, CRITERIA_ID, (void*) 7);
1.4 misho 98: break;
1.2 misho 99: }
100: }
101:
102: int
103: main(int argc, char **argv)
104: {
105: struct timespec ts = { 20, 0 };
106: // struct timespec p = { 0, 10000000 };
1.4 misho 107: int f = 0;
108: struct sigaction sa;
109: #ifdef EVFILT_USER
110: sched_task_t *t[4];
111: #endif
1.5 misho 112: sched_task_t *task;
1.4 misho 113:
114: sa.sa_handler = sig;
115: sigemptyset(&sa.sa_mask);
116: sigaction(SIGTERM, &sa, NULL);
117: sigaction(SIGUSR1, &sa, NULL);
1.2 misho 118:
119: root = schedBegin();
120: if (!root) {
121: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
122: return 1;
123: }
124:
1.4 misho 125: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 126: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
127: return 4;
128: } else
129: ts.tv_sec = 15;
1.4 misho 130: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 131: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
132: return 4;
133: } else
134: ts.tv_sec = 10;
135:
136: if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
137: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
138: return 2;
139: }
140:
1.7 misho 141: if (!schedTask(root, regular, "piuk", 11, NULL, 0)) {
142: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
143: return 3;
144: }
145: if (!schedTask(root, regular, "piuk", 1, NULL, 0)) {
146: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
147: return 3;
148: }
149: if (!schedTask(root, regular, "piuk", 0, NULL, 0)) {
150: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
151: return 3;
152: }
153: if (!schedTask(root, regular, "piuk", 1000001, NULL, 0)) {
1.2 misho 154: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
155: return 3;
156: }
157:
1.4 misho 158: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 159: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
160: return 4;
161: }
162:
1.4 misho 163: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.3 misho 164: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
165: return 5;
166: } else {
167: ts.tv_sec = 3;
168: ts.tv_nsec = 500000000;
169: }
170:
1.4 misho 171: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
1.3 misho 172: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
173: return 5;
174: }
1.4 misho 175: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
1.3 misho 176: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
177: return 5;
178: } else {
179: ts.tv_sec = 0;
180: ts.tv_nsec = 700000000;
181: }
1.4 misho 182: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
1.3 misho 183: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
184: return 5;
185: }
186:
1.4 misho 187: #ifdef EVFILT_USER
188: if (!(t[0] = schedUser(root, user, NULL, 42, 0, 0))) {
189: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
190: return 6;
191: }
192: if (!(t[1] = schedUser(root, user, NULL, 1, 0, 73))) {
193: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
194: return 6;
195: }
196: if (!(t[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
197: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
198: return 6;
199: }
200: if (!(t[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
201: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
202: return 6;
203: }
204: #endif
1.5 misho 205: if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
206: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
207: return 6;
208: }
209: if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
210: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
211: return 6;
212: }
213: if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
214: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
215: return 6;
216: }
1.4 misho 217:
218: if (argc > 1)
219: if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
220: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
221: return 7;
222: }
223: if (argc > 2) {
224: f = open(argv[2], O_RDWR);
225: if (f == -1) {
226: perror("open()");
227: return 8;
228: }
229: if (!schedNode(root, node, argv[2], f, 0, 0)) {
230: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
231: close(f);
232: return 8;
233: }
234: }
235:
236: #ifdef EVFILT_USER
237: schedTrigger(t[3]);
238: schedTrigger(t[1]);
239: #endif
1.5 misho 240: schedResumeby(root, CRITERIA_DATA, task);
1.4 misho 241:
242: if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
243: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
244: close(f);
245: return 9;
246: }
247:
248: #ifdef EVFILT_USER
249: schedTrigger(t[2]);
250: schedTrigger(t[0]);
251: #endif
252:
1.2 misho 253: schedCallOnce(root, once, "000000", 42, NULL, 0);
254:
255: printf("read_queue=%d timer_queue=%d\n",
256: ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
257:
258: // schedPolling(root, &p, NULL);
259: schedRun(root, &Kill);
260: schedEnd(&root);
1.4 misho 261:
262: if (f > 2)
263: close(f);
1.2 misho 264: return 0;
265: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>