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  };
246  uint64_t reserved_size[8];
247  };
248  };
249 
251  typedef struct ach_attr ach_attr_t;
252 
254  void ach_attr_init( ach_attr_t *attr );
255 
258  ach_attr_set_shm( ach_attr_t *attr, struct ach_header *shm );
259 
266  union {
267  struct{
268  union {
269  int map_anon;
270  enum ach_map map;
271  };
272  struct ach_header *shm;
273  clockid_t clock;
275  unsigned int truncate : 1;
276  unsigned int set_clock : 1;
277  };
278  uint64_t reserved[16];
279  };
280  };
281 
284 
287 
291 
295 
299 
302  ach_create_attr_get_shm( ach_create_attr_t *attr, struct ach_header **shm );
303 
305  struct ach_channel_vtab;
306 
320  typedef struct ach_channel {
321  union {
322  struct {
323  struct ach_header *shm;
324  size_t len;
325  int fd;
326  union {
327  uint64_t seq_num;
329  };
330  size_t next_index;
331  clockid_t clock;
332  volatile sig_atomic_t cancel;
333  const struct ach_channel_vtab *vtab;
334  };
335  uint64_t reserved[16];
336  };
337  } ach_channel_t;
338 
341  ach_channel_fd( const struct ach_channel *channel, int *file_descriptor );
342 
345  ach_channel_map( const struct ach_channel *channel, enum ach_map *map );
346 
349  ach_channel_clock( const struct ach_channel *channel, clockid_t *clock );
350 
373  ach_create( const char *name,
374  size_t frame_cnt, size_t frame_size,
375  ach_create_attr_t *attr );
376 
396  ach_open( ach_channel_t *channel, const char *name,
397  ach_attr_t *attr );
398 
463  ach_get( ach_channel_t *chan, void *buf, size_t size,
464  size_t *frame_size,
465  const struct timespec *ACH_RESTRICT abstime,
466  int options );
467 
489  enum ach_status
490  ach_put( ach_channel_t *channel, const void *buf, size_t len );
491 
492 
497  ach_flush( ach_channel_t *chan );
498 
507  ach_close( ach_channel_t *chan );
508 
511  const char *ach_result_to_string(ach_status_t result);
512 
513 
516  const char *ach_errstr();
517 
522  void ach_dump( struct ach_header *shm);
523 
525  enum ach_status
526  ach_chmod( ach_channel_t *chan, mode_t mode );
527 
533  ach_unlink( const char *name );
534 
540  typedef struct ach_cancel_attr {
541  union {
542  struct {
545  unsigned int async_unsafe : 1;
546  };
547  int64_t reserved[8];
548  };
550 
552  void
554 
557  ach_cancel_attr_set_async_unsafe( ach_cancel_attr_t *attr, int asyn_unsafe );
558 
561  ach_cancel( ach_channel_t *chan, const ach_cancel_attr_t *attr );
562 
563 #ifdef __cplusplus
564 }
565 #endif
566 
567 #endif /* ACH_H */
uint64_t seq_num
last sequence number read
Definition: ach.h:327
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 reserved[16]
Reserve space to compatibly add future options.
Definition: ach.h:335
Attributes to pass to ach_create.
Definition: ach.h:265
unsigned int set_clock
if true, set the clock of the condition variable
Definition: ach.h:276
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:323
Use kernel memory for channels - requires ach kernel module to be loaded.
Definition: ach.h:213
enum ach_map map
replaces map_anon
Definition: ach.h:270
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:278
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:269
unsigned int truncate
remove and recreate an existing shm file
Definition: ach.h:275
volatile sig_atomic_t cancel
cancel a waiting ach_get
Definition: ach.h:332
Struct containing 'cache' of kernel module data to avoid updating when no changes exist...
Definition: generic.h:257
achk_opt_t k_opts
Used by kernel devices.
Definition: ach.h:328
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:545
size_t next_index
next index entry to try get from
Definition: ach.h:330
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:540
struct ach_cancel_attr ach_cancel_attr_t
Attributes parameter for ach_cancel.
#define ACH_WARN_UNUSED
Warn if result is unused.
Definition: generic.h:71
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:273
uint64_t reserved_size[8]
Reserve space to compatibly add future options.
Definition: ach.h:246
Attributes to pass to ach_open.
Definition: ach.h:229
Descriptor for an Ach channel.
Definition: ach.h:320
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:78
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:331
int fd
file descriptor of mmap'ed file
Definition: ach.h:325
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:333
struct ach_header * shm
pointer to channel, set on output of create iff map_anon
Definition: ach.h:272
#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:324
This file contains declarations needed by both the userspace public interface and the Linux kernel im...