gusimplewhiteboard
gusimplewhiteboard.c File Reference
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <gu_util.h>
#include "gusimplewhiteboard.h"
#include "guwhiteboardtypelist_c_generated.h"
Include dependency graph for gusimplewhiteboard.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE
 
#define __block   _xblock
 
#define __block   __attribute__((__blocks__(byref)))
 
#define WHITEBOARD_MAGIC   0xfeeda11deadbeef5ULL
 
#define SEMAPHORE_MAGIC_KEY   4242
 
#define SEM_ERROR   -1
 
#define SEM_A   0200 /* alter permission */
 
#define SEM_R   0400 /* read permission */
 

Typedefs

typedef void(* gsw_sig_t) (int sig)
 

Functions

void gsw_init_semaphores (gsw_sema_t s)
 initialise the whiteboard semaphores More...
 
gsw_sema_t gsw_setup_semaphores (int key)
 set up a semaphore array for the whiteboard More...
 
gu_simple_whiteboard_descriptorgsw_new_numbered_whiteboard (const char *name, int n)
 access a named whiteboard: this is the designated standard wb constructor for C programs that want to assign a whiteboard number (uses a different semaphore than the default) More...
 
gu_simple_whiteboard_descriptorgsw_new_custom_whiteboard (const char *name, const char *message_names[], int num_messages, int semaphore_magic_key)
 Access a named, custom whiteboard. More...
 
gu_simple_whiteboard_descriptorgswr_new_whiteboard (int i)
 access a remote named whiteboard: this is the designated constructore for C programs More...
 
gu_simple_whiteboard_descriptorgsw_new_whiteboard (const char *name)
 access a named whiteboard: this is the designated constructore for C programs More...
 
void gsw_free_whiteboard (gu_simple_whiteboard_descriptor *wbd)
 free the given whiteboard descriptor More...
 
gu_simple_whiteboard_descriptorget_local_singleton_whiteboard (void)
 create a simple whiteboard for the local singleton wb pointer More...
 
gu_simple_whiteboardgsw_create (const char *name, int *fdp, bool *initial)
 create a simple whiteboard More...
 
void gsw_free (gu_simple_whiteboard *wb, int fd)
 free the whiteboard More...
 
int gsw_procure (gsw_sema_t sem, enum gsw_semaphores s)
 grab a whiteboard semaphore More...
 
int gsw_vacate (gsw_sema_t sem, enum gsw_semaphores s)
 release a whiteboard semaphore More...
 
int gsw_register_message_type (gu_simple_whiteboard_descriptor *wbd, const char *name)
 register a new whiteboard message type More...
 
int gsw_offset_for_message_type (gu_simple_whiteboard_descriptor *wbd, const char *name)
 get the numerical index of a whiteboard message type More...
 
gu_simple_messagegsw_current_message (gu_simple_whiteboard *wb, int i)
 get the current shared memory location for the given whiteboard message type i More...
 
gu_simple_messagegsw_next_message (gu_simple_whiteboard *wb, int i)
 get the next shared memory location for the given whiteboard message type i More...
 
void gsw_increment (gu_simple_whiteboard *wb, int i)
 get the next shared memory location for the given whiteboard message type i More...
 
void gsw_increment_event_counter (gu_simple_whiteboard *wb, int i)
 add to a messages event counter on the wb More...
 
void gsw_add_process (gu_simple_whiteboard_descriptor *wbd, const pid_t proc)
 add process for subscription signalling More...
 
void gsw_remove_process (gu_simple_whiteboard_descriptor *wbd, const pid_t proc)
 remove process for subscription signalling More...
 
void gsw_signal_subscribers (gu_simple_whiteboard *wb)
 signal all subscribing processes More...
 
void gsw_add_wbd_signal_handler (gu_simple_whiteboard_descriptor *wbd)
 add subscription signal handler More...
 
void gsw_remove_wbd_signal_handler (gu_simple_whiteboard_descriptor *wbd)
 remove subscription signal handler More...
 

Macro Definition Documentation

◆ __block [1/2]

#define __block   _xblock

Definition at line 94 of file gusimplewhiteboard.c.

◆ __block [2/2]

#define __block   __attribute__((__blocks__(byref)))

Definition at line 94 of file gusimplewhiteboard.c.

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Definition at line 74 of file gusimplewhiteboard.c.

◆ SEM_A

#define SEM_A   0200 /* alter permission */

Definition at line 120 of file gusimplewhiteboard.c.

◆ SEM_ERROR

#define SEM_ERROR   -1

Definition at line 116 of file gusimplewhiteboard.c.

◆ SEM_R

