1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Mike Day (mdday@us.ibm.com)
33 //
34 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef Pegasus_Semaphore_h
37 #define Pegasus_Semaphore_h
38
39 #include <Pegasus/Common/Config.h>
40 #include <Pegasus/Common/Linkage.h>
41 #include <Pegasus/Common/Threads.h>
42
43 mike 1.2 //==============================================================================
44 //
45 // Select sempahore implementation by defining one of these:
46 //
47 // PEGASUS_USE_POSIX_SEMAPHORE
48 // PEGASUS_USE_PTHREAD_SEMAPHORE
49 // PEGASUS_USE_WINDOWS_SEMAPHORE
50 //
51 //==============================================================================
52
53 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) || \
54 defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || \
55 defined(PEGASUS_PLATFORM_DARWIN_PPC_GNU) || \
56 defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || \
57 defined(PEGASUS_PLATFORM_VMS_IA64_DECCXX) || \
58 defined(PEGASUS_PLATFORM_VMS_ALPHA_DECCXX)
59 # define PEGASUS_USE_PTHREAD_SEMAPHORE
60 #elif defined(PEGASUS_OS_TYPE_WINDOWS)
61 # define PEGASUS_USE_WINDOWS_SEMAPHORE
62 #else
63 # define PEGASUS_USE_POSIX_SEMAPHORE
64 mike 1.2 #endif
65
66 #if defined(PEGASUS_USE_POSIX_SEMAPHORE)
67 # include <semaphore.h>
68 #endif
69
70 PEGASUS_NAMESPACE_BEGIN
71
72 //==============================================================================
73 //
74 // SemaphoreRep
75 //
76 //==============================================================================
77
78 #if defined(PEGASUS_USE_PTHREAD_SEMAPHORE)
79 struct SemaphoreRep
80 {
81 Uint32 waiters;
82 pthread_mutex_t mutex;
83 pthread_cond_t cond;
84 ThreadType owner;
85 mike 1.2 };
86 #endif /* PEGASUS_USE_PTHREAD_SEMAPHORE */
87
88 #if defined(PEGASUS_USE_POSIX_SEMAPHORE)
89 struct SemaphoreRep
90 {
91 sem_t sem;
92 ThreadType owner;
93 };
94 #endif /* defined(PEGASUS_USE_POSIX_SEMAPHORE) */
95
96 #if defined(PEGASUS_USE_WINDOWS_SEMAPHORE)
97 struct SemaphoreRep
98 {
99 HANDLE sem;
100 ThreadType owner;
101 };
102 #endif /* PEGASUS_USE_WINDOWS_SEMAPHORE */
103
104 //==============================================================================
105 //
106 mike 1.2 // Semaphore
107 //
108 //==============================================================================
109
110 class PEGASUS_COMMON_LINKAGE Semaphore
111 {
112 public:
113
114 /** Creates a semaphore and sets its initial value as specified.
115 @param initial The initial value for the Semaphore (defaults to 1).
116 */
117 Semaphore(Uint32 initial = 1);
118
119 /** Destructor.
120 */
121 ~Semaphore();
122
123 /** Blocks until this Semaphore is in a signalled state.
124 @param ignoreInterrupt Indicates whether the wait operation should
125 continue (true) or an exception should be thrown (false) when an
126 interrupt is received.
127 mike 1.2 @exception WaitFailed If unable to block on the semaphore.
128 @exception WaitInterrupted If the operation is interrupted.
129 */
130 void wait(Boolean ignoreInterrupt = true);
131
132 /** Checks whether the Semaphore is signalled without waiting. This method
133 returns normally if the Semaphore has a non-zero count.
134 @exception WaitFailed If the wait operation does not immediately
135 succeed.
136 */
137 void try_wait();
138
139 /** Waits for the Semaphore to be signalled for a specified time interval.
140 This method returns normally if the Semaphore has a non-zero count or
141 it is signalled during the specified time interval.
142 @param milliseconds The time interval to wait (in milliseconds).
143 @exception TimeOut If the wait operation does not succeed within
144 the specified time interval.
145 */
146 void time_wait(Uint32 milliseconds);
147
148 mike 1.2 /** Increments the count of the semaphore.
149 */
150 void signal();
151
152 /** Return the count of the semaphore.
153 */
154 int count() const;
155
156 private:
157
158 Semaphore(const Semaphore& x); // Unimplemented
159 Semaphore& operator=(const Semaphore& x); // Unimplemented
160
161 mutable int _count;
162 mutable SemaphoreRep _rep;
163 friend class Condition;
164 };
165
166 PEGASUS_NAMESPACE_END
167
168 #endif /* Pegasus_Semaphore_h */
|