1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_arch_file_io.h"
19 APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type)
22 /* The File locking is unsuported on WCE */
25 const DWORD len = 0xffffffff;
28 flags = ((type & APR_FLOCK_NONBLOCK) ? LOCKFILE_FAIL_IMMEDIATELY : 0)
29 + (((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
30 ? 0 : LOCKFILE_EXCLUSIVE_LOCK);
31 if (apr_os_level >= APR_WIN_NT) {
32 /* Syntax is correct, len is passed for LengthLow and LengthHigh*/
34 memset (&offset, 0, sizeof(offset));
35 if (!LockFileEx(thefile->filehand, flags, 0, len, len, &offset))
36 return apr_get_os_error();
39 /* On Win9x, LockFile() never blocks. Hack in a crufty poll.
41 * Note that this hack exposes threads to being unserviced forever,
42 * in the situation that the given lock has low availability.
43 * When implemented in the kernel, LockFile will typically use
44 * FIFO or round robin distribution to ensure all threads get
45 * one crack at the lock; but in this case we can't emulate that.
47 * However Win9x are barely maintainable anyways, if the user does
48 * choose to build to them, this is the best we can do.
50 while (!LockFile(thefile->filehand, 0, 0, len, 0)) {
51 DWORD err = GetLastError();
52 if ((err == ERROR_LOCK_VIOLATION) && !(type & APR_FLOCK_NONBLOCK))
54 Sleep(500); /* pause for a half second */
55 continue; /* ... and then poll again */
57 return APR_FROM_OS_ERROR(err);
62 #endif /* !defined(_WIN32_WCE) */
65 APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile)
70 DWORD len = 0xffffffff;
72 if (apr_os_level >= APR_WIN_NT) {
73 /* Syntax is correct, len is passed for LengthLow and LengthHigh*/
75 memset (&offset, 0, sizeof(offset));
76 if (!UnlockFileEx(thefile->filehand, 0, len, len, &offset))
77 return apr_get_os_error();
80 if (!UnlockFile(thefile->filehand, 0, 0, len, 0))
81 return apr_get_os_error();
85 #endif /* !defined(_WIN32_WCE) */