Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
embedded-linux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Maul
embedded-linux
Commits
fb2f04ad
Commit
fb2f04ad
authored
4 months ago
by
Leon Bohnwagner
Browse files
Options
Downloads
Patches
Plain Diff
feat: add timed kernel thread
parent
15b1b4d0
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
kernel/kmod.c
+39
-0
39 additions, 0 deletions
kernel/kmod.c
with
39 additions
and
0 deletions
kernel/kmod.c
+
39
−
0
View file @
fb2f04ad
...
@@ -5,11 +5,13 @@
...
@@ -5,11 +5,13 @@
#include
<linux/vmalloc.h>
#include
<linux/vmalloc.h>
#include
<linux/uaccess.h>
#include
<linux/uaccess.h>
#include
<linux/hrtimer.h>
#include
<linux/hrtimer.h>
#include
<linux/kthread.h>
#include
"../common/include/message.h"
#include
"../common/include/message.h"
#define DEVICE_NAME "amogus"
#define DEVICE_NAME "amogus"
#define CLASS_NAME "amogus_class"
#define CLASS_NAME "amogus_class"
#define SLOTS 4
#define SLOT_INTERVAL_NS 250000
#define SLOT_INTERVAL_NS 250000
static
int
major_number
;
static
int
major_number
;
...
@@ -21,6 +23,13 @@ static message_t* message;
...
@@ -21,6 +23,13 @@ static message_t* message;
static
struct
hrtimer
timer
;
static
struct
hrtimer
timer
;
static
ktime_t
interval
;
static
ktime_t
interval
;
static
struct
task_struct
*
timed_thread
;
static
DECLARE_WAIT_QUEUE_HEAD
(
wq
);
static
atomic_t
wake_counter
=
ATOMIC_INIT
(
0
);
static
int
slot
=
0
;
static
int
device_open
(
struct
inode
*
inode
,
struct
file
*
file
)
static
int
device_open
(
struct
inode
*
inode
,
struct
file
*
file
)
{
{
return
0
;
return
0
;
...
@@ -55,10 +64,26 @@ static const struct file_operations fops = {
...
@@ -55,10 +64,26 @@ static const struct file_operations fops = {
};
};
static
enum
hrtimer_restart
timer_callback
(
struct
hrtimer
*
timer
)
{
static
enum
hrtimer_restart
timer_callback
(
struct
hrtimer
*
timer
)
{
atomic_inc
(
&
wake_counter
);
wake_up_interruptible
(
&
wq
);
hrtimer_forward_now
(
timer
,
interval
);
hrtimer_forward_now
(
timer
,
interval
);
return
HRTIMER_RESTART
;
return
HRTIMER_RESTART
;
}
}
static
int
timed_thread_fn
(
void
*
args
)
{
while
(
!
kthread_should_stop
())
{
wait_event_interruptible
(
wq
,
atomic_read
(
&
wake_counter
)
>
0
);
atomic_dec
(
&
wake_counter
);
if
(
slot
==
0
)
{}
slot
=
(
slot
+
1
)
%
SLOTS
;
}
return
0
;
}
static
int
__init
lkm_init
(
void
)
static
int
__init
lkm_init
(
void
)
{
{
dev_t
dev
;
dev_t
dev
;
...
@@ -110,6 +135,20 @@ static int __init lkm_init(void)
...
@@ -110,6 +135,20 @@ static int __init lkm_init(void)
timer
.
function
=
timer_callback
;
timer
.
function
=
timer_callback
;
hrtimer_start
(
&
timer
,
interval
,
HRTIMER_MODE_REL
);
hrtimer_start
(
&
timer
,
interval
,
HRTIMER_MODE_REL
);
timed_thread
=
kthread_run
(
timed_thread_fn
,
NULL
,
"timed_thread"
);
if
(
IS_ERR
(
timed_thread
))
{
vfree
(
message
);
cdev_del
(
&
mycdev
);
device_destroy
(
class
,
MKDEV
(
major_number
,
0
));
class_destroy
(
class
);
unregister_chrdev_region
(
MKDEV
(
major_number
,
0
),
1
);
hrtimer_cancel
(
&
timer
);
printk
(
KERN_ALERT
"Failed to create timed thread
\n
"
);
return
-
1
;
}
return
0
;
return
0
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment