forked from Tencent/libco
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_specific_macro.cpp
77 lines (69 loc) · 1.91 KB
/
example_specific_macro.cpp
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
using namespace std;
struct stRoutineArgs_t
{
stCoRoutine_t* co;
int routine_id;
};
struct stRoutineSpecificData_t
{
int idx;
};
static pthread_once_t _routine_once_stRoutineSpecificData_t = 0;
static pthread_key_t _routine_key_stRoutineSpecificData_t;
static int _routine_init_stRoutineSpecificData_t = 0;
static void _routine_make_key_stRoutineSpecificData_t()
{
(void) pthread_key_create(&_routine_key_stRoutineSpecificData_t, __null);
}
template <class T>class clsRoutineData_routine_stRoutineSpecificData_t
{
public:
inline T* operator->()
{
if (!_routine_init_stRoutineSpecificData_t)
{
pthread_once(&_routine_once_stRoutineSpecificData_t,_routine_make_key_stRoutineSpecificData_t);
_routine_init_stRoutineSpecificData_t = 1;
}
T* p = (T*)co_getspecific(_routine_key_stRoutineSpecificData_t);
if (!p)
{
p = (T*)calloc(1,sizeof(T));
int ret = co_setspecific(_routine_key_stRoutineSpecificData_t,p) ;
if (ret)
{
if (p)
{
free(p);
p = __null;
}
}
}
return p;
}
};
static clsRoutineData_routine_stRoutineSpecificData_t<stRoutineSpecificData_t> __routine;;
void* RoutineFunc(void* args)
{
co_enable_hook_sys();
stRoutineArgs_t* routine_args = (stRoutineArgs_t*)args;
__routine->idx = routine_args->routine_id;
while (true)
{
printf("%s:%d routine specific data idx %d\n", __func__, 45, __routine->idx);
poll(__null, 0, 1000);
}
return __null;
}
int main()
{
stRoutineArgs_t args[10];
for (int i = 0; i < 10; i++)
{
args[i].routine_id = i;
co_create(&args[i].co, __null, RoutineFunc, (void*)&args[i]);
co_resume(args[i].co);
}
co_eventloop(co_get_epoll_ct(), __null, __null);
return 0;
}