winverbs: delete critical sections when no longer needed
[mirror/winof/.git] / core / winverbs / user / wv_base.cpp
1 /*\r
2  * Copyright (c) 2008 Intel Corporation. All rights reserved.\r
3  * Portions Copyright (c) 2009 Microsoft Corporation.  All rights reserved.\r
4  *\r
5  * This software is available to you under the OpenIB.org BSD license\r
6  * below:\r
7  *\r
8  *     Redistribution and use in source and binary forms, with or\r
9  *     without modification, are permitted provided that the following\r
10  *     conditions are met:\r
11  *\r
12  *      - Redistributions of source code must retain the above\r
13  *        copyright notice, this list of conditions and the following\r
14  *        disclaimer.\r
15  *\r
16  *      - Redistributions in binary form must reproduce the above\r
17  *        copyright notice, this list of conditions and the following\r
18  *        disclaimer in the documentation and/or other materials\r
19  *        provided with the distribution.\r
20  *\r
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV\r
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
25  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
26  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
28  * SOFTWARE.\r
29  */\r
30 \r
31 #include <iba\ib_ci.h>\r
32 #include "wv_base.h"\r
33 #include "wv_cq.h"\r
34 #include "wv_qp.h"\r
35 #include "wv_ioctl.h"\r
36 \r
37 CWVBase::CWVBase()\r
38 {\r
39         InitializeCriticalSection(&m_CritSec);\r
40         m_Overlap.hEvent = NULL;\r
41         m_nRef = 1;\r
42         m_Id = 0;\r
43 }\r
44 \r
45 CWVBase::~CWVBase()\r
46 {\r
47         if (m_Overlap.hEvent != NULL) {\r
48                 CloseHandle(m_Overlap.hEvent);\r
49         }\r
50         DeleteCriticalSection(&m_CritSec);\r
51 }\r
52 \r
53 STDMETHODIMP CWVBase::\r
54 Init(void)\r
55 {\r
56         m_Overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);\r
57         if (m_Overlap.hEvent == NULL)\r
58                 return WV_INSUFFICIENT_RESOURCES;\r
59 \r
60         m_Overlap.hEvent = (HANDLE) ((ULONG_PTR) m_Overlap.hEvent | 1);\r
61         return WV_SUCCESS;\r
62 }\r
63 \r
64 STDMETHODIMP_(ULONG) CWVBase::\r
65 AddRef(void)\r
66 {\r
67         return InterlockedIncrement(&m_nRef);\r
68 }\r
69 \r
70 STDMETHODIMP_(ULONG) CWVBase::\r
71 Release(void)\r
72 {\r
73         ULONG ref;\r
74 \r
75         ref = (ULONG) InterlockedDecrement(&m_nRef);\r
76         if (ref == 0) {\r
77                 Delete();\r
78         }\r
79         return ref;\r
80 }\r
81 \r
82 BOOL CWVBase::\r
83 WvDeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,\r
84                                   LPVOID lpInBuffer, DWORD nInBufferSize,\r
85                                   LPVOID lpOutBuffer, DWORD nOutBufferSize,\r
86                                   LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped)\r
87 {\r
88         BOOL ret;\r
89 \r
90         if (lpOverlapped == NULL) {\r
91                 EnterCriticalSection(&m_CritSec);\r
92                 DeviceIoControl(hDevice, dwIoControlCode,\r
93                                                 lpInBuffer, nInBufferSize,\r
94                                                 lpOutBuffer, nOutBufferSize,\r
95                                                 lpBytesReturned, &m_Overlap);\r
96 \r
97                 ret = GetOverlappedResult(hDevice, &m_Overlap, lpBytesReturned, TRUE);\r
98                 LeaveCriticalSection(&m_CritSec);\r
99         } else {\r
100                 ret = DeviceIoControl(hDevice, dwIoControlCode,\r
101                                                           lpInBuffer, nInBufferSize,\r
102                                                           lpOutBuffer, nOutBufferSize,\r
103                                                           lpBytesReturned, lpOverlapped);\r
104         }\r
105 \r
106         return ret;\r
107 }\r