-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.h
125 lines (108 loc) · 3.11 KB
/
event.h
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
/**
* Copyright (c) 2016 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file event.h
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2016/06/13
* @license
* @description
*/
#ifndef __event_H_wHDIbK0w_lgzZ_HWrU_sXAR_ufL9vTlxh0qh__
#define __event_H_wHDIbK0w_lgzZ_HWrU_sXAR_ufL9vTlxh0qh__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Macro Definition
//=============================================================================
#if defined(__MINGW32__)
#include <windows.h>
#undef sleep
#define sleep(x) Sleep(x * 1000)
#undef usleep
#define usleep(x) Sleep((unsigned long)x / 1000ul)
#endif
//=============================================================================
// Structure Definition
//=============================================================================
typedef struct event
{
unsigned int uid;
// pid_t pid;
unsigned int post_events;
// unsigned int wait_events;
} event_t;
//=============================================================================
// Global Data Definition
//=============================================================================
//=============================================================================
// Private Function Definition
//=============================================================================
static event_t*
event_create(void)
{
event_t *pHEvent = malloc(sizeof(event_t));
if( pHEvent )
{
memset(pHEvent, 0x0, sizeof(event_t));
pHEvent->uid = (clock() & 0xFFFFFFFF);
}
return pHEvent;
}
static void
event_destroy(event_t *pHEvent)
{
if( pHEvent ) free(pHEvent);
return;
}
static int
event_wait(
event_t *pHEvent,
unsigned int mask,
unsigned int timeout_ms,
unsigned int *pEvent_flag)
{
unsigned int bCheckTimeout = (timeout_ms) ? 1 : 0;
unsigned int period_ms = 100;
while(1)
{
if( pHEvent->post_events & mask )
{
if( pEvent_flag )
*pEvent_flag = pHEvent->post_events & mask;
break;
}
if( bCheckTimeout )
{
if( !timeout_ms )
break;
timeout_ms -= period_ms;
timeout_ms = (timeout_ms > 0) ? timeout_ms : 0;
}
usleep(period_ms * 1000);
}
pHEvent->post_events = 0;
return 0;
}
static int
event_set(event_t *pHEvent, unsigned int flags)
{
pHEvent->post_events |= flags;
return 0;
}
//=============================================================================
// Public Function Definition
//=============================================================================
#ifdef __cplusplus
}
#endif
#endif