-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.c
65 lines (52 loc) · 1.16 KB
/
global.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
#include <string.h>
#include <assert.h>
#include <time.h>
#include <pthread.h>
#include "global.h"
//void bzero(void *base, unsigned int size)
//{
// assert(size >= 0);
// memset(base, 0, size);
//}
void m_tolower(char *str)
{
assert(str != NULL);
int i, len;
len = strlen(str);
for (i = 0; i < len; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = 'a' + str[i] - 'A';
}
}
}
/**
* @brief current thread wait some time
*
* @param sec, the internal the thread wait
*/
void thread_wait(const int sec)
{
struct timespec timeout;
pthread_mutex_t mutex;
pthread_cond_t cond;
/* there is no need to wait */
if (sec <= 0)
{
return;
}
/**
* @param mutex: for synchronization
* @param cond: wait sec seconds
*/
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_mutex_lock(&mutex);
/* wait till : current time + sec */
timeout.tv_sec = time(NULL) + sec;
timeout.tv_nsec = 0;
pthread_cond_timedwait(&cond, &mutex, &timeout);
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
}