#define SEM_R   0400 /* read permission */

Definition at line 121 of file gusimplewhiteboard.c.

◆ SEMAPHORE_MAGIC_KEY

#define SEMAPHORE_MAGIC_KEY   4242

Definition at line 115 of file gusimplewhiteboard.c.

◆ WHITEBOARD_MAGIC

#define WHITEBOARD_MAGIC   0xfeeda11deadbeef5ULL

Definition at line 114 of file gusimplewhiteboard.c.

Typedef Documentation

◆ gsw_sig_t

typedef void(* gsw_sig_t) (int sig)

Definition at line 603 of file gusimplewhiteboard.c.

Function Documentation

◆ get_local_singleton_whiteboard()

gu_simple_whiteboard_descriptor * get_local_singleton_whiteboard ( void  )

create a simple whiteboard for the local singleton wb pointer

Definition at line 281 of file gusimplewhiteboard.c.

Referenced by whiteboard_watcher::whiteboard_watcher(), generic_whiteboard_object< object_type >::init(), and SimpleWhiteboardTestPlainC::setUp.

◆ gsw_add_process()

void gsw_add_process ( gu_simple_whiteboard_descriptor wbd,
const pid_t  proc 
)

◆ gsw_add_wbd_signal_handler()

void gsw_add_wbd_signal_handler ( gu_simple_whiteboard_descriptor wbd)

add subscription signal handler

Definition at line 613 of file gusimplewhiteboard.c.

References gsw_procure(), GSW_SEM_PROC, and gsw_whiteboard_s::sem.

Referenced by whiteboard_watcher::subscribe(), and guWhiteboard::Whiteboard::subscribeToMessage().

◆ gsw_create()

gu_simple_whiteboard * gsw_create ( const char *  name,
int *  fdp,
bool *  ini 
)

create a simple whiteboard

Parameters
namename of the whiteboard
fdppointer to internal file descriptor storage (NULL if not needed)
inipointer to boolean recording wb initialisation

Definition at line 296 of file gusimplewhiteboard.c.

References GSW_DEFAULT_NAME, gsw_free(), GU_SIMPLE_WHITEBOARD_BUFSIZE, GU_SIMPLE_WHITEBOARD_VERSION, gsw_simple_whiteboard_s::magic, gsw_simple_whiteboard_s::version, and WHITEBOARD_MAGIC.

Referenced by gsw_new_custom_whiteboard().

◆ gsw_current_message()

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

Definition at line 498 of file gusimplewhiteboard.c.

References GU_SIMPLE_WHITEBOARD_GENERATIONS, gsw_simple_whiteboard_s::indexes, gsw_simple_whiteboard_s::messages, and u_int8_t.

Referenced by generic_whiteboard_object< object_type >::get(), and guWhiteboard::Whiteboard::getMessage().

◆ gsw_free()

void gsw_free ( gu_simple_whiteboard wb,
int  fd 
)

free the whiteboard

Parameters
wbwhiteboard to free
fdfile descriptor to close (-1 to skip)

Definition at line 351 of file gusimplewhiteboard.c.

Referenced by gsw_create().

◆ gsw_free_whiteboard()

void gsw_free_whiteboard ( gu_simple_whiteboard_descriptor wbd)

free the given whiteboard descriptor

Parameters
wbddescriptor for the whiteboard

Definition at line 254 of file gusimplewhiteboard.c.

Referenced by guWhiteboard::Whiteboard::~Whiteboard(), whiteboard_watcher::~whiteboard_watcher(), and gsw_new_custom_whiteboard().

◆ gsw_increment()

void gsw_increment ( gu_simple_whiteboard wb,
int  i 
)

get the next shared memory location for the given whiteboard message type i

Definition at line 514 of file gusimplewhiteboard.c.

References GU_SIMPLE_WHITEBOARD_GENERATIONS, gsw_simple_whiteboard_s::indexes, and u_int8_t.

Referenced by guWhiteboard::Whiteboard::addMessage(), and generic_whiteboard_object< object_type >::set().

◆ gsw_increment_event_counter()

void gsw_increment_event_counter ( gu_simple_whiteboard wb,
int  i 
)

add to a messages event counter on the wb

Definition at line 521 of file gusimplewhiteboard.c.

References gsw_simple_whiteboard_s::event_counters.

Referenced by guWhiteboard::Whiteboard::addMessage(), and generic_whiteboard_object< object_type >::set().

◆ gsw_init_semaphores()

void gsw_init_semaphores ( gsw_sema_t  sem)

initialise the whiteboard semaphores

Parameters
semwhiteboard semaphore descriptor

Definition at line 136 of file gusimplewhiteboard.c.

References GSW_NUM_SEM.

Referenced by gsw_new_custom_whiteboard(), and gsw_setup_semaphores().

◆ gsw_new_custom_whiteboard()

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.

This is the designated custom wb constructor for C programs

Parameters
nameThe name of the whiteboard to create/access
message_namesArray of char * containing the pre-defined (static) message names
num_messagesNumber of messages in the message_names array
semaphore_magic_keySemaphore magic key to use

Definition at line 206 of file gusimplewhiteboard.c.

References gsw_whiteboard_s::callback_queue, gsw_whiteboard_s::fd, gsw_create(), gsw_free_whiteboard(), gsw_init_semaphores(), gsw_register_message_type(), gsw_setup_semaphores(), gsw_whiteboard_s::sem, SEM_ERROR, and gsw_whiteboard_s::wb.

Referenced by gsw_new_numbered_whiteboard().

◆ gsw_new_numbered_whiteboard()

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 assign a whiteboard number (uses a different semaphore than the default)

Parameters
namename of the whiteboard
numwhiteboard number (0 for local, default whiteboard)

Definition at line 181 of file gusimplewhiteboard.c.

References gsw_new_custom_whiteboard(), GSW_NUM_RESERVED, GSW_NUM_TYPES_DEFINED, gsw_register_message_type(), SEMAPHORE_MAGIC_KEY, and WBTypes_stringValues.

Referenced by guWhiteboard::Whiteboard::Whiteboard(), gsw_new_whiteboard(), and gswr_new_whiteboard().

◆ gsw_new_whiteboard()

gu_simple_whiteboard_descriptor * gsw_new_whiteboard ( const char *  name)

access a named whiteboard: this is the designated constructore for C programs

Parameters
namename of the whiteboard

Definition at line 248 of file gusimplewhiteboard.c.

References gsw_new_numbered_whiteboard().

◆ gsw_next_message()

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

Definition at line 506 of file gusimplewhiteboard.c.

References GU_SIMPLE_WHITEBOARD_GENERATIONS, gsw_simple_whiteboard_s::indexes, gsw_simple_whiteboard_s::messages, and u_int8_t.

Referenced by guWhiteboard::Whiteboard::addMessage(), and generic_whiteboard_object< object_type >::set().

◆ gsw_offset_for_message_type()

int gsw_offset_for_message_type ( gu_simple_whiteboard_descriptor wbd,
const char *  name 
)

get the numerical index of a whiteboard message type

Parameters
wbddescriptor for the whiteboard
namestring to use for identification
Returns
numerical identifier to use

Definition at line 476 of file gusimplewhiteboard.c.

References gsw_whiteboard_s::wb.

◆ gsw_procure()

◆ gsw_register_message_type()

int gsw_register_message_type ( gu_simple_whiteboard_descriptor wbd,
const char *  name 
)

register a new whiteboard message type

Parameters
wbddescriptor for the whiteboard
namestring to use for identification
Returns
numerical identifier to use

Definition at line 432 of file gusimplewhiteboard.c.

References gsw_procure(), GSW_SEM_MSGTYPE, gsw_whiteboard_s::sem, and gsw_whiteboard_s::wb.

Referenced by gsw_new_custom_whiteboard(), and gsw_new_numbered_whiteboard().

◆ gsw_remove_process()

◆ gsw_remove_wbd_signal_handler()

void gsw_remove_wbd_signal_handler ( gu_simple_whiteboard_descriptor wbd)

remove subscription signal handler

Definition at line 630 of file gusimplewhiteboard.c.

References gsw_whiteboard_s::exit_monitor, gsw_whiteboard_s::got_monitor, and WHITEBOARD_POLL_PERIOD.

◆ gsw_setup_semaphores()

gsw_sema_t gsw_setup_semaphores ( int  key)

set up a semaphore array for the whiteboard

Parameters
keysemaphore key
Returns
semaphore array to use

Definition at line 160 of file gusimplewhiteboard.c.

References gsw_init_semaphores(), GSW_NUM_SEM, SEM_A, SEM_ERROR, and SEM_R.

Referenced by gsw_new_custom_whiteboard().

◆ gsw_signal_subscribers()

◆ gsw_vacate()

◆ gswr_new_whiteboard()

gu_simple_whiteboard_descriptor * gswr_new_whiteboard ( int  i)

access a remote named whiteboard: this is the designated constructore for C programs

Parameters
imachine id of the remote whiteboard

Definition at line 241 of file gusimplewhiteboard.c.

References gsw_new_numbered_whiteboard(), and GSWR_BASE_NAME.