Skip to content

Commit 41699f7

Browse files
committed
Add semaphore APIs
1 parent eb637d2 commit 41699f7

File tree

11 files changed

+70
-0
lines changed

11 files changed

+70
-0
lines changed

libc-test/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ fn main() {
9696
cfg.header("termios.h");
9797
cfg.header("poll.h");
9898
cfg.header("syslog.h");
99+
cfg.header("semaphore.h");
99100
}
100101

101102
if android {
@@ -285,6 +286,8 @@ fn main() {
285286
// uuid_t is a struct, not an integer.
286287
"uuid_t" if dragonfly => true,
287288
n if n.starts_with("pthread") => true,
289+
// sem_t is a struct or pointer
290+
"sem_t" if openbsd || freebsd || rumprun => true,
288291

289292
// windows-isms
290293
n if n.starts_with("P") => true,
@@ -363,6 +366,10 @@ fn main() {
363366
// we turn into an error) so just ignore it.
364367
"daemon" if apple => true,
365368

369+
// Deprecated on OSX
370+
"sem_destroy" if apple => true,
371+
"sem_init" if apple => true,
372+
366373
// These functions presumably exist on netbsd but don't look like
367374
// they're implemented on rumprun yet, just let them slide for now.
368375
// Some of them look like they have headers but then don't have

src/unix/bsd/apple/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub type speed_t = ::c_ulong;
2020
pub type tcflag_t = ::c_ulong;
2121
pub type nl_item = ::c_int;
2222
pub type id_t = ::c_uint;
23+
pub type sem_t = ::c_int;
2324

2425
pub enum timezone {}
2526

@@ -1248,6 +1249,8 @@ pub const PRIO_DARWIN_PROCESS: ::c_int = 4;
12481249
pub const PRIO_DARWIN_BG: ::c_int = 0x1000;
12491250
pub const PRIO_DARWIN_NONUI: ::c_int = 0x1001;
12501251

1252+
pub const SEM_FAILED: *mut sem_t = -1isize as *mut ::sem_t;
1253+
12511254
f! {
12521255
pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
12531256
status >> 8

src/unix/bsd/freebsdlike/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub type tcflag_t = ::c_uint;
1111
pub type speed_t = ::c_uint;
1212
pub type nl_item = ::c_int;
1313
pub type id_t = i64;
14+
pub type sem_t = _sem;
1415

1516
pub enum timezone {}
1617

@@ -149,6 +150,11 @@ s! {
149150
pub int_p_sign_posn: ::c_char,
150151
pub int_n_sign_posn: ::c_char,
151152
}
153+
154+
// internal structure has changed over time
155+
pub struct _sem {
156+
data: [u32; 4],
157+
}
152158
}
153159

154160
pub const LC_COLLATE_MASK: ::c_int = (1 << 0);
@@ -677,6 +683,8 @@ pub const LOG_NFACILITIES: ::c_int = 24;
677683
pub const TIOCGWINSZ: ::c_ulong = 0x40087468;
678684
pub const TIOCSWINSZ: ::c_ulong = 0x80087467;
679685

686+
pub const SEM_FAILED: *mut sem_t = 0 as *mut sem_t;
687+
680688
#[link(name = "util")]
681689
extern {
682690
pub fn getnameinfo(sa: *const ::sockaddr,

src/unix/bsd/openbsdlike/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ pub type tcflag_t = ::c_uint;
1111
pub type nl_item = c_long;
1212
pub type clockid_t = ::c_int;
1313
pub type id_t = ::uint32_t;
14+
pub type sem_t = *mut sem;
1415

1516
pub enum timezone {}
17+
pub enum sem {}
1618

1719
s! {
1820
pub struct sigaction {
@@ -445,6 +447,8 @@ pub const LOG_NFACILITIES: ::c_int = 24;
445447

446448
pub const HW_NCPU: ::c_int = 3;
447449

450+
pub const SEM_FAILED: *mut sem_t = 0 as *mut sem_t;
451+
448452
#[link(name = "util")]
449453
extern {
450454
pub fn mincore(addr: *mut ::c_void, len: ::size_t,

src/unix/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,20 @@ extern {
673673
pub fn setlocale(category: ::c_int,
674674
locale: *const ::c_char) -> *mut ::c_char;
675675
pub fn localeconv() -> *mut lconv;
676+
677+
pub fn sem_destroy(sem: *mut sem_t) -> ::c_int;
678+
pub fn sem_open(name: *const ::c_char, oflag: ::c_int, ...) -> *mut sem_t;
679+
pub fn sem_close(sem: *mut sem_t) -> ::c_int;
680+
pub fn sem_unlink(name: *const ::c_char) -> ::c_int;
681+
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
682+
link_name = "sem_wait$UNIX2003")]
683+
pub fn sem_wait(sem: *mut sem_t) -> ::c_int;
684+
pub fn sem_trywait(sem: *mut sem_t) -> ::c_int;
685+
pub fn sem_post(sem: *mut sem_t) -> ::c_int;
686+
pub fn sem_init(sem: *mut sem_t,
687+
pshared: ::c_int,
688+
value: ::c_uint)
689+
-> ::c_int;
676690
}
677691

678692
// TODO: get rid of this cfg(not(...))

src/unix/notbsd/android/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ s! {
9595
#[cfg(target_pointer_width = "32")]
9696
__bits: [__CPU_BITTYPE; 1],
9797
}
98+
99+
pub struct sem_t {
100+
count: ::c_uint,
101+
}
98102
}
99103

100104
pub const BUFSIZ: ::c_uint = 1024;
@@ -472,6 +476,8 @@ pub const RTLD_NOLOAD: ::c_int = 0x4;
472476
pub const RTLD_NOW: ::c_int = 0;
473477
pub const RTLD_DEFAULT: *mut ::c_void = -1isize as *mut ::c_void;
474478

479+
pub const SEM_FAILED: *mut sem_t = 0 as *mut sem_t;
480+
475481
f! {
476482
pub fn sigemptyset(set: *mut sigset_t) -> ::c_int {
477483
*set = 0;

src/unix/notbsd/linux/mips.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ s! {
189189
pub mem_unit: ::c_uint,
190190
pub _f: [::c_char; 8],
191191
}
192+
193+
// FIXME this is actually a union
194+
pub struct sem_t {
195+
#[cfg(target_pointer_width = "32")]
196+
__size: [::c_char; 16],
197+
#[cfg(target_pointer_width = "64")]
198+
__size: [::c_char; 32],
199+
__align: [::c_long; 0],
200+
}
192201
}
193202

194203
pub const BUFSIZ: ::c_uint = 8192;

src/unix/notbsd/linux/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ pub const AF_NETLINK: ::c_int = 16;
452452

453453
pub const LOG_NFACILITIES: ::c_int = 24;
454454

455+
pub const SEM_FAILED: *mut ::sem_t = 0 as *mut sem_t;
456+
455457
f! {
456458
pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
457459
for slot in cpuset.bits.iter_mut() {

src/unix/notbsd/linux/musl/b32/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ s! {
2020
pub msg_controllen: ::socklen_t,
2121
pub msg_flags: ::c_int,
2222
}
23+
24+
pub struct sem_t {
25+
__val: [::c_int; 4],
26+
}
2327
}
2428

2529
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 32;

src/unix/notbsd/linux/musl/b64/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ s! {
100100
__pad2: ::socklen_t,
101101
pub msg_flags: ::c_int,
102102
}
103+
104+
pub struct sem_t {
105+
__val: [::c_int; 8],
106+
}
103107
}
104108

105109
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56;

src/unix/notbsd/linux/other/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ s! {
121121
__unused4: ::c_ulong,
122122
__unused5: ::c_ulong
123123
}
124+
125+
// FIXME this is actually a union
126+
pub struct sem_t {
127+
#[cfg(target_pointer_width = "32")]
128+
__size: [::c_char; 16],
129+
#[cfg(target_pointer_width = "64")]
130+
__size: [::c_char; 32],
131+
__align: [::c_long; 0],
132+
}
124133
}
125134

126135
pub const RLIMIT_RSS: ::c_int = 5;

0 commit comments

Comments
 (0)