-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdefaults.c
170 lines (157 loc) · 4.12 KB
/
hdefaults.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************
* Name: hdefaults.h
* Purpose: 实现一些一些默认函数
* Author: HYH (hyhsystem.cn)
* Created: 2023-07-14
* Copyright: HYH (hyhsystem.cn)
* License: MIT
**************************************************************/
#include "hcompiler.h"
#include "hdefaults.h"
#ifdef __RTTHREAD__
#include "rtthread.h"
#endif // __RTTHREAD__
#ifdef WIN32
#include "windows.h"
static CRITICAL_SECTION g_mutex_lock;
static bool g_is_mutex_lock_init=false;
void check_mutex_lock()
{
if(!g_is_mutex_lock_init)
{
InitializeCriticalSection(&g_mutex_lock);
g_is_mutex_lock_init=true;
}
}
#endif // WIN32
#ifdef __unix__
#include "pthread.h"
static pthread_mutex_t g_mutex_lock;
static pthread_mutexattr_t g_mutex_lock_attr;
static bool g_is_mutex_lock_init=false;
void check_mutex_lock()
{
if(!g_is_mutex_lock_init)
{
pthread_mutexattr_init(&g_mutex_lock_attr);
pthread_mutexattr_settype(&g_mutex_lock_attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&g_mutex_lock,&g_mutex_lock_attr);
g_is_mutex_lock_init=true;
}
}
#endif // __unix__
#ifdef HDEFAULTS_TICK_GET
extern hdefaults_tick_t HDEFAULTS_TICK_GET(void);
#endif // HDEFAULTS_TICK_GET
hdefaults_tick_t hdefaults_tick_get(void)
{
#ifdef HDEFAULTS_TICK_GET
return HDEFAULTS_TICK_GET();
#elif defined(HDEFAULTS_OS_RTTHREAD)
return rt_tick_get_millisecond();
#elif defined(HDEFAULTS_OS_WINDOWS)
return GetTickCount();
#elif defined(HDEFAULTS_OS_UNIX)
{
hdefaults_tick_t ret=0;
struct timeval tv= {0};
struct timezone tz= {0};
gettimeofday(&tv, &tz);
ret+=tv.tv_sec*1000;
ret+=tv.tv_usec/1000;
return ret;
}
#else
return 0;//默认永远返回0
#endif // HDEFAULTS_TICK_GET
}
#ifdef USING_HMEMORYHEAP
extern void* hmemoryheap_malloc(size_t nBytes);
#endif // USING_HMEMORYHEAP
#ifdef HDEFAULTS_MALLOC
extern void * HDEFAULTS_MALLOC(size_t bytes);
#endif // HDEFAULTS_MALLOC
void * hdefaults_malloc(size_t nBytes,void *usr)
{
UNUSED(usr);
#ifdef HDEFAULTS_MALLOC
return HDEFAULTS_MALLOC(nBytes);
#elif defined(__RTTHREAD__)
return rt_malloc(nBytes);
#elif defined(USING_HMEMORYHEAP)
return hmemoryheap_malloc(nBytes);
#else
return malloc(nBytes);
#endif
}
#ifdef USING_HMEMORYHEAP
extern void hmemoryheap_free(void*);
#endif // USING_HMEMORYHEAP
#ifdef HDEFAULTS_FREE
extern void HDEFAULTS_FREE(void *ptr);
#endif // HDEFAULTS_FREE
void hdefaults_free(void *ptr,void *usr)
{
UNUSED(usr);
#ifdef HDEFAULTS_FREE
HDEFAULTS_FREE(ptr);
#elif defined(__RTTHREAD__)
rt_free(ptr);
#elif defined(USING_HMEMORYHEAP)
hmemoryheap_free(ptr);
#else
free(ptr);
#endif
}
#ifdef HDEFAULTS_MUTEX_LOCK
extern void HDEFAULTS_MUTEX_LOCK();
#endif // HDEFAULTS_MUTEX_LOCK
void hdefaults_mutex_lock(void *usr)
{
UNUSED(usr);
#ifdef HDEFAULTS_MUTEX_LOCK
HDEFAULTS_MUTEX_LOCK();
#elif defined(__RTTHREAD__)
rt_enter_critical();
#elif defined(WIN32)
check_mutex_lock();
EnterCriticalSection(&g_mutex_lock);
#elif defined(__unix__)
check_mutex_lock();
pthread_mutex_lock(&g_mutex_lock);
#else
#warning "hdefaults_mutex_lock is invalid!"
#endif
}
#ifdef HDEFAULTS_MUTEX_UNLOCK
extern void HDEFAULTS_MUTEX_UNLOCK();
#endif // HDEFAULTS_MUTEX_UNLOCK
void hdefaults_mutex_unlock(void *usr)
{
UNUSED(usr);
#ifdef HDEFAULTS_MUTEX_UNLOCK
HDEFAULTS_MUTEX_UNLOCK();
#elif defined(__RTTHREAD__)
rt_exit_critical();
#elif defined(WIN32)
check_mutex_lock();
LeaveCriticalSection(&g_mutex_lock);
#elif defined(__unix__)
check_mutex_lock();
pthread_mutex_unlock(&g_mutex_lock);
#else
#warning "hdefaults_mutex_unlock is invalid!"
#endif
}
const hdefaults_api_table_t defalut_table=
{
hdefaults_tick_get,
hdefaults_malloc,
hdefaults_free,
hdefaults_mutex_lock,
hdefaults_mutex_unlock,
};
const hdefaults_api_table_t * hdefaults_get_api_table(void)
{
return &defalut_table;
}