diff --git a/Foundation/include/Poco/NamedEvent_UNIX.h b/Foundation/include/Poco/NamedEvent_UNIX.h index f4b5255f793..4359e387a0a 100644 --- a/Foundation/include/Poco/NamedEvent_UNIX.h +++ b/Foundation/include/Poco/NamedEvent_UNIX.h @@ -43,6 +43,7 @@ class Foundation_API NamedEventImpl sem_t* _sem; #else int _semid; // semaphore id + bool _createdId; // semaphore id was created with this instance #endif }; diff --git a/Foundation/src/NamedEvent_UNIX.cpp b/Foundation/src/NamedEvent_UNIX.cpp index 686d770c8df..22eaa335317 100644 --- a/Foundation/src/NamedEvent_UNIX.cpp +++ b/Foundation/src/NamedEvent_UNIX.cpp @@ -58,6 +58,7 @@ NamedEventImpl::NamedEventImpl(const std::string& name): if ((long) _sem == (long) SEM_FAILED) throw SystemException(Poco::format("cannot create named mutex %s (sem_open() failed, errno=%d)", fileName, errno), _name); #else + _createdId = false; int fd = open(fileName.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1 && errno == ENOENT) fd = open(fileName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -71,6 +72,7 @@ NamedEventImpl::NamedEventImpl(const std::string& name): _semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL); if (_semid >= 0) { + _createdId = true; union semun arg; arg.val = 0; semctl(_semid, 0, SETVAL, arg); @@ -88,6 +90,11 @@ NamedEventImpl::~NamedEventImpl() { #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) sem_close(_sem); +#else + if (_createdId) + { + semctl(_semid, 0, IPC_RMID); + } #endif } diff --git a/Foundation/testsuite/src/NamedEventTest.cpp b/Foundation/testsuite/src/NamedEventTest.cpp index d0dd5043ee1..6cc810fd806 100644 --- a/Foundation/testsuite/src/NamedEventTest.cpp +++ b/Foundation/testsuite/src/NamedEventTest.cpp @@ -15,7 +15,7 @@ #include "Poco/Thread.h" #include "Poco/Runnable.h" #include "Poco/Timestamp.h" - +#include "Poco/Exception.h" using Poco::NamedEvent; using Poco::Thread; @@ -101,6 +101,27 @@ void NamedEventTest::testNamedEvent() } +void NamedEventTest::testCreateManyNamedEvents() +{ + std::string name; + try + { + int i = 0; + for (; i < 40000; i++) + { + name = std::string("TestEvent ") + std::to_string(i); + NamedEvent* ne = new NamedEvent(name); + delete ne; + } + assertEqual(i, 40000); + } + catch (const Poco::Exception& e) + { + fail (std::string("Failed creating named event: ").append(name).append(" ").append(e.displayText())); + } +} + + void NamedEventTest::setUp() { } @@ -116,6 +137,7 @@ CppUnit::Test* NamedEventTest::suite() CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest"); CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent); + CppUnit_addTest(pSuite, NamedEventTest, testCreateManyNamedEvents); return pSuite; } diff --git a/Foundation/testsuite/src/NamedEventTest.h b/Foundation/testsuite/src/NamedEventTest.h index 5b3d0c9605b..57aefeaff1a 100644 --- a/Foundation/testsuite/src/NamedEventTest.h +++ b/Foundation/testsuite/src/NamedEventTest.h @@ -25,6 +25,7 @@ class NamedEventTest: public CppUnit::TestCase ~NamedEventTest(); void testNamedEvent(); + void testCreateManyNamedEvents(); void setUp(); void tearDown();