27 bool freeAttr =
false;
31 int key = ftok(NAMED_MEMORY, ID_TAG);
34 printf(
"Unable to name shared memory\n");
39 int m_iShmid = shmget(key, TOTAL_SIZE, READ_WRITE_PERMISSIONS | IPC_CREAT | IPC_EXCL);
45 m_iShmid = shmget(key, TOTAL_SIZE, READ_WRITE_PERMISSIONS);
49 printf(
"Unable to create shared memory - %s\n",strerror(errno));
53 printf(
"Attached to the existing shared memory\n");
56 printf(
"Created new shared memory\n");
59 mutex =
reinterpret_cast<pthread_mutex_t*
>(shmat(m_iShmid,
NULL, 0));
60 if (
reinterpret_cast<pthread_mutex_t*
>(-1) == mutex)
62 printf(
"Unable to attach shared memory to the process - %s\n",strerror(errno));
67 pthread_mutex_t* mutex;
68 pthread_mutexattr_t mutexAttr;
69 ret = pthread_mutexattr_init(&mutexAttr);
72 printf(
"pthread_mutexattr_init failed - err=%d\n",ret);
75 ret = pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
78 printf(
"pthread_mutexattr_setpshared failed - err=%d\n",ret);
81 ret = pthread_mutexattr_setrobust_np(&mutexAttr, PTHREAD_MUTEX_ROBUST_NP);
84 printf(
"pthread_mutexattr_setrobust_np failed - err=%d\n",ret);
87 ret = pthread_mutex_init(mutex, &mutexAttr);
90 printf(
"pthread_mutex_init failed - err=%d\n",ret);
97 pthread_mutexattr_t attr;
98 int rc = pthread_mutexattr_init(&attr);
108 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
118 rc = pthread_mutex_init(&mMutex, &attr);
129 rc = pthread_mutexattr_destroy(&attr);
142 rc = pthread_mutex_destroy(&mMutex);
156 if (mOwner && (mOwner == pthread_self()))
167 if (timeout == 0xffffffff)
169 ts.tv_sec += 60 * 60 * 24 * 365;
174 uint64_t bigtimeout = (uint64_t) timeout *
MIL_2_NSEC;
176 ts.tv_nsec += bigtimeout %
MAX_NSEC;
189 if (err == ETIMEDOUT)
198 mOwner = pthread_self();
208 if (mOwner != pthread_self())
222 pthread_mutex_unlock(&mMutex);