OSDN Git Service

Removed TcpSocketThread.
[greensite/jasmine.git] / file / qtlockedfile_win.cxx
1 /****************************************************************************
2 ** 
3 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 ** 
7 ** This file is part of a Qt Solutions component.
8 **
9 ** Commercial Usage  
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Solutions Commercial License Agreement provided
12 ** with the Software or, alternatively, in accordance with the terms
13 ** contained in a written agreement between you and Nokia.
14 ** 
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file.  Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 ** 
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 ** 
28 ** GNU General Public License Usage 
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file.  Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 ** 
36 ** Please note Third Party Software included with Qt Solutions may impose
37 ** additional restrictions and it is the user's responsibility to ensure
38 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
39 ** Solutions Commercial license and the relevant license of the Third
40 ** Party Software they are using.
41 ** 
42 ** If you are unsure which license is appropriate for your use, please
43 ** contact Nokia at qt-info@nokia.com.
44 ** 
45 ****************************************************************************/
46
47 #include "qtlockedfile.h"
48 #include <qt_windows.h>
49 #include <QtCore/QFileInfo>
50
51 #define MUTEX_PREFIX "QtLockedFile mutex "
52 // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
53 #define MAX_READERS MAXIMUM_WAIT_OBJECTS
54
55 Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
56 {
57     if (mutexname.isEmpty()) {
58         QFileInfo fi(*this);
59         mutexname = QString::fromLatin1(MUTEX_PREFIX)
60                     + fi.absoluteFilePath().toLower();
61     }
62     QString mname(mutexname);
63     if (idx >= 0)
64         mname += QString::number(idx);
65
66     Qt::HANDLE mutex;
67     if (doCreate) {
68         QT_WA( { mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); },
69                { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } );
70         if (!mutex) {
71             qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
72             return 0;
73         }
74     }
75     else {
76         QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); },
77                { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } );
78         if (!mutex) {
79             if (GetLastError() != ERROR_FILE_NOT_FOUND)
80                 qErrnoWarning("QtLockedFile::lock(): OpenMutex failed");
81             return 0;
82         }
83     }
84     return mutex;
85 }
86
87 bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
88 {
89     Q_ASSERT(mutex);
90     DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
91     switch (res) {
92     case WAIT_OBJECT_0:
93     case WAIT_ABANDONED:
94         return true;
95         break;
96     case WAIT_TIMEOUT:
97         break;
98     default:
99         qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
100     }
101     return false;
102 }
103
104
105
106 bool QtLockedFile::lock(LockMode mode, bool block)
107 {
108     if (!isOpen()) {
109         qWarning("QtLockedFile::lock(): file is not opened");
110         return false;
111     }
112
113     if (mode == NoLock)
114         return unlock();
115
116     if (mode == m_lock_mode)
117         return true;
118
119     if (m_lock_mode != NoLock)
120         unlock();
121
122     if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
123         return false;
124
125     if (!waitMutex(wmutex, block))
126         return false;
127
128     if (mode == ReadLock) {
129         int idx = 0;
130         for (; idx < MAX_READERS; idx++) {
131             rmutex = getMutexHandle(idx, false);
132             if (!rmutex || waitMutex(rmutex, false))
133                 break;
134             CloseHandle(rmutex);
135         }
136         bool ok = true;
137         if (idx >= MAX_READERS) {
138             qWarning("QtLockedFile::lock(): too many readers");
139             rmutex = 0;
140             ok = false;
141         }
142         else if (!rmutex) {
143             rmutex = getMutexHandle(idx, true);
144             if (!rmutex || !waitMutex(rmutex, false))
145                 ok = false;
146         }
147         if (!ok && rmutex) {
148             CloseHandle(rmutex);
149             rmutex = 0;
150         }
151         ReleaseMutex(wmutex);
152         if (!ok)
153             return false;
154     }
155     else {
156         Q_ASSERT(rmutexes.isEmpty());
157         for (int i = 0; i < MAX_READERS; i++) {
158             Qt::HANDLE mutex = getMutexHandle(i, false);
159             if (mutex)
160                 rmutexes.append(mutex);
161         }
162         if (rmutexes.size()) {
163             DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
164                                                TRUE, block ? INFINITE : 0);
165             if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) {
166                 if (res != WAIT_TIMEOUT)
167                     qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
168                 m_lock_mode = WriteLock;  // trick unlock() to clean up - semiyucky
169                 unlock();
170                 return false;
171             }
172         }
173     }
174
175     m_lock_mode = mode;
176     return true;
177 }
178
179 bool QtLockedFile::unlock()
180 {
181     if (!isOpen()) {
182         qWarning("QtLockedFile::unlock(): file is not opened");
183         return false;
184     }
185
186     if (!isLocked())
187         return true;
188
189     if (m_lock_mode == ReadLock) {
190         ReleaseMutex(rmutex);
191         CloseHandle(rmutex);
192         rmutex = 0;
193     }
194     else {
195         foreach(Qt::HANDLE mutex, rmutexes) {
196             ReleaseMutex(mutex);
197             CloseHandle(mutex);
198         }
199         rmutexes.clear();
200         ReleaseMutex(wmutex);
201     }
202
203     m_lock_mode = QtLockedFile::NoLock;
204     return true;
205 }
206
207 QtLockedFile::~QtLockedFile()
208 {
209     if (isOpen())
210         unlock();
211     if (wmutex)
212         CloseHandle(wmutex);
213 }