Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Q
qjs-wrapper
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
qjs-wrapper
Commits
75c4c3e1
Commit
75c4c3e1
authored
Mar 11, 2024
by
Léo-Paul Géneau
👾
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a queue instead of a mutex to receive messages
parent
2a2d821d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
65 deletions
+75
-65
include/pubsub.h
include/pubsub.h
+11
-2
qjs_wrapper.c
qjs_wrapper.c
+64
-63
No files found.
include/pubsub.h
View file @
75c4c3e1
...
...
@@ -17,6 +17,15 @@
#define MAX_STR_SIZE 1024
struct
strNode
{
char
*
str
;
struct
strNode
*
next
;
};
typedef
struct
{
struct
strNode
*
head
;
struct
strNode
*
tail
;
}
StrQueue
;
typedef
struct
{
UA_UInt16
id
;
UA_Double
latitude
;
...
...
@@ -27,8 +36,8 @@ typedef struct {
UA_Float
yaw
;
UA_Float
speed
;
UA_Float
climbRate
;
char
message
[
MAX_STR_SIZE
]
;
char
log
[
MAX_STR_SIZE
]
;
StrQueue
receiveMessageQueue
;
StrQueue
receiveLogQueue
;
}
JSDroneData
;
typedef
struct
{
...
...
qjs_wrapper.c
View file @
75c4c3e1
...
...
@@ -9,14 +9,6 @@
#define DRONE_VARIABLE_NB 4
#define SUBSCRIBER_VARIABLE_NB 1
struct
strNode
{
char
*
str
;
struct
strNode
*
next
;
};
typedef
struct
{
struct
strNode
*
head
;
struct
strNode
*
tail
;
}
StrQueue
;
UA_Int64
positionArray
[
POSITION_ARRAY_SIZE
]
=
{
0
};
UA_UInt32
positionArrayDims
[]
=
{
POSITION_ARRAY_SIZE
};
...
...
@@ -115,19 +107,16 @@ static UA_Boolean pubsubExited = true;
static
UA_UInt32
nbDrone
;
static
UA_UInt32
nbSubscriber
;
static
JSValueConst
*
droneObjectIdList
;
static
StrQueue
m
essageQueue
=
{
static
StrQueue
sendM
essageQueue
=
{
.
head
=
NULL
,
.
tail
=
NULL
,
};
UA_String
currentMessage
;
static
StrQueue
l
ogQueue
=
{
UA_String
current
Send
Message
;
static
StrQueue
sendL
ogQueue
=
{
.
head
=
NULL
,
.
tail
=
NULL
,
};
UA_String
currentLog
;
pthread_mutex_t
mutex
;
pthread_cond_t
threadCond
;
UA_String
currentSendLog
;
bool
isADrone
;
...
...
@@ -201,9 +190,27 @@ static const JSCFunctionListEntry js_position_proto_funcs[] = {
// Drone class functions
static
void
delete_str_node
(
struct
strNode
*
node
)
{
free
(
node
->
str
);
free
(
node
);
}
static
void
cleanQueue
(
StrQueue
*
pQueue
)
{
struct
strNode
*
current
;
while
(
pQueue
->
head
!=
NULL
)
{
current
=
pQueue
->
head
;
pQueue
->
head
=
current
->
next
;
delete_str_node
(
current
);
}
}
static
void
js_drone_finalizer
(
JSRuntime
*
rt
,
JSValue
val
)
{
JSDroneData
*
s
=
(
JSDroneData
*
)
JS_GetOpaque
(
val
,
jsDroneClassId
);
cleanQueue
(
&
(
s
->
receiveMessageQueue
));
cleanQueue
(
&
(
s
->
receiveLogQueue
));
js_free_rt
(
rt
,
s
);
}
...
...
@@ -221,6 +228,14 @@ static JSValue js_drone_ctor(JSContext *ctx, JSValueConst newTarget,
if
(
JS_ToUint32
(
ctx
,
&
uint32
,
argv
[
0
]))
goto
fail
;
s
->
id
=
(
uint16_t
)
uint32
;
s
->
receiveMessageQueue
=
(
StrQueue
){
.
head
=
NULL
,
.
tail
=
NULL
,
};
s
->
receiveLogQueue
=
(
StrQueue
){
.
head
=
NULL
,
.
tail
=
NULL
,
};
proto
=
JS_GetPropertyStr
(
ctx
,
newTarget
,
"prototype"
);
if
(
JS_IsException
(
proto
))
...
...
@@ -250,15 +265,19 @@ static JSValue js_drone_init(JSContext *ctx, JSValueConst thisVal,
return
JS_UNDEFINED
;
}
static
JSValue
readDroneDataStr
(
JSContext
*
ctx
,
char
*
str
)
static
JSValue
readDroneDataStr
(
JSContext
*
ctx
,
StrQueue
*
pQueue
)
{
JSValue
res
;
struct
strNode
*
current
;
pthread_mutex_lock
(
&
mutex
);
res
=
JS_NewString
(
ctx
,
str
);
strcpy
(
str
,
""
);
pthread_cond_signal
(
&
threadCond
);
pthread_mutex_unlock
(
&
mutex
);
current
=
pQueue
->
head
;
if
(
current
!=
NULL
)
{
res
=
JS_NewString
(
ctx
,
current
->
str
);
pQueue
->
head
=
current
->
next
==
NULL
?
(
pQueue
->
tail
=
NULL
)
:
current
->
next
;
delete_str_node
(
current
);
}
else
{
res
=
JS_NewString
(
ctx
,
""
);
}
return
res
;
}
...
...
@@ -285,9 +304,9 @@ static JSValue js_drone_get(JSContext *ctx, JSValueConst thisVal, int magic)
case
7
:
return
JS_NewFloat64
(
ctx
,
s
->
climbRate
);
case
8
:
return
readDroneDataStr
(
ctx
,
s
->
message
);
return
readDroneDataStr
(
ctx
,
&
(
s
->
receiveMessageQueue
)
);
case
9
:
return
readDroneDataStr
(
ctx
,
s
->
log
);
return
readDroneDataStr
(
ctx
,
&
(
s
->
receiveLogQueue
)
);
case
10
:
return
JS_NewInt64
(
ctx
,
s
->
timestamp
);
default:
...
...
@@ -295,7 +314,7 @@ static JSValue js_drone_get(JSContext *ctx, JSValueConst thisVal, int magic)
}
}
static
void
addStrToQueue
(
const
char
*
str
,
StrQueue
*
pQueue
)
static
void
addStrToQueue
(
const
char
*
str
,
StrQueue
*
pQueue
)
{
struct
strNode
*
newNode
;
newNode
=
(
struct
strNode
*
)
malloc
(
sizeof
(
struct
strNode
));
...
...
@@ -310,7 +329,8 @@ static void addStrToQueue(const char* str, StrQueue* pQueue)
}
}
static
JSValue
add_jsstr_to_queue
(
JSContext
*
ctx
,
JSValueConst
jsStr
,
StrQueue
*
queue
)
static
JSValue
add_jsstr_to_queue
(
JSContext
*
ctx
,
JSValueConst
jsStr
,
StrQueue
*
pQueue
)
{
const
char
*
str
;
str
=
JS_ToCString
(
ctx
,
jsStr
);
...
...
@@ -319,7 +339,7 @@ static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *
return
JS_EXCEPTION
;
}
addStrToQueue
(
str
,
q
ueue
);
addStrToQueue
(
str
,
pQ
ueue
);
JS_FreeCString
(
ctx
,
str
);
return
JS_UNDEFINED
;
}
...
...
@@ -327,18 +347,13 @@ static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *
static
JSValue
js_drone_set_message
(
JSContext
*
ctx
,
JSValueConst
thisVal
,
int
argc
,
JSValueConst
*
argv
)
{
return
add_jsstr_to_queue
(
ctx
,
argv
[
0
],
&
m
essageQueue
);
return
add_jsstr_to_queue
(
ctx
,
argv
[
0
],
&
sendM
essageQueue
);
}
static
JSValue
js_drone_set_log
(
JSContext
*
ctx
,
JSValueConst
thisVal
,
int
argc
,
JSValueConst
*
argv
)
{
return
add_jsstr_to_queue
(
ctx
,
argv
[
0
],
&
logQueue
);
}
static
void
delete_str_node
(
struct
strNode
*
node
)
{
free
(
node
->
str
);
free
(
node
);
return
add_jsstr_to_queue
(
ctx
,
argv
[
0
],
&
sendLogQueue
);
}
static
UA_Boolean
UA_String_isEmpty
(
const
UA_String
*
s
)
{
...
...
@@ -350,15 +365,15 @@ static void clear_str(UA_String *pstr) {
UA_String_clear
(
pstr
);
}
static
UA_String
get_StrQueue_content
(
StrQueue
*
q
ueue
,
UA_String
currentStr
)
static
UA_String
get_StrQueue_content
(
StrQueue
*
pQ
ueue
,
UA_String
currentStr
)
{
struct
strNode
*
current
;
current
=
q
ueue
->
head
;
current
=
pQ
ueue
->
head
;
if
(
current
!=
NULL
)
{
clear_str
(
&
currentStr
);
currentStr
=
UA_STRING_ALLOC
(
current
->
str
);
queue
->
head
=
current
->
next
==
NULL
?
(
q
ueue
->
tail
=
NULL
)
:
current
->
next
;
pQueue
->
head
=
current
->
next
==
NULL
?
(
pQ
ueue
->
tail
=
NULL
)
:
current
->
next
;
delete_str_node
(
current
);
}
return
currentStr
;
...
...
@@ -366,12 +381,12 @@ static UA_String get_StrQueue_content(StrQueue *queue, UA_String currentStr)
UA_String
get_message
(
void
)
{
return
get_StrQueue_content
(
&
messageQueue
,
current
Message
);
return
get_StrQueue_content
(
&
sendMessageQueue
,
currentSend
Message
);
}
UA_String
get_log
(
void
)
{
return
get_StrQueue_content
(
&
logQueue
,
current
Log
);
return
get_StrQueue_content
(
&
sendLogQueue
,
currentSend
Log
);
}
static
JSClassDef
jsDroneClass
=
{
...
...
@@ -460,17 +475,13 @@ VariableData pubsub_get_value(UA_String identifier) {
return
varDetails
;
}
static
void
setDroneDataStr
(
void
*
data
,
char
*
str
)
static
void
setDroneDataStr
(
void
*
data
,
StrQueue
*
pQueue
)
{
UA_String
uaStr
=
*
(
UA_String
*
)
data
;
pthread_mutex_lock
(
&
mutex
);
while
(
strlen
(
str
)
!=
0
)
pthread_cond_wait
(
&
threadCond
,
&
mutex
);
char
str
[
MAX_STR_SIZE
];
memcpy
(
str
,
uaStr
.
data
,
uaStr
.
length
);
str
[
uaStr
.
length
]
=
'\0'
;
pthread_mutex_unlock
(
&
mutex
);
addStrToQueue
(
str
,
pQueue
);
}
static
void
pubsub_update_variables
(
UA_UInt32
id
,
const
UA_DataValue
*
var
,
bool
print
)
...
...
@@ -508,11 +519,11 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
}
return
;
}
else
if
(
pubsubIdsArray
[
i
].
message
==
id
)
{
setDroneDataStr
(
var
->
value
.
data
,
s
->
message
);
setDroneDataStr
(
var
->
value
.
data
,
&
(
s
->
receiveMessageQueue
)
);
return
;
}
else
if
(
pubsubIdsArray
[
i
].
log
==
id
)
{
if
(
!
isADrone
)
{
setDroneDataStr
(
var
->
value
.
data
,
s
->
log
);
setDroneDataStr
(
var
->
value
.
data
,
&
(
s
->
receiveLogQueue
)
);
}
return
;
}
...
...
@@ -610,24 +621,12 @@ static JSValue js_init_pubsub(JSContext *ctx, JSValueConst thisVal,
if
(
JS_ToUint32
(
ctx
,
&
nbSubscriber
,
argv
[
1
]))
return
JS_EXCEPTION
;
currentMessage
=
UA_STRING
(
""
);
currentLog
=
UA_STRING
(
""
);
current
Send
Message
=
UA_STRING
(
""
);
current
Send
Log
=
UA_STRING
(
""
);
droneObjectIdList
=
(
JSValue
*
)
malloc
((
nbDrone
+
nbSubscriber
)
*
sizeof
(
JSValueConst
));
return
JS_NewInt32
(
ctx
,
0
);
}
static
void
cleanQueue
(
StrQueue
queue
,
UA_String
current_str
)
{
struct
strNode
*
current
;
while
(
queue
.
head
!=
NULL
)
{
current
=
queue
.
head
;
queue
.
head
=
current
->
next
;
delete_str_node
(
current
);
}
clear_str
(
&
current_str
);
}
static
JSValue
js_stop_pubsub
(
JSContext
*
ctx
,
JSValueConst
thisVal
,
int
argc
,
JSValueConst
*
argv
)
{
...
...
@@ -636,8 +635,10 @@ static JSValue js_stop_pubsub(JSContext *ctx, JSValueConst thisVal,
sleep
(
1
);
free
(
droneObjectIdList
);
cleanQueue
(
messageQueue
,
currentMessage
);
cleanQueue
(
logQueue
,
currentLog
);
cleanQueue
(
&
sendMessageQueue
);
clear_str
(
&
currentSendMessage
);
cleanQueue
(
&
sendLogQueue
);
clear_str
(
&
currentSendLog
);
return
JS_NewInt32
(
ctx
,
0
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment