1 /* 2 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. 3 * 4 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). 5 * 6 * Use of this source code is governed by MIT license that can be found in the 7 * LICENSE file in the root of the source tree. All contributing project authors 8 * may be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MK_THREAD_H 12 #define MK_THREAD_H 13 14 #include <assert.h> 15 #include "mk_common.h" 16 #include "mk_tcp.h" 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 ///////////////////////////////////////////事件线程///////////////////////////////////////////// 23 typedef void* mk_thread; 24 25 /** 26 * 获取tcp会话对象所在事件线程 27 * @param ctx tcp会话对象 28 * @return 对象所在事件线程 29 */ 30 API_EXPORT mk_thread API_CALL mk_thread_from_tcp_session(mk_tcp_session ctx); 31 32 /** 33 * 获取tcp客户端对象所在事件线程 34 * @param ctx tcp客户端 35 * @return 对象所在事件线程 36 */ 37 API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx); 38 39 /** 40 * 根据负载均衡算法,从事件线程池中随机获取一个事件线程 41 * 如果在事件线程内执行此函数将返回本事件线程 42 * 事件线程指的是定时器、网络io事件线程 43 * @return 事件线程 44 */ 45 API_EXPORT mk_thread API_CALL mk_thread_from_pool(); 46 47 /** 48 * 根据负载均衡算法,从后台线程池中随机获取一个线程 49 * 后台线程本质与事件线程相同,只是优先级更低,同时可以执行短时间的阻塞任务 50 * ZLMediaKit中后台线程用于dns解析、mp4点播时的文件解复用 51 * @return 后台线程 52 */ 53 API_EXPORT mk_thread API_CALL mk_thread_from_pool_work(); 54 55 typedef void* mk_thread_pool; 56 57 /** 58 * 创建线程池 59 * @param name 线程池名称,方便调试 60 * @param n_thread 线程个数,0时为cpu个数 61 * @param priority 线程优先级,分为PRIORITY_LOWEST = 0,PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_HIGHEST 62 * @return 线程池 63 */ 64 API_EXPORT mk_thread_pool API_CALL mk_thread_pool_create(const char *name, size_t n_thread, int priority); 65 66 /** 67 * 销毁线程池 68 * @param pool 线程池 69 * @return 0:成功 70 */ 71 API_EXPORT int API_CALL mk_thread_pool_release(mk_thread_pool pool); 72 73 /** 74 * 从线程池获取一个线程 75 * @param pool 线程池 76 * @return 线程 77 */ 78 API_EXPORT mk_thread API_CALL mk_thread_from_thread_pool(mk_thread_pool pool); 79 80 ///////////////////////////////////////////线程切换///////////////////////////////////////////// 81 typedef void (API_CALL *on_mk_async)(void *user_data); 82 83 /** 84 * 切换到事件线程并异步执行 85 * @param ctx 事件线程 86 * @param cb 回调函数 87 * @param user_data 用户数据指针 88 */ 89 API_EXPORT void API_CALL mk_async_do(mk_thread ctx, on_mk_async cb, void *user_data); 90 91 /** 92 * 切换到事件线程并延时执行 93 * @param ctx 事件线程 94 * @param ms 延时时间,单位毫秒 95 * @param cb 回调函数 96 * @param user_data 用户数据指针 97 */ 98 API_EXPORT void API_CALL mk_async_do_delay(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data); 99 100 /** 101 * 切换到事件线程并同步执行 102 * @param ctx 事件线程 103 * @param cb 回调函数 104 * @param user_data 用户数据指针 105 */ 106 API_EXPORT void API_CALL mk_sync_do(mk_thread ctx, on_mk_async cb, void *user_data); 107 108 ///////////////////////////////////////////定时器///////////////////////////////////////////// 109 typedef void* mk_timer; 110 111 /** 112 * 定时器触发事件 113 * @return 下一次触发延时(单位毫秒),返回0则不再重复 114 */ 115 typedef uint64_t (API_CALL *on_mk_timer)(void *user_data); 116 117 /** 118 * 创建定时器 119 * @param ctx 线程对象 120 * @param delay_ms 执行延时,单位毫秒 121 * @param cb 回调函数 122 * @param user_data 用户数据指针 123 * @return 定时器对象 124 */ 125 API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data); 126 127 /** 128 * 销毁和取消定时器 129 * @param ctx 定时器对象 130 */ 131 API_EXPORT void API_CALL mk_timer_release(mk_timer ctx); 132 133 ///////////////////////////////////////////信号量///////////////////////////////////////////// 134 135 typedef void* mk_sem; 136 137 /** 138 * 创建信号量 139 */ 140 API_EXPORT mk_sem API_CALL mk_sem_create(); 141 142 /** 143 * 销毁信号量 144 */ 145 API_EXPORT void API_CALL mk_sem_release(mk_sem sem); 146 147 /** 148 * 信号量加n 149 */ 150 API_EXPORT void API_CALL mk_sem_post(mk_sem sem, size_t n); 151 152 /** 153 * 信号量减1 154 * @param sem 155 */ 156 API_EXPORT void API_CALL mk_sem_wait(mk_sem sem); 157 158 #ifdef __cplusplus 159 } 160 #endif 161 #endif //MK_THREAD_H 162