timer/thread/wait_queue例子

下面这段code,包含相关头文件,声明定时器函数,定义定时器和等待队列。


#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/wait.h>

static void timer_func(struct timer_list *timer);
static DEFINE_TIMER(gtimer, timer_func);
static struct task_struct *gtask;
static DECLARE_WAIT_QUEUE_HEAD(wait_queue);
static int waited = 0;

 

下面这段code,分别为定时器函数和线程函数。其中定时器函数实现了,唤醒等待队列;线程函数在一直等待队列唤醒时间和等待标志,成功后修改定时器为100ms后再次唤醒。


static void timer_func(struct timer_list *timer)
{
        if (waited == 0) {
                waited = 1;
                wake_up_interruptible(&wait_queue);
                printk("wake up. jiffies:%lx\n", jiffies);
        }
}

static int mock_wait_thread(void *data)
{
        int error = 0;

        while (!kthread_should_stop()) {
                error = wait_event_interruptible(wait_queue, waited);
                printk("state:%d\n", error);

                if (!error) {
                        printk("waited, jiffies:%lx\n", jiffies);
                        waited = 0;
                        mod_timer(&gtimer, jiffies + msecs_to_jiffies(100));
                        //msleep(1000);
                }
        }

        return 0;
}

下面这段code,为模块的初始化和退出函数。在初始化函数中,添加定时器,创建线程;在退出函数中,停止线程,删除定时器。


static int __init timer_thread_init(void)
{
        add_timer(&gtimer);
        //init_waitqueue_head(&wait_queue);
        gtask = kthread_run(mock_wait_thread, NULL, "mock irq");
        if (!gtask) {
                printk("create thread failed\n");
        }
        printk("timer_thread module init\n");
        return 0;
}

static void __exit timer_thread_exit(void)
{
        kthread_stop(gtask);
        del_timer(&gtimer);
        printk("timer_thread module exit\n");
}

module_init(timer_thread_init);
module_exit(timer_thread_exit);

MODULE_LICENSE("GPL");

以上code的运行效果是,会每间隔100毫秒交叉打印"waited..."和"wake up"。

新增代码修改: 1,当设备工作在托管设备模式时,此消息类型将用于将设备测量数据从设备直接发送到客户端服务器。服务器向设备回复“请求过多”错误(http 代码:429)。此时,设备将禁用数据重传机制; #ifndef __HTTP_C__ #define __HTTP_C__ /*=========================================================================== * Header file *===========================================================================*/ #include "qapi_timer.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include "stdarg.h" //#include "test_utils.h" #include "data_mgr_intr.h" #include "https.h" #include "qapi_diag.h" #include "qapi_socket.h" #include "qapi_dnsc.h" #include "qapi_dss.h" #include "qapi_timer.h" #include "qapi_uart.h" #include "qapi_fs.h" #include "qapi_status.h" #include "qapi_netservices.h" #include "qapi_httpc.h" #include "qapi_device_info.h" #include "data_mgr.h" #include "data_sender.h" #ifdef ENABLE_GBS2104 #include "electronic_scale.h" #endif #include "data_mgr_https.h" #include "ls_utils.h" /*=========================================================================== * DEFINITION *===========================================================================*/ static qapi_TIMER_handle_t dog_timer; #define HTTPS_SUPPORT #define DSS_ADDR_INFO_SIZE 5 #define DSS_ADDR_SIZE 16 #define GET_ADDR_INFO_MIN(a, b) ((a) > (b) ? (b) : (a)) #define FIBO_HTTP_UART_DBG #define THREAD_STACK_SIZE (1024 * 10) #define THREAD_PRIORITY (101) //#define NW_SIGNAL_ENABLE //for checking network status by device_info #define HTTP_UART_DBG(...) /*=========================================================================== * Global variable *===========================================================================*/ static TX_THREAD *http_thread_handle; static unsigned char http_client_stack[THREAD_STACK_SIZE]; //TX_EVENT_FLAGS_GROUP *http_signal_handle; TX_EVENT_FLAGS_GROUP *http_release_handle; TX_QUEUE *http_queue = NULL; void *http_queue_addr = NULL; #define HTTP_MAX_DA
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值