2 #include <BeOSBuildCompatibility.h>
8 #include <SupportDefs.h>
10 // We assume that everything is single-threaded, so we don't need real
11 // semaphores. Simple fakes are sufficient.
19 static const int kSemaphoreCount
= 40960;
20 static semaphore sSemaphores
[kSemaphoreCount
];
26 if (id
< 0 || id
>= kSemaphoreCount
)
28 return sSemaphores
[id
].inUse
;
33 create_sem(int32 count
, const char *name
)
35 for (int i
= 0; i
< kSemaphoreCount
; i
++) {
36 semaphore
&sem
= sSemaphores
[i
];
38 sem
.name
= strdup(name
? name
: "unnamed sem");
49 return B_NO_MORE_SEMS
;
59 sSemaphores
[id
].inUse
= false;
60 free(sSemaphores
[id
].name
);
61 sSemaphores
[id
].name
= NULL
;
68 acquire_sem(sem_id id
)
70 return acquire_sem_etc(id
, 1, 0, 0);
75 acquire_sem_etc(sem_id id
, int32 count
, uint32 flags
, bigtime_t timeout
)
83 semaphore
&sem
= sSemaphores
[id
];
84 if (sem
.count
>= count
) {
92 bool noTimeout
= false;
93 if (flags
& B_RELATIVE_TIMEOUT
) {
94 // relative timeout: get the absolute time when to time out
96 // special case: on timeout == 0 we return B_WOULD_BLOCK
100 bigtime_t currentTime
= system_time();
101 if (timeout
> B_INFINITE_TIMEOUT
|| currentTime
>= B_INFINITE_TIMEOUT
- timeout
) {
104 timeout
+= currentTime
;
107 } else if (flags
& B_ABSOLUTE_TIMEOUT
) {
116 debugger("Would block on a semaphore without timeout in a "
117 "single-threaded context!");
121 // wait for the time out time
122 status_t error
= snooze_until(timeout
, B_SYSTEM_TIMEBASE
);
131 release_sem(sem_id id
)
133 return release_sem_etc(id
, 1, 0);
138 release_sem_etc(sem_id id
, int32 count
, uint32 flags
)
146 semaphore
&sem
= sSemaphores
[id
];
154 get_sem_count(sem_id id
, int32
*threadCount
)
162 *threadCount
= sSemaphores
[id
].count
;
168 set_sem_owner(sem_id id
, team_id team
)
178 _get_sem_info(sem_id id
, struct sem_info
*info
, size_t infoSize
)
188 strlcpy(info
->name
, sSemaphores
[id
].name
, sizeof(info
->name
));
189 info
->count
= sSemaphores
[id
].count
;
190 info
->latest_holder
= -1;
195 // _get_next_sem_info
197 _get_next_sem_info(team_id team
, int32
*cookie
, struct sem_info
*info
,
200 if (team
< 0 || team
> 2)
201 return B_BAD_TEAM_ID
;
203 for (int i
= *cookie
; i
< kSemaphoreCount
; i
++) {
204 if (sSemaphores
[i
].inUse
) {
206 return _get_sem_info(i
, info
, infoSize
);
210 return B_ENTRY_NOT_FOUND
;