I have an ALooper with 2 pipes and 2 callback functions in C,
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);
and here are the 2 callback functions which work correctly
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
}
the problem is that they are being called out of order in my code, resulting in say a clearScreen()
at different times than when I wrote to the pipes in the code.
If anyone knows how to synchronize these alooper callbacks please fill me in. I am using ALooper because I am using pthread in C and the writes to the pipes happen from the pthread and Alooper is the way that I call back to the UI thread in android
thank you for your time
this is how I wound up doing it.
in my file with the callbacks make
_Atomic bool command = false;
_Atomic bool writing = false;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
then in my callbacks I put this
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;
}
and
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
}
then depending on which pipe I was writing to this
//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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.