|
0
|
1 #ifndef CYGONCE_KERNEL_SEMA_HXX |
|
|
2 #define CYGONCE_KERNEL_SEMA_HXX |
|
|
3 |
|
|
4 //========================================================================== |
|
|
5 // |
|
2
|
6 // sema.hxx |
|
0
|
7 // |
|
2
|
8 // Semaphore class declarations |
|
0
|
9 // |
|
|
10 //========================================================================== |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
28 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
2
|
35 // Author(s): nickg |
|
|
36 // Contributors: nickg |
|
|
37 // Date: 1997-09-09 |
|
|
38 // Purpose: Define Semaphore class interfaces |
|
|
39 // Description: The classes defined here provide the APIs for binary |
|
0
|
40 // and counting semaphores. |
|
2
|
41 // Usage: #include <cyg/kernel/sema.hxx> |
|
|
42 // |
|
0
|
43 // |
|
|
44 //####DESCRIPTIONEND#### |
|
|
45 // |
|
|
46 //========================================================================== |
|
|
47 |
|
|
48 #include <cyg/kernel/ktypes.h> |
|
|
49 #include <cyg/infra/cyg_ass.h> // assertion macros |
|
|
50 #include <cyg/kernel/thread.hxx> |
|
|
51 |
|
|
52 // ------------------------------------------------------------------------- |
|
|
53 // Binary semaphore. This has only two states: posted and not-posted. |
|
|
54 |
|
|
55 class Cyg_Binary_Semaphore |
|
|
56 { |
|
|
57 cyg_bool state; // The binary semaphore state |
|
|
58 |
|
|
59 Cyg_ThreadQueue queue; // Queue of waiting threads |
|
|
60 |
|
|
61 public: |
|
|
62 |
|
|
63 CYGDBG_DEFINE_CHECK_THIS |
|
|
64 |
|
|
65 Cyg_Binary_Semaphore( // Constructor |
|
|
66 cyg_bool init_state = false // Initial state value |
|
|
67 ); |
|
|
68 |
|
|
69 ~Cyg_Binary_Semaphore(); // Destructor |
|
|
70 |
|
|
71 cyg_bool wait(); // Wait until state == true |
|
|
72 |
|
|
73 cyg_bool trywait(); // Set state false if possible |
|
|
74 |
|
|
75 void post(); // Increment count |
|
|
76 |
|
|
77 cyg_bool posted(); // Get current state |
|
|
78 |
|
|
79 }; |
|
|
80 |
|
|
81 // ------------------------------------------------------------------------- |
|
|
82 // Counting semaphore. This implements the usual counter based semaphore. |
|
|
83 |
|
|
84 class Cyg_Counting_Semaphore |
|
|
85 { |
|
|
86 cyg_count32 count; // The semaphore count |
|
|
87 |
|
|
88 Cyg_ThreadQueue queue; // Queue of waiting threads |
|
|
89 |
|
|
90 public: |
|
|
91 |
|
|
92 CYGDBG_DEFINE_CHECK_THIS |
|
|
93 |
|
|
94 Cyg_Counting_Semaphore( // Constructor |
|
|
95 cyg_count32 init_count = 0 // Initial count value |
|
|
96 ); |
|
|
97 |
|
|
98 ~Cyg_Counting_Semaphore(); // Destructor |
|
|
99 |
|
|
100 cyg_bool wait(); // Wait until decrement |
|
|
101 |
|
|
102 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
103 cyg_bool wait( cyg_tick_count timeout ); |
|
|
104 #endif // Wait until decrement or timeout |
|
|
105 |
|
|
106 cyg_bool trywait(); // Try to decrement |
|
|
107 |
|
|
108 void post(); // Increment count |
|
|
109 |
|
|
110 cyg_count32 peek(); // Get current count value |
|
|
111 |
|
|
112 inline |
|
2
|
113 cyg_bool waiting() // Is anyone waiting? |
|
0
|
114 { |
|
|
115 return !queue.empty(); |
|
|
116 } |
|
|
117 |
|
|
118 }; |
|
|
119 |
|
|
120 // ------------------------------------------------------------------------- |
|
|
121 #endif // ifndef CYGONCE_KERNEL_SEMA_HXX |
|
|
122 // EOF sema.hxx |