Tizen RT Libs&Environment  v1.1 D4
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 /****************************************************************************
74  * Pre-processor Definitions
75  ****************************************************************************/
76 
77 /* Bit definitions for the struct sem_s flags field */
78 
79 #define PRIOINHERIT_FLAGS_DISABLE (1 << 0) /* Bit 0: Priority inheritance
80  * is disabled for this semaphore */
81 
82 /****************************************************************************
83  * Public Type Declarations
84  ****************************************************************************/
85 
86 /* This structure contains information about the holder of a semaphore */
87 
88 #ifdef CONFIG_PRIORITY_INHERITANCE
89 struct tcb_s; /* Forward reference */
94 struct semholder_s {
95 #if CONFIG_SEM_PREALLOCHOLDERS > 0
96  struct semholder_s *flink; /* Implements singly linked list */
97 #endif
98  FAR struct tcb_s *htcb; /* Holder TCB */
99  int16_t counts; /* Number of counts owned by this holder */
100 };
101 
102 #if CONFIG_SEM_PREALLOCHOLDERS > 0
103 #define SEMHOLDER_INITIALIZER {NULL, NULL, 0}
104 #else
105 #define SEMHOLDER_INITIALIZER {NULL, 0}
106 #endif
107 #endif /* CONFIG_PRIORITY_INHERITANCE */
108 
113 struct sem_s {
114  int16_t semcount; /* >0 -> Num counts available */
115  /* <0 -> Num tasks waiting for semaphore */
116  /* If priority inheritance is enabled, then we have to keep track of which
117  * tasks hold references to the semaphore.
118  */
119 
120 #ifdef CONFIG_PRIORITY_INHERITANCE
121  uint8_t flags; /* See PRIOINHERIT_FLAGS_* definitions */
122 #if CONFIG_SEM_PREALLOCHOLDERS > 0
123  FAR struct semholder_s *hhead; /* List of holders of semaphore counts */
124 #else
125  struct semholder_s holder; /* Single holder */
126 #endif
127 #endif
128 };
129 
130 typedef struct sem_s sem_t;
131 
132 /* Initializers */
137 #ifdef CONFIG_PRIORITY_INHERITANCE
138 #if CONFIG_SEM_PREALLOCHOLDERS > 0
139 #define SEM_INITIALIZER(c) {(c), 0, NULL} /* semcount, flags, hhead */
140 #else
141 #define SEM_INITIALIZER(c) {(c), 0, SEMHOLDER_INITIALIZER} /* semcount, flags, holder */
142 #endif
143 #else
144 #define SEM_INITIALIZER(c) {(c)} /* semcount */
145 #endif
146 
147 /****************************************************************************
148  * Public Variables
149  ****************************************************************************/
150 
151 #ifdef __cplusplus
152 #define EXTERN extern "C"
153 extern "C"
154 {
155 #else
156 #define EXTERN extern
157 #endif
158 
159 /****************************************************************************
160  * Public Function Prototypes
161  ****************************************************************************/
162 /* Forward references needed by some prototypes */
163 
164 struct timespec; /* Defined in time.h */
165 
166 /* Counting Semaphore Interfaces (based on POSIX APIs) */
174 int sem_init(FAR sem_t *sem, int pshared, unsigned int value);
175 
184 int sem_destroy(FAR sem_t *sem);
193 int sem_wait(FAR sem_t *sem);
202 int sem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime);
211 int sem_trywait(FAR sem_t *sem);
220 int sem_post(FAR sem_t *sem);
228 int sem_getvalue(FAR sem_t *sem, FAR int *sval);
229 
230 void sem_timeout(int argc, uint32_t pid);
231 
232 #ifdef CONFIG_FS_NAMED_SEMAPHORES
233 
237 FAR sem_t *sem_open(FAR const char *name, int oflag, ...);
241 int sem_close(FAR sem_t *sem);
245 int sem_unlink(FAR const char *name);
249 #endif
250 
251 #undef EXTERN
252 #ifdef __cplusplus
253 }
254 #endif
255 
256 #endif /* __INCLUDE_SEMAPHORE_H */
int sem_trywait(FAR sem_t *sem)
lock a semaphore
int16_t counts
Definition: semaphore.h:99
int sem_wait(FAR sem_t *sem)
lock a semaphore
Structure of semholder.
Definition: semaphore.h:94
This is the common part of the task control block (TCB). The TCB is the heart of the TinyAra task-con...
Definition: sched.h:474
int sem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime)
lock a semaphore
int sem_getvalue(FAR sem_t *sem, FAR int *sval)
get the value of a semaphore
int sem_init(FAR sem_t *sem, int pshared, unsigned int value)
initialize an unnamed semaphore
FAR struct tcb_s * htcb
Definition: semaphore.h:98
int sem_destroy(FAR sem_t *sem)
destroy an unnamed semaphore
int sem_post(FAR sem_t *sem)
unlock a semaphore
Structure of generic semaphore.
Definition: semaphore.h:113
struct semholder_s * flink
Definition: semaphore.h:96
uint8_t flags
Definition: semaphore.h:121
structure represents an elapsed time
Definition: time.h:152
FAR struct semholder_s * hhead
Definition: semaphore.h:123
int16_t semcount
Definition: semaphore.h:114
void sem_timeout(int argc, uint32_t pid)