|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | #include <errno.h>
|
| 17 | +#include <string.h> |
| 18 | + |
17 | 19 | #include <zephyr/posix/fcntl.h>
|
18 | 20 | #include <zephyr/kernel.h>
|
19 | 21 | #include <zephyr/sys/fdtable.h>
|
@@ -41,17 +43,23 @@ static struct fd_entry fdtable[CONFIG_POSIX_MAX_FDS] = {
|
41 | 43 | {
|
42 | 44 | /* STDIN */
|
43 | 45 | .vtable = &stdinout_fd_op_vtable,
|
44 |
| - .refcount = ATOMIC_INIT(1) |
| 46 | + .refcount = ATOMIC_INIT(1), |
| 47 | + .lock = Z_MUTEX_INITIALIZER(fdtable[0].lock), |
| 48 | + .cond = Z_CONDVAR_INITIALIZER(fdtable[0].cond), |
45 | 49 | },
|
46 | 50 | {
|
47 | 51 | /* STDOUT */
|
48 | 52 | .vtable = &stdinout_fd_op_vtable,
|
49 |
| - .refcount = ATOMIC_INIT(1) |
| 53 | + .refcount = ATOMIC_INIT(1), |
| 54 | + .lock = Z_MUTEX_INITIALIZER(fdtable[1].lock), |
| 55 | + .cond = Z_CONDVAR_INITIALIZER(fdtable[1].cond), |
50 | 56 | },
|
51 | 57 | {
|
52 | 58 | /* STDERR */
|
53 | 59 | .vtable = &stdinout_fd_op_vtable,
|
54 |
| - .refcount = ATOMIC_INIT(1) |
| 60 | + .refcount = ATOMIC_INIT(1), |
| 61 | + .lock = Z_MUTEX_INITIALIZER(fdtable[2].lock), |
| 62 | + .cond = Z_CONDVAR_INITIALIZER(fdtable[2].cond), |
55 | 63 | },
|
56 | 64 | #else
|
57 | 65 | {
|
@@ -124,6 +132,30 @@ static int _check_fd(int fd)
|
124 | 132 | return 0;
|
125 | 133 | }
|
126 | 134 |
|
| 135 | +#ifdef CONFIG_ZTEST |
| 136 | +bool fdtable_fd_is_initialized(int fd) |
| 137 | +{ |
| 138 | + struct k_mutex ref_lock; |
| 139 | + struct k_condvar ref_cond; |
| 140 | + |
| 141 | + if (fd < 0 || fd >= ARRAY_SIZE(fdtable)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + ref_lock = (struct k_mutex)Z_MUTEX_INITIALIZER(fdtable[fd].lock); |
| 146 | + if (memcmp(&ref_lock, &fdtable[fd].lock, sizeof(ref_lock)) != 0) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + ref_cond = (struct k_condvar)Z_CONDVAR_INITIALIZER(fdtable[fd].cond); |
| 151 | + if (memcmp(&ref_cond, &fdtable[fd].cond, sizeof(ref_cond)) != 0) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + return true; |
| 156 | +} |
| 157 | +#endif /* CONFIG_ZTEST */ |
| 158 | + |
127 | 159 | void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
|
128 | 160 | {
|
129 | 161 | struct fd_entry *entry;
|
|
0 commit comments