Skip to content

Commit b24a6a1

Browse files
committed
use msleep to block instead of PV
1 parent 14e3076 commit b24a6a1

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

assignment02/part1/hc_sr04-test.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
#include<errno.h>
66
#include<fcntl.h>
77
#include<stdlib.h>
8-
#include<sys/ioctl.h>
98
#include"hc_sr04_user.h"
10-
#include"hw_setting.h"
119
int func(char(*pin)[5][2], char*(*pin_str)[5]) {
1210
printf("%d, %s\n", pin[1][3][0], pin_str[1][3]);
1311
return 0;
1412
}
15-
typedef struct pdata {
16-
int fd; char*name;
17-
hcsr_set set;
18-
} pdata;
13+
void* readfunc(void* datain) {
14+
int i = 0;
15+
pdata* data = (pdata*) datain;
16+
read(data->fd, &i, sizeof(int)); PRINT(data->name, data->fd);
17+
printf("READ UNBLOCKED\n");
18+
return NULL;
19+
}
1920
void* pfunction(void* datain) {
2021
int i = 0;
2122
pdata* data = (pdata*) datain;
@@ -74,8 +75,9 @@ int main(int argc, char*argv[]) {
7475
ret = ioctl(fdB, SETMODE, &Bset, 0);
7576
if(0 != ret)
7677
perror("SETMODE");
77-
read(fdB, &i, sizeof(int)); PRINT("HCSR_2", fdB);
78-
sleep(1);
78+
Bdata.fd = fdB; Bdata.name = "HCSR_2";
79+
pthread_create((pthread_t*)&i, NULL, readfunc, &Bdata);
80+
//sleep(1);
7981

8082
i = 1; //start thread
8183
write(fdB, &i, sizeof(int)); PRINT("HCSR_2", fdB);

assignment02/part1/hc_sr04.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,6 @@ static irqreturn_t echo_recv_isr(int irq, void *data) {
173173
printk(KERN_ALERT "echo_recv_isr: %d CM, newest idx:%d, from %s, mode:%d \n",distance, hcsr->cirb.newest, hcsr->hc_sr04->name, hcsr->kconfig.set.working_mode.mode);
174174
spin_unlock(&(hcsr->cirb.cir_buf_lock));
175175

176-
spin_lock(&(hcsr->kconfig.kconfig_lock));
177-
if(ONE_SHOT == hcsr->kconfig.set.working_mode.mode) {
178-
spin_unlock(&(hcsr->kconfig.kconfig_lock));
179-
} else {
180-
spin_unlock(&(hcsr->kconfig.kconfig_lock));
181-
wake_up_all(&(hcsr->wq));
182-
}
183-
184-
185176
RETURNED:
186177
spin_lock(&(hcsr->ongoing_lock));
187178
hcsr->ongoing = STOPPING;
@@ -351,10 +342,18 @@ static ssize_t hc_sr04_read(struct file *file, char *buf, size_t count, loff_t *
351342
} else {
352343
spin_unlock(&(hcsr->cirb.cir_buf_lock));
353344

345+
RECHECK_BUF:
346+
spin_lock(&(hcsr->cirb.cir_buf_lock));
354347
printk("read Blocked:%s \n", hcsr->hc_sr04->name);
355-
wait_event_interruptible((hcsr->wq),BUFF_IN);
348+
if(-1 == hcsr->cirb.newest) {
349+
spin_unlock(&(hcsr->cirb.cir_buf_lock));
350+
msleep(10);
351+
goto RECHECK_BUF;
352+
}
353+
spin_unlock(&(hcsr->cirb.cir_buf_lock));
356354

357355
spin_lock(&(hcsr->cirb.cir_buf_lock));
356+
printk(KERN_ALERT "read unBlocked-idx:%d,%d\n", hcsr->cirb.newest, (hcsr->cirb.buf.data[hcsr->cirb.newest]));
358357
ret = copy_to_user(buf, &(hcsr->cirb.buf.data[hcsr->cirb.newest]), sizeof(int));
359358
spin_unlock(&(hcsr->cirb.cir_buf_lock));
360359
if(ret) return -EAGAIN;
@@ -447,7 +446,6 @@ static void dereg_misc(struct miscdevice* md) {
447446
static void init_hcsr_struct(hcsr_struct* hcsr, struct miscdevice* md, char(*pins)[5][2], char*(*pin_str)[5]) {
448447
/*!!!!!*/
449448
//CHECK!!! INIT
450-
init_waitqueue_head(&(hcsr->wq));
451449
hcsr->kthread = NULL;
452450
hcsr->hc_sr04 = md;
453451
hcsr->irq_done = IRQ_NOT_DONE;
@@ -457,7 +455,12 @@ static void init_hcsr_struct(hcsr_struct* hcsr, struct miscdevice* md, char(*pin
457455
// hcsr->kconfig.set.mode = -1;
458456
hcsr->cirb.newest = -1;
459457
hcsr->pin_str = pin_str;
458+
#ifdef __EXTENNTION__
459+
hcsr->pv.cnt = 0;
460+
sema_init(&(hcsr->pv.block_sem),0);
461+
spin_lock_init(&(hcsr->pv.cnt_lock));
460462
spin_lock_init(&(hcsr->irq_done_lock));
463+
#endif
461464
spin_lock_init(&(hcsr->ongoing_lock));
462465
spin_lock_init(&(hcsr->cirb.cir_buf_lock));
463466
spin_lock_init(&(hcsr->kconfig.kconfig_lock));

assignment02/part1/hc_sr04_kernel.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "hc_sr04_common.h"
55
#include "hw_setting.h"
66
#include <linux/spinlock.h>
7+
#include <linux/semaphore.h>
78
#include <linux/wait.h>
89
#define BUFF_IN (0x00000001)
910
#define COUNT_MAX (3)
@@ -15,6 +16,13 @@
1516
#define IRQ_DONE (1)
1617
#define IRQ_NOT_DONE (0)
1718

19+
#ifdef __EXTENNTION__
20+
typedef struct PV{
21+
long cnt;
22+
spinlock_t cnt_lock;
23+
struct semaphore block_sem;
24+
} PV;
25+
#endif
1826

1927
typedef struct cir_buf {
2028
int newest;
@@ -31,8 +39,10 @@ typedef struct hcsr_struct {
3139
struct miscdevice* hc_sr04;
3240
int irq_done;
3341
int echo_isr_number;
34-
wait_queue_head_t wq;
3542
ktime_t kstart;
43+
#ifdef __EXTENNTION__
44+
PV pv;
45+
#endif
3646
struct task_struct* kthread;
3747
spinlock_t kthread_lock;
3848
spinlock_t irq_done_lock;
@@ -54,6 +64,29 @@ static int __init hc_sr04_init(void);
5464
static void __exit hc_sr04_exit(void);
5565

5666
static irqreturn_t echo_recv_isr(int irq, void *data);
67+
#ifdef __EXTENNTION__
68+
int P(PV* pv) {
69+
70+
spin_lock(&(pv->cnt_lock));
71+
cnt--;
72+
spin_unlock(&(pv->cnt_lock));
73+
74+
if(0 > cnt) {
75+
down(&(pv->block_sem));
76+
}
77+
}
78+
79+
int V(PV* pv) {
80+
spin_lock(&(pv->cnt_lock));
81+
while(0 > cnt) {
82+
cnt++;
83+
up(&(pv->block_sem));
84+
}
85+
cnt++;
86+
spin_unlock(&(pv->cnt_lock));
87+
}
88+
#endif
89+
5790
int set_ISR(struct hcsr_struct* hcsr) {
5891
//For Echo ISR
5992
int ret = 0;
@@ -168,7 +201,7 @@ int setEcho (struct hcsr_struct* hcsr, int pin) {
168201
}
169202

170203
if(HC_GPIO_MUX0 == m || HC_GPIO_MUX1 == m) {
171-
gpio_set_value(all_pins[i][m][PIN_INDEX], all_pins[i][m][VAL_INDEX]);
204+
gpio_direction_output(all_pins[i][m][PIN_INDEX], all_pins[i][m][VAL_INDEX]);
172205
}
173206
else if(HC_GPIO_PULL == m) {
174207
gpio_direction_output(all_pins[i][m][PIN_INDEX], PULL_DOWN);

assignment02/part1/hc_sr04_user.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#ifndef _HC_SR04_USR_H_
22
#define _HC_SR04_USR_H_
33
#include"hc_sr04_common.h"
4+
#include"hw_setting.h"
45
#include <unistd.h>
6+
#include<sys/ioctl.h>
57
#define __HC_DEBUG_USER__
68
#ifdef __HC_DEBUG_USER__
79
#define PRINT(name, fd) do {int TMP; read((fd), &TMP,sizeof(int)); printf("%s's distance: %d-\n", name, TMP);}while(0);
810
#else
911
#define PRINT(name, fd)
1012
#endif
13+
typedef struct pdata {
14+
int fd; char*name;
15+
int get_i;
16+
hcsr_set set;
17+
} pdata;
1118
#endif

0 commit comments

Comments
 (0)