Tizen RT Libs&Environment  v1.0 D5
semaphore.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 /****************************************************************************
19  * include/semaphore.h
20  *
21  * Copyright (C) 2007-2009, 2012-2013 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in
32  * the documentation and/or other materials provided with the
33  * distribution.
34  * 3. Neither the name NuttX nor the names of its contributors may be
35  * used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
45  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  ****************************************************************************/
58 
61 #ifndef __INCLUDE_SEMAPHORE_H
62 #define __INCLUDE_SEMAPHORE_H
63 
64 /****************************************************************************
65  * Included Files
66  ****************************************************************************/
67 
68 #include <tinyara/config.h>
69 
70 #include <stdint.h>
71 #include <limits.h>
72 
73 #ifdef __cplusplus
74 #define EXTERN extern "C"
75 extern "C" {
76 #else
77 #define EXTERN extern
78 #endif
79 
80 /****************************************************************************
81  * Pre-processor Definitions
82  ****************************************************************************/
83 
84 /****************************************************************************
85  * Public Type Declarations
86  ****************************************************************************/
87 
88 /* This structure contains information about the holder of a semaphore */
89 
90 #ifdef CONFIG_PRIORITY_INHERITANCE
91 struct tcb_s; /* Forward reference */
96 struct semholder_s {
97 #if CONFIG_SEM_PREALLOCHOLDERS > 0
98  struct semholder_s *flink; /* Implements singly linked list */
99 #endif
100  FAR struct tcb_s *htcb; /* Holder TCB */
101  int16_t counts; /* Number of counts owned by this holder */
102 };
103 
104 #if CONFIG_SEM_PREALLOCHOLDERS > 0
105 #define SEMHOLDER_INITIALIZER {NULL, NULL, 0}
106 #else
107 #define SEMHOLDER_INITIALIZER {NULL, 0}
108 #endif
109 #endif /* CONFIG_PRIORITY_INHERITANCE */
110 
115 struct sem_s {
116  int16_t semcount; /* >0 -> Num counts available */
117  /* <0 -> Num tasks waiting for semaphore */
118  /* If priority inheritance is enabled, then we have to keep track of which
119  * tasks hold references to the semaphore.
120  */
121 
122 #ifdef CONFIG_PRIORITY_INHERITANCE
123 #if CONFIG_SEM_PREALLOCHOLDERS > 0
124  FAR struct semholder_s *hhead; /* List of holders of semaphore counts */
125 #else
126  struct semholder_s holder; /* Single holder */
127 #endif
128 #endif
129 };
130 
131 typedef struct sem_s sem_t;
132 
133 /* Initializers */
138 #ifdef CONFIG_PRIORITY_INHERITANCE
139 #if CONFIG_SEM_PREALLOCHOLDERS > 0
140 #define SEM_INITIALIZER(c) {(c), NULL} /* semcount, hhead */
141 #else
142 #define SEM_INITIALIZER(c) {(c), SEMHOLDER_INITIALIZER} /* semcount, holder */
143 #endif
144 #else
145 #define SEM_INITIALIZER(c) {(c)} /* semcount */
146 #endif
147 
148 /****************************************************************************
149  * Public Variables
150  ****************************************************************************/
151 
152 /****************************************************************************
153  * Public Function Prototypes
154  ****************************************************************************/
155 /* Forward references needed by some prototypes */
156 
157 struct timespec; /* Defined in time.h */
158 
159 /* Counting Semaphore Interfaces (based on POSIX APIs) */
165 int sem_init(FAR sem_t *sem, int pshared, unsigned int value);
166 
173 int sem_destroy(FAR sem_t *sem);
180 int sem_wait(FAR sem_t *sem);
187 int sem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime);
194 int sem_trywait(FAR sem_t *sem);
201 int sem_post(FAR sem_t *sem);
207 int sem_getvalue(FAR sem_t *sem, FAR int *sval);
208 
209 #ifdef CONFIG_FS_NAMED_SEMAPHORES
210 
214 FAR sem_t *sem_open(FAR const char *name, int oflag, ...);
218 int sem_close(FAR sem_t *sem);
222 int sem_unlink(FAR const char *name);
226 #endif
227 
228 #undef EXTERN
229 #ifdef __cplusplus
230 }
231 #endif
232 
233 #endif /* __INCLUDE_SEMAPHORE_H */
int sem_trywait(FAR sem_t *sem)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
int16_t counts
Definition: semaphore.h:101
int sem_wait(FAR sem_t *sem)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
Structure of semholder.
Definition: semaphore.h:96
This is the common part of the task control block (TCB). The TCB is the heart of the TinyAra task-con...
Definition: sched.h:456
int sem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
int sem_getvalue(FAR sem_t *sem, FAR int *sval)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
int sem_init(FAR sem_t *sem, int pshared, unsigned int value)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
FAR struct tcb_s * htcb
Definition: semaphore.h:100
int sem_destroy(FAR sem_t *sem)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
int sem_post(FAR sem_t *sem)
POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/)
Structure of generic semaphore.
Definition: semaphore.h:115
struct semholder_s * flink
Definition: semaphore.h:98
structure represents an elapsed time
Definition: time.h:152
FAR struct semholder_s * hhead
Definition: semaphore.h:124
int16_t semcount
Definition: semaphore.h:116