gusimplewhiteboard
gusimplewhiteboard.h
Go to the documentation of this file.
1/*
2 * gusimplewhiteboard.h
3 *
4 * Created by René Hexel on 20/12/11.
5 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2020 Rene Hexel.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58#ifndef gusimplewhiteboard_gusimplewhiteboard_h
59#define gusimplewhiteboard_gusimplewhiteboard_h
60
61#pragma clang diagnostic push
62#pragma clang diagnostic ignored "-Wpadded"
63#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
64#pragma clang diagnostic ignored "-Wunused-macros"
65#pragma clang diagnostic ignored "-Wreserved-id-macro"
66#pragma clang diagnostic ignored "-Wdeprecated"
67#pragma clang diagnostic ignored "-Wold-style-cast"
68
69#ifdef __APPLE__
70# ifndef TARGET_OS_IPHONE
71# include <AvailabilityMacros.h>
72# endif
73#endif
74#ifndef WITHOUT_LIBDISPATCH
75#include <dispatch/dispatch.h>
76#endif
77#include <sys/types.h>
78
79#ifndef __cplusplus
80#include <stdbool.h>
81#include <stdint.h>
82#else
83#if __cplusplus < 201103L
84#include <stdbool.h>
85#else
86#include <cstdbool>
87#endif
88
89extern "C"
90{
91#endif // __cplusplus
92#undef __block
93#define __block _xblock
94#include <unistd.h>
95#undef __block
96#define __block __attribute__((__blocks__(byref)))
97
98#ifndef __has_feature // Optional of course.
99#define __has_feature(x) 0 // Compatibility with non-clang compilers.
100#endif
101
102#ifdef __cplusplus
103
104#pragma clang diagnostic ignored "-Wc++11-long-long"
105
106#ifdef bool
107#undef bool
108#endif
109
110#ifdef true
111#undef true
112#undef false
113#endif
114#endif // __cplusplus
115
116#ifndef u_int64_t
117# define u_int64_t uint64_t
118#endif
119#ifndef u_int32_t
120# define u_int32_t uint32_t
121#endif
122#ifndef u_int16_t
123# define u_int16_t uint16_t
124#endif
125#ifndef u_int8_t
126# define u_int8_t uint8_t
127#endif
128
129#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
130#define GSW_IOS_SIMULATOR
131#endif
132
133#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE && !defined (GSW_IOS_SIMULATOR)
134#define GSW_IOS_DEVICE
135#endif
136
137#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE || defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
138#define GSW_IOS
139#endif
140
141#define GU_SIMPLE_WHITEBOARD_VERSION 6
142#ifndef GU_SIMPLE_WHITEBOARD_GENERATIONS
143#define GU_SIMPLE_WHITEBOARD_GENERATIONS 4
144#endif
145#ifndef GU_SIMPLE_WHITEBOARD_BUFSIZE
146#define GU_SIMPLE_WHITEBOARD_BUFSIZE 128
147#endif
148#ifndef GSW_TOTAL_MESSAGE_TYPES
149#define GSW_TOTAL_MESSAGE_TYPES 512
150#endif
151#ifndef GSW_NUM_RESERVED
152#define GSW_NUM_RESERVED (GSW_TOTAL_MESSAGE_TYPES/2) // message types (max)
153#endif
154#define GSW_TOTAL_PROCESSES 256
155#define GSW_NON_RESERVED_MESSAGE_TYPES (GSW_TOTAL_MESSAGE_TYPES-GSW_NUM_RESERVED)
156
158#ifndef GSW_DEFAULT_ENV
159#define GSW_DEFAULT_ENV "WHITEBOARD_NAME"
160#endif
161#ifndef GSW_DEFAULT_NAME
162#define GSW_DEFAULT_NAME "guWhiteboard"
163#endif
164#ifndef GSWR_BASE_NAME
165#define GSWR_BASE_NAME "guudpwhiteboard"
166#endif
167#ifndef WHITEBOARD_SIGNAL
168#define WHITEBOARD_SIGNAL SIGUSR2
169#endif
170#ifndef WHITEBOARD_POLL_PERIOD
171#define WHITEBOARD_POLL_PERIOD 10000
172#endif
173
175{
182
183struct gsw_whiteboard_s;
184
185typedef void (*gsw_subscription_f)(struct gsw_whiteboard_s *wbd);
186
189{
190 /*
191 * standard types
192 */
193 unsigned long long ulonglong;
194 long long slonglong;
195 unsigned long ulong;
196 long slong;
197 unsigned uint;
198 int sint;
199 unsigned short ushort;
200 short sshort;
201 unsigned char uchar;
202 signed char schar;
203 char xchar;
204
205 long double slongdouble;
206 double sdouble;
207 float sfloat;
208
209 /*
210 * POSIX defined length types
211 */
216
217 int64_t s64;
218 int32_t s32;
219 int16_t s16;
220 int8_t s8;
221
225 unsigned long long ullvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(unsigned long long)];
226 unsigned long ulvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(unsigned long)];
227 unsigned uivec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(unsigned)];
228 unsigned short usvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(unsigned short)];
229 unsigned char ucvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(unsigned char)];
230
231 long long llvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(long long)];
234 short svec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(short)];
235 signed char cvec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(signed char)];
237
242
243 int64_t s64vec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(int64_t)];
244 int32_t s32vec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(int32_t)];
245 int16_t s16vec[GU_SIMPLE_WHITEBOARD_BUFSIZE/sizeof(int16_t)];
247
248 /*
249 * simple compound types
250 */
251 struct {int16_t x,y,z,a,b,c,d,e; } coord;
252 struct {double x,y,z,a,b,c,d,e;} dcoord;
253 struct {float x,y,z,a,b,c,d,e;} fcoord;
254
257
259 struct { u_int16_t value; char string[GU_SIMPLE_WHITEBOARD_BUFSIZE-sizeof(u_int16_t)]; } hash;
260
262 struct { char data[GU_SIMPLE_WHITEBOARD_BUFSIZE-2]; unsigned char len; unsigned char type; } wbmsg;
264
267{
272
275
280
285
290
295
301
302#ifdef GSW_IOS_DEVICE
303typedef dispatch_semaphore_t __strong *gsw_sema_t;
304#else
305typedef int gsw_sema_t;
306#endif
307
309typedef struct gsw_whiteboard_s
310{
313 int fd;
314#if __has_feature(objc_arc) || defined(WITHOUT_LIBDISPATCH)
315 void *callback_queue;
316#else
317 dispatch_queue_t callback_queue;
318#endif
320 void *context;
324
328extern const char *gsw_global_whiteboard_name;
329
330//backwards compat for GU whiteboard defaults
331//---------------
335#define GSW_NUM_TYPES_DEFINED num_types_defined
336
340extern int num_types_defined;
341
345extern const char **WBTypes_stringValues;
346//---------------
347
348
349
355
363extern gu_simple_whiteboard_descriptor *gsw_new_custom_whiteboard(const char *name, const char *message_names[], int num_messages, int semaphore_magic_key);
364
370
377extern gu_simple_whiteboard_descriptor *gsw_new_numbered_whiteboard(const char *name, int num);
378
384
391extern int gsw_register_message_type(gu_simple_whiteboard_descriptor *wbd, const char *name);
392
399extern int gsw_offset_for_message_type(gu_simple_whiteboard_descriptor *wbd, const char *name);
400
407extern gu_simple_whiteboard *gsw_create(const char *name, int *fdp, bool *ini);
408
413
419extern void gsw_free(gu_simple_whiteboard *wb, int fd);
420
426extern gsw_sema_t gsw_setup_semaphores(int key);
427
433extern int gsw_procure(gsw_sema_t sem, enum gsw_semaphores s);
434
440extern int gsw_vacate(gsw_sema_t sem, enum gsw_semaphores s);
441
446extern void gsw_init_semaphores(gsw_sema_t sem);
447
452
457
461void gsw_increment(gu_simple_whiteboard *wb, int i);
462
467
468
469#pragma mark - subscription and callbacks
470
474extern void gsw_add_process(gu_simple_whiteboard_descriptor *wbd, const pid_t proc);
475
480
485
489void gsw_add_process(gu_simple_whiteboard_descriptor *wbd, const pid_t proc);
490
494void gsw_remove_process(gu_simple_whiteboard_descriptor *wbd, const pid_t proc);
495
500
501#ifdef __cplusplus
502}
503#endif
504
505#pragma clang diagnostic pop
506
507#endif
gu_simple_message * gsw_next_message(gu_simple_whiteboard *wb, int i)
get the next shared memory location for the given whiteboard message type i
#define GU_SIMPLE_WHITEBOARD_GENERATIONS
lifespan (max)
void gsw_add_wbd_signal_handler(gu_simple_whiteboard_descriptor *wbd)
add subscription signal handler
void gsw_free_whiteboard(gu_simple_whiteboard_descriptor *wbd)
free the given whiteboard descriptor
void gsw_free(gu_simple_whiteboard *wb, int fd)
free the whiteboard
gsw_sema_t gsw_setup_semaphores(int key)
set up a semaphore array for the whiteboard
struct gsw_whiteboard_s gu_simple_whiteboard_descriptor
the underlying whiteboard object
void gsw_increment(gu_simple_whiteboard *wb, int i)
get the next shared memory location for the given whiteboard message type i
void gsw_increment_event_counter(gu_simple_whiteboard *wb, int i)
add to a messages event counter on the wb
gu_simple_whiteboard_descriptor * gsw_new_custom_whiteboard(const char *name, const char *message_names[], int num_messages, int semaphore_magic_key)
Access a named, custom whiteboard.
#define GSW_TOTAL_PROCESSES
maximum subscriber procs
gu_simple_whiteboard_descriptor * get_local_singleton_whiteboard(void)
create a simple whiteboard for the local singleton wb pointer
gu_simple_whiteboard_descriptor * gsw_new_whiteboard(const char *name)
access a named whiteboard: this is the designated constructore for C programs
const char ** WBTypes_stringValues
allow whiteboard to use old functions for whiteboard initialisation and choose which messages and con...
int num_types_defined
allow whiteboard to use old functions for whiteboard initialisation and choose which messages and con...
gu_simple_whiteboard_descriptor * gsw_new_numbered_whiteboard(const char *name, int num)
access a named whiteboard: this is the designated standard wb constructor for C programs that want to...
#define u_int16_t
int gsw_sema_t
gu_simple_message * gsw_current_message(gu_simple_whiteboard *wb, int i)
get the current shared memory location for the given whiteboard message type i
gu_simple_whiteboard * gsw_create(const char *name, int *fdp, bool *ini)
create a simple whiteboard
int gsw_procure(gsw_sema_t sem, enum gsw_semaphores s)
grab a whiteboard semaphore
void(* gsw_subscription_f)(struct gsw_whiteboard_s *wbd)
int gsw_offset_for_message_type(gu_simple_whiteboard_descriptor *wbd, const char *name)
get the numerical index of a whiteboard message type
struct gsw_simple_whiteboard_s gu_simple_whiteboard
the actual whiteboard in shared mem
int gsw_register_message_type(gu_simple_whiteboard_descriptor *wbd, const char *name)
register a new whiteboard message type
#define u_int32_t
#define GSW_TOTAL_MESSAGE_TYPES
message types (max)
union gsw_simple_message gu_simple_message
union type that is used to store data in shared memory
#define u_int8_t
void gsw_init_semaphores(gsw_sema_t sem)
initialise the whiteboard semaphores
void gsw_remove_wbd_signal_handler(gu_simple_whiteboard_descriptor *wbd)
remove subscription signal handler
const char * gsw_global_whiteboard_name
global whiteboard name
Definition: Whiteboard.cc:78
gsw_semaphores
@ GSW_SEM_CALLBACK
semaphore for callback data
@ GSW_NUM_SEM
number of semaphores
@ GSW_SEM_MSGTYPE
semaphore for message type registration
@ GSW_SEM_PROC
semaphore for process registration
@ GSW_SEM_PUTMSG
semaphore for adding to the whiteboard
void gsw_remove_process(gu_simple_whiteboard_descriptor *wbd, const pid_t proc)
remove process for subscription signalling
int gsw_vacate(gsw_sema_t sem, enum gsw_semaphores s)
release a whiteboard semaphore
#define u_int64_t
void gsw_add_process(gu_simple_whiteboard_descriptor *wbd, const pid_t proc)
subscribe a new process to receive signals
gu_simple_whiteboard_descriptor * gswr_new_whiteboard(int i)
access a remote named whiteboard: this is the designated constructore for C programs
void gsw_signal_subscribers(gu_simple_whiteboard *wb)
signal all subscribing processes
#define GU_SIMPLE_WHITEBOARD_BUFSIZE
message len (max)
the actual whiteboard in shared mem
uint16_t version
whiteboard version
uint16_t eventcount
current event count
gu_simple_message typenames[512]
message types for numbers
pid_t processes[256]
list of subscribed processes
uint8_t indexes[512]
ring buffer indexes
gu_simple_message hashes[512]
hashes for registered message types
uint16_t num_types
total number of current, registered types
uint64_t magic
end of whiteboard marker
uint16_t event_counters[512]
event counter loops
gu_simple_message messages[512][4]
the actual messages stored in the whiteboard
uint16_t subscribed
subscribed processes
the underlying whiteboard object
bool got_monitor
have a running monitor
bool exit_monitor
exit the monitor
gsw_subscription_f callback
subscription callback function
dispatch_queue_t callback_queue
subscription callback queue
int fd
the associated memory-mapped file
void * context
callback context
gsw_sema_t sem
semaphore to use
gu_simple_whiteboard * wb
the actual whiteboard in shared mem
union type that is used to store data in shared memory
int32_t s32
signed 32 bit value
uint8_t u8
unsigned 8 bit value
uint64_t u64vec[128/sizeof(uint64_t)]
u_int64_t array
unsigned long ulong
unsigned long value
float sfloat
signed float
unsigned long ulvec[128/sizeof(unsigned long)]
unsigned long array
long slong
singed long value
unsigned uivec[128/sizeof(unsigned)]
unsigned array
int64_t s64vec[128/sizeof(int64_t)]
int64_t array
long long llvec[128/sizeof(long long)]
long long array
unsigned char uchar
unsigned char
unsigned short usvec[128/sizeof(unsigned short)]
unsigned short array
struct gsw_simple_message::@2 fcoord
simple float coordinates
char xchar
standard char
struct gsw_simple_message::@1 dcoord
simple double coordinates
short sshort
signed short
long lvec[128/sizeof(long)]
long array
unsigned uint
unsigned integer
struct gsw_simple_message::@3 hash
whiteboard hash type
uint8_t u8vec[128]
u_int8_t array
struct gsw_simple_message::@0 coord
simple 16 bit integer coordinates
uint64_t u64
unsigned 64 bit value
bool bvec[128/sizeof(bool)]
bool array
long double slongdouble
long double
unsigned short ushort
unsigned short
int64_t s64
signed 64 bit value
long long slonglong
long long value
uint32_t u32
unsigned 32 bit value
int ivec[128/sizeof(int)]
int array
unsigned char ucvec[128/sizeof(unsigned char)]
unsigned char array
struct gsw_simple_message::@4 wbmsg
compatibility WBMsg type
int16_t s16vec[128/sizeof(int16_t)]
int16_t array
double sdouble
signed double
signed char schar
signed char
uint32_t u32vec[128/sizeof(uint32_t)]
u_int32_t array
uint16_t u16
unsigned 16 bit value
int32_t s32vec[128/sizeof(int32_t)]
int32_t array
short svec[128/sizeof(short)]
short array
int sint
signed integer
int16_t s16
signed 16 bit value
int8_t s8
signed 8 bit value
signed char cvec[128/sizeof(signed char)]
signed char array
int8_t s8vec[128]
int8_t array
unsigned long long ulonglong
unsigned long long value
uint16_t u16vec[128/sizeof(uint16_t)]
u_int16_t array
unsigned long long ullvec[128/sizeof(unsigned long long)]
vector types