-
Notifications
You must be signed in to change notification settings - Fork 3
/
util_os.c
100 lines (98 loc) · 1.99 KB
/
util_os.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
#include "util_os.h"
//time
int ut_usectime(ulong* sec, ulong* ms){
struct timeval tv;
int ret;
ret = gettimeofday(&tv, NULL);
if(ret != -1){
*sec = (ulong)tv.tv_sec;
*ms = (ulong)tv.tv_usec;
}
return ret;
}
//mutex
void mutex_create(
mutex_t* mutex
#ifdef UNIV_DEBUG
,const char* cmutex_name
#endif
)
{
pthread_mutex_init(&mutex->os_mutex,FAST_MUTEX_INIT);
#ifdef UNIV_DEBUG
mutex->mutex_name = cmutex_name;
mutex->count_using = 0;
mutex->count_os_yield = 0;
mutex->spent_time = 0;
mutex->max_spent_time = 0;
#endif
}
void mutex_destroy(mutex_t* mutex){
//assert(mutex->waiters == 0)
pthread_mutex_destroy(&mutex->os_mutex);
}
void mutex_enter(mutex_t* mutex){
int ret;
ut_d(mutex->count_using++);
ret = pthread_mutex_trylock(&mutex->os_mutex);
if(ret == 0){
//succeed
#ifdef UNIV_DEBUG
ut_d(mutex->thread_id = pthread_self());
#endif
return;
}else{
//spin wait
#ifdef UNIV_DEBUG
ulonglong start_time, finish_time, diff_time;
ulong ms, sec;
mutex->count_os_yield++;
//deal with time
ut_usectime(&ms, &sec);
start_time= (ulonglong)sec * 1000000 + ms;
#endif
pthread_mutex_lock(&mutex->os_mutex);
#ifdef UNIV_DEBUG
//deal with time
ut_usectime(&ms, &sec);
finish_time= (ulonglong)sec * 1000000 + ms;
diff_time = finish_time - start_time;
mutex->spent_time += diff_time;
if( diff_time > mutex->max_spent_time )
mutex->max_spent_time = diff_time;
#endif
}
}
void mutex_exit(mutex_t* mutex){
#ifdef UNIV_DEBUG
ut_d(mutex->thread_id = (pthred_t_id)NULL);
#endif
pthread_mutex_unlock(&mutex->os_mutex);
}
#ifdef UNIV_DEBUG
ibool mutex_own(mutex_t* mutex){
return (mutex->thread_id == pthread_self());
}
#endif
//mem
void ut_memset(byte* ptr, ulong n){
memset(ptr, '\0', n);
}
void* ut_malloc_low(ulong n, int set_to_zero){
void* ret;
ret = malloc(n);
assert(ret); //
if(set_to_zero)
memset(ret, '\0', n);
return ret;
}
void* ut_malloc(ulong n){
return ut_malloc_low(n, FALSE);
}
void ut_free(void *ptr){
if(ptr == NULL)
return;
else
free(ptr);
return;
}