1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtCore module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtsemaphore.h"
27 \class QxtSemaphore QxtSemaphore
29 \brief system wide semaphore (former QxtSingleInstance)
33 QxtSemaphore instance("com.mycompany.foobla.uniquestring");
34 if(!instance.trylock())
36 qDebug("already started")
41 Note that the semaphore is autoaticly unlocked on destruction, but not on segfault,sigkill,etc...!
47 class QxtSemaphorePrivate : public QxtPrivate<QxtSemaphore>
59 sem_m = (unsigned ) CreateSemaphoreA ( NULL , 1 , 2 , qPrintable("Global\\"+name) );
68 return CloseHandle((void *)sem_m);
76 #include <semaphore.h>
80 class QxtSemaphorePrivate : public QxtPrivate<QxtSemaphore>
94 m_sem=sem_open(qPrintable(name), O_CREAT, S_IRUSR | S_IWUSR, 1);
95 if (m_sem==(sem_t*)(SEM_FAILED) || sem_trywait(m_sem))
112 return (sem_close(m_sem)==0);
118 QxtSemaphore::QxtSemaphore(QString uniqueID)
120 qxt_d().name=uniqueID;
124 QxtSemaphore::~QxtSemaphore()
129 bool QxtSemaphore::trylock()
131 return qxt_d().trylock();
133 bool QxtSemaphore::unlock()
135 return qxt_d().unlock();