summaryrefslogtreecommitdiffstats
path: root/src/gui/embedded/qwslock.cpp
blob: 9914a247fbfc1974901f4bd230e2b46fc4293a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwslock_p.h"

#ifndef QT_NO_QWS_MULTIPROCESS

#include "qwssignalhandler_p.h"

#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#ifndef QT_POSIX_IPC
#include <sys/sem.h>
#endif
#include <sys/time.h>
#include <time.h>
#ifdef Q_OS_LINUX
#include <linux/version.h>
#endif
#include <unistd.h>

#include <private/qcore_unix_p.h>

QT_BEGIN_NAMESPACE

#ifdef QT_NO_SEMAPHORE
#error QWSLock currently requires semaphores
#endif

#ifdef QT_POSIX_IPC
#include <QtCore/QAtomicInt>

static QBasicAtomicInt localUniqueId = Q_BASIC_ATOMIC_INITIALIZER(1);
#endif

QWSLock::QWSLock(int id) : semId(id)
{
    static unsigned short initialValues[3] = { 1, 1, 0 };

#ifndef QT_NO_QWS_SIGNALHANDLER
    QWSSignalHandler::instance()->addWSLock(this);
#endif

#ifndef QT_POSIX_IPC
    if (semId == -1) {
        semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
        if (semId == -1) {
            perror("QWSLock::QWSLock");
            qFatal("Unable to create semaphore");
        }

        qt_semun semval;
        semval.array = initialValues;
        if (semctl(semId, 0, SETALL, semval) == -1) {
            perror("QWSLock::QWSLock");
            qFatal("Unable to initialize semaphores");
        }
    }
#else
    sems[0] = sems[1] = sems[2] = SEM_FAILED;
    owned = false;

    if (semId == -1) {
        // ### generate really unique IDs
        semId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
        owned = true;
    }

    QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
    QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
    for (int i = 0; i < 3; ++i) {
        if (owned)
            sem_unlink(keys[i].constData());
        do {
            sems[i] = sem_open(keys[i].constData(), (owned ? O_CREAT : 0), 0666, initialValues[i]);
        } while (sems[i] == SEM_FAILED && errno == EINTR);
        if (sems[i] == SEM_FAILED) {
            perror("QWSLock::QWSLock");
            qFatal("Unable to %s semaphore", (owned ? "create" : "open"));
        }
    }
#endif

    lockCount[0] = lockCount[1] = 0;
}

QWSLock::~QWSLock()
{
#ifndef QT_NO_QWS_SIGNALHANDLER
    QWSSignalHandler::instance()->removeWSLock(this);
#endif

    if (semId != -1) {
#ifndef QT_POSIX_IPC
        qt_semun semval;
        semval.val = 0;
        semctl(semId, 0, IPC_RMID, semval);
        semId = -1;
#else
        // emulate the SEM_UNDO behavior for the BackingStore lock
        while (hasLock(BackingStore))
            unlock(BackingStore);

        QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
        QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
        for (int i = 0; i < 3; ++i) {
            if (sems[i] != SEM_FAILED) {
                sem_close(sems[i]);
                sems[i] = SEM_FAILED;
            }
            if (owned)
                sem_unlink(keys[i].constData());
        }
#endif
    }
}

bool QWSLock::up(unsigned short semNum)
{
    int ret;

#ifndef QT_POSIX_IPC
    sembuf sops = { semNum, 1, 0 };
    // As the BackingStore lock is a mutex, and only one process may own
    // the lock, it's safe to use SEM_UNDO. On the other hand, the
    // Communication lock is locked by the client but unlocked by the
    // server and therefore can't use SEM_UNDO.
    if (semNum == BackingStore)
        sops.sem_flg |= SEM_UNDO;

    EINTR_LOOP(ret, semop(semId, &sops, 1));
#else
    ret = sem_post(sems[semNum]);
#endif
    if (ret == -1) {
        qDebug("QWSLock::up(): %s", strerror(errno));
        return false;
    }

    return true;
}

bool QWSLock::down(unsigned short semNum, int)
{
    int ret;

#ifndef QT_POSIX_IPC
    sembuf sops = { semNum, -1, 0 };
    // As the BackingStore lock is a mutex, and only one process may own
    // the lock, it's safe to use SEM_UNDO. On the other hand, the
    // Communication lock is locked by the client but unlocked by the
    // server and therefore can't use SEM_UNDO.
    if (semNum == BackingStore)
        sops.sem_flg |= SEM_UNDO;

    EINTR_LOOP(ret, semop(semId, &sops, 1));
#else
    EINTR_LOOP(ret, sem_wait(sems[semNum]));
#endif
    if (ret == -1) {
        qDebug("QWSLock::down(): %s", strerror(errno));
        return false;
    }

    return true;
}

int QWSLock::getValue(unsigned short semNum) const
{
    int ret;
#ifndef QT_POSIX_IPC
    ret = semctl(semId, semNum, GETVAL, 0);
#else
    if (sem_getvalue(sems[semNum], &ret) == -1)
        ret = -1;
#endif
    if (ret == -1)
        qDebug("QWSLock::getValue(): %s", strerror(errno));
    return ret;
}

bool QWSLock::lock(LockType type, int timeout)
{
    if (type == RegionEvent)
        return up(type);

    if (lockCount[type] > 0) {
        ++lockCount[type];
        return true;
    }

    if (down(type, timeout)) {
        ++lockCount[type];
        return true;
    }

    return false;
}

bool QWSLock::hasLock(LockType type)
{
    if (type == RegionEvent)
        return getValue(type) == 0;

    return lockCount[type] > 0;
}

void QWSLock::unlock(LockType type)
{
    if (type == RegionEvent) {
        down(type, -1);
        return;
    }

    if (lockCount[type] > 0) {
        --lockCount[type];
        if (lockCount[type] > 0)
            return;
    }

    up(type);
}

bool QWSLock::wait(LockType type, int timeout)
{
    bool ok = down(type, timeout);
    if (ok)
        unlock(type);
    return ok;
}

QT_END_NAMESPACE

#endif // QT_NO_QWS_MULTIPROCESS