Shared memory routines.
More...
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/shm.h"
Go to the source code of this file.
Shared memory routines.
- LastChangedDate
- 2013-02-07 16:51:44 -0700 (Thu, 07 Feb 2013)
- Rev
- 2675
- Basic convenience funtions.
- Shared memory implementation of the POSIX thread mutex. With shared memory, the mutex may span mutilple applications, not just threads within one application.
- Other functions as needed.
- Author
- Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
- Copyright
- © 2013-2018. RoadNarrows LLC..
http://www.roadnarrows.com
All Rights Reserved
Definition in file shm.c.
| int shm_close |
( |
shm_t * |
pshm | ) |
|
Close shared memory segement.
The segment is marked for deletion, and is actually destroy after the last process detaches.
- Parameters
-
| [in,out] | pshm | Pointer to shared memory structure defined in caller's space. |
- Returns
- On success, returns OK(0).
On failure, errno is set and RC_ERROR(-1) is returned.
Definition at line 178 of file shm.c.
References LOGDIAG3, LOGERROR, LOGSYSERROR, shm_t::m_shmAddr, shm_t::m_shmId, shm_t::m_shmKey, shm_t::m_shmSize, NULL, OK, and RC_ERROR.
Referenced by shm_mutex_destroy().
182 LOGERROR(
"shm_t structure is NULL.");
188 else if( shmctl(pshm->
m_shmId, IPC_RMID,
NULL) < 0 )
#define LOGDIAG3(fmt,...)
Standard Diagnostic Level 3 logging.
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define RC_ERROR
common function error return code
#define LOGERROR(fmt,...)
Standard Error logging.
int m_shmId
shared memory identifier
key_t m_shmKey
shared memory key
size_t m_shmSize
shared memory size in bytes
void * m_shmAddr
starting addressed of attached shared memory
| static void shm_mutex_create |
( |
key_t |
key, |
|
|
void * |
addr, |
|
|
size_t |
size |
|
) |
| |
|
static |
Create the share memory mutex.
- Parameters
-
| key | Shared memory key. |
| addr | Starting address of attached shared memory. |
| size | Shared memory size. |
Definition at line 223 of file shm.c.
References LOGDIAG4, and LOGSYSERROR.
Referenced by shm_mutex_init().
225 pthread_mutex_t *pmutex = (pthread_mutex_t *)addr;
226 pthread_mutexattr_t mutexAttr;
228 pthread_mutexattr_init(&mutexAttr);
233 if(pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED) < 0)
244 else if( pthread_mutexattr_setrobust(&mutexAttr, PTHREAD_MUTEX_ROBUST) < 0 )
252 else if( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE) < 0 )
261 else if( pthread_mutex_init(pmutex, &mutexAttr) < 0 )
271 LOGDIAG4(
"Shared memory mutex created.");
274 pthread_mutexattr_destroy(&mutexAttr);
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Destroy a shared memory mutex.
The mutex is actually only destroyed when the last process attached to this mutex calls destroy.
- Parameters
-
| [in,out] | pshmmutex | Pointer to shared memory mutext structure defined in caller's space. |
- Returns
- On success, returns 0.
On failure, errno is set and returned.
Definition at line 300 of file shm.c.
References shm_close().
Referenced by runExample().
int shm_close(shm_t *pshm)
Close shared memory segement.
| int shm_mutex_init |
( |
key_t |
key, |
|
|
shm_mutex_t * |
pshmmutex |
|
) |
| |
Create and initialize a shared memory mutex.
- Parameters
-
| key | Shared memory key. |
| [in,out] | pshmmutex | Pointer to shared memory mutext structure defined in caller's space. |
- Returns
- On success, returns 0.
On failure, errno is set and returned.
Definition at line 277 of file shm.c.
References LOGDIAG3, LOGERROR, LOGSYSERROR, NULL, OK, RC_ERROR, shm_mutex_create(), and shm_open().
Referenced by runExample().
279 size_t size =
sizeof(pthread_mutex_t);
281 if( pshmmutex ==
NULL )
283 LOGERROR(
"shm_mutex_t structure is NULL.");
291 LOGSYSERROR(
"shm_mutex_init(%d=0x%x, ...)", key, key);
295 LOGDIAG3(
"Shared memory mutex initialized (key=%d=0x%x, ...)", key, key);
int shm_open(key_t key, size_t size, shm_mem_init_func_t mem_init, shm_t *pshm)
Open shared memory segement.
#define LOGDIAG3(fmt,...)
Standard Diagnostic Level 3 logging.
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define RC_ERROR
common function error return code
#define LOGERROR(fmt,...)
Standard Error logging.
static void shm_mutex_create(key_t key, void *addr, size_t size)
Create the share memory mutex.
Lock the mutex.
- Parameters
-
| [in] | pshmmutex | Pointer to shared memory mutext structure defined in caller's space. |
- Returns
- On success, returns 0.
On failure, errno is set and returned.
Definition at line 305 of file shm.c.
References LOGERROR, shm_t::m_shmAddr, NULL, OK, SHM_MUTEX_N_TRIES, and SHM_MUTEX_T_TRIES.
Referenced by lock().
307 pthread_mutex_t *pmutex;
311 if( pshmmutex ==
NULL )
313 LOGERROR(
"shm_mutex_t structure is NULL.");
318 pmutex = (pthread_mutex_t *)(pshmmutex->
m_shmAddr);
322 if( (rc = pthread_mutex_lock(pmutex)) == 0 )
331 pthread_mutex_consistent(pmutex);
#define LOGERROR(fmt,...)
Standard Error logging.
#define SHM_MUTEX_N_TRIES
maximum number of tries to acquire lock
#define SHM_MUTEX_T_TRIES
usec time between tries to acquire lock
void * m_shmAddr
starting addressed of attached shared memory
Try to lock the mutex.
- Parameters
-
| [in] | pshmmutex | Pointer to shared memory mutext structure defined in caller's space. |
- Returns
- On success, returns 0.
If the mutex is already locked, EBUSY is returned.
Otherwise, on failure, errno is set and returned.
Definition at line 350 of file shm.c.
References LOGERROR, shm_t::m_shmAddr, NULL, OK, SHM_MUTEX_N_TRIES, and SHM_MUTEX_T_TRIES.
352 pthread_mutex_t *pmutex;
356 if( pshmmutex ==
NULL )
358 LOGERROR(
"shm_mutex_t structure is NULL.");
363 pmutex = (pthread_mutex_t *)(pshmmutex->
m_shmAddr);
367 if( (rc = pthread_mutex_trylock(pmutex)) == 0 )
376 pthread_mutex_consistent(pmutex);
#define LOGERROR(fmt,...)
Standard Error logging.
#define SHM_MUTEX_N_TRIES
maximum number of tries to acquire lock
#define SHM_MUTEX_T_TRIES
usec time between tries to acquire lock
void * m_shmAddr
starting addressed of attached shared memory
Unlock a lock the mutex.
- Parameters
-
| [in] | pshmmutex | Pointer to shared memory mutext structure defined in caller's space. |
- Returns
- On success, returns 0.
On failure, errno is set and returned.
Definition at line 396 of file shm.c.
References LOGERROR, shm_t::m_shmAddr, main(), and NULL.
Referenced by unlock().
398 pthread_mutex_t *pmutex;
400 if( pshmmutex ==
NULL )
402 LOGERROR(
"shm_mutex_t structure is NULL.");
407 pmutex = (pthread_mutex_t *)(pshmmutex->
m_shmAddr);
409 return pthread_mutex_unlock(pmutex);
#define LOGERROR(fmt,...)
Standard Error logging.
void * m_shmAddr
starting addressed of attached shared memory
Open shared memory segement.
If the segment, associated by key, does not exist, then the shared memory is created and, optionally, initialized.
The shared memory segment is attached to the application address space.
- Parameters
-
| key | Shared memory key. |
| size | Shared memory size. |
| mem_init | Optional memory initializer function. Set to NULL for no initialization. |
| [in,out] | pshm | Pointer to shared memory structure defined in caller's space. |
- Returns
- On success, returns OK(0).
On failure, errno is set and RC_ERROR(-1) is returned.
Definition at line 74 of file shm.c.
References LOGDIAG3, LOGERROR, LOGSYSERROR, shm_t::m_shmAddr, shm_t::m_shmId, shm_t::m_shmKey, shm_t::m_shmSize, NULL, OK, and RC_ERROR.
Referenced by shm_mutex_init().
79 int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
86 LOGERROR(
"shm_t structure is NULL.");
101 shmId = shmget(key, size, IPC_CREAT | IPC_EXCL | perm);
109 addr = shmat(shmId,
NULL, 0);
111 if( addr == (
void *)(-1) )
115 shmctl(shmId, IPC_RMID,
NULL);
125 if( mem_init !=
NULL )
127 mem_init(key, addr, size);
134 else if( errno == EEXIST )
137 shmId = shmget(key, size, perm);
141 LOGSYSERROR(
"shmget(%d=0x%x, %zu, ...)", key, key, size);
146 addr = shmat(shmId,
NULL, 0);
148 if( addr == (
void *)(-1) )
152 shmctl(shmId, IPC_RMID,
NULL);
163 LOGSYSERROR(
"shmget(%d=0x%x, %zu, ...)", key, key, size);
173 LOGDIAG3(
"Opened shared memory (id=%d, size=%zu).", key, size);
#define LOGDIAG3(fmt,...)
Standard Diagnostic Level 3 logging.
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define RC_ERROR
common function error return code
#define LOGERROR(fmt,...)
Standard Error logging.
int m_shmId
shared memory identifier
key_t m_shmKey
shared memory key
size_t m_shmSize
shared memory size in bytes
void * m_shmAddr
starting addressed of attached shared memory