FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-usb/src/uv_async_queue.h at master · SkyRocketDevelopment/node-usb · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SkyRocketDevelopment
/
node-usb
Public
forked from
node-usb/node-usb
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node-usb
/
src
/
uv_async_queue.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (62 loc) · 1.43 KB
Breadcrumbs
node-usb
/
src
/
uv_async_queue.h
Copy path
File metadata and controls
73 lines (62 loc) · 1.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
ifndef
SRC_UV_ASYNC_QUEUE_H
#
define
SRC_UV_ASYNC_QUEUE_H
#
include
<
uv.h
>
#
include
<
node_version.h
>
#
include
<
queue
>
#
include
"
polyfill.h
"
template
<
class
T
>
class
UVQueue
{
public:
typedef
void
(*fptr)(T);
UVQueue
(fptr cb,
int
_ref_count=
0
): callback(cb), ref_count(_ref_count) {
uv_mutex_init
(&mutex);
uv_async_init
(
uv_default_loop
(), &async, UVQueue::internal_callback);
async.
data
=
this
;
if
(ref_count <
1
) {
uv_unref
((
uv_handle_t
*)&async);
}
}
void
post
(T value){
uv_mutex_lock
(&mutex);
queue.
push
(value);
uv_mutex_unlock
(&mutex);
uv_async_send
(&async);
}
~UVQueue
(){
uv_mutex_destroy
(&mutex);
uv_close
((
uv_handle_t
*)&async,
NULL
);
//
TODO: maybe we can't delete UVQueue until callback?
}
void
ref
(){
ref_count++;
if
(ref_count ==
1
) {
uv_ref
((
uv_handle_t
*)&async);
}
}
void
unref
(){
ref_count--;
if
(ref_count ==
0
) {
uv_unref
((
uv_handle_t
*)&async);
}
}
private:
fptr callback;
std::queue<T> queue;
uv_mutex_t
mutex;
uv_async_t
async;
int
ref_count;
static
UV_ASYNC_CB
(internal_callback){
UVQueue* uvqueue =
static_cast
<UVQueue*>(handle->
data
);
while
(
1
){
uv_mutex_lock
(&uvqueue->
mutex
);
if
(uvqueue->
queue
.
empty
()){
uv_mutex_unlock
(&uvqueue->
mutex
);
break
;
}
T item = uvqueue->
queue
.
front
();
uvqueue->
queue
.
pop
();
uv_mutex_unlock
(&uvqueue->
mutex
);
uvqueue->
callback
(item);
}
}
};
#
endif
Back
|
FazBrowse Home
|
New Git URL