Annotation of libaitsched/example/test.c, revision 1.9.2.4
1.2 misho 1: #include <stdio.h>
1.7 misho 2: #include <stdlib.h>
3: #include <string.h>
1.2 misho 4: #include <unistd.h>
5: #include <fcntl.h>
1.5 misho 6: #include <signal.h>
1.2 misho 7: #include <sys/types.h>
8: #include <sys/stat.h>
9: #include <sys/socket.h>
10: #include <netinet/in.h>
1.7 misho 11: #include "../inc/config.h"
1.2 misho 12: #include <aitsched.h>
13:
14: intptr_t Kill;
1.7 misho 15: #ifdef AIO_SUPPORT
16: struct iovec iv[3], wiv[3], riv[3];
17: #endif
1.2 misho 18:
19: void *event(sched_task_t *arg)
20: {
21: printf("Event::\n");
1.9.2.1 misho 22: taskExit(arg, NULL);
1.2 misho 23: }
24:
1.8 misho 25: void *regular(sched_task_t *arg)
1.2 misho 26: {
1.8 misho 27: printf("Task::\n");
1.9.2.1 misho 28: taskExit(arg, NULL);
1.2 misho 29: }
30:
31: void *timer(sched_task_t *arg)
32: {
1.6 misho 33: printf("Timer %p sec::\n", TASK_ARG(arg));
1.9.2.1 misho 34: taskExit(arg, NULL);
1.2 misho 35: }
36:
37: void *r(sched_task_t *arg)
38: {
1.9 misho 39: printf("read:: %d bytes wait\n", TASK_RET(arg));
1.2 misho 40: Kill++;
1.9.2.1 misho 41: taskExit(arg, NULL);
1.2 misho 42: }
43:
44: void *w(sched_task_t *arg)
45: {
1.9 misho 46: printf("write:: %d bytes wait\n", TASK_RET(arg));
1.9.2.1 misho 47: taskExit(arg, NULL);
1.2 misho 48: }
49:
50: void *once(sched_task_t *arg)
51: {
52: printf("once::\n");
1.9.2.1 misho 53: taskExit(arg, NULL);
1.2 misho 54: }
55:
1.7 misho 56: #ifdef AIO_SUPPORT
57: void *aioread(sched_task_t *arg);
58: void *aiowrite(sched_task_t *arg)
59: {
60: char *ole = malloc(BUFSIZ);
61:
62: printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg),
63: TASK_DATA(arg));
64: free(TASK_DATA(arg));
65:
66: memset(ole, 0, BUFSIZ);
67: schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
1.9.2.1 misho 68: taskExit(arg, NULL);
1.7 misho 69: }
70:
71: void *aioread(sched_task_t *arg)
72: {
73: char *ole = malloc(BUFSIZ);
74: int len;
75:
76: printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg),
77: (char*) TASK_DATA(arg));
78:
79: if (TASK_ARG(arg)) {
80: len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
81: printf("sched Write len=%d %p\n", len, ole);
82: schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole, len, -1);
83:
84: }
85: free(TASK_DATA(arg));
1.9.2.1 misho 86: taskExit(arg, NULL);
1.7 misho 87: }
88:
89: void *aiobulkread(sched_task_t *arg)
90: {
91: struct iovec *iv = TASK_DATA(arg);
92: register int i;
93:
94: printf("aioBULKread::\n");
95: for (i = 0; i < 3; i++) {
96: printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
97: free(iv[i].iov_base);
98: }
99:
1.9.2.1 misho 100: taskExit(arg, NULL);
1.7 misho 101: }
102:
103: void *aiobulkwrite(sched_task_t *arg)
104: {
105: struct iovec *iv = TASK_DATA(arg);
106: register int i;
107:
108: printf("aioBULKwrite::\n");
109: for (i = 0; i < 3; i++) {
110: printf("%d) wlen=%d\n", i, iv[i].iov_len);
111: free(iv[i].iov_base);
112: }
113:
1.9.2.1 misho 114: taskExit(arg, NULL);
1.7 misho 115: }
116: #endif
117:
1.9 misho 118: void *thr(sched_task_t *arg)
119: {
1.9.2.4 ! misho 120: printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
! 121: printf("thread(%lx):: %s\n", TASK_VAL(arg), (char*) TASK_ARG(arg));
1.9 misho 122: taskExit(arg, 42);
123: }
124:
1.9.2.2 misho 125: void *thr4kill(sched_task_t *arg)
126: {
1.9.2.4 ! misho 127: char blah[BUFSIZ];
! 128:
! 129: printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
! 130:
! 131: printf("canceltype aaaaaaaaaaaaaaaaa:: %s (%lx)\n", __func__, TASK_VAL(arg));
! 132: read(0, blah, sizeof blah);
! 133: printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
1.9.2.2 misho 134: taskExit(arg, 0);
135: }
136:
1.5 misho 137: void sig(int s)
138: {
139: switch (s) {
140: case SIGTERM:
141: Kill++;
142: break;
143: }
144: }
145:
1.2 misho 146: int
147: main(int argc, char **argv)
148: {
149: sched_root_task_t *root;
1.7 misho 150: int f, fd;
1.2 misho 151: struct sockaddr_in sin;
1.5 misho 152: struct timespec ts = { 20, 0 };
153: // struct timespec p = { 0, 10000000 };
154: struct sigaction sa;
1.9.2.2 misho 155: sched_task_t *t;
1.7 misho 156: #ifdef AIO_SUPPORT
157: char *ole = malloc(BUFSIZ);
158: register int i;
159: #endif
1.5 misho 160:
161: sa.sa_handler = sig;
162: sigemptyset(&sa.sa_mask);
163: sigaction(SIGTERM, &sa, NULL);
1.2 misho 164:
165: f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
166: if (f == -1)
167: return 1;
168: sin.sin_len = sizeof sin;
169: sin.sin_family = AF_INET;
170: sin.sin_port = htons(2345);
171: sin.sin_addr.s_addr = INADDR_ANY;
172: if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
173: return 1;
174:
1.7 misho 175: fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
176: if (fd == -1)
177: return 1;
178: printf("fd=%d\n", fd);
179:
1.2 misho 180: root = schedBegin();
181: if (!root) {
182: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
183: return 1;
184: }
185:
1.6 misho 186: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5 misho 187: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
188: return 4;
189: } else
190: ts.tv_sec = 15;
1.6 misho 191: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5 misho 192: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
193: return 4;
194: } else
195: ts.tv_sec = 10;
196:
1.4 misho 197: if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
1.2 misho 198: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
199: return 2;
200: }
201:
1.8 misho 202: if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
1.2 misho 203: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
204: return 3;
205: }
206:
1.6 misho 207: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 208: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
209: return 4;
210: }
211:
1.4 misho 212: if (!schedRead(root, r, "rrr", f, NULL, 0)) {
1.2 misho 213: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
214: return 5;
215: }
216:
1.4 misho 217: if (!schedWrite(root, w, "www", f, NULL, 0)) {
1.2 misho 218: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
219: return 6;
220: }
221:
1.9.2.4 ! misho 222: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
1.9 misho 223: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
224: return 7;
225: }
1.9.2.4 ! misho 226: if (!schedThread(root, thr, "mdaaaa this is thread task", 0, NULL, 0)) {
1.9.2.2 misho 227: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
228: return 7;
229: }
1.9 misho 230: if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 42, NULL, 0)) {
231: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
232: return 7;
233: }
234: if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
235: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
236: return 7;
237: }
1.9.2.4 ! misho 238: printf("try to cancel tid = %lx\n", TASK_VAL(t));
1.9.2.3 misho 239: schedCancel(t);
1.9.2.4 ! misho 240: if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
! 241: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
! 242: return 7;
! 243: }
1.9 misho 244:
1.7 misho 245: #ifdef AIO_SUPPORT
246: memset(ole, 0, BUFSIZ);
247: if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
248: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
249:
250:
251: iv[0].iov_len = 5;
252: iv[1].iov_len = 2;
253: iv[2].iov_len = 50;
254: for (i = 0; i < 3; i++)
255: iv[i].iov_base = malloc(iv[i].iov_len);
256: if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
257: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
258: fsync(fd);
259: for (i = 0; i < 3; i++) {
260: wiv[i].iov_len = 100;
261: wiv[i].iov_base = malloc(wiv[i].iov_len);
262: }
263: strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
264: wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
265: strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
266: wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
267: strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
268: wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
269: if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
270: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
271:
272: for (i = 0; i < 3; i++) {
273: riv[i].iov_len = 5;
274: riv[i].iov_base = malloc(riv[i].iov_len + 1);
275: memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
276: }
277: if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
278: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
279: #endif
280:
1.4 misho 281: schedCallOnce(root, once, "000000", 42, NULL, 0);
1.2 misho 282:
1.5 misho 283: // schedPolling(root, &p, NULL);
1.2 misho 284: schedRun(root, &Kill);
285: schedEnd(&root);
286:
1.7 misho 287: #ifdef AIO_SUPPORT
288: for (i = 0; i < 3; i++)
289: free(iv[i].iov_base);
290: #endif
291:
292: close(fd);
1.2 misho 293: close(f);
294: return 0;
295: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>