Skip to content

Commit 5db629e

Browse files
committed
Add TX thread pool prototype
1 parent 7f550b6 commit 5db629e

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

virtio-snd.c

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
#define PRIV(x) ((virtio_snd_config_t *) x->priv)
2222

23-
static int cnt = 0;
24-
2523
enum {
2624
VSND_QUEUE_CTRL = 0,
2725
VSND_QUEUE_EVT = 1,
@@ -277,6 +275,8 @@ typedef struct {
277275
static vsnd_stream_sel_t v;
278276

279277
static pthread_mutex_t virtio_snd_mutex = PTHREAD_MUTEX_INITIALIZER;
278+
static pthread_cond_t virtio_snd_foo_cond = PTHREAD_COND_INITIALIZER;
279+
static int ev_notify;
280280

281281
/* Forward declaration */
282282
static void virtio_snd_cb(struct CNFADriver *dev,
@@ -801,6 +801,14 @@ static int virtio_snd_tx_desc_handler(virtio_snd_state_t *vsnd,
801801
return 0;
802802
}
803803

804+
/* XXX: merely testing the effectiveness of TX thread pool */
805+
static int virtio_snd_foo_desc_handler(virtio_snd_state_t *vsnd,
806+
const virtio_snd_queue_t *queue,
807+
uint32_t desc_idx,
808+
uint32_t *plen) {
809+
return 0;
810+
}
811+
804812
static void virtio_queue_notify_handler(
805813
virtio_snd_state_t *vsnd,
806814
int index,
@@ -873,6 +881,25 @@ static void virtio_queue_notify_handler(
873881
}
874882
}
875883

884+
/* XXX: used for TX thread testing */
885+
static void *func(void *args) {
886+
virtio_snd_state_t *vsnd = (virtio_snd_state_t *)args;
887+
fprintf(stderr, "*** foo desc handler is called ***\n");
888+
pthread_mutex_lock(&virtio_snd_mutex);
889+
while(ev_notify <= 0) {
890+
pthread_cond_wait(&virtio_snd_foo_cond, &virtio_snd_mutex);
891+
fprintf(stderr, "wait for cond\n");
892+
sleep(1);
893+
}
894+
fprintf(stderr, "*** start tx critical section ***\n");
895+
ev_notify--;
896+
fprintf(stderr, "foo desc handler\n");
897+
virtio_queue_notify_handler(vsnd, 2, virtio_snd_foo_desc_handler);
898+
fprintf(stderr, "*** end tx cirtical section ***\n");
899+
pthread_mutex_unlock(&virtio_snd_mutex);
900+
return NULL;
901+
}
902+
876903
static bool virtio_snd_reg_read(virtio_snd_state_t *vsnd,
877904
uint32_t addr,
878905
uint32_t *value)
@@ -985,8 +1012,9 @@ static bool virtio_snd_reg_write(virtio_snd_state_t *vsnd,
9851012
break;
9861013
case VSND_QUEUE_TX:
9871014
fprintf(stderr, "TX start\n");
988-
virtio_queue_notify_handler(vsnd, value,
989-
virtio_snd_tx_desc_handler);
1015+
ev_notify++;
1016+
/*virtio_queue_notify_handler(vsnd, value,
1017+
virtio_snd_foo_desc_handler);*/
9901018
sleep(1);
9911019
fprintf(stderr, "TX end\n");
9921020
break;
@@ -1024,8 +1052,6 @@ void virtio_snd_read(hart_t *vm,
10241052
uint8_t width,
10251053
uint32_t *value)
10261054
{
1027-
fprintf(stderr, "=== start read lock ===\n");
1028-
pthread_mutex_lock(&virtio_snd_mutex);
10291055
switch (width) {
10301056
case RV_MEM_LW:
10311057
if (!virtio_snd_reg_read(vsnd, addr >> 2, value))
@@ -1042,18 +1068,13 @@ void virtio_snd_read(hart_t *vm,
10421068
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
10431069
break;
10441070
}
1045-
cnt++;
1046-
fprintf(stderr, "=== end read lock count %d ===\n", cnt);
1047-
pthread_mutex_unlock(&virtio_snd_mutex);
10481071
}
10491072
void virtio_snd_write(hart_t *vm,
10501073
virtio_snd_state_t *vsnd,
10511074
uint32_t addr,
10521075
uint8_t width,
10531076
uint32_t value)
10541077
{
1055-
fprintf(stderr, "=== start write lock ===\n");
1056-
pthread_mutex_lock(&virtio_snd_mutex);
10571078
switch (width) {
10581079
case RV_MEM_SW:
10591080
if (!virtio_snd_reg_write(vsnd, addr >> 2, value))
@@ -1067,9 +1088,6 @@ void virtio_snd_write(hart_t *vm,
10671088
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
10681089
break;
10691090
}
1070-
cnt++;
1071-
fprintf(stderr, "=== end write lock count %d ===\n", cnt);
1072-
pthread_mutex_unlock(&virtio_snd_mutex);
10731091
}
10741092

10751093
bool virtio_snd_init(virtio_snd_state_t *vsnd)
@@ -1090,5 +1108,12 @@ bool virtio_snd_init(virtio_snd_state_t *vsnd)
10901108
PRIV(vsnd)->controls =
10911109
0; /* virtio-snd device does not support control elements */
10921110

1111+
ev_notify = 0;
1112+
pthread_t tid;
1113+
if(pthread_create(&tid, NULL, func, vsnd) != 0) {
1114+
fprintf(stderr, "cannot create TX thread\n");
1115+
return false;
1116+
}
1117+
10931118
return true;
10941119
}

0 commit comments

Comments
 (0)