Annotation of libaitsched/example/test_time.c, revision 1.4.6.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.4.6.1 ! misho 14: sched_root_task_t *root;
1.2 misho 15:
16: void *event(sched_task_t *arg)
17: {
18: printf("Event::\n");
19: return NULL;
20: }
21:
22: void *eventlo(sched_task_t *arg)
23: {
24: printf("EventLOW::\n");
25: return NULL;
26: }
27:
28: void *timer(sched_task_t *arg)
29: {
1.4 misho 30: printf("Timer %p sec::\n", TASK_ARG(arg));
1.2 misho 31: return NULL;
32: }
33:
1.3 misho 34: void *alarmz(sched_task_t *arg)
35: {
1.4 misho 36: printf("Alarm %ld sec::\n", (u_long) TASK_ARG(arg));
1.3 misho 37: return NULL;
38: }
39:
1.4 misho 40: void *node(sched_task_t *arg)
41: {
42: printf("Node %s fflags 0x%X\n", (char*) TASK_ARG(arg), (u_int) TASK_DATLEN(arg));
43: return NULL;
44: }
45:
46: void *proc(sched_task_t *arg)
47: {
48: printf("Proc pid=%ld fflags 0x%X data %p\n", TASK_VAL(arg), (u_int) TASK_DATLEN(arg),
49: TASK_DATA(arg));
50: return NULL;
51: }
52:
53: void *sigz(sched_task_t *arg)
54: {
55: printf("Signal signal=%ld\n", TASK_VAL(arg));
56: return 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_DATLEN(arg) & NOTE_FFLAGSMASK);
63: return NULL;
64: }
65: #endif
66:
1.4.6.1 ! misho 67: void *susp1(sched_task_t *arg)
! 68: {
! 69: printf("Suspend 1 =%ld\n", TASK_VAL(arg));
! 70: return NULL;
! 71: }
! 72: void *susp2(sched_task_t *arg)
! 73: {
! 74: printf("Suspend 2 =%ld\n", TASK_VAL(arg));
! 75: return NULL;
! 76: }
! 77: void *susp3(sched_task_t *arg)
! 78: {
! 79: printf("Suspend 3 =%ld\n", TASK_VAL(arg));
! 80: return NULL;
! 81: }
! 82:
1.2 misho 83: void *once(sched_task_t *arg)
84: {
85: printf("once::\n");
86: return NULL;
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.4.6.1 ! 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.4.6.1 ! 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:
141: if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
142: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
143: return 3;
144: }
145:
1.4 misho 146: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 147: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
148: return 4;
149: }
150:
1.4 misho 151: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.3 misho 152: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
153: return 5;
154: } else {
155: ts.tv_sec = 3;
156: ts.tv_nsec = 500000000;
157: }
158:
1.4 misho 159: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 1, 0)) {
1.3 misho 160: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
161: return 5;
162: }
1.4 misho 163: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 2, 0)) {
1.3 misho 164: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
165: return 5;
166: } else {
167: ts.tv_sec = 0;
168: ts.tv_nsec = 700000000;
169: }
1.4 misho 170: if (!schedAlarm(root, alarmz, (void*) (intptr_t) ts.tv_sec, ts, (void*) 3, 0)) {
1.3 misho 171: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
172: return 5;
173: }
174:
1.4 misho 175: #ifdef EVFILT_USER
176: if (!(t[0] = schedUser(root, user, NULL, 42, 0, 0))) {
177: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
178: return 6;
179: }
180: if (!(t[1] = schedUser(root, user, NULL, 1, 0, 73))) {
181: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
182: return 6;
183: }
184: if (!(t[2] = schedUser(root, user, NULL, 0xaa, 0, NOTE_FFAND | 0xaa))) {
185: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
186: return 6;
187: }
188: if (!(t[3] = schedUser(root, user, NULL, -1, 0, NOTE_FFCOPY | 1003))) {
189: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
190: return 6;
191: }
192: #endif
1.4.6.1 ! misho 193: if (!schedSuspend(root, susp1, NULL, 7, NULL, 0)) {
! 194: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 195: return 6;
! 196: }
! 197: if (!(task = schedSuspend(root, susp2, NULL, 1, NULL, 0))) {
! 198: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 199: return 6;
! 200: }
! 201: if (!schedSuspend(root, susp3, NULL, 0, NULL, 0)) {
! 202: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 203: return 6;
! 204: }
1.4 misho 205:
206: if (argc > 1)
207: if (!schedProc(root, proc, NULL, atoi(argv[1]), 0, 0)) {
208: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
209: return 7;
210: }
211: if (argc > 2) {
212: f = open(argv[2], O_RDWR);
213: if (f == -1) {
214: perror("open()");
215: return 8;
216: }
217: if (!schedNode(root, node, argv[2], f, 0, 0)) {
218: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
219: close(f);
220: return 8;
221: }
222: }
223:
224: #ifdef EVFILT_USER
225: schedTrigger(t[3]);
226: schedTrigger(t[1]);
227: #endif
1.4.6.1 ! misho 228: schedResumeby(root, CRITERIA_DATA, task);
1.4 misho 229:
230: if (!schedSignal(root, sigz, NULL, SIGUSR1, 0, 0)) {
231: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
232: close(f);
233: return 9;
234: }
235:
236: #ifdef EVFILT_USER
237: schedTrigger(t[2]);
238: schedTrigger(t[0]);
239: #endif
240:
1.2 misho 241: schedCallOnce(root, once, "000000", 42, NULL, 0);
242:
243: printf("read_queue=%d timer_queue=%d\n",
244: ROOT_QUEUE_EMPTY(root, read), ROOT_QUEUE_EMPTY(root, timer));
245:
246: // schedPolling(root, &p, NULL);
247: schedRun(root, &Kill);
248: schedEnd(&root);
1.4 misho 249:
250: if (f > 2)
251: close(f);
1.2 misho 252: return 0;
253: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>