]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/include/flag.hxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / kernel / v2_0 / include / flag.hxx
1 #ifndef CYGONCE_KERNEL_FLAG_HXX
2 #define CYGONCE_KERNEL_FLAG_HXX
3
4 //==========================================================================
5 //
6 //      flag.hxx
7 //
8 //      Flag object class declarations
9 //
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 //
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
19 //
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 // for more details.
24 //
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 //
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
35 //
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
38 //
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
45 //
46 // Author(s):   hmt
47 // Contributors:        hmt
48 // Date:        1998-02-10
49 // Purpose:     Define Flag class interfaces
50 // Description: The classes defined here provide the APIs for flags.
51 // Usage:       #include <cyg/kernel/flag.hxx>
52 //              
53 //
54 //####DESCRIPTIONEND####
55 //
56 //==========================================================================
57
58 #include <cyg/kernel/ktypes.h>
59 #include <cyg/infra/cyg_ass.h>                     // assertion macros
60 #include <cyg/kernel/thread.hxx>                   // Cyg_Thread
61
62 #include <cyg/kernel/thread.inl>                   // queue implementation
63 // -------------------------------------------------------------------------
64
65 // Flag object.  This class implements a queue of threads waiting for a
66 // boolean expression of the flag value (an integer) to become true; all
67 // relevant threads are awoken, not just the first.  A variant on this is
68 // the single-bit flag, which is implemented by means of default arguments.
69
70 #ifdef CYGIMP_FLAGS_16BIT
71 typedef cyg_uint16 Cyg_FlagValue;
72 #define CYG_FLAGS_SIZE 16
73 #endif
74 #ifdef CYGIMP_FLAGS_32BIT
75 typedef cyg_uint32 Cyg_FlagValue;
76 #define CYG_FLAGS_SIZE 32
77 #endif
78 #ifdef CYGIMP_FLAGS_64BIT
79 typedef cyg_uint64 Cyg_FlagValue;
80 #define CYG_FLAGS_SIZE 64
81 #endif
82
83 #ifndef CYG_FLAGS_SIZE
84 typedef cyg_uint32 Cyg_FlagValue;
85 #define CYG_FLAGS_SIZE 32
86 #endif
87
88
89
90 class Cyg_Flag
91 {
92 private:
93     Cyg_FlagValue value;
94
95     class FlagWaitInfo
96     {
97     public:
98         Cyg_FlagValue   allmask;        // these are separate words to
99         Cyg_FlagValue   anymask;        // save time in wakeup.
100         Cyg_FlagValue   value_out;      // return the value that satisfied
101         cyg_bool        do_clear;
102
103         FlagWaitInfo() { value_out = 0; }
104     };
105
106     Cyg_ThreadQueue     queue;          // Queue of waiting threads
107
108 public:
109
110     CYGDBG_DEFINE_CHECK_THIS
111     
112     Cyg_Flag( Cyg_FlagValue init = 0 ); // Constructor
113     ~Cyg_Flag();                        // Destructor
114         
115     void
116     setbits( Cyg_FlagValue arg = ~0 );  // -OR- the arg in
117     // not inlined; this function awakens affected threads.
118
119     void
120     maskbits( Cyg_FlagValue arg = 0 );  // -AND- it in
121     // this is not inlined because it needs to lock the scheduler;
122     // it only really does value &= arg; nobody can be awoken in consequence.
123
124     typedef cyg_uint8 WaitMode;
125     // These values are chosen to map directly to uITRON for emulation
126     // purposes:
127     static const WaitMode AND = 0;      // all specified bits must be set
128     static const WaitMode OR  = 2;      // any specified bit must be set
129     static const WaitMode CLR = 1;      // clear value when satisfied
130     static const WaitMode MASK= 3;      // might be useful
131
132     // Wait for a match on our pattern, according to the flags given.
133     // Return the matching value, or zero if interrupted.
134     Cyg_FlagValue
135     wait( Cyg_FlagValue pattern, WaitMode mode );
136
137     // Wait for a match on our pattern, with an absolute timeout.
138     // Return the matching value, or zero if timed out/interrupted.
139     // (zero cannot match any pattern).
140 #ifdef CYGFUN_KERNEL_THREADS_TIMER
141     Cyg_FlagValue
142     wait( Cyg_FlagValue pattern, WaitMode mode,
143           cyg_tick_count abs_timeout );
144 #endif
145     // Test for a match on our pattern, according to the flags given.
146     // Return the matching value if success, else zero.
147     Cyg_FlagValue
148     poll( Cyg_FlagValue pattern, WaitMode mode ); 
149
150     inline Cyg_FlagValue
151     peek()                              // Get current value
152     {
153         return value;                   // NOT atomic wrt threads
154     }
155
156     inline cyg_bool
157     waiting()                           // Any threads waiting?
158     {
159         return !queue.empty();
160     }
161 };
162
163
164
165 // -------------------------------------------------------------------------
166 #endif // ifndef CYGONCE_KERNEL_FLAG_HXX
167 // EOF flag.hxx