Add iomux_wake example
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <ucore/iomux.h>
|
||||
#include <ucore/iomux_waker.h>
|
||||
|
||||
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int counter = 0;
|
||||
|
||||
void wake_cb(struct IOMuxWaker *wk)
|
||||
{
|
||||
int local_counter;
|
||||
int rc;
|
||||
|
||||
rc = pthread_mutex_lock(&lock);
|
||||
assert(rc == 0);
|
||||
local_counter = counter;
|
||||
printf("counter is %d\n", local_counter);
|
||||
|
||||
rc = pthread_mutex_unlock(&lock);
|
||||
assert(rc == 0);
|
||||
|
||||
if (local_counter == 13) {
|
||||
rc = uc_iomux_waker_destroy(wk);
|
||||
assert(rc == 0);
|
||||
}
|
||||
}
|
||||
|
||||
void *waker(void *arg)
|
||||
{
|
||||
struct IOMuxSignaller *s = arg;
|
||||
int i;
|
||||
int rc;
|
||||
|
||||
rc = pthread_mutex_lock(&lock);
|
||||
assert(rc == 0);
|
||||
for (i = 0; i < 3; i++) {
|
||||
|
||||
counter++;
|
||||
//with the signal in here,
|
||||
//the wake_cb might be woken up
|
||||
//more than once after the loop is done
|
||||
//reading the same counter value more than once.
|
||||
rc = uc_iomux_waker_signal(s);
|
||||
assert(rc == 0);
|
||||
}
|
||||
rc = pthread_mutex_unlock(&lock);
|
||||
assert(rc == 0);
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
|
||||
rc = pthread_mutex_lock(&lock);
|
||||
assert(rc == 0);
|
||||
counter++;
|
||||
rc = pthread_mutex_unlock(&lock);
|
||||
assert(rc == 0);
|
||||
rc = uc_iomux_waker_signal(s);
|
||||
assert(rc == 0);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct IOMux *mux = iomux_create(IOMUX_TYPE_SELECT);
|
||||
struct IOMuxWaker wk;
|
||||
pthread_t tid;
|
||||
|
||||
uc_iomux_waker_init(mux, &wk, wake_cb);
|
||||
|
||||
pthread_create(&tid,NULL,waker, &wk.signaller);
|
||||
iomux_run(mux);
|
||||
|
||||
printf("reader mux returned\n");
|
||||
pthread_join(tid, NULL);
|
||||
iomux_delete(mux);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user