《C语言编程实例 —— 函数参数为函数,作为回调函数》

1.

typedef int (*GET_VIDEO_FLAG_CALLBACK)(void* param);
typedef struct {
    volatile int8_t pm_lock_ref;
    pthread_mutex_t pm_lock_mutex;
    pthread_t pm_task;
    GET_VIDEO_FLAG_CALLBACK on_get_video_flag;
} power_manager_t;

static power_manager_t power_ctx;

static int on_get_video_flag(void* param) {
    tag_CAT_CONTEXT* cat_ctx = param;

    return cat_ctx->is_have_video;
}
yd_power_manager_init(on_get_video_flag);
int yd_power_manager_init(int (*on_get_video_flag)(void* param)) { int ret = 0; pthread_attr_t attr; memset(&power_ctx, 0, sizeof(power_manager_t)); power_ctx.pm_lock_ref = 0; power_ctx.on_get_video_flag = on_get_video_flag; pthread_mutex_init(&power_ctx.pm_lock_mutex, NULL); pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 0x1000); pthread_create(&power_ctx.pm_task, &attr, run_pm_task, &power_ctx); pthread_attr_destroy(&attr); return ret; } static void* run_pm_task(void* arg) { power_manager_t* power_ctx = (power_manager_t*)arg; if (power_ctx == NULL) { return NULL; } arch_os_queue_t msg_quene_id = get_msg_queque_id(); prctl(PR_SET_NAME, "pm"); pthread_detach(pthread_self()); while (1) {if (power_ctx == NULL) { break; } if (power_ctx->pm_lock_ref == 0 && get_is_battery_powered()) { printf("power down %llu ms\n", arch_os_ms_now()); send_message(msg_quene_id, CAT_MSG_POWER_DOWN_REQ); } } return NULL; } int yd_pm_power_down(void* cat_ctx, int* isOsdupdating) { int ret = 0; uint8_t is_have_video = 0;
   //该函数直接调用,判断返回值
if (power_ctx.on_get_video_flag) { xxxx; } return ret; }

 

posted @ 2022-09-29 17:16  一个不知道干嘛的小萌新  阅读(54)  评论(0编辑  收藏  举报