Apama  10.15.2.4
sag_connectivity_threading.h File Reference

Provides macros for threading and concurrency primitives. More...

#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <signal.h>
#include <stdexcept>

Go to the source code of this file.

Classes

class  com::softwareag::connectivity::SAG_LOCK_GUARD
 A simple RAII wrapper for SAG_MUTEX_T that locks the specified mutex in its constructor, and guarantees that it will be unlocked when this object goes out of scope unless it has been unlocked already by that point. More...
 

Namespaces

 com::softwareag::connectivity
 Contains classes relating to the connectivity system.
 

Macros

#define SAG_MUTEX_LOCK(m)   pthread_mutex_lock(&m)
 Lock a mutex. More...
 
#define SAG_MUTEX_UNLOCK(m)   pthread_mutex_unlock(&m)
 Unlock a mutex. More...
 
#define SAG_MUTEX_INIT(m)   pthread_mutex_init(&m, NULL)
 Create a mutex. More...
 
#define SAG_MUTEX_DESTROY(m)   pthread_mutex_destroy(&m)
 Destroy a mutex. More...
 
#define SAG_MUTEX_T   pthread_mutex_t
 A helper that provides cross-platform mutex functionality for use on old compilers. More...
 
#define SAG_SEMA_WAIT(s)   {while (sem_wait(&s));}
 Wait/acquire a semaphore. More...
 
#define SAG_SEMA_POST(s)   sem_post(&s)
 Post/signal/release a semaphore. More...
 
#define SAG_SEMA_INIT(s)   sem_init(&s, 0, 0)
 Create a semaphore. More...
 
#define SAG_SEMA_DESTROY(s)   sem_destroy(&s)
 Destroy a semaphore. More...
 
#define SAG_SEMA_T   sem_t
 A helper that provides cross-platform semaphore functionality for use on old compilers. More...
 
#define SAG_THREAD_CREATE(id, func, params)   pthread_create(& id, NULL, func, params)
 Create a thread which will call func with params. More...
 
#define SAG_THREAD_JOIN(id)   pthread_join(id, NULL)
 Wait the given thread to terminate. More...
 
#define SAG_THREAD_RETURN   return NULL;
 The statement to return from a thread function. More...
 
#define SAG_THREAD_T   pthread_t
 The type of a thread. More...
 
#define SAG_THREAD_RET_T   void*
 The return type of a thread function. More...
 

Detailed Description

Provides macros for threading and concurrency primitives.

Note that you should only use these if your compiler is too old to support C++11 threading primitives. On modern compilers you should use std::thread, std::mutex, std::unique_lock, std::condition_variable etc.

These macros are defined for both windows and linux to allow creation of a cross-platform plugin. If you don't need your plugin to work on multiple platforms then you can use platform-specific threading mechanisms if easier.

This file defines three types:

  • SAG_MUTEX_T - mutexe
  • SAG_SEMA_T - semaphors
  • SAG_THREAD_T - threa.

There are functions to init and destroy mutex and semaphore variables:

The mutex/semaphore-specific functions:

Threads are created with:

SAG_THREAD_T id; SAG_THREAD_CREATE(id, func, args)

func must match this type:

SAG_THREAD_RET_T func(void* args) { ... SAG_THREAD_RETURN; }

You can join a running thread with SAG_THREAD_JOIN(id);

Macro Definition Documentation

◆ SAG_MUTEX_DESTROY

#define SAG_MUTEX_DESTROY (   m)    pthread_mutex_destroy(&m)

Destroy a mutex.

◆ SAG_MUTEX_INIT

#define SAG_MUTEX_INIT (   m)    pthread_mutex_init(&m, NULL)

Create a mutex.

◆ SAG_MUTEX_LOCK

#define SAG_MUTEX_LOCK (   m)    pthread_mutex_lock(&m)

Lock a mutex.

In most situations com::softwareag::connectivity::SAG_LOCK_GUARD should be used instead of calling this directly.

◆ SAG_MUTEX_T

#define SAG_MUTEX_T   pthread_mutex_t

A helper that provides cross-platform mutex functionality for use on old compilers.

Note that you should only use this if your compiler is too old to support C++11 threading primitives. On modern compilers you should use std::mutex.

◆ SAG_MUTEX_UNLOCK

#define SAG_MUTEX_UNLOCK (   m)    pthread_mutex_unlock(&m)

Unlock a mutex.

In most situations com::softwareag::connectivity::SAG_LOCK_GUARD should be used instead of calling this directly.

◆ SAG_SEMA_DESTROY

#define SAG_SEMA_DESTROY (   s)    sem_destroy(&s)

Destroy a semaphore.

◆ SAG_SEMA_INIT

#define SAG_SEMA_INIT (   s)    sem_init(&s, 0, 0)

Create a semaphore.

◆ SAG_SEMA_POST

#define SAG_SEMA_POST (   s)    sem_post(&s)

Post/signal/release a semaphore.

◆ SAG_SEMA_T

#define SAG_SEMA_T   sem_t

A helper that provides cross-platform semaphore functionality for use on old compilers.

Note that you should only use this if your compiler is too old to support C++11 threading primitives. On modern compilers you should use std::mutex and std::condition_variable.

◆ SAG_SEMA_WAIT

#define SAG_SEMA_WAIT (   s)    {while (sem_wait(&s));}

Wait/acquire a semaphore.

◆ SAG_THREAD_CREATE

#define SAG_THREAD_CREATE (   id,
  func,
  params 
)    pthread_create(& id, NULL, func, params)

Create a thread which will call func with params.

id will be written with the thread ID

Note that you should only use this if your compiler is too old to support C++11 threading primitives. On modern compilers you should use std::thread.

◆ SAG_THREAD_JOIN

#define SAG_THREAD_JOIN (   id)    pthread_join(id, NULL)

Wait the given thread to terminate.

◆ SAG_THREAD_RET_T

#define SAG_THREAD_RET_T   void*

The return type of a thread function.

◆ SAG_THREAD_RETURN

#define SAG_THREAD_RETURN   return NULL;

The statement to return from a thread function.

◆ SAG_THREAD_T

#define SAG_THREAD_T   pthread_t

The type of a thread.