Skip to content

Commit 6995160

Browse files
committed
Fixes #152 ("Handle EINTR in shared_memory_object")
1 parent 4bec5d1 commit 6995160

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

doc/interprocess.qbk

+3-1
Original file line numberDiff line numberDiff line change
@@ -6778,11 +6778,13 @@ thank them:
67786778
You can obtain the pre-Boost 1.87 ABI #defining `BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI` to `1` before including Boost.Interprocess headers.
67796779

67806780
* Fixed bugs:
6781+
* [@https://github.com/boostorg/interprocess/issues/152 GitHub #152 (['"Handle EINTR in shared_memory_object"])].
6782+
* [@https://github.com/boostorg/interprocess/issues/173 GitHub #173 (['"Managed shared memory segment value not aligned"])].
67816783
* [@https://github.com/boostorg/interprocess/issues/192 GitHub #192 (['"managed_windows_shared_memory crash on destruction"])].
67826784
* [@https://github.com/boostorg/interprocess/issues/199 GitHub #199 (['"missing/misused m_is_wide in char_wchar_holder assignment operators"])].
67836785
* [@https://github.com/boostorg/interprocess/issues/210 GitHub #210 (['"Bug in boost::interprocess::ipcdetail::sync_handles::obtain_mutex"])].
67846786
* [@https://github.com/boostorg/interprocess/issues/215 GitHub #215 (['"Alignment problem with boost/interprocess/segment_manager on SPARC 32Bit"])].
6785-
6787+
* [@https://github.com/boostorg/interprocess/issues/217 GitHub #217 (['"managed_map_file find_or_construct does not return Cache aligned memory"])].
67866788

67876789
[endsect]
67886790

include/boost/interprocess/shared_memory_object.hpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ class shared_memory_object
186186
file_handle_t m_handle;
187187
mode_t m_mode;
188188
char_wchar_holder m_filename;
189+
190+
#ifdef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS
191+
static int eintr_aware_shm_open(const char* name, int oflag, ::mode_t mode);
192+
#endif
193+
189194
#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
190195
};
191196

@@ -374,13 +379,13 @@ inline bool shared_memory_object::priv_open_or_create
374379
case ipcdetail::DoOpen:
375380
{
376381
//No oflag addition
377-
m_handle = shm_open(fname.c_str(), oflag, unix_perm);
382+
m_handle = eintr_aware_shm_open(fname.c_str(), oflag, unix_perm);
378383
}
379384
break;
380385
case ipcdetail::DoCreate:
381386
{
382387
oflag |= (O_CREAT | O_EXCL);
383-
m_handle = shm_open(fname.c_str(), oflag, unix_perm);
388+
m_handle = eintr_aware_shm_open(fname.c_str(), oflag, unix_perm);
384389
if(m_handle >= 0){
385390
::fchmod(m_handle, unix_perm);
386391
}
@@ -392,14 +397,14 @@ inline bool shared_memory_object::priv_open_or_create
392397
//with "O_CREAT" only we don't know if we've created or opened the shm.
393398
while(true){
394399
//Try to create shared memory
395-
m_handle = shm_open(fname.c_str(), oflag | (O_CREAT | O_EXCL), unix_perm);
400+
m_handle = eintr_aware_shm_open(fname.c_str(), oflag | (O_CREAT | O_EXCL), unix_perm);
396401
//If successful change real permissions
397402
if(m_handle >= 0){
398403
::fchmod(m_handle, unix_perm);
399404
}
400405
//If already exists, try to open
401406
else if(errno == EEXIST){
402-
m_handle = shm_open(fname.c_str(), oflag, unix_perm);
407+
m_handle = eintr_aware_shm_open(fname.c_str(), oflag, unix_perm);
403408
//If open fails and errno tells the file does not exist
404409
//(shm was removed between creation and opening tries), just retry
405410
if(m_handle < 0 && errno == ENOENT){
@@ -486,6 +491,15 @@ inline void shared_memory_object::priv_close()
486491
}
487492
}
488493

494+
inline int shared_memory_object::eintr_aware_shm_open(const char* name, int oflag, ::mode_t mode)
495+
{
496+
int shm_open_ret;
497+
do {
498+
shm_open_ret = shm_open(name, oflag, mode);
499+
} while (shm_open_ret == -1 && errno == EINTR);
500+
return shm_open_ret;
501+
}
502+
489503
#endif
490504

491505
//!A class that stores the name of a shared memory

0 commit comments

Comments
 (0)