52 #include <sys/types.h> 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);
182 LOGERROR(
"shm_t structure is NULL.");
188 else if( shmctl(pshm->
m_shmId, IPC_RMID,
NULL) < 0 )
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);
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);
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);
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);
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);
413 static pthread_mutex_t * GlobalMutex;
414 static char * myLine = PROG1;
419 void * getSharedMemPtr(key_t key)
424 sizeof(pthread_mutex_t) +
sizeof(pthread_once_t),
425 IPC_CREAT | IPC_EXCL | 0x1b6);
432 printf(
"Could not create shared mem.\n");
433 printf(
"Will attempt to find it.\n");
436 sizeof(pthread_mutex_t) +
sizeof(pthread_once_t),
443 printf(
"\tCouldnt find it either\n");
449 fp = fopen(
"MutextestFile.txt",
"r+");
453 printf(
"\tFound shared memory");
454 void *
const poSharedMem = shmat(shmid,
NULL, 0);
456 if(((
void *)-1) == poSharedMem)
458 printf(
"Could not attatch shared memory to address space.\n");
465 printf(
"Shared memory attached\n");
472 fp = fopen(
"MutextestFile.txt",
"w+");
476 printf(
"Shared memory created.\n");
478 void *
const poSharedMem = shmat(shmid,
NULL, 0);
480 if(((
void *)-1) == poSharedMem)
482 printf(
"Could not attatch shared memory to address space.\n");
489 printf(
"Shared memory attached\n");
497 int detatchFromSharedMem(
void * poSharedMem)
499 printf(
"Marking shared memory for destruction and detaching from it.\n");
500 shmctl(shmid, IPC_RMID,
NULL);
501 return shmdt(poSharedMem);
510 void * poSharedMem = getSharedMemPtr();
511 GlobalMutex = (pthread_mutex_t *)poSharedMem;
513 pthread_once_t * pOnce = (pthread_once_t *)( ((
char *)poSharedMem) +
sizeof(pthread_mutex_t) );
515 pthread_once(pOnce, sharedMemoryMutexInit);
517 if (GlobalMutex ==
NULL)
524 for (
int i =0; i < 100000; i ++)
526 mutexLockResult = pthread_mutex_lock(GlobalMutex);
527 if (0 == mutexLockResult)
529 fp = fopen(
"MutextestFile.txt",
"r+");
532 fseek(fp, 0, SEEK_END);
533 fprintf(fp,
"%4d - ", i+1);
538 pthread_mutex_unlock(GlobalMutex);
540 else if (EOWNERDEAD == mutexLockResult)
542 fp = fopen(
"MutextestFile.txt",
"r+");
545 fseek(fp, 0, SEEK_END);
546 fprintf(fp,
"%4d - ", i+1);
551 pthread_mutex_consistent_np(GlobalMutex);
552 pthread_mutex_unlock(GlobalMutex);
557 if(
NULL != GlobalMutex)
559 detatchFromSharedMem(GlobalMutex);
int shm_open(key_t key, size_t size, shm_mem_init_func_t mem_init, shm_t *pshm)
Open shared memory segement.
int shm_mutex_unlock(shm_mutex_t *pshmmutex)
Unlock a lock the mutex.
#define LOGDIAG3(fmt,...)
Standard Diagnostic Level 3 logging.
int shm_mutex_lock(shm_mutex_t *pshmmutex)
Lock the mutex.
int shm_close(shm_t *pshm)
Close shared memory segement.
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define RC_ERROR
common function error return code
#define LOGERROR(fmt,...)
Standard Error logging.
void(* shm_mem_init_func_t)(key_t key, void *add, size_t size)
User-supplied shared memory initialization function.
Shared memory structure type.
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
int main(int argc, char *argv[])
Example main.
static void shm_mutex_create(key_t key, void *addr, size_t size)
Create the share memory mutex.
RoadNarrows Robotics common configuration file.
int m_shmId
shared memory identifier
int shm_mutex_init(key_t key, shm_mutex_t *pshmmutex)
Create and initialize a shared memory mutex.
int shm_mutex_destroy(shm_mutex_t *pshmmutex)
Destroy a shared memory mutex.
#define SHM_MUTEX_N_TRIES
maximum number of tries to acquire lock
#define SHM_MUTEX_T_TRIES
usec time between tries to acquire lock
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
int shm_mutex_trylock(shm_mutex_t *pshmmutex)
Try to lock the mutex.