hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#ifndef _COPPERPLATE_SYNCOBJ_H
#define _COPPERPLATE_SYNCOBJ_H
 
#include <pthread.h>
#include <time.h>
#include <boilerplate/list.h>
#include <boilerplate/lock.h>
#include <copperplate/reference.h>
 
/* syncobj->flags */
#define SYNCOBJ_FIFO    0x0
#define SYNCOBJ_PRIO    0x1
#define SYNCOBJ_LOCKED    0x2
 
/* threadobj->wait_status */
#define SYNCOBJ_FLUSHED        0x1
#define SYNCOBJ_SIGNALED    0x2
#define SYNCOBJ_DRAINWAIT    0x4
 
#define SYNCOBJ_MAGIC  0xf9f99f9f
 
struct threadobj;
 
struct syncstate {
   int state;
};
 
#ifdef CONFIG_XENO_COBALT
 
#include <boilerplate/atomic.h>
#include <cobalt/uapi/monitor.h>
 
struct syncobj_corespec {
   cobalt_monitor_t monitor;
};
 
#else  /* CONFIG_XENO_MERCURY */
 
struct syncobj_corespec {
   pthread_mutex_t lock;
   pthread_cond_t drain_sync;
};
 
#endif /* CONFIG_XENO_MERCURY */
 
struct syncobj {
   unsigned int magic;
   int flags;
   int wait_count;
   struct listobj grant_list;
   int grant_count;
   struct listobj drain_list;
   int drain_count;
   struct syncobj_corespec core;
   fnref_type(void (*)(struct syncobj *sobj)) finalizer;
};
 
#define syncobj_for_each_grant_waiter(sobj, pos)        \
   list_for_each_entry(pos, &(sobj)->grant_list, wait_link)
 
#define syncobj_for_each_grant_waiter_safe(sobj, pos, tmp)    \
   list_for_each_entry_safe(pos, tmp, &(sobj)->grant_list, wait_link)
 
#define syncobj_for_each_drain_waiter(sobj, pos)        \
   list_for_each_entry(pos, &(sobj)->drain_list, wait_link)
 
#define syncobj_for_each_drain_waiter_safe(sobj, pos, tmp)    \
   list_for_each_entry_safe(pos, tmp, &(sobj)->drain_list, wait_link)
 
void __syncobj_cleanup_wait(struct syncobj *sobj,
               struct threadobj *thobj);
 
#ifdef CONFIG_XENO_DEBUG
 
static inline void __syncobj_tag_locked(struct syncobj *sobj)
{
   sobj->flags |= SYNCOBJ_LOCKED;
}
 
static inline void __syncobj_tag_unlocked(struct syncobj *sobj)
{
   assert(sobj->flags & SYNCOBJ_LOCKED);
   sobj->flags &= ~SYNCOBJ_LOCKED;
}
 
static inline void __syncobj_check_locked(struct syncobj *sobj)
{
   assert(sobj->flags & SYNCOBJ_LOCKED);
}
 
#else /* !CONFIG_XENO_DEBUG */
 
static inline void __syncobj_tag_locked(struct syncobj *sobj)
{
}
 
static inline void __syncobj_tag_unlocked(struct syncobj *sobj)
{
}
 
static inline void __syncobj_check_locked(struct syncobj *sobj)
{
}
 
#endif /* !CONFIG_XENO_DEBUG */
 
#ifdef __cplusplus
extern "C" {
#endif
 
int __syncobj_broadcast_drain(struct syncobj *sobj, int reason);
 
int __syncobj_broadcast_grant(struct syncobj *sobj, int reason);
 
int syncobj_init(struct syncobj *sobj, clockid_t clk_id, int flags,
        fnref_type(void (*)(struct syncobj *sobj)) finalizer) __must_check;
 
int syncobj_wait_grant(struct syncobj *sobj,
        const struct timespec *timeout,
        struct syncstate *syns) __must_check;
 
struct threadobj *syncobj_grant_one(struct syncobj *sobj);
 
void syncobj_grant_to(struct syncobj *sobj,
             struct threadobj *thobj);
 
struct threadobj *syncobj_peek_grant(struct syncobj *sobj);
 
struct threadobj *syncobj_peek_drain(struct syncobj *sobj);
 
int syncobj_lock(struct syncobj *sobj,
        struct syncstate *syns) __must_check;
 
void syncobj_unlock(struct syncobj *sobj,
           struct syncstate *syns);
 
int syncobj_wait_drain(struct syncobj *sobj,
              const struct timespec *timeout,
              struct syncstate *syns) __must_check;
 
int syncobj_destroy(struct syncobj *sobj,
           struct syncstate *syns);
 
void syncobj_uninit(struct syncobj *sobj);
 
static inline int syncobj_grant_wait_p(struct syncobj *sobj)
{
   __syncobj_check_locked(sobj);
 
   return !list_empty(&sobj->grant_list);
}
 
static inline int syncobj_count_grant(struct syncobj *sobj)
{
   __syncobj_check_locked(sobj);
 
   return sobj->grant_count;
}
 
static inline int syncobj_count_drain(struct syncobj *sobj)
{
   __syncobj_check_locked(sobj);
 
   return sobj->drain_count;
}
 
static inline int syncobj_drain_wait_p(struct syncobj *sobj)
{
   __syncobj_check_locked(sobj);
 
   return !list_empty(&sobj->drain_list);
}
 
static inline int syncobj_drain(struct syncobj *sobj)
{
   int ret = 0;
 
   __syncobj_check_locked(sobj);
 
   if (sobj->drain_count > 0)
       ret = __syncobj_broadcast_drain(sobj, SYNCOBJ_SIGNALED);
 
   return ret;
}
 
static inline int syncobj_grant_all(struct syncobj *sobj)
{
   int ret = 0;
 
   __syncobj_check_locked(sobj);
 
   if (sobj->grant_count > 0)
       ret = __syncobj_broadcast_grant(sobj, SYNCOBJ_SIGNALED);
 
   return ret;
}
 
static inline int syncobj_flush(struct syncobj *sobj)
{
   __syncobj_check_locked(sobj);
 
   if (sobj->grant_count > 0)
       __syncobj_broadcast_grant(sobj, SYNCOBJ_FLUSHED);
 
   if (sobj->drain_count > 0)
       __syncobj_broadcast_drain(sobj, SYNCOBJ_FLUSHED);
 
   return sobj->wait_count;
}
 
#ifdef __cplusplus
}
#endif
 
#endif /* _COPPERPLATE_SYNCOBJ_H */