Skip to content

Commit 4f4247d

Browse files
author
ming.li
committed
update
1 parent 1be6aa3 commit 4f4247d

3 files changed

Lines changed: 455 additions & 9 deletions

File tree

workspace/misc/test/test.c

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ MODULE_AUTHOR("ming.li");
77
//MODULE_VERSION(version_string);
88
//MODULE_DEVICE_TABLE(table_info);
99
//MODULE_ALIAS(alternate_name);
10-
MODULE_LICENSE("Dual BSD/GPL");
11-
1210
//#include <linux/moduleparam.h>
1311
//module_param(variable, type, perm);
1412
//module_param_array(name, type, nump, perm)
@@ -123,18 +121,21 @@ static struct file_operations proc_fops = {
123121

124122
};
125123

126-
127124
static void proc_init(void)
128125
{
129126
sema_init(&sem,0);
130-
127+
128+
#if 0
131129
test = create_proc_entry(proc_entry_name, proc_mod, NULL);
132130

133131
if (test)
134132
{
135133
test->proc_fops = &proc_fops;
136134
test->data = NULL;
137135
}
136+
#else
137+
test = proc_create(proc_entry_name, proc_mod, NULL, &proc_fops);
138+
#endif
138139

139140
proc_buf = kmalloc(PROC_BUF_SIZE,GFP_KERNEL);
140141

@@ -172,10 +173,13 @@ static struct task_struct *test_task_1,*test_task_2;
172173
static struct semaphore sem_thread;
173174

174175
#define THREAD_WORK_INIT_VALUE 10
175-
enum{
176-
thread_id_0 = 0,
177-
thread_id_1
178-
};
176+
typedef enum{
177+
thread_id_0 = 0,
178+
thread_id_1
179+
} thread_id_t;
180+
181+
struct mutex lock_a,lock_b;
182+
179183
static void thread_cmd(void)
180184
{
181185
printk (KERN_DEBUG "[%s] [%s] ...",MODULE_TAG,__FUNCTION__);
@@ -184,9 +188,67 @@ static void thread_cmd(void)
184188

185189
static void do_work(void *data)
186190
{
191+
thread_id_t thread_id = (long)data;
192+
187193
printk (KERN_DEBUG "[%s] [%s] [%ld]",MODULE_TAG,__FUNCTION__,(long)data);
194+
switch (thread_id)
195+
{
196+
case thread_id_0:
197+
198+
printk (KERN_DEBUG "[%ld] getting lock a",(long)data);
199+
200+
mutex_lock(&lock_a);
201+
202+
printk (KERN_DEBUG "[%ld] got lock a",(long)data);
203+
204+
printk (KERN_DEBUG "[%ld] getting lock b",(long)data);
205+
206+
mutex_lock(&lock_b);
207+
208+
printk (KERN_DEBUG "[%ld] got lock b",(long)data);
209+
210+
msleep(100);
211+
212+
printk (KERN_DEBUG "[%ld] unlock b",(long)data);
213+
214+
mutex_unlock(&lock_b);
215+
216+
printk (KERN_DEBUG "[%ld] unlock a",(long)data);
217+
218+
mutex_unlock(&lock_a);
188219

189-
msleep(100);
220+
break;
221+
case thread_id_1:
222+
223+
printk (KERN_DEBUG "[%ld] getting lock b",(long)data);
224+
225+
mutex_lock(&lock_b);
226+
227+
printk (KERN_DEBUG "[%ld] got lock b",(long)data);
228+
229+
printk (KERN_DEBUG "[%ld] getting lock a",(long)data);
230+
231+
mutex_lock(&lock_a);
232+
233+
printk (KERN_DEBUG "[%ld] got lock a",(long)data);
234+
235+
msleep(100);
236+
237+
238+
printk (KERN_DEBUG "[%ld] unlock a",(long)data);
239+
240+
mutex_unlock(&lock_a);
241+
242+
printk (KERN_DEBUG "[%ld] unlock b",(long)data);
243+
244+
mutex_unlock(&lock_b);
245+
246+
break;
247+
default:
248+
printk(KERN_ERR "err thread id");
249+
break;
250+
251+
}
190252
}
191253

192254
static int test_thread(void *data)
@@ -220,6 +282,10 @@ static int thread_init(void)
220282
printk (KERN_DEBUG "[%s] [%s] ...",MODULE_TAG,__FUNCTION__);
221283

222284
sema_init(&sem_thread, THREAD_WORK_INIT_VALUE);
285+
286+
mutex_init(&lock_a);
287+
mutex_init(&lock_b);
288+
223289
test_task_1 = kthread_create(test_thread, (void *)thread_id_0, "test_task_2");
224290

225291
if(IS_ERR(test_task_1))
@@ -244,6 +310,7 @@ static int thread_init(void)
244310

245311
wake_up_process(test_task_2);
246312

313+
247314
return 0;
248315
}
249316
static void thread_exit(void)
@@ -274,6 +341,7 @@ static __exit void test_exit(void)
274341
//EXPORT_SYMBOL (symbol);
275342
//EXPORT_SYMBOL_GPL (symbol);
276343
//
344+
MODULE_LICENSE("Dual BSD/GPL");
277345

278346
module_init(test_init);
279347
module_exit(test_exit);

workspace/misc/test2/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# If KERNELRELEASE is defined, we've been invoked from the
2+
# kernel build system and can use its language.
3+
4+
#module name
5+
#OBJ = TEMPLATE
6+
7+
8+
PWD?=$(shell PWD)
9+
10+
#auto get the module name by dir
11+
OBJ?=$(notdir $(PWD))
12+
#$(info "OJB = "$(OBJ))
13+
C_SOURCES = $(shell find $(PWD) -name "*.c")
14+
C_FILES = $(notdir $(C_SOURCES))
15+
C_OBJECTS = $(patsubst %.c, %.o, $(C_FILES))
16+
#S_SOURCES = $(shell find . -name "*.s")
17+
#S_OBJECTS = $(patsubst %.s, %.o, $(S_SOURCES))
18+
19+
# module execute objs
20+
#$(OBJ)-objs = $(OBJ)
21+
22+
#multi files
23+
obj-m := $(OBJ).o
24+
module-objs := $(C_OBJECTS)
25+
26+
27+
ifneq ($(KERNELRELEASE),)
28+
obj-m := $(C_OBJECTS)
29+
# Otherwise we were called directly from the command
30+
# line; invoke the kernel build system.
31+
else
32+
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
33+
default:
34+
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
35+
install:
36+
sudo insmod $(OBJ).ko
37+
uninstall:
38+
sudo rmmod $(OBJ)
39+
clean:
40+
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
41+
check:
42+
@sudo lsmod |grep $(OBJ) ||echo no module $(OBJ)
43+
endif
44+

0 commit comments

Comments
 (0)