Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
| 20 | unit Thrift.Utils; |
| 21 | |
| 22 | interface |
| 23 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, SyncObjs; |
| 26 | |
| 27 | type |
| 28 | IOverlappedHelper = interface |
| 29 | ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}'] |
| 30 | function Overlapped : TOverlapped; |
| 31 | function OverlappedPtr : POverlapped; |
| 32 | function WaitHandle : THandle; |
| 33 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 34 | end; |
| 35 | |
| 36 | TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper) |
| 37 | strict protected |
| 38 | FOverlapped : TOverlapped; |
| 39 | FEvent : TEvent; |
| 40 | |
| 41 | // IOverlappedHelper |
| 42 | function Overlapped : TOverlapped; |
| 43 | function OverlappedPtr : POverlapped; |
| 44 | function WaitHandle : THandle; |
| 45 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 46 | public |
| 47 | constructor Create; |
| 48 | destructor Destroy; override; |
| 49 | end; |
| 50 | |
| 51 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | implementation |
| 53 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 54 | { TOverlappedHelperImpl } |
| 55 | |
| 56 | constructor TOverlappedHelperImpl.Create; |
| 57 | begin |
| 58 | inherited Create; |
| 59 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 60 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 61 | FOverlapped.hEvent := FEvent.Handle; |
| 62 | end; |
| 63 | |
| 64 | |
| 65 | |
| 66 | destructor TOverlappedHelperImpl.Destroy; |
| 67 | begin |
| 68 | try |
| 69 | FOverlapped.hEvent := 0; |
| 70 | FreeAndNil( FEvent); |
| 71 | |
| 72 | finally |
| 73 | inherited Destroy; |
| 74 | end; |
| 75 | |
| 76 | end; |
| 77 | |
| 78 | |
| 79 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 80 | begin |
| 81 | result := FOverlapped; |
| 82 | end; |
| 83 | |
| 84 | |
| 85 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 86 | begin |
| 87 | result := @FOverlapped; |
| 88 | end; |
| 89 | |
| 90 | |
| 91 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 92 | begin |
| 93 | result := FOverlapped.hEvent; |
| 94 | end; |
| 95 | |
| 96 | |
| 97 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 98 | begin |
| 99 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 100 | end; |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | end. |