1 | /* |
---|
2 | * Copyright 2011, Alin Rus <alin.codejunkie@gmail.com> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #define CONFIGURE_INIT |
---|
15 | #include "system.h" |
---|
16 | #include <rtems.h> |
---|
17 | #include "tmacros.h" |
---|
18 | #include <rtems/posix/aio_misc.h> |
---|
19 | #include <aio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <unistd.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <sched.h> |
---|
24 | #include <fcntl.h> |
---|
25 | #include <rtems/chain.h> |
---|
26 | |
---|
27 | const char rtems_test_name[] = "PSXAIO 3"; |
---|
28 | |
---|
29 | /* forward declarations to avoid warnings */ |
---|
30 | struct aiocb *create_aiocb(int fd); |
---|
31 | void free_aiocb(struct aiocb *aiocbp); |
---|
32 | |
---|
33 | #define FD_COUNT 6 |
---|
34 | #define BUFSIZE 128 |
---|
35 | |
---|
36 | struct aiocb * |
---|
37 | create_aiocb (int fd) |
---|
38 | { |
---|
39 | struct aiocb *aiocbp; |
---|
40 | |
---|
41 | aiocbp = malloc (sizeof (struct aiocb)); |
---|
42 | memset (aiocbp, 0, sizeof (struct aiocb)); |
---|
43 | aiocbp->aio_buf = malloc (BUFSIZE * sizeof (char)); |
---|
44 | aiocbp->aio_nbytes = BUFSIZE; |
---|
45 | aiocbp->aio_offset = 0; |
---|
46 | aiocbp->aio_reqprio = 0; |
---|
47 | aiocbp->aio_fildes = fd; |
---|
48 | |
---|
49 | return aiocbp; |
---|
50 | } |
---|
51 | |
---|
52 | void |
---|
53 | free_aiocb (struct aiocb *aiocbp) |
---|
54 | { |
---|
55 | free ((char*) aiocbp->aio_buf); |
---|
56 | free (aiocbp); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void * |
---|
61 | POSIX_Init (void *argument) |
---|
62 | { |
---|
63 | int fd[FD_COUNT]; |
---|
64 | struct aiocb *aiocbp[FD_COUNT+1]; |
---|
65 | int status, i, policy = SCHED_FIFO; |
---|
66 | char filename[BUFSIZE]; |
---|
67 | struct sched_param param; |
---|
68 | |
---|
69 | status = rtems_aio_init (); |
---|
70 | rtems_test_assert (status == 0); |
---|
71 | |
---|
72 | param.sched_priority = 30; |
---|
73 | status = pthread_setschedparam (pthread_self(), policy, ¶m); |
---|
74 | rtems_test_assert (status == 0); |
---|
75 | |
---|
76 | status = mkdir ("/tmp", S_IRWXU); |
---|
77 | rtems_test_assert (!status); |
---|
78 | |
---|
79 | TEST_BEGIN(); |
---|
80 | |
---|
81 | puts (" Init: Open files "); |
---|
82 | |
---|
83 | for (i=0; i<FD_COUNT; i++) |
---|
84 | { |
---|
85 | sprintf (filename, "/tmp/aio_fildes%d",i); |
---|
86 | fd[i] = open (filename, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
87 | rtems_test_assert ( fd[i] != -1); |
---|
88 | } |
---|
89 | |
---|
90 | puts (" Init: [WQ] aio_write on 1st file "); |
---|
91 | aiocbp[0] = create_aiocb (fd[0]); |
---|
92 | status = aio_write (aiocbp[0]); |
---|
93 | rtems_test_assert (status != -1); |
---|
94 | |
---|
95 | puts (" Init: [WQ] aio_write on 2nd file "); |
---|
96 | aiocbp[1] = create_aiocb (fd[1]); |
---|
97 | status = aio_write (aiocbp[1]); |
---|
98 | rtems_test_assert (status != -1); |
---|
99 | |
---|
100 | puts (" Init: [WQ] aio_read on 2nd file add by priority "); |
---|
101 | aiocbp[2] = create_aiocb (fd[1]); |
---|
102 | status = aio_read (aiocbp[2]); |
---|
103 | rtems_test_assert (status != -1); |
---|
104 | |
---|
105 | puts (" Init: [WQ] aio_write on 3rd file "); |
---|
106 | aiocbp[3] = create_aiocb (fd[2]); |
---|
107 | status = aio_write (aiocbp[3]); |
---|
108 | rtems_test_assert (status != -1); |
---|
109 | |
---|
110 | puts (" Init: [WQ] aio_write on 4th file "); |
---|
111 | aiocbp[4] = create_aiocb (fd[3]); |
---|
112 | status = aio_write (aiocbp[4]); |
---|
113 | rtems_test_assert (status != -1); |
---|
114 | |
---|
115 | puts (" Init: [WQ] aio_write on 5th file -- [WQ] full "); |
---|
116 | aiocbp[5] = create_aiocb (fd[4]); |
---|
117 | status = aio_write (aiocbp[5]); |
---|
118 | rtems_test_assert (status != -1); |
---|
119 | |
---|
120 | puts (" Init: [IQ] aio_write on 6th file "); |
---|
121 | aiocbp[6] = create_aiocb (fd[5]); |
---|
122 | status = aio_read (aiocbp[6]); |
---|
123 | rtems_test_assert (status != -1); |
---|
124 | |
---|
125 | puts (" Init: going to sleep for 5 sec "); |
---|
126 | sleep (5); |
---|
127 | puts (" Init: going to sleep again for 5 sec "); |
---|
128 | sleep (5); |
---|
129 | puts (" Init: going to sleep again for 5 sec "); |
---|
130 | sleep (5); |
---|
131 | |
---|
132 | TEST_END(); |
---|
133 | |
---|
134 | for (i = 0; i < FD_COUNT; i++) |
---|
135 | { |
---|
136 | close (fd[i]); |
---|
137 | free_aiocb (aiocbp[i]); |
---|
138 | } |
---|
139 | free_aiocb (aiocbp[i]); |
---|
140 | rtems_test_exit (0); |
---|
141 | |
---|
142 | return NULL; |
---|
143 | |
---|
144 | } |
---|
145 | |
---|