Actual ALooper implementation

815 views
Skip to first unread message

Luca Castoro

unread,
Sep 17, 2013, 6:02:51 PM9/17/13
to andro...@googlegroups.com
Hello World!

Does someone know where the actual C/C++ implementation for the <android/looper.h> interface is?
I found that it *could* be in platform/frameworks/native/libs/utils/Looper.cpp but i'm not sure about it and by the way i cannot figure out how the C binding for it is.

Actually i'm trying to find out if a file descriptor previously submitted to an ALooper* via ALooper_addFd() is removed AND CLOSED when its ALooper_callbackFunc returns 0.

From <android/looper.h> (platform-18)

line 155: about typedef int (*ALooper_callbackFunc)(int fd, int events, void* data);

/*
 * Implementations should return 1 to continue receiving callbacks, or 0
 * to have this file descriptor and callback unregistered from the looper.
 */


line 233: about int ALooper_removeFd(ALooper* looper, int fd);

/*
 * When this method returns, it is safe to close the file descriptor since the looper
 * will no longer have a reference to it.  However, it is possible for the callback to
 * already be running or for it to run one last time if the file descriptor was already
 * signalled.  Calling code is responsible for ensuring that this case is safely handled.
 * For example, if the callback takes care of removing itself during its own execution either
 * by returning 0 or by calling this method, then it can be guaranteed to not be invoked
 * again at any later time unless registered anew.

  */

Still returning zero from a ALooper_callbackFunc this way: ( error checking and actual work removed for clarity )

static int loaderCallback( int fd , int events , void* any )
{
    Args* args = (Args*)any;
    ALooper* loop;
    int check;
    void* ret;

    if( 1 != ( check = read( fd , &ret , 1 ) ) ) LOGE( "read( %d , 1 ) = %d, errno = %d: %s" , fd , check , errno , strerror( errno ) );

    if( ( loop = ALooper_forThread() ) )
    {
        ALooper_removeFd( loop , args->pipe[0] );
    }
    else LOGE( "ALooper_forThread() == NULL, check ure threading soldier!" );

    close( args->pipe[P_IN ] );
    close( args->pipe[P_OUT] );

    return 0;
}


causes my device ( ASUS TF101 @ 4.0.3 ) to CRASH and REBOOT immediately, while this way...

static int loaderCallback( int fd , int events , void* any )
{
   
Args* args = (Args*)any;
    ALooper* loop;
    int check;
    void* ret;

    if( 1 != ( check = read( fd , &ret , 1 ) ) ) LOGE( "read( %d , 1 ) = %d, errno = %d: %s" , fd , check , errno , strerror( errno ) );
/*
    if( ( loop = ALooper_forThread() ) )
    {
        ALooper_removeFd( loop , args->pipe[0] );
    }
    else LOGE( "ALooper_forThread() == NULL, check ure threading soldier!" );

    close( args->pipe[P_IN ] );
*/

    close( args->pipe[P_OUT] );

    return 0;
}


...the code runs flawlessy. ( "args" is a 'casted to struct' pointer to "void* any", P_IN and P_OUT are 0 & 1 respectively and args->pipe[P_IN] == fd )

While I opened both file descriptors by myself i would like to assure that they will be both closed at some time after this routine returns ( I'm a kinda reentrant code maniac =P ).

Thanks for ure patience on reading this thing and for any help / advice about the topic!

mic _

unread,
Sep 18, 2013, 6:20:26 AM9/18/13
to andro...@googlegroups.com
>I found that it *could* be in platform/frameworks/native/libs/utils/Looper.cpp but i'm not sure about it and by the way i cannot figure out how the C binding for it is.

Looks right to me. 
frameworks/native/libs/utils/Looper.cpp is built into libutils (https://www.codeaurora.org/cgit/quic/la/platform/frameworks/native/tree/libs/utils/Android.mk?h=jb_mr1), and libandroid (where you can find the ALooper functions: https://www.codeaurora.org/cgit/quic/la/platform/frameworks/base/tree/native/android/looper.cpp?h=jb_mr1) links against libutils.

/Michael
 



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

Luca Castoro

unread,
Sep 19, 2013, 6:33:30 PM9/19/13
to andro...@googlegroups.com
Oh snap! It was right under my nose :)
Thank you very mouch for ure help!
Also I didn't hear about the project aurora, so thank you also for this "extra"!

Luca Castoro

unread,
Sep 20, 2013, 11:01:49 AM9/20/13
to andro...@googlegroups.com
Just for a sake of completeness and for those who don't find it obvious,it is not safe to call ALooper_removeFd( ... ) while returning zero from a callback, because it will be recalled after the funcion returns.
If u want ( as i do ) close the file descriptor after the deregistration from inside a callback you shoud:
- call ALooper_removeFd();
- close the fd
- return 1 at callback end.
Reply all
Reply to author
Forward
0 new messages