@@ -324,6 +324,32 @@ impl Client {
324324 } )
325325 }
326326
327+ /// Acquires a token from this jobserver client in a non-blocking way.
328+ ///
329+ /// # Return value
330+ ///
331+ /// On successful acquisition of a token an instance of [`Acquired`] is
332+ /// returned. This structure, when dropped, will release the token back to
333+ /// the jobserver. It's recommended to avoid leaking this value.
334+ ///
335+ /// # Errors
336+ ///
337+ /// If an I/O error happens while acquiring a token then this function will
338+ /// return immediately with the error. If an error is returned then a token
339+ /// was not acquired.
340+ ///
341+ /// If non-blocking acquire is not supported, the return error will have its `kind()`
342+ /// set to [`io::ErrorKind::Unsupported`].
343+ pub fn try_acquire ( & self ) -> io:: Result < Option < Acquired > > {
344+ let ret = self . inner . try_acquire ( ) ?;
345+
346+ Ok ( ret. map ( |data| Acquired {
347+ client : self . inner . clone ( ) ,
348+ data,
349+ disabled : false ,
350+ } ) )
351+ }
352+
327353 /// Returns amount of tokens in the read-side pipe.
328354 ///
329355 /// # Return value
@@ -607,52 +633,76 @@ fn find_jobserver_auth(var: &str) -> Option<&str> {
607633 . and_then ( |s| s. split ( ' ' ) . next ( ) )
608634}
609635
610- #[ test]
611- fn no_helper_deadlock ( ) {
612- let x = crate :: Client :: new ( 32 ) . unwrap ( ) ;
613- let _y = x. clone ( ) ;
614- std:: mem:: drop ( x. into_helper_thread ( |_| { } ) . unwrap ( ) ) ;
615- }
636+ #[ cfg( test) ]
637+ mod test {
638+ use super :: * ;
639+
640+ pub ( super ) fn run_named_fifo_try_acquire_tests ( client : & Client ) {
641+ assert ! ( client. try_acquire( ) . unwrap( ) . is_none( ) ) ;
642+ client. release_raw ( ) . unwrap ( ) ;
616643
617- #[ test]
618- fn test_find_jobserver_auth ( ) {
619- let cases = [
620- ( "" , None ) ,
621- ( "-j2" , None ) ,
622- ( "-j2 --jobserver-auth=3,4" , Some ( "3,4" ) ) ,
623- ( "--jobserver-auth=3,4 -j2" , Some ( "3,4" ) ) ,
624- ( "--jobserver-auth=3,4" , Some ( "3,4" ) ) ,
625- ( "--jobserver-auth=fifo:/myfifo" , Some ( "fifo:/myfifo" ) ) ,
626- ( "--jobserver-auth=" , Some ( "" ) ) ,
627- ( "--jobserver-auth" , None ) ,
628- ( "--jobserver-fds=3,4" , Some ( "3,4" ) ) ,
629- ( "--jobserver-fds=fifo:/myfifo" , Some ( "fifo:/myfifo" ) ) ,
630- ( "--jobserver-fds=" , Some ( "" ) ) ,
631- ( "--jobserver-fds" , None ) ,
632- (
633- "--jobserver-auth=auth-a --jobserver-auth=auth-b" ,
634- Some ( "auth-b" ) ,
635- ) ,
636- (
637- "--jobserver-auth=auth-b --jobserver-auth=auth-a" ,
638- Some ( "auth-a" ) ,
639- ) ,
640- ( "--jobserver-fds=fds-a --jobserver-fds=fds-b" , Some ( "fds-b" ) ) ,
641- ( "--jobserver-fds=fds-b --jobserver-fds=fds-a" , Some ( "fds-a" ) ) ,
642- (
643- "--jobserver-auth=auth-a --jobserver-fds=fds-a --jobserver-auth=auth-b" ,
644- Some ( "auth-b" ) ,
645- ) ,
646- (
647- "--jobserver-fds=fds-a --jobserver-auth=auth-a --jobserver-fds=fds-b" ,
648- Some ( "auth-a" ) ,
649- ) ,
650- ] ;
651- for ( var, expected) in cases {
652- let actual = find_jobserver_auth ( var) ;
653- assert_eq ! (
654- actual, expected,
655- "expect {expected:?}, got {actual:?}, input `{var:?}`"
656- ) ;
644+ let acquired = client. try_acquire ( ) . unwrap ( ) . unwrap ( ) ;
645+ assert ! ( client. try_acquire( ) . unwrap( ) . is_none( ) ) ;
646+
647+ drop ( acquired) ;
648+ client. try_acquire ( ) . unwrap ( ) . unwrap ( ) ;
649+ }
650+
651+ #[ cfg( not( unix) ) ]
652+ #[ test]
653+ fn test_try_acquire ( ) {
654+ let client = Client :: new ( 0 ) . unwrap ( ) ;
655+
656+ run_named_fifo_try_acquire_tests ( & client) ;
657+ }
658+
659+ #[ test]
660+ fn no_helper_deadlock ( ) {
661+ let x = crate :: Client :: new ( 32 ) . unwrap ( ) ;
662+ let _y = x. clone ( ) ;
663+ std:: mem:: drop ( x. into_helper_thread ( |_| { } ) . unwrap ( ) ) ;
664+ }
665+
666+ #[ test]
667+ fn test_find_jobserver_auth ( ) {
668+ let cases = [
669+ ( "" , None ) ,
670+ ( "-j2" , None ) ,
671+ ( "-j2 --jobserver-auth=3,4" , Some ( "3,4" ) ) ,
672+ ( "--jobserver-auth=3,4 -j2" , Some ( "3,4" ) ) ,
673+ ( "--jobserver-auth=3,4" , Some ( "3,4" ) ) ,
674+ ( "--jobserver-auth=fifo:/myfifo" , Some ( "fifo:/myfifo" ) ) ,
675+ ( "--jobserver-auth=" , Some ( "" ) ) ,
676+ ( "--jobserver-auth" , None ) ,
677+ ( "--jobserver-fds=3,4" , Some ( "3,4" ) ) ,
678+ ( "--jobserver-fds=fifo:/myfifo" , Some ( "fifo:/myfifo" ) ) ,
679+ ( "--jobserver-fds=" , Some ( "" ) ) ,
680+ ( "--jobserver-fds" , None ) ,
681+ (
682+ "--jobserver-auth=auth-a --jobserver-auth=auth-b" ,
683+ Some ( "auth-b" ) ,
684+ ) ,
685+ (
686+ "--jobserver-auth=auth-b --jobserver-auth=auth-a" ,
687+ Some ( "auth-a" ) ,
688+ ) ,
689+ ( "--jobserver-fds=fds-a --jobserver-fds=fds-b" , Some ( "fds-b" ) ) ,
690+ ( "--jobserver-fds=fds-b --jobserver-fds=fds-a" , Some ( "fds-a" ) ) ,
691+ (
692+ "--jobserver-auth=auth-a --jobserver-fds=fds-a --jobserver-auth=auth-b" ,
693+ Some ( "auth-b" ) ,
694+ ) ,
695+ (
696+ "--jobserver-fds=fds-a --jobserver-auth=auth-a --jobserver-fds=fds-b" ,
697+ Some ( "auth-a" ) ,
698+ ) ,
699+ ] ;
700+ for ( var, expected) in cases {
701+ let actual = find_jobserver_auth ( var) ;
702+ assert_eq ! (
703+ actual, expected,
704+ "expect {expected:?}, got {actual:?}, input `{var:?}`"
705+ ) ;
706+ }
657707 }
658708}
0 commit comments