|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * This file contains implementation of synchronization objects. These are Mutex, Semaphore and |
|
16 * Event classes. |
|
17 */ |
|
18 |
|
19 // INCLUDES |
|
20 #include "sync.h" |
|
21 |
|
22 //********************************************************************************** |
|
23 // Class Mutex |
|
24 // |
|
25 // This class implements a Mutex using Windows API synchronization mechanism |
|
26 //********************************************************************************** |
|
27 |
|
28 Mutex::Mutex(void) |
|
29 { |
|
30 InitializeCriticalSection(&m_Lock); |
|
31 } |
|
32 |
|
33 Mutex::~Mutex(void) |
|
34 { |
|
35 } |
|
36 |
|
37 void Mutex::Lock(void) |
|
38 { |
|
39 EnterCriticalSection(&m_Lock); |
|
40 } |
|
41 |
|
42 void Mutex::Unlock(void) |
|
43 { |
|
44 LeaveCriticalSection(&m_Lock); |
|
45 } |
|
46 |
|
47 //********************************************************************************** |
|
48 // Class Semaphore |
|
49 // |
|
50 // This class encapsulates Windows API Semaphore functionality |
|
51 //********************************************************************************** |
|
52 |
|
53 Semaphore::Semaphore(void) |
|
54 { |
|
55 m_Semaphore = CreateSemaphore(NULL, 0, 0x7FFFFFF, NULL); |
|
56 } |
|
57 |
|
58 Semaphore::Semaphore(int available) |
|
59 { |
|
60 m_Semaphore = CreateSemaphore(NULL, available, 0x7FFFFFF, NULL); |
|
61 } |
|
62 |
|
63 Semaphore::~Semaphore(void) |
|
64 { |
|
65 CloseHandle(m_Semaphore); |
|
66 } |
|
67 |
|
68 DWORD Semaphore::Wait(DWORD timeout) |
|
69 { |
|
70 DWORD dwResult = WaitForSingleObject(m_Semaphore, timeout); |
|
71 switch (dwResult) |
|
72 { |
|
73 case WAIT_OBJECT_0: |
|
74 return 0; |
|
75 case WAIT_TIMEOUT: |
|
76 return WAIT_TIMEOUT; |
|
77 default: |
|
78 return 1; |
|
79 } |
|
80 } |
|
81 |
|
82 void Semaphore::Notify(void) |
|
83 { |
|
84 ReleaseSemaphore(m_Semaphore, 1, NULL); |
|
85 } |
|
86 |
|
87 void Semaphore::Notify(int how_many) |
|
88 { |
|
89 ReleaseSemaphore(m_Semaphore, how_many, NULL); |
|
90 } |
|
91 |
|
92 //********************************************************************************** |
|
93 // Class Event |
|
94 // |
|
95 // This class encapsulates Windows API Event functionality |
|
96 //********************************************************************************** |
|
97 |
|
98 Event::Event(bool manual_reset) |
|
99 { |
|
100 m_Event = CreateEvent(NULL, manual_reset, false, NULL); |
|
101 } |
|
102 |
|
103 Event::~Event(void) |
|
104 { |
|
105 CloseHandle(m_Event); |
|
106 } |
|
107 |
|
108 void Event::Set(void) |
|
109 { |
|
110 SetEvent(m_Event); |
|
111 } |
|
112 |
|
113 void Event::Reset(void) |
|
114 { |
|
115 ResetEvent(m_Event); |
|
116 } |
|
117 |
|
118 DWORD Event::Wait(DWORD timeout) |
|
119 { |
|
120 DWORD dwResult = WaitForSingleObject(m_Event, timeout); |
|
121 switch (dwResult) |
|
122 { |
|
123 case WAIT_OBJECT_0: |
|
124 return 0; |
|
125 case WAIT_TIMEOUT: |
|
126 return WAIT_TIMEOUT; |
|
127 default: |
|
128 return 1; |
|
129 } |
|
130 } |
|
131 |
|
132 HANDLE Event::EventHandle() const |
|
133 { |
|
134 return m_Event; |
|
135 } |
|
136 |
|
137 // End of the file |