struct thread {
qurt_thread_t tid;
qurt_sem_t sem;
boolean running;
void* stack;
}
static int thread_start(void* data) {
struct thread* th = (struct thread*)data;
while(th->running) {
qurt_sem_down(&th->sem);
}
return 0;
}
static void thread_dtor(void* data) {
struct thread* th = (struct thread*)data;
if(th->tid) {
int err;
th->running = 0;
qurt_sem_up(&th->sem);
qurt_thread_join(th->tid, &err);
}
if(th->stack) {
free(th->stack);
qurt_sem_destroy(&th->sem);
}
}
static int thread_ctor (void* ctx, void* data) {
//this constructor may be called twice, but only one instance will survive
qurt_thread_attr_t attr;
struct thread* th = (struct thread*)data;
int size = 1024*8;
if(0 == (th->stack = malloc(size))) {
goto error;
}
qurt_sem_init_val(&th->sem, 0);
qurt_thread_attr_set_stack_size (&attr, size);
qurt_thread_attr_set_stack_addr (&attr, (unsigned long long *)th->stack);
qurt_thread_attr_set_name (&attr, "my thread");
th->running = 1;
if(0 != qurt_thread_create(&th->tid, &attr, (void*)thread_start, th)) {
goto error;
}
return 0;
error:
thread_dtor(th);
return -1;
}
//the first time this function is called the thread will be constructed
//all subsequent calls will return the same thread instance or non-zero failure
//this function should only be called from a FastRPC provided thread.
static int thread_instance(struct thread* th) {
return HAP_pls_add_lookup((uint32)thread_ctor, //type, some unique address
0, //key, for different type instances
sizeof(struct thread), //size of our struture
thread_ctor, //structure ctor
0, //aditional user context for ctor
thread_dtor, //desturctor
&th); //result
}至此,我们就介绍了使用FastRPC过程中的一些基本步骤以及快速上手代码,如果您还有更多的疑惑,可以查看官方提供的文献,作为一种可以实现远程实时开发的技术,FastRPC为我们提供了难以想象的方便,当开发安卓系统的时候,相信各位能有更好的体验。