Lely core libraries 2.3.4
handle.h
Go to the documentation of this file.
1
21#ifndef LELY_IO_INTERN_HANDLE_H_
22#define LELY_IO_INTERN_HANDLE_H_
23
24#include "io.h"
25#include <lely/libc/stdatomic.h>
26#if !LELY_NO_THREADS
27#include <lely/libc/threads.h>
28#endif
29
30struct io_handle_vtab;
31
33struct io_handle {
35 const struct io_handle_vtab *vtab;
37#if !LELY_NO_ATOMICS
38 atomic_size_t ref;
39#elif !LELY_NO_THREADS && _WIN32
40 volatile LONG ref;
41#else
42 size_t ref;
43#endif
45#if _WIN32
46 HANDLE fd;
47#else
48 int fd;
49#endif
54 int flags;
55#if !LELY_NO_THREADS
57 mtx_t mtx;
58#endif
59};
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
71 int type;
73 size_t size;
75 void (*fini)(struct io_handle *handle);
77 int (*flags)(struct io_handle *handle, int flags);
79 ssize_t (*read)(struct io_handle *handle, void *buf, size_t nbytes);
81 ssize_t (*write)(struct io_handle *handle, const void *buf,
82 size_t nbytes);
84 int (*flush)(struct io_handle *handle);
86 io_off_t (*seek)(struct io_handle *handle, io_off_t offset, int whence);
88 ssize_t (*pread)(struct io_handle *handle, void *buf, size_t nbytes,
89 io_off_t offset);
91 ssize_t (*pwrite)(struct io_handle *handle, const void *buf,
92 size_t nbytes, io_off_t offset);
94 int (*purge)(struct io_handle *handle, int flags);
96 ssize_t (*recv)(struct io_handle *handle, void *buf, size_t nbytes,
97 io_addr_t *addr, int flags);
99 ssize_t (*send)(struct io_handle *handle, const void *buf,
100 size_t nbytes, const io_addr_t *addr, int flags);
102 struct io_handle *(*accept)(struct io_handle *handle, io_addr_t *addr);
104 int (*connect)(struct io_handle *handle, const io_addr_t *addr);
105};
106
115struct io_handle *io_handle_alloc(const struct io_handle_vtab *vtab);
116
118void io_handle_free(struct io_handle *handle);
119
124void io_handle_fini(struct io_handle *handle);
125
130void io_handle_destroy(struct io_handle *handle);
131
138#if LELY_NO_THREADS
139#define io_handle_lock(handle)
140#else
141void io_handle_lock(struct io_handle *handle);
142#endif
143
145#if LELY_NO_THREADS
146#define io_handle_unlock(handle)
147#else
148void io_handle_unlock(struct io_handle *handle);
149#endif
150
151#ifdef __cplusplus
152}
153#endif
154
155#endif // !LELY_IO_INTERN_HANDLE_H_
void io_handle_fini(struct io_handle *handle)
Finalizes an I/O device handle by invoking its fini method, if available.
Definition handle.c:110
void io_handle_destroy(struct io_handle *handle)
Destroys an I/O device handle.
Definition handle.c:132
void io_handle_free(struct io_handle *handle)
Frees an I/O device handle.
Definition handle.c:120
struct io_handle * io_handle_alloc(const struct io_handle_vtab *vtab)
Allocates a new I/O device handle from a virtual table.
Definition handle.c:81
void io_handle_unlock(struct io_handle *handle)
Unlocks a locked I/O device handle.
Definition handle.c:151
void io_handle_lock(struct io_handle *handle)
Locks an unlocked I/O device handle, so the flags (and other device-specific fields) can safely be ac...
Definition handle.c:143
int64_t io_off_t
A file offset type.
Definition io.h:37
This is the internal header file of the Windows-specific I/O declarations.
This header file is part of the C11 and POSIX compatibility library; it includes <stdatomic....
An opaque network address type.
Definition addr.h:30
The virtual table of an I/O device handle.
Definition handle.h:66
int(* flush)(struct io_handle *handle)
A pointer to the flush method.
Definition handle.h:84
void(* fini)(struct io_handle *handle)
A pointer to the fini method.
Definition handle.h:75
int(* flags)(struct io_handle *handle, int flags)
A pointer to the static flags method.
Definition handle.h:77
ssize_t(* send)(struct io_handle *handle, const void *buf, size_t nbytes, const io_addr_t *addr, int flags)
A pointer to the send method.
Definition handle.h:99
ssize_t(* pread)(struct io_handle *handle, void *buf, size_t nbytes, io_off_t offset)
A pointer to the pread method.
Definition handle.h:88
int(* purge)(struct io_handle *handle, int flags)
A pointer to the purge method.
Definition handle.h:94
ssize_t(* write)(struct io_handle *handle, const void *buf, size_t nbytes)
A pointer to the write method.
Definition handle.h:81
size_t size
The size (in bytes) of the handle struct.
Definition handle.h:73
ssize_t(* read)(struct io_handle *handle, void *buf, size_t nbytes)
A pointer to the read method.
Definition handle.h:79
int type
The type of the device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE, IO_TYPE_SERIAL or IO_TYPE_SOC...
Definition handle.h:71
io_off_t(* seek)(struct io_handle *handle, io_off_t offset, int whence)
A pointer to the seek method.
Definition handle.h:86
int(* connect)(struct io_handle *handle, const io_addr_t *addr)
A pointer to the connect method.
Definition handle.h:104
ssize_t(* pwrite)(struct io_handle *handle, const void *buf, size_t nbytes, io_off_t offset)
A pointer to the pwrite method.
Definition handle.h:91
ssize_t(* recv)(struct io_handle *handle, void *buf, size_t nbytes, io_addr_t *addr, int flags)
A pointer to the recv method.
Definition handle.h:96
An I/O device handle.
Definition handle.h:33
int fd
The native file descriptor.
Definition handle.h:48
atomic_size_t ref
The reference count.
Definition handle.h:38
int flags
The I/O device flags (any combination of IO_FLAG_NO_CLOSE and IO_FLAG_NONBLOCK).
Definition handle.h:54
mtx_t mtx
The mutex protecting flags (and other device-specific fields).
Definition handle.h:57
const struct io_handle_vtab * vtab
A pointer to the virtual table.
Definition handle.h:35
This header file is part of the C11 and POSIX compatibility library; it includes <threads....
ptrdiff_t ssize_t
Used for a count of bytes or an error indication.
Definition types.h:43