Skip to content

Commit 6ac7673

Browse files
unix_api: Add support for pthreads (#288)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 627ff47 commit 6ac7673

File tree

12 files changed

+668
-6
lines changed

12 files changed

+668
-6
lines changed

pkgs/unix_api/a.out

-32.6 KB
Binary file not shown.

pkgs/unix_api/constants.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@
6464
- EMFILE
6565
- ENOENT
6666
- ENOSPC
67+
- ENOSYS
6768
- ENOTDIR
6869
- ENOTEMPTY
6970
- EPERM
71+
- ETIMEDOUT
7072
"<fcntl.h>":
7173
- AT_FDCWD
7274
- AT_REMOVEDIR

pkgs/unix_api/hook/build.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void main(List<String> args) async {
1616
assetName: 'libc_shim',
1717
libraries: [
1818
if ([OS.linux].contains(input.config.code.targetOS)) 'crypt',
19+
if (input.config.code.targetOS != OS.android) 'pthread',
1920
],
2021
sources: [
2122
'src/libc_shim.c',

pkgs/unix_api/lib/src/bespoke.dart

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'dart:ffi' as ffi;
66

77
import 'errno.dart';
88
import 'libc_bindings.g.dart';
9-
export 'libc_bindings.g.dart' show DIR, dirent, Stat, timespec;
9+
export 'libc_bindings.g.dart'
10+
show DIR, dirent, Stat, timespec, pthread_t, pthread_attr_t;
1011

1112
/// Gets metadata for a file.
1213
///
@@ -65,3 +66,103 @@ extension DirentPtrExtensions on ffi.Pointer<dirent> {
6566
/// Need because of https://github.com/dart-lang/sdk/issues/41237.
6667
ffi.Pointer<ffi.Char> get d_name_ptr => libc_shim_d_name_ptr(this);
6768
}
69+
70+
// <pthread.h>
71+
72+
/// Unlocks all threads waiting on a condition variable.
73+
///
74+
/// See the [POSIX specification for `pthread_cond_broadcast`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html).
75+
int pthread_cond_broadcast(ffi.Pointer<pthread_cond_t> cond) =>
76+
libc_shim_pthread_cond_broadcast(cond, errnoPtr);
77+
78+
/// Destroys a condition variable.
79+
///
80+
/// See the [POSIX specification for `pthread_cond_destroy`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html).
81+
int pthread_cond_destroy(ffi.Pointer<pthread_cond_t> cond) =>
82+
libc_shim_pthread_cond_destroy(cond, errnoPtr);
83+
84+
/// Initializes a condition variable.
85+
///
86+
/// See the [POSIX specification for `pthread_cond_init`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html).
87+
int pthread_cond_init(
88+
ffi.Pointer<pthread_cond_t> cond,
89+
ffi.Pointer<pthread_condattr_t> attr,
90+
) => libc_shim_pthread_cond_init(cond, attr, errnoPtr);
91+
92+
/// Unlocks one thread waiting on a condition variable.
93+
///
94+
/// See the [POSIX specification for `pthread_cond_signal`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html).
95+
int pthread_cond_signal(ffi.Pointer<pthread_cond_t> cond) =>
96+
libc_shim_pthread_cond_signal(cond, errnoPtr);
97+
98+
/// Waits on a condition variable for a given amount of time.
99+
///
100+
/// See the [POSIX specification for `pthread_cond_timedwait`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html).
101+
int pthread_cond_timedwait(
102+
ffi.Pointer<pthread_cond_t> cond,
103+
ffi.Pointer<pthread_mutex_t> mutex,
104+
ffi.Pointer<timespec> abstime,
105+
) => libc_shim_pthread_cond_timedwait(cond, mutex, abstime, errnoPtr);
106+
107+
/// Waits on a condition variable.
108+
///
109+
/// See the [POSIX specification for `pthread_cond_wait`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html).
110+
int pthread_cond_wait(
111+
ffi.Pointer<pthread_cond_t> cond,
112+
ffi.Pointer<pthread_mutex_t> mutex,
113+
) => libc_shim_pthread_cond_wait(cond, mutex, errnoPtr);
114+
115+
/// Creates a new thread.
116+
///
117+
/// See the [POSIX specification for `pthread_create`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_create.html).
118+
int pthread_create(
119+
ffi.Pointer<pthread_t> thread,
120+
ffi.Pointer<pthread_attr_t> attr,
121+
ffi.Pointer<
122+
ffi.NativeFunction<ffi.Pointer<ffi.Void> Function(ffi.Pointer<ffi.Void>)>
123+
>
124+
start_routine,
125+
ffi.Pointer<ffi.Void> arg,
126+
) => libc_shim_pthread_create(thread, attr, start_routine, arg, errnoPtr);
127+
128+
/// Detaches a thread.
129+
///
130+
/// See the [POSIX specification for `pthread_detach`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_detach.html).
131+
int pthread_detach(pthread_t thread) =>
132+
libc_shim_pthread_detach(thread, errnoPtr);
133+
134+
/// Destroys a mutex.
135+
///
136+
/// See the [POSIX specification for `pthread_mutex_destroy`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_destroy.html).
137+
int pthread_mutex_destroy(ffi.Pointer<pthread_mutex_t> mutex) =>
138+
libc_shim_pthread_mutex_destroy(mutex, errnoPtr);
139+
140+
/// Initializes a mutex.
141+
///
142+
/// See the [POSIX specification for `pthread_mutex_init`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html).
143+
int pthread_mutex_init(
144+
ffi.Pointer<pthread_mutex_t> mutex,
145+
ffi.Pointer<pthread_mutexattr_t> attr,
146+
) => libc_shim_pthread_mutex_init(mutex, attr, errnoPtr);
147+
148+
/// Locks a mutex.
149+
///
150+
/// See the [POSIX specification for `pthread_mutex_lock`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html).
151+
int pthread_mutex_lock(ffi.Pointer<pthread_mutex_t> mutex) =>
152+
libc_shim_pthread_mutex_lock(mutex, errnoPtr);
153+
154+
/// Locks a mutex, failing if the lock is not acquired before a timeout.
155+
///
156+
/// Available on Android and Linux.
157+
///
158+
/// See the [POSIX specification for `pthread_mutex_timedlock`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html).
159+
int pthread_mutex_timedlock(
160+
ffi.Pointer<pthread_mutex_t> mutex,
161+
ffi.Pointer<timespec> abs_timeout,
162+
) => libc_shim_pthread_mutex_timedlock(mutex, abs_timeout, errnoPtr);
163+
164+
/// Unlocks a mutex.
165+
///
166+
/// See the [POSIX specification for `pthread_mutex_unlock`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_unlock.html).
167+
int pthread_mutex_unlock(ffi.Pointer<pthread_mutex_t> mutex) =>
168+
libc_shim_pthread_mutex_unlock(mutex, errnoPtr);

pkgs/unix_api/lib/src/constant_bindings.g.dart

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/unix_api/lib/src/constants.g.dart

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/unix_api/lib/src/libc_bindings.g.dart

Lines changed: 168 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/unix_api/src/constants.g.c

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)