Lely core libraries  2.2.5
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 #ifndef LELY_NO_ATOMICS
26 #define LELY_NO_ATOMICS 1
27 #ifndef LELY_NO_THREADS
28 #include <lely/libc/stdatomic.h>
29 #ifndef __STDC_NO_ATOMICS__
30 #undef LELY_NO_ATOMICS
31 #endif
32 #endif
33 #endif
34 #ifndef LELY_NO_THREADS
35 #include <lely/libc/threads.h>
36 #endif
37 
38 struct io_handle_vtab;
39 
41 struct io_handle {
43  const struct io_handle_vtab *vtab;
45 #ifndef LELY_NO_ATOMICS
46  atomic_size_t ref;
47 #elif !defined(LELY_NO_THREADS) && defined(_WIN32)
48  volatile LONG ref;
49 #else
50  size_t ref;
51 #endif
52 #ifdef _WIN32
54  HANDLE fd;
55 #else
56  int fd;
57 #endif
58 
62  int flags;
63 #ifndef LELY_NO_THREADS
64  mtx_t mtx;
66 #endif
67 };
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
79  int type;
81  size_t size;
83  void (*fini)(struct io_handle *handle);
85  int (*flags)(struct io_handle *handle, int flags);
87  ssize_t (*read)(struct io_handle *handle, void *buf, size_t nbytes);
89  ssize_t (*write)(struct io_handle *handle, const void *buf,
90  size_t nbytes);
92  int (*flush)(struct io_handle *handle);
94  io_off_t (*seek)(struct io_handle *handle, io_off_t offset, int whence);
96  ssize_t (*pread)(struct io_handle *handle, void *buf, size_t nbytes,
97  io_off_t offset);
99  ssize_t (*pwrite)(struct io_handle *handle, const void *buf,
100  size_t nbytes, io_off_t offset);
102  int (*purge)(struct io_handle *handle, int flags);
104  ssize_t (*recv)(struct io_handle *handle, void *buf, size_t nbytes,
105  io_addr_t *addr, int flags);
107  ssize_t (*send)(struct io_handle *handle, const void *buf,
108  size_t nbytes, const io_addr_t *addr, int flags);
110  struct io_handle *(*accept)(struct io_handle *handle, io_addr_t *addr);
112  int (*connect)(struct io_handle *handle, const io_addr_t *addr);
113 };
114 
123 struct io_handle *io_handle_alloc(const struct io_handle_vtab *vtab);
124 
126 void io_handle_free(struct io_handle *handle);
127 
132 void io_handle_fini(struct io_handle *handle);
133 
138 void io_handle_destroy(struct io_handle *handle);
139 
146 #ifdef LELY_NO_THREADS
147 #define io_handle_lock(handle)
148 #else
149 void io_handle_lock(struct io_handle *handle);
150 #endif
151 
153 #ifdef LELY_NO_THREADS
154 #define io_handle_unlock(handle)
155 #else
156 void io_handle_unlock(struct io_handle *handle);
157 #endif
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif // !LELY_IO_INTERN_HANDLE_H_
io_handle_unlock
void io_handle_unlock(struct io_handle *handle)
Unlocks a locked I/O device handle.
Definition: handle.c:147
io_handle_vtab::seek
io_off_t(* seek)(struct io_handle *handle, io_off_t offset, int whence)
A pointer to the seek method.
Definition: handle.h:94
io_handle_lock
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:139
io_handle_vtab::flush
int(* flush)(struct io_handle *handle)
A pointer to the flush method.
Definition: handle.h:92
io_handle_vtab::write
ssize_t(* write)(struct io_handle *handle, const void *buf, size_t nbytes)
A pointer to the write method.
Definition: handle.h:89
io_handle_vtab::flags
int(* flags)(struct io_handle *handle, int flags)
A pointer to the static flags method.
Definition: handle.h:85
threads.h
io_handle_alloc
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:77
__io_addr
An opaque network address type.
Definition: addr.h:30
io_off_t
int64_t io_off_t
A file offset type.
Definition: io.h:37
io_handle_vtab::type
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:79
io_handle_vtab::recv
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:104
io_handle
An I/O device handle.
Definition: handle.h:41
io_handle_destroy
void io_handle_destroy(struct io_handle *handle)
Destroys an I/O device handle.
Definition: handle.c:128
mtx_t
pthread_mutex_t mtx_t
A complete object type that holds an identifier for a mutex.
Definition: threads.h:102
io_handle_fini
void io_handle_fini(struct io_handle *handle)
Finalizes an I/O device handle by invoking its fini method, if available.
Definition: handle.c:106
io_handle_vtab::connect
int(* connect)(struct io_handle *handle, const io_addr_t *addr)
A pointer to the connect method.
Definition: handle.h:112
io.h
io_handle_vtab::send
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:107
io_handle_vtab::size
size_t size
The size (in bytes) of the handle struct.
Definition: handle.h:81
io_handle::vtab
const struct io_handle_vtab * vtab
A pointer to the virtual table.
Definition: handle.h:43
io_handle::flags
int flags
The I/O device flags (any combination of IO_FLAG_NO_CLOSE and IO_FLAG_NONBLOCK).
Definition: handle.h:62
io_handle_vtab::purge
int(* purge)(struct io_handle *handle, int flags)
A pointer to the purge method.
Definition: handle.h:102
io_handle_vtab::pread
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:96
io_handle_vtab::pwrite
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:99
stdatomic.h
io_handle_vtab
The virtual table of an I/O device handle.
Definition: handle.h:74
io_handle::fd
int fd
The native file descriptor.
Definition: handle.h:56
io_handle_vtab::read
ssize_t(* read)(struct io_handle *handle, void *buf, size_t nbytes)
A pointer to the read method.
Definition: handle.h:87
io_handle_vtab::fini
void(* fini)(struct io_handle *handle)
A pointer to the fini method.
Definition: handle.h:83
io_handle_free
void io_handle_free(struct io_handle *handle)
Frees an I/O device handle.
Definition: handle.c:116
io_handle::ref
size_t ref
The reference count.
Definition: handle.h:50
io_handle::mtx
mtx_t mtx
The mutex protecting flags (and other device-specific fields).
Definition: handle.h:65