Assume that the shared POSIX mutex has allready been initialized (using PTHREAD_PROCESS_SHARED).
Then, consider the following procedure:
typedef struct {
pthread_mutex_t mutex;
// ...
} Shared;
Shared *shared = (Shared *)mmap(...); // MAP
pthread_mutex_lock(&shared->mutex); // LOCK
// REMAP
munmap(shared, ...);
shared = (Shared *)mmap(...);
pthread_mutex_unlock(&shared->mutex); // UNLOCK
Does POSIX guarantee that this will work as "naively" intended?
As far as I can see, none of the relevant man pages mention such a scenario.
Also, what about the following Linux specific alternative:
Shared *shared = (Shared *)mmap(...); // MAP
pthread_mutex_lock(&shared->mutex); // LOCK
shared = (Shared *)mremap(shared, ...); // MREMAP_MAYMOVE
pthread_mutex_unlock(&shared->mutex); // UNLOCK
I can imagine, for example, a PTHREADS implementation that will store a pointer to a locked mutex somewhere inside the process. If the mutex is configured as robust (PTHREAD_MUTEX_ROBUST), it would allow the implementation to mark the mutex as 'abandoned' if the process dies while the mutex is locked.
I have no idea whether such a scheme would actually work, whether it is allowed by POSIX, or how mutex robustness is actually implemented on any platform, but if an implementation along these lines would work, and would be valid according to POSIX, then the remapping scenario above would have undefined behaviour.