a7be0fdf43ee1fbf9dcf044fe1f5ea7425c93959
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtsemaphore.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtsemaphore.h"
25
26 /**
27 \class QxtSemaphore QxtSemaphore
28 \ingroup QxtCore
29 \brief system wide semaphore (former QxtSingleInstance)
30
31
32 \code
33         QxtSemaphore instance("com.mycompany.foobla.uniquestring");
34         if(!instance.trylock())
35                 {
36                 qDebug("already started")
37                 }
38
39 \endcode
40
41 Note that the semaphore is autoaticly unlocked on destruction, but not on segfault,sigkill,etc...!
42 */
43
44 #ifdef Q_WS_WIN
45 #include "Windows.h"
46
47 class QxtSemaphorePrivate : public QxtPrivate<QxtSemaphore>
48 {
49 public:
50     QString name;
51     unsigned sem_m;
52     void init()
53     {
54         sem_m=0;
55     }
56
57     bool trylock()
58     {
59         sem_m = (unsigned ) CreateSemaphoreA ( NULL , 1 , 2 , qPrintable("Global\\"+name) );
60         if (sem_m == 0 )
61             return false;
62         return true;
63     }
64     bool unlock()
65     {
66         if (sem_m==0)
67             return false;
68         return CloseHandle((void *)sem_m);
69     }
70 };
71
72
73
74 #else
75
76 #include <semaphore.h>
77 #include <fcntl.h>
78 #include <errno.h>
79
80 class QxtSemaphorePrivate : public QxtPrivate<QxtSemaphore>
81 {
82 public:
83     QString name;
84     sem_t* m_sem;
85     bool s_N;
86     void init()
87     {
88         s_N=false;
89         m_sem=NULL;
90     }
91
92     bool trylock()
93     {
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))
96         {
97             m_sem=NULL;
98             s_N=true;
99             return false;
100         }
101         s_N=false;
102         return true;
103     }
104     bool unlock()
105     {
106         if (m_sem==NULL)
107             return false;
108         if (!s_N)
109         {
110             sem_post(m_sem);
111         }
112         return (sem_close(m_sem)==0);
113     }
114 };
115
116 #endif
117
118 QxtSemaphore::QxtSemaphore(QString uniqueID)
119 {
120     qxt_d().name=uniqueID;
121     qxt_d().init();
122 }
123
124 QxtSemaphore::~QxtSemaphore()
125 {
126     unlock();
127 }
128
129 bool QxtSemaphore::trylock()
130 {
131     return qxt_d().trylock();
132 }
133 bool QxtSemaphore::unlock()
134 {
135     return qxt_d().unlock();
136 }