upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / win32 / flock.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "apr_arch_file_io.h"
18
19 APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type)
20 {
21 #ifdef _WIN32_WCE
22     /* The File locking is unsuported on WCE */
23     return APR_ENOTIMPL;
24 #else
25     const DWORD len = 0xffffffff;
26     DWORD flags; 
27
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*/
33         OVERLAPPED offset;
34         memset (&offset, 0, sizeof(offset));
35         if (!LockFileEx(thefile->filehand, flags, 0, len, len, &offset))
36             return apr_get_os_error();
37     }
38     else {
39         /* On Win9x, LockFile() never blocks.  Hack in a crufty poll.
40          *
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.
46          *
47          * However Win9x are barely maintainable anyways, if the user does
48          * choose to build to them, this is the best we can do.
49          */
50         while (!LockFile(thefile->filehand, 0, 0, len, 0)) {
51             DWORD err = GetLastError();
52             if ((err == ERROR_LOCK_VIOLATION) && !(type & APR_FLOCK_NONBLOCK))
53             {
54                 Sleep(500); /* pause for a half second */
55                 continue;   /* ... and then poll again */
56             }
57             return APR_FROM_OS_ERROR(err);
58         }
59     }
60
61     return APR_SUCCESS;
62 #endif /* !defined(_WIN32_WCE) */
63 }
64
65 APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile)
66 {
67 #ifdef _WIN32_WCE
68     return APR_ENOTIMPL;
69 #else
70     DWORD len = 0xffffffff;
71
72     if (apr_os_level >= APR_WIN_NT) {
73         /* Syntax is correct, len is passed for LengthLow and LengthHigh*/
74         OVERLAPPED offset;
75         memset (&offset, 0, sizeof(offset));
76         if (!UnlockFileEx(thefile->filehand, 0, len, len, &offset))
77             return apr_get_os_error();
78     }
79     else {
80         if (!UnlockFile(thefile->filehand, 0, 0, len, 0))
81             return apr_get_os_error();
82     }
83
84     return APR_SUCCESS;
85 #endif /* !defined(_WIN32_WCE) */
86 }