OpenCores
URL https://opencores.org/ocsvn/openfire2/openfire2/trunk

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [sw/] [freertos/] [semphr.h] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 toni32
/*
2
        FreeRTOS.org V4.2.0 - Copyright (C) 2003-2007 Richard Barry.
3
 
4
        This file is part of the FreeRTOS.org distribution.
5
 
6
        FreeRTOS.org is free software; you can redistribute it and/or modify
7
        it under the terms of the GNU General Public License as published by
8
        the Free Software Foundation; either version 2 of the License, or
9
        (at your option) any later version.
10
 
11
        FreeRTOS.org is distributed in the hope that it will be useful,
12
        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
        GNU General Public License for more details.
15
 
16
        You should have received a copy of the GNU General Public License
17
        along with FreeRTOS.org; if not, write to the Free Software
18
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
        A special exception to the GPL can be applied should you wish to distribute
21
        a combined work that includes FreeRTOS.org, without being obliged to provide
22
        the source code for any proprietary components.  See the licensing section
23
        of http://www.FreeRTOS.org for full details of how and when the exception
24
        can be applied.
25
 
26
        ***************************************************************************
27
        See http://www.FreeRTOS.org for documentation, latest information, license
28
        and contact details.  Please ensure to read the configuration and relevant
29
        port sections of the online documentation.
30
        ***************************************************************************
31
*/
32
 
33
#include "queue.h"
34
 
35
#ifndef SEMAPHORE_H
36
#define SEMAPHORE_H
37
 
38
typedef xQueueHandle xSemaphoreHandle;
39
 
40
#define semBINARY_SEMAPHORE_QUEUE_LENGTH        ( ( unsigned portCHAR ) 1 )
41
#define semSEMAPHORE_QUEUE_ITEM_LENGTH          ( ( unsigned portCHAR ) 0 )
42
#define semGIVE_BLOCK_TIME                                      ( ( portTickType ) 0 )
43
 
44
 
45
/**
46
 * semphr. h
47
 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
48
 *
49
 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
50
 * The queue length is 1 as this is a binary semaphore.  The data size is 0
51
 * as we don't want to actually store any data - we just want to know if the
52
 * queue is empty or full.
53
 *
54
 * @param xSemaphore Handle to the created semaphore.  Should be of type xSemaphoreHandle.
55
 *
56
 * Example usage:
57
 <pre>
58
 xSemaphoreHandle xSemaphore;
59
 
60
 void vATask( void * pvParameters )
61
 {
62
    // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
63
    // This is a macro so pass the variable in directly.
64
    vSemaphoreCreateBinary( xSemaphore );
65
 
66
    if( xSemaphore != NULL )
67
    {
68
        // The semaphore was created successfully.
69
        // The semaphore can now be used.
70
    }
71
 }
72
 </pre>
73
 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
74
 * \ingroup Semaphores
75
 */
76
#define vSemaphoreCreateBinary( xSemaphore )            {                                                                                                                                                                                       \
77
                                                                                                                xSemaphore = xQueueCreate( ( unsigned portCHAR ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH );   \
78
                                                                                                                if( xSemaphore != NULL )                                                                                                                                \
79
                                                                                                                {                                                                                                                                                                               \
80
                                                                                                                        xSemaphoreGive( xSemaphore );                                                                                                           \
81
                                                                                                                }                                                                                                                                                                               \
82
                                                                                                        }
83
 
84
/**
85
 * semphr. h
86
 * xSemaphoreTake(
87
 *                   xSemaphoreHandle xSemaphore,
88
 *                   portTickType xBlockTime
89
 *               )</pre>
90
 *
91
 * <i>Macro</i> to obtain a semaphore.  The semaphore must of been created using
92
 * vSemaphoreCreateBinary ().
93
 *
94
 * @param xSemaphore A handle to the semaphore being obtained.  This is the
95
 * handle returned by vSemaphoreCreateBinary ();
96
 *
97
 * @param xBlockTime The time in ticks to wait for the semaphore to become
98
 * available.  The macro portTICK_RATE_MS can be used to convert this to a
99
 * real time.  A block time of zero can be used to poll the semaphore.
100
 *
101
 * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime
102
 * expired without the semaphore becoming available.
103
 *
104
 * Example usage:
105
 <pre>
106
 xSemaphoreHandle xSemaphore = NULL;
107
 
108
 // A task that creates a semaphore.
109
 void vATask( void * pvParameters )
110
 {
111
    // Create the semaphore to guard a shared resource.
112
    vSemaphoreCreateBinary( xSemaphore );
113
 }
114
 
115
 // A task that uses the semaphore.
116
 void vAnotherTask( void * pvParameters )
117
 {
118
    // ... Do other things.
119
 
120
    if( xSemaphore != NULL )
121
    {
122
        // See if we can obtain the semaphore.  If the semaphore is not available
123
        // wait 10 ticks to see if it becomes free.
124
        if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
125
        {
126
            // We were able to obtain the semaphore and can now access the
127
            // shared resource.
128
 
129
            // ...
130
 
131
            // We have finished accessing the shared resource.  Release the
132
            // semaphore.
133
            xSemaphoreGive( xSemaphore );
134
        }
135
        else
136
        {
137
            // We could not obtain the semaphore and can therefore not access
138
            // the shared resource safely.
139
        }
140
    }
141
 }
142
 </pre>
143
 * \defgroup xSemaphoreTake xSemaphoreTake
144
 * \ingroup Semaphores
145
 */
146
#define xSemaphoreTake( xSemaphore, xBlockTime )        xQueueReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime )
147
 
148
/**
149
 * semphr. h
150
 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
151
 *
152
 * <i>Macro</i> to release a semaphore.  The semaphore must of been created using
153
 * vSemaphoreCreateBinary (), and obtained using sSemaphoreTake ().
154
 *
155
 * This must not be used from an ISR.  See xSemaphoreGiveFromISR () for
156
 * an alternative which can be used from an ISR.
157
 *
158
 * @param xSemaphore A handle to the semaphore being released.  This is the
159
 * handle returned by vSemaphoreCreateBinary ();
160
 *
161
 * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.
162
 * Semaphores are implemented using queues.  An error can occur if there is
163
 * no space on the queue to post a message - indicating that the
164
 * semaphore was not first obtained correctly.
165
 *
166
 * Example usage:
167
 <pre>
168
 xSemaphoreHandle xSemaphore = NULL;
169
 
170
 void vATask( void * pvParameters )
171
 {
172
    // Create the semaphore to guard a shared resource.
173
    vSemaphoreCreateBinary( xSemaphore );
174
 
175
    if( xSemaphore != NULL )
176
    {
177
        if( xSemaphoreGive( xSemaphore ) != pdTRUE )
178
        {
179
            // We would expect this call to fail because we cannot give
180
            // a semaphore without first "taking" it!
181
        }
182
 
183
        // Obtain the semaphore - don't block if the semaphore is not
184
        // immediately available.
185
        if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
186
        {
187
            // We now have the semaphore and can access the shared resource.
188
 
189
            // ...
190
 
191
            // We have finished accessing the shared resource so can free the
192
            // semaphore.
193
            if( xSemaphoreGive( xSemaphore ) != pdTRUE )
194
            {
195
                // We would not expect this call to fail because we must have
196
                // obtained the semaphore to get here.
197
            }
198
        }
199
    }
200
 }
201
 </pre>
202
 * \defgroup xSemaphoreGive xSemaphoreGive
203
 * \ingroup Semaphores
204
 */
205
#define xSemaphoreGive( xSemaphore )                            xQueueSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME )
206
 
207
/**
208
 * semphr. h
209
 * <pre>
210
 xSemaphoreGiveFromISR(
211
                          xSemaphoreHandle xSemaphore,
212
                          portSHORT sTaskPreviouslyWoken
213
                      )</pre>
214
 *
215
 * <i>Macro</i> to  release a semaphore.  The semaphore must of been created using
216
 * vSemaphoreCreateBinary (), and obtained using xSemaphoreTake ().
217
 *
218
 * This macro can be used from an ISR.
219
 *
220
 * @param xSemaphore A handle to the semaphore being released.  This is the
221
 * handle returned by vSemaphoreCreateBinary ();
222
 *
223
 * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls
224
 * to xSemaphoreGiveFromISR () from a single interrupt.  The first call
225
 * should always pass in pdFALSE.  Subsequent calls should pass in
226
 * the value returned from the previous call.  See the file serial .c in the
227
 * PC port for a good example of using xSemaphoreGiveFromISR ().
228
 *
229
 * @return pdTRUE if a task was woken by releasing the semaphore.  This is
230
 * used by the ISR to determine if a context switch may be required following
231
 * the ISR.
232
 *
233
 * Example usage:
234
 <pre>
235
 #define LONG_TIME 0xffff
236
 #define TICKS_TO_WAIT  10
237
 xSemaphoreHandle xSemaphore = NULL;
238
 
239
 // Repetitive task.
240
 void vATask( void * pvParameters )
241
 {
242
    for( ;; )
243
    {
244
        // We want this task to run every 10 ticks or a timer.  The semaphore
245
        // was created before this task was started
246
 
247
        // Block waiting for the semaphore to become available.
248
        if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
249
        {
250
            // It is time to execute.
251
 
252
            // ...
253
 
254
            // We have finished our task.  Return to the top of the loop where
255
            // we will block on the semaphore until it is time to execute
256
            // again.
257
        }
258
    }
259
 }
260
 
261
 // Timer ISR
262
 void vTimerISR( void * pvParameters )
263
 {
264
 static unsigned portCHAR ucLocalTickCount = 0;
265
 
266
    // A timer tick has occurred.
267
 
268
    // ... Do other time functions.
269
 
270
    // Is it time for vATask () to run?
271
    ucLocalTickCount++;
272
    if( ucLocalTickCount >= TICKS_TO_WAIT )
273
    {
274
        // Unblock the task by releasing the semaphore.
275
        xSemaphoreGive( xSemaphore );
276
 
277
        // Reset the count so we release the semaphore again in 10 ticks time.
278
        ucLocalTickCount = 0;
279
    }
280
 }
281
 </pre>
282
 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
283
 * \ingroup Semaphores
284
 */
285
#define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken )                       xQueueSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken )
286
 
287
 
288
#endif
289
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.