From f0838d63f30556cb7c74257a4612b3d418153ce4 Mon Sep 17 00:00:00 2001 From: Stefan Neis Date: Sat, 21 Feb 2009 13:52:20 +0000 Subject: [PATCH] Added wxSingleInstanceChecker for OS/2 (patch #10491). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59068 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- build/bakefiles/files.bkl | 1 + src/os2/snglinst.cpp | 132 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/os2/snglinst.cpp diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index ef2cc9efb1..028f35933f 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -235,6 +235,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/unix/timerunx.cpp src/os2/dir.cpp src/os2/mimetype.cpp + src/os2/snglinst.cpp src/os2/stdpaths.cpp src/os2/thread.cpp src/os2/utils.cpp diff --git a/src/os2/snglinst.cpp b/src/os2/snglinst.cpp new file mode 100644 index 0000000000..eb95aa3c39 --- /dev/null +++ b/src/os2/snglinst.cpp @@ -0,0 +1,132 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: os2/snglinst.cpp +// Purpose: implements wxSingleInstanceChecker class for OS/2 using +// named mutexes +// Author: Vadim Zeitlin +// Modified by: Lauri Nurmi (modified for OS/2) +// Created: 08.02.2009 +// RCS-ID: $Id$ +// Copyright: (c) 2001 Vadim Zeitlin +// License: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#if wxUSE_SNGLINST_CHECKER + +#ifndef WX_PRECOMP + #include "wx/string.h" + #include "wx/log.h" +#endif //WX_PRECOMP + +#include "wx/snglinst.h" + +#define INCL_DOSSEMAPHORES +#define INCL_DOSERRORS +#include + +#include "wx/os2/private.h" + +// ---------------------------------------------------------------------------- +// wxSingleInstanceCheckerImpl: the real implementation class +// ---------------------------------------------------------------------------- + +class WXDLLEXPORT wxSingleInstanceCheckerImpl +{ +public: + wxSingleInstanceCheckerImpl() + { + m_hMutex = NULL; + m_anotherRunning = false; + } + + bool Create(const wxString& name) + { + wxString semName; + semName.Printf(wxT("\\SEM32\\%s"), name.c_str()); + int rc = DosCreateMutexSem(semName.c_str(), &m_hMutex, DC_SEM_SHARED, 1); + + if ( rc == NO_ERROR ) { + m_anotherRunning = false; + return true; + } else if ( rc == ERROR_DUPLICATE_NAME ) { + m_anotherRunning = true; + return true; + } else { + m_anotherRunning = false; // we don't know for sure in this case + wxLogLastError(_T("DosCreateMutexSem")); + return false; + } + } + + bool IsAnotherRunning() const + { + return m_anotherRunning; + } + + ~wxSingleInstanceCheckerImpl() + { + if ( m_hMutex ) + { + if ( !::DosCloseMutexSem(m_hMutex) ) + { + wxLogLastError(_T("DosCloseMutexSem")); + } + } + } + +private: + // if true, creating the mutex either succeeded + // or we know it failed because another app is running. + bool m_anotherRunning; + + // the mutex handle, may be NULL + HMTX m_hMutex; + + DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl) +}; + +// ============================================================================ +// wxSingleInstanceChecker implementation +// ============================================================================ + +bool wxSingleInstanceChecker::Create(const wxString& name, + const wxString& WXUNUSED(path)) +{ + wxASSERT_MSG( !m_impl, + _T("calling wxSingleInstanceChecker::Create() twice?") ); + + // creating unnamed mutex doesn't have the same semantics! + wxASSERT_MSG( !name.empty(), _T("mutex name can't be empty") ); + + m_impl = new wxSingleInstanceCheckerImpl; + + return m_impl->Create(name); +} + +bool wxSingleInstanceChecker::IsAnotherRunning() const +{ + wxCHECK_MSG( m_impl, false, _T("must call Create() first") ); + + return m_impl->IsAnotherRunning(); +} + +wxSingleInstanceChecker::~wxSingleInstanceChecker() +{ + delete m_impl; +} + +#endif // wxUSE_SNGLINST_CHECKER -- 2.45.2