ach
Real-Time IPC Library
ach.h
Go to the documentation of this file.
1 /* -*- mode: C; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2008-2013, Georgia Tech Research Corporation
5  * Copyright (C) 2015, Rice University
6  * All rights reserved.
7  *
8  * Author(s): Neil T. Dantam <ntd@rice.edu>
9  * Georgia Tech Humanoid Robotics Lab
10  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
11  *
12  *
13  * This file is provided under the following "BSD-style" License:
14  *
15  *
16  * Redistribution and use in source and binary forms, with or
17  * without modification, are permitted provided that the following
18  * conditions are met:
19  *
20  * * Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  *
23  * * Redistributions in binary form must reproduce the above
24  * copyright notice, this list of conditions and the following
25  * disclaimer in the documentation and/or other materials provided
26  * with the distribution.
27  *
28  * * Neither the name of the copyright holder the names of its
29  * contributors may be used to endorse or promote products derived
30  * from this software without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
41  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  * POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
130 /*
131  * Shared Memory Layout:
132  *
133  * ________
134  * | Header |
135  * |--------|
136  * | GUARDH |
137  * |--------|
138  * | Index |
139  * | |
140  * | |
141  * |--------|
142  * | GUARDI |
143  * |--------|
144  * | Data |
145  * | |
146  * | |
147  * | |
148  * | |
149  * | |
150  * | |
151  * |--------|
152  * | GUARDD |
153  * |________|
154  */
155 
156 #ifndef ACH_H
157 #define ACH_H
158 
159 #include <signal.h>
160 
161 /* restict only in C99 */
162 #ifdef __cplusplus
163 
164 # define ACH_RESTRICT
165 #elif __STDC_VERSION__ < 199901L
166 
167 # define ACH_RESTRICT
168 #else
169 
170 # define ACH_RESTRICT restrict
171 #endif
172 
173 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
174 
175 #define ACH_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
176 #elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
177 
178 #define ACH_DEPRECATED(msg) __attribute__((__deprecated__))
179 #else
180 
181 #define ACH_DEPRECATED(msg)
182 #endif /* __GNUC__ */
183 
184 /* Determine a default clock */
185 #ifdef CLOCK_MONOTONIC
186 
187 #define ACH_DEFAULT_CLOCK CLOCK_MONOTONIC
188 #elif defined CLOCK_HIGHRES /* Old Solaris lacks CLOCK_MONOTONIC,
189  CLOCK_HIGHRES looks the same */
190 
191 #define ACH_DEFAULT_CLOCK CLOCK_HIGHRES
192 #elif defined CLOCK_REALTIME /* Try fallback to CLOCK_REALTIME */
193 
194 #define ACH_DEFAULT_CLOCK CLOCK_REALTIME
195 #else
196 #error No valid CLOCKS defined. Expecting CLOCK_MONOTONIC.
197 #endif /* CLOCK_MONOTONIC */
198 
199 #include "ach/generic.h"
200 
201 #ifdef __cplusplus
202 extern "C" {
203 #endif
204 
208  enum ach_map {
216  };
217 
222  struct ach_header;
223 
229  struct ach_attr {
230  union {
231  struct{
232  union {
237  int map_anon
238  ACH_DEPRECATED("The 'map_anon' field is replaced by the 'map' field of type 'enum ach_map'")
239  ;
242  enum ach_map map;
243  };
244  struct ach_header *shm;
245  union {
246  uint64_t reserve_bits;
247  struct {
248  unsigned int lock_source : 1;
249  };
250  };
251  };
252  uint64_t reserved_size[8];
253  };
254  };
255 
257  typedef struct ach_attr ach_attr_t;
258 
260  void ach_attr_init( ach_attr_t *attr );
261 
264  ach_attr_set_shm( ach_attr_t *attr, struct ach_header *shm );
265 
267  enum ach_status
269 
276  union {
277  struct{
278  union {
279  int map_anon;
280  enum ach_map map;
281  };
282  struct ach_header *shm;
283  clockid_t clock;
285  union {
286  uint64_t reserve_bits;
287  struct {
288  unsigned int truncate : 1;
289  unsigned int set_clock : 1;
290  };
291  };
292  };
293  uint64_t reserved[16];
294  };
295  };
296 
299 
302 
306 
310 
314 
317  ach_create_attr_get_shm( ach_create_attr_t *attr, struct ach_header **shm );
318 
320  struct ach_channel_vtab;
321 
335  typedef struct ach_channel {
336  union {
337  struct {
338  struct ach_header *shm;
339  size_t len;
340  int fd;
341  union {
342  uint64_t seq_num;
344  };
345  size_t next_index;
346  clockid_t clock;
347  volatile sig_atomic_t cancel;
348  const struct ach_channel_vtab *vtab;
350  };
351  uint64_t reserved[16];
352  };
353  } ach_channel_t;
354 
357  ach_channel_fd( const struct ach_channel *channel, int *file_descriptor );
358 
361  ach_channel_map( const struct ach_channel *channel, enum ach_map *map );
362 
365  ach_channel_clock( const struct ach_channel *channel, clockid_t *clock );
366 
389  ach_create( const char *name,
390  size_t frame_cnt, size_t frame_size,
391  ach_create_attr_t *attr );
392 
412  ach_open( ach_channel_t *channel, const char *name,
413  ach_attr_t *attr );
414 
479  ach_get( ach_channel_t *chan, void *buf, size_t size,
480  size_t *frame_size,
481  const struct timespec *ACH_RESTRICT abstime,
482  int options );
483 
505  enum ach_status
506  ach_put( ach_channel_t *channel, const void *buf, size_t len );
507 
508 
513  ach_flush( ach_channel_t *chan );
514 
523  ach_close( ach_channel_t *chan );
524 
527  const char *ach_result_to_string(ach_status_t result);
528 
529 
532  const char *ach_errstr();
533 
538  void ach_dump( struct ach_header *shm);
539 
541  enum ach_status
542  ach_chmod( ach_channel_t *chan, mode_t mode );
543 
549  ach_unlink( const char *name );
550 
556  typedef struct ach_cancel_attr {
557  union {
558  struct {
561  unsigned int async_unsafe : 1;
562  };
563  int64_t reserved[8];
564  };
566 
568  void
570 
573  ach_cancel_attr_set_async_unsafe( ach_cancel_attr_t *attr, int asyn_unsafe );
574 
577  ach_cancel( ach_channel_t *chan, const ach_cancel_attr_t *attr );
578 
579 #ifdef __cplusplus
580 }
581 #endif
582 
583 #endif /* ACH_H */
uint64_t seq_num
last sequence number read
Definition: ach.h:342
enum ach_status ACH_WARN_UNUSED ach_create_attr_set_map(ach_create_attr_t *attr, enum ach_map map)
Set the mapping.
enum ach_status ACH_WARN_UNUSED ach_channel_fd(const struct ach_channel *channel, int *file_descriptor)
Return the file descriptor associated with this channel.
Use the default mapping for channels.
Definition: ach.h:209
enum ach_status ACH_WARN_UNUSED ach_open(ach_channel_t *channel, const char *name, ach_attr_t *attr)
Opens a handle to channel.
uint64_t reserve_bits
reserve space for bit flags
Definition: ach.h:286
uint64_t reserved[16]
Reserve space to compatibly add future options.
Definition: ach.h:351
Attributes to pass to ach_create.
Definition: ach.h:275
unsigned int set_clock
if true, set the clock of the condition variable
Definition: ach.h:289
struct ach_header * shm
the memory buffer used by anonymous channels
Definition: ach.h:244
struct ach_header * shm
pointer to mmap'ed block
Definition: ach.h:338
Use kernel memory for channels - requires ach kernel module to be loaded.
Definition: ach.h:213
unsigned int lock_source
if true, take the source lock when opening or fail
Definition: ach.h:248
enum ach_map map
replaces map_anon
Definition: ach.h:280
enum ach_status ach_attr_set_lock_source(ach_attr_t *attr, int lock_source)
Set lock source value.
int map_anon
anonymous channel (put it in process heap, not shm).
Definition: ach.h:239
uint64_t reserved[16]
Reserve space to compatibly add future options.
Definition: ach.h:293
enum ach_status ACH_WARN_UNUSED ach_create_attr_set_clock(ach_create_attr_t *attr, clockid_t clock)
Set the clockid.
int map_anon
allocate channel in heap, rather than shm
Definition: ach.h:279
unsigned int truncate
remove and recreate an existing shm file
Definition: ach.h:288
volatile sig_atomic_t cancel
cancel a waiting ach_get
Definition: ach.h:347
Struct containing 'cache' of kernel module data to avoid updating when no changes exist...
Definition: generic.h:269
achk_opt_t k_opts
Used by kernel devices.
Definition: ach.h:343
void ach_attr_init(ach_attr_t *attr)
Initialize attributes for opening channels.
#define ACH_RESTRICT
Alias restrict keyword.
Definition: ach.h:167
enum ach_status ach_chmod(ach_channel_t *chan, mode_t mode)
Sets permissions of chan to specified mode.
void ach_create_attr_init(ach_create_attr_t *attr)
Initialize attributes for creating channels.
enum ach_status ACH_WARN_UNUSED ach_get(ach_channel_t *chan, void *buf, size_t size, size_t *frame_size, const struct timespec *ACH_RESTRICT abstime, int options)
Pulls a message from the channel.
enum ach_status ach_status_t
return status codes for ach functions.
enum ach_map map
Where to put channel backing memory.
Definition: ach.h:242
const char * ach_errstr()
Return a string describing the most recent ach error in detail.
enum ach_status ACH_WARN_UNUSED ach_create_attr_get_shm(ach_create_attr_t *attr, struct ach_header **shm)
Get backing memory for anonymous channel.
enum ach_status ACH_WARN_UNUSED ach_unlink(const char *name)
Delete an ach channel.
ach_map
Options to specify the mapping for a channels backing memory buffer.
Definition: ach.h:208
const char * ach_result_to_string(ach_status_t result)
Converts return code from ach call to a human readable string;.
unsigned int async_unsafe
If true, permit calls that are unsafe in a signal handler.
Definition: ach.h:561
size_t next_index
next index entry to try get from
Definition: ach.h:345
enum ach_status ACH_WARN_UNUSED ach_create_attr_set_truncate(ach_create_attr_t *attr, int truncate)
Set to truncate.
anonymous channel - use heap memory
Definition: ach.h:210
enum ach_status ACH_WARN_UNUSED ach_cancel_attr_set_async_unsafe(ach_cancel_attr_t *attr, int asyn_unsafe)
Set async unsafe field.
enum ach_status ACH_WARN_UNUSED ach_cancel(ach_channel_t *chan, const ach_cancel_attr_t *attr)
Cancel a pending ach_get() on channel.
Attributes parameter for ach_cancel.
Definition: ach.h:556
struct ach_cancel_attr ach_cancel_attr_t
Attributes parameter for ach_cancel.
int fd_source_lock
file descriptor for source lock
Definition: ach.h:349
#define ACH_WARN_UNUSED
Warn if result is unused.
Definition: generic.h:73
enum ach_status ACH_WARN_UNUSED ach_channel_map(const struct ach_channel *channel, enum ach_map *map)
Return the mapping of the channel.
void ach_cancel_attr_init(ach_cancel_attr_t *attr)
Initialize attributes.
clockid_t clock
Which clock to use if set_clock is true.
Definition: ach.h:283
uint64_t reserved_size[8]
Reserve space to compatibly add future options.
Definition: ach.h:252
Attributes to pass to ach_open.
Definition: ach.h:229
uint64_t reserve_bits
reserve space for bit flags
Definition: ach.h:246
Descriptor for an Ach channel.
Definition: ach.h:335
enum ach_status ACH_WARN_UNUSED ach_attr_set_shm(ach_attr_t *attr, struct ach_header *shm)
Set shared memory area for anonymous channels.
struct ach_channel ach_channel_t
Descriptor for an Ach channel.
void ach_dump(struct ach_header *shm)
Prints information about the channel shm to stderr.
ach_status
return status codes for ach functions.
Definition: generic.h:84
enum ach_status ACH_WARN_UNUSED ach_channel_clock(const struct ach_channel *channel, clockid_t *clock)
Return the clock used by the channel.
Use shared memory for channels.
Definition: ach.h:211
clockid_t clock
attributes used to create this channel
Definition: ach.h:346
int fd
file descriptor of mmap'ed file
Definition: ach.h:340
enum ach_status ACH_WARN_UNUSED ach_create(const char *name, size_t frame_cnt, size_t frame_size, ach_create_attr_t *attr)
Creates a new channel.
enum ach_status ACH_WARN_UNUSED ach_flush(ach_channel_t *chan)
Discards all previously received messages for this handle.
enum ach_status ach_put(ach_channel_t *channel, const void *buf, size_t len)
Copy a new message into the channel.
const struct ach_channel_vtab * vtab
virtual method table
Definition: ach.h:348
struct ach_header * shm
pointer to channel, set on output of create iff map_anon
Definition: ach.h:282
#define ACH_DEPRECATED(msg)
Deprecated old symbol.
Definition: ach.h:181
enum ach_status ACH_WARN_UNUSED ach_close(ach_channel_t *chan)
Closes the shared memory block.
size_t len
length of memory mapping
Definition: ach.h:339
This file contains declarations needed by both the userspace public interface and the Linux kernel im...