ALooper回调函数同步

ALooper callback functions syncronization

我在 C 语言中有一个带 2 个管道和 2 个回调函数的 ALooper,

mainThreadLooper = ALooper_forThread(); // get looper for this thread
ALooper_acquire(mainThreadLooper); // add reference to keep object alive
pipe(messagePipe); 
pipe(commandPipe);

ALooper_addFd(mainThreadLooper, messagePipe[0],
        0, ALOOPER_EVENT_INPUT, looperCallback, NULL);
ALooper_addFd(mainThreadLooper, commandPipe[0],
        0, ALOOPER_EVENT_INPUT, commandLooperCallback, NULL);

下面是 2 个正确工作的回调函数

static int commandLooperCallback(int fd, int events, void* data){
    LOGD("command pipe callback");
    wchar_t c;
    read(fd, &c,sizeof(c));
    if(running){
        if(c == 0){
            return 1;
        }
        if(c == CLEAR_SCREEN){
            LOGD("clear screen");
            clearScreen();
            return 1;
        }
    }

    return 1;
}


static int looperCallback(int fd, int events, void* data) {
    //char msg[100];
    wchar_t msg[100];

    int length = read(fd, &msg,sizeof(msg)); //99); // read message from pipe
    LOGD("string recieved: %ls", msg);
    char str[length];
    wcstombs(str,msg,length);
    LOGD("char string %s", str);
  //msg[length] = 0;
    if(running){
        printToTextView(str);
    }

    //LOGD("returning from looper callback");
    return 1; // continue listening for events
}

问题是它们在我的代码中被乱序调用,导致 clearScreen() 与我在代码中写入管道的时间不同。

如果有人知道如何同步这些 alooper 回调,请填写。我正在使用 ALooper,因为我在 C 中使用 pthread,并且对管道的写入是从 pthread 发生的,而 Alooper 是我回调的方式UI 线程 android

感谢您的宝贵时间

我就是这样完成的。

在我的文件中使用回调 make

_Atomic bool command = false;
_Atomic bool writing = false;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

然后在我的回调中我把这个

static int commandLooperCallback(int fd, int events, void* data){
    if(!command){
        return 1;
    }
    LOGD("command pipe callback");
    wchar_t c;
    read(fd, &c,sizeof(c));
    if(running){
        if(c == 0){
            return 1;
        }
        if(c == CLEAR_SCREEN){
            LOGD("clear screen");


            clearScreen();
            command = false;
            pthread_mutex_lock(&lock);
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&lock);
            //wait = false;
            return 1;
        }
    }

    return 1;
}

static int looperCallback(int fd, int events, void* data) {
    //char msg[100];
    if(!writing){
        return 1;
    }
    wchar_t msg[100];

    int length = read(fd, &msg,sizeof(msg)); //99); // read message from pipe
    LOGD("string recieved: %ls", msg);
    char str[length];
    wcstombs(str,msg,length);
    LOGD("char string %s", str);
  //msg[length] = 0;
    if(running){
        printToTextView(str);
    }
    writing = false;
    pthread_mutex_lock(&lock);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&lock);
    //LOGD("returning from looper callback");
    return 1; // continue listening for events
}

然后取决于我正在写入哪个管道

//and opposite for commandPipe and making command bool true
write(messagePipe[1],NameBuffer, sizeof(NameBuffer));//strlen(s));
    writing = true;
    pthread_mutex_lock(&lock);
    pthread_cond_wait(&cond, &lock);
    pthread_mutex_unlock(&lock);