Skip to content

Commit c32c009

Browse files
committed
Ensure a jack client is initialized when calling get_time
1 parent 1daee27 commit c32c009

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

src/client/client_impl.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ impl Client {
9797
AsyncClient::new(self, notification_handler, process_handler)
9898
}
9999

100+
/// Return JACK's current system time in microseconds, using the JACK clock
101+
/// source.
102+
///
103+
/// Note: Although attached a `Client` method, this should use the same time clock as all
104+
/// clients.
105+
pub fn time(&self) -> Time {
106+
// Despite not needing a ptr to the client, this function often segfaults if a client has
107+
// not been initialized.
108+
unsafe { jack_sys::jack_get_time() }
109+
}
110+
100111
/// The sample rate of the JACK system, as set by the user when jackd was
101112
/// started.
102113
pub fn sample_rate(&self) -> usize {

src/client/test.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ fn open_test_client(name: &str) -> (Client, ClientStatus) {
66
Client::new(name, ClientOptions::NO_START_SERVER).unwrap()
77
}
88

9+
#[test]
10+
fn time_can_get_time() {
11+
open_test_client("tcgt").0.time();
12+
}
13+
14+
#[test]
15+
fn time_is_monotonically_increasing() {
16+
let c = open_test_client("tcgt").0;
17+
let initial_t = c.time();
18+
std::thread::sleep(std::time::Duration::from_millis(100));
19+
let later_t = c.time();
20+
assert!(initial_t < later_t);
21+
}
22+
923
#[test]
1024
fn client_valid_client_name_size() {
1125
assert!(*CLIENT_NAME_SIZE > 0);

src/lib.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,15 @@ mod transport;
8080
/// Properties
8181
mod properties;
8282

83+
static TIME_CLIENT: std::sync::LazyLock<Client> = std::sync::LazyLock::new(|| {
84+
Client::new("deprecated_get_time", ClientOptions::NO_START_SERVER)
85+
.unwrap()
86+
.0
87+
});
88+
8389
/// Return JACK's current system time in microseconds, using the JACK clock
8490
/// source.
91+
#[deprecated = "Prefer using Client::time. get_time will be eventually be removed and it requires an extra client initialization."]
8592
pub fn get_time() -> primitive_types::Time {
86-
unsafe { jack_sys::jack_get_time() }
87-
}
88-
89-
#[cfg(test)]
90-
mod test {
91-
use super::*;
92-
use std::{thread, time};
93-
94-
#[test]
95-
fn time_can_get_time() {
96-
get_time();
97-
}
98-
99-
#[test]
100-
fn time_is_monotonically_increasing() {
101-
let initial_t = get_time();
102-
thread::sleep(time::Duration::from_millis(100));
103-
let later_t = get_time();
104-
assert!(initial_t < later_t);
105-
}
93+
TIME_CLIENT.time()
10694
}

src/properties.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ mod metadata {
6161
}
6262

6363
/// A piece of Metadata on a Jack `subject`: either a port or a client.
64-
/// See the JACK Metadata API [description](https://jackaudio.org/metadata/) and [documentation](https://jackaudio.org/api/group__Metadata.html) and for more info.
64+
///
65+
/// See the JACK Metadata API [description](https://jackaudio.org/metadata/) and
66+
/// [documentation](https://jackaudio.org/api/group__Metadata.html) and for more info.
6567
#[derive(Debug, Clone, PartialEq, Eq)]
6668
pub struct Property {
6769
value: String,

0 commit comments

Comments
 (0)