diff options
Diffstat (limited to 'common/rt')
| -rw-r--r-- | common/rt/include/rt_common.h | 23 | ||||
| -rw-r--r-- | common/rt/include/rt_file.h | 7 | ||||
| -rw-r--r-- | common/rt/include/rt_stdlib.h | 15 | ||||
| -rw-r--r-- | common/rt/include/rt_string.h | 74 | ||||
| -rw-r--r-- | common/rt/include/rt_sync.h | 20 | ||||
| -rw-r--r-- | common/rt/include/rt_time.h | 12 | ||||
| -rw-r--r-- | common/rt/include/rt_tmr.h | 25 | ||||
| -rw-r--r-- | common/rt/src/rt_file.cpp | 32 | ||||
| -rw-r--r-- | common/rt/src/rt_stdlib.cpp | 29 | ||||
| -rw-r--r-- | common/rt/src/rt_string.cpp | 35 | ||||
| -rw-r--r-- | common/rt/src/rt_time.cpp | 58 | ||||
| -rw-r--r-- | common/rt/src/rt_tmr.cpp | 222 |
12 files changed, 552 insertions, 0 deletions
diff --git a/common/rt/include/rt_common.h b/common/rt/include/rt_common.h new file mode 100644 index 0000000..8df8766 --- /dev/null +++ b/common/rt/include/rt_common.h @@ -0,0 +1,23 @@ +#ifndef __RT_COMMON_H__ +#define __RT_COMMON_H__ + +#include <assert.h> +#define EVAL_TM_STYLE "%Y-%m-%d" + +/** Alway treated the expr as true */ +#ifndef likely +#define likely(expr) __builtin_expect(!!(expr), 1) +#endif + +/** Alway treated the expr as false */ +#ifndef unlikely +#define unlikely(expr) __builtin_expect(!!(expr), 0) +#endif + +#define __rt_always_inline__ __attribute__((always_inline)) inline + +#define CHECK_OR_EXIT(condition, fmt, ...) \ +do { if(!(condition)) { mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, fmt, ##__VA_ARGS__); exit(EXIT_FAILURE); } } while(0) \ + + +#endif diff --git a/common/rt/include/rt_file.h b/common/rt/include/rt_file.h new file mode 100644 index 0000000..66563ac --- /dev/null +++ b/common/rt/include/rt_file.h @@ -0,0 +1,7 @@ +#ifndef __RT_FILE_H__ +#define __RT_FILE_H__ + +extern int rt_file_exsit(const char *); +extern int rt_dir_exsit (const char *realpath); + +#endif diff --git a/common/rt/include/rt_stdlib.h b/common/rt/include/rt_stdlib.h new file mode 100644 index 0000000..a8cc1ec --- /dev/null +++ b/common/rt/include/rt_stdlib.h @@ -0,0 +1,15 @@ +#ifndef __RT_STDLIB_H__ +#define __RT_STDLIB_H__ + +#include <stdlib.h> + +/** memory flags */ +#define MPF_CLR (1 << 0) /** Clear it after allocated */ + +extern void kfree(void *p); + +void *kmalloc(int s, int flags, int __attribute__((__unused__)) node); + + +#endif + diff --git a/common/rt/include/rt_string.h b/common/rt/include/rt_string.h new file mode 100644 index 0000000..c183db5 --- /dev/null +++ b/common/rt/include/rt_string.h @@ -0,0 +1,74 @@ +#ifndef __UTIL_STRING_H__ +#define __UTIL_STRING_H__ + +#include <string.h> +#include <stdio.h> +#include <stdint.h> + +#ifndef STRLEN +#define STRLEN(STR) ((int)strlen((const char *)STR)) +#endif + +#ifndef ISALNUM +#define ISALNUM(S) (isalnum((const char)S)) +#endif + +#ifndef STRCMP +#define STRCMP(S,D) (strcmp((const char *)S, (const char *)D)) +#endif + +#ifndef STRSTR +#define STRSTR(D,S) (strstr((const char *)D,(const char *)S)) +#endif + +#ifndef STRNCMP +#define STRNCMP(S,D,L) (strncmp((const char *)S, (const char *)D, L)) +#endif + +#ifndef STRCAT +#define STRCAT(D,S) (strcat((char *)D, (const char *)S)) +#endif + +#ifndef FOREVER +#define FOREVER for(;;) +#endif + +#ifndef MIN +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif + +typedef struct atomic { + volatile int counter; +} atomic_t; + +struct value_string { + unsigned int value; + const char *strptr; +}; + +extern const char* +val_to_str(const unsigned int val, const struct value_string *vs); + +typedef struct{ +#define ATOMIC64_SIZE sizeof(long long) + int64_t counter; +}atomic64_t; + +#define ATOMIC_INIT(i) { (i) } + +#define atomic_read(v) (*(volatile int *)&(v)->counter) +#define atomic64_read(v) (*(volatile int64_t *)&(v)->counter) + +#define atomic_add(x, y) (__sync_add_and_fetch((&(((atomic_t *)x)->counter)), (y))) +#define atomic64_add(x, y) (__sync_add_and_fetch((&(((atomic64_t *)x)->counter)), (y))) +#define atomic64_sub(x, y) (__sync_sub_and_fetch((&(((atomic64_t *)x)->counter)), (y))) + +#define atomic64_inc(x) (atomic64_add((x), 1)) +#define atomic64_dec(x) (atomic64_sub((x), 1)) + +static inline void atomic64_set(atomic64_t *v, int64_t val) +{ + v->counter = val; +} + +#endif diff --git a/common/rt/include/rt_sync.h b/common/rt/include/rt_sync.h new file mode 100644 index 0000000..b8fc272 --- /dev/null +++ b/common/rt/include/rt_sync.h @@ -0,0 +1,20 @@ +#ifndef __SYSTEM_SYNC_H__ +#define __SYSTEM_SYNC_H__ + +#include <pthread.h> + +#define rt_pthread_attr pthread_attr_t +#define rt_pthread pthread_t +#define rt_mutex pthread_mutex_t +#define rt_mutex_attr pthread_mutexattr_t +#define rt_mutex_init(mut, mutattr ) pthread_mutex_init(mut, mutattr) +#define rt_mutex_lock(mut) pthread_mutex_lock(mut) +#define rt_mutex_trylock(mut) pthread_mutex_trylock(mut) +#define rt_mutex_unlock(mut) pthread_mutex_unlock(mut) +#define rt_mutex_destroy(mut) pthread_mutex_destroy(mut) + +#define INIT_MUTEX(name)\ + rt_mutex name = PTHREAD_MUTEX_INITIALIZER; + +#endif + diff --git a/common/rt/include/rt_time.h b/common/rt/include/rt_time.h new file mode 100644 index 0000000..ea3ec1c --- /dev/null +++ b/common/rt/include/rt_time.h @@ -0,0 +1,12 @@ +#ifndef __RT_TM_H__ +#define __RT_TM_H__ + +struct tm *rt_localtime(time_t timep, struct tm *result); +int rt_tms2str(uint64_t ts, const char *tm_form, char *date, size_t len); +int rt_curr_tms2str(const char *tm_form, char *date, size_t len); + +u_int64_t rt_time_s(void); +u_int64_t rt_time_ms(void); +u_int64_t rt_time_ns(); + +#endif diff --git a/common/rt/include/rt_tmr.h b/common/rt/include/rt_tmr.h new file mode 100644 index 0000000..3368f79 --- /dev/null +++ b/common/rt/include/rt_tmr.h @@ -0,0 +1,25 @@ +/************************************************************************* + > File Name: rt_tmr.h + > Author: + > Mail: + > Created Time: 2018年09月19日 星期三 15时58分04秒 + ************************************************************************/ + +#ifndef _RT_TMR_H +#define _RT_TMR_H + +#define RT_TMR_ADVANCED + +extern void tmr_start(uint32_t uid); +extern void tmr_stop(uint32_t uid); + +/** +* routine: must be a reentrant function. +* An unknown error ocurrs if a thread-safe function called as a routione. +*/ +extern uint32_t tmr_create(int module, + const char *desc, + void (*routine)(uint32_t, int, char **), int argc, char **argv, int sec); + +#endif + diff --git a/common/rt/src/rt_file.cpp b/common/rt/src/rt_file.cpp new file mode 100644 index 0000000..1b3746e --- /dev/null +++ b/common/rt/src/rt_file.cpp @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <unistd.h> +#include <sys/types.h> +#include <dirent.h> + +#include "rt_common.h" + +int rt_file_exsit(const char *realpath_file) +{ + return (!access(realpath_file, F_OK)); +} + +int rt_dir_exsit (const char *realpath) +{ + DIR *dir = NULL; + int exsit = 0; + + if (unlikely (!realpath)) + goto finish; + + dir = opendir(realpath); + if (!dir) goto finish; + + exsit = 1; + closedir(dir); +finish: + return exsit; +} + diff --git a/common/rt/src/rt_stdlib.cpp b/common/rt/src/rt_stdlib.cpp new file mode 100644 index 0000000..a4d6b58 --- /dev/null +++ b/common/rt/src/rt_stdlib.cpp @@ -0,0 +1,29 @@ + +#include <stdlib.h> +#include <string.h> +#include "rt_common.h" +#include "rt_stdlib.h" + +void *kmalloc(int s, + int flags, + int __attribute__((__unused__)) node) +{ + void *p; + + if(likely((p = malloc(s)) != NULL)){ + if(flags & MPF_CLR) + memset(p, 0, s); + } + + return p; +} + +void kfree(void *p) +{ + if(likely(p != NULL)){ + free(p); + } + + p = NULL; +} + diff --git a/common/rt/src/rt_string.cpp b/common/rt/src/rt_string.cpp new file mode 100644 index 0000000..bb2ef4c --- /dev/null +++ b/common/rt/src/rt_string.cpp @@ -0,0 +1,35 @@ +#include <stdint.h> +#include <string.h> +#include "rt_string.h" + +const char * +try_val_to_str_idx(const unsigned int val, const struct value_string *vs, int *idx) +{ + int i = 0; + if (idx == NULL){ + goto finish; + } + + if(vs) { + while (vs[i].strptr) { + if (vs[i].value == val) { + *idx = i; + return(vs[i].strptr); + } + i++; + } + } + +finish: + *idx = -1; + return NULL; +} + +const char* +val_to_str(const unsigned int val, const struct value_string *vs) +{ + int ignore_me; + + return try_val_to_str_idx(val, vs, &ignore_me); +} + diff --git a/common/rt/src/rt_time.cpp b/common/rt/src/rt_time.cpp new file mode 100644 index 0000000..feef593 --- /dev/null +++ b/common/rt/src/rt_time.cpp @@ -0,0 +1,58 @@ +/* +* rt_time.c +* Created by fengweihao +* 30 May, 2018 +* Func: Time Component +*/ +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <time.h> +#include <assert.h> +#include <errno.h> + +#include "rt_time.h" + +struct tm *rt_localtime(time_t timep, struct tm *result) +{ + return localtime_r(&timep, result); +} + +uint64_t rt_time_s(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec + tv.tv_usec/1000/1000); /** CST timestamp */ +}; + +uint64_t rt_time_ms(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 + tv.tv_usec/1000); +} + +uint64_t rt_time_ns() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 * 1000 + tv.tv_usec); +} + +int rt_tms2str(uint64_t ts, const char *tm_form, char *date, size_t len) +{ + struct tm *tm = NULL; + + assert(date); + /** Convert ts to localtime with local time-zone */ + tm = localtime((time_t *)&ts); + return (int)strftime(date, len - 1, tm_form, tm); +} + +int rt_curr_tms2str(const char *tm_form, char *date, size_t len) +{ + return rt_tms2str(time(NULL), tm_form, date, len); +} + diff --git a/common/rt/src/rt_tmr.cpp b/common/rt/src/rt_tmr.cpp new file mode 100644 index 0000000..958408b --- /dev/null +++ b/common/rt/src/rt_tmr.cpp @@ -0,0 +1,222 @@ +/************************************************************************* + > File Name: rt_tmr.c + > Author: + > Mail: + > Created Time: 2018年09月19日 星期三 15时57分58秒 + ************************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <assert.h> +#include <sys/time.h> +#include <stdint.h> +#include <signal.h> + +#include "rt_stdlib.h" +#include "rt_sync.h" +#include "rt_string.h" +#include "rt_common.h" + +static int init; + +enum { + ALLOWED, + FORBIDDEN +}; + +enum tmr_type{ + TMR_PERIODIC, + TMR_ASHOT +}; + +enum tmr_status{ + TMR_STOPPED, + TMR_STARTED +}; + +struct tmr_task{ + rt_pthread pid; + + rt_pthread_attr *attr; + + void * (*routine)(void *); +}; + +struct rt_timer_t{ + +#define TMR_INVALID -1 + + int module; + + /** unique id */ + uint32_t uid; + + /** Make sure that allocate desc member with malloc like function. */ + char *desc; + + /** in ms */ + int64_t interval; + + int64_t curr_ticks; + + enum tmr_type type; + + enum tmr_status status; + + void (*routine)(uint32_t uid, int argc, char **argv); + + int argc; + + char **argv; + + int recycle; +}; + +struct rt_timer_t *_this; + +static volatile atomic64_t g_sys_cur_ticks = ATOMIC_INIT(0); + +struct rt_timer_t * +tmr_set(uint32_t uid, + void (*proc)(struct rt_timer_t *t)) +{ + uid = uid; + + if(likely(proc)) + proc(_this); + + return _this; +} + +static void tmr_enable(struct rt_timer_t *t) +{ + t->status = TMR_STARTED; +} + +static void tmr_disable(struct rt_timer_t *t) +{ + t->status = TMR_STOPPED; +} + +void tmr_start(uint32_t uid) +{ + tmr_set(uid, tmr_enable); +} + +void tmr_stop(uint32_t uid) +{ + tmr_set(uid, tmr_disable); +} + +static void tmr_handler(int __attribute__((__unused__))sig) +{ + int64_t old_ticks = 0; + + old_ticks = atomic64_add(&g_sys_cur_ticks, 1); + if (likely(_this->status == TMR_STARTED)){ + if (likely(old_ticks >= _this->curr_ticks)) { + _this->routine(_this->uid, _this->argc, _this->argv); + if (likely(TMR_ASHOT == _this->type)) + _this->status= TMR_STOPPED; + else + _this->curr_ticks = old_ticks + _this->interval; + } + } +} + +static void realtimer_init() +{ + static struct itimerval tmr; + + signal(SIGALRM, tmr_handler); + tmr.it_interval.tv_sec = 1; + tmr.it_interval.tv_usec = 0; + tmr.it_value.tv_sec = 1; + tmr.it_value.tv_usec = 0; + setitimer(ITIMER_REAL, &tmr, NULL); + +} + +static void +tmr_default_routine(uint32_t uid, int __attribute__((__unused__))argc, + char __attribute__((__unused__))**argv) +{ + static int count[2] = {0}; + + count[uid%2] ++; + printf ("default timer [%u, %d] routine has occured\n", uid, count[uid%2]); +} + +static __rt_always_inline__ void * +tmr_alloc() +{ + struct rt_timer_t *t; + + t = (struct rt_timer_t *)kmalloc(sizeof(struct rt_timer_t), MPF_CLR, -1); + if(likely(t)){ + t->uid = TMR_INVALID; + t->routine = tmr_default_routine; + t->interval = 3; //tmr_internal_trans(3); + t->recycle = ALLOWED; + t->type = TMR_ASHOT; + t->status = TMR_STOPPED; + } + return t; +} + +static void * +tmr_daemon(void __attribute__((__unused__))*pv_par ) +{ + FOREVER{ + sleep(86400); + } + return NULL; +} + +uint32_t tmr_create(int module, + const char *desc, + void (*routine)(uint32_t, int, char **), int argc, char **argv, int sec) +{ + uint32_t uid = -1; + + if(unlikely(!desc)){ + goto finish; + } + + _this = (struct rt_timer_t *)tmr_alloc(); + if(unlikely(!_this)){ + goto finish; + } + + _this->module = module; + _this->desc = strdup(desc); + _this->curr_ticks = 0; + _this->interval = sec; + _this->routine = routine ? routine : tmr_default_routine; + _this->type = TMR_PERIODIC; + _this->uid = uid = 1; + _this->recycle = ALLOWED; + _this->status = TMR_STOPPED; + _this->argc = argc; + _this->argv = argv; + + if(likely(init == 0)){ + struct tmr_task *task = (struct tmr_task*)kmalloc(sizeof(struct tmr_task), MPF_CLR, -1); + if (likely(task)){ + if (pthread_create(&task->pid, task->attr, tmr_daemon, NULL)){ + goto finish; + } + if (pthread_detach(task->pid)){ + goto finish; + } + } + init = 1; + realtimer_init(); + } + +finish: + return uid; +} + |
