Skip to content

Commit f14249d

Browse files
JianyuWang0623cederom
authored andcommitted
system/uORB: Add API for sending loop exit event
Add orb_loop_exit_async() API to send exit event to uORB loop. Closing immediately after a write may cause missing a wakeup. When all file descriptors associated with the same eventfd object have been closed, the resources for object are freed by the kernel. --EVENTFD(2) Signed-off-by: wangjianyu3 <[email protected]>
1 parent 3b0181d commit f14249d

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

system/uorb/uORB/epoll.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const struct orb_loop_ops_s g_orb_loop_epoll_ops =
5858

5959
static int orb_loop_epoll_init(FAR struct orb_loop_s *loop)
6060
{
61-
loop->running = false;
6261
loop->fd = epoll_create1(EPOLL_CLOEXEC);
6362
if (loop->fd < 0)
6463
{
@@ -75,13 +74,7 @@ static int orb_loop_epoll_run(FAR struct orb_loop_s *loop)
7574
int nfds;
7675
int i;
7776

78-
if (loop->running)
79-
{
80-
return -EBUSY;
81-
}
82-
83-
loop->running = true;
84-
while (loop->running)
77+
while (1)
8578
{
8679
nfds = epoll_wait(loop->fd, et, CONFIG_UORB_LOOP_MAX_EVENTS, -1);
8780
if (nfds == -1 && errno != EINTR)
@@ -96,6 +89,10 @@ static int orb_loop_epoll_run(FAR struct orb_loop_s *loop)
9689
{
9790
continue;
9891
}
92+
else if (handle == &loop->exit_handle)
93+
{
94+
return OK;
95+
}
9996

10097
if (et[i].events & EPOLLIN)
10198
{
@@ -151,7 +148,6 @@ static int orb_loop_epoll_uninit(FAR struct orb_loop_s *loop)
151148
{
152149
int ret;
153150

154-
loop->running = false;
155151
ret = close(loop->fd);
156152
if (ret < 0)
157153
{

system/uorb/uORB/loop.c

+20-14
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,14 @@ int orb_loop_init(FAR struct orb_loop_s *loop, enum orb_loop_type_e type)
100100

101101
int orb_loop_run(FAR struct orb_loop_s *loop)
102102
{
103-
loop->self = gettid();
104103
return loop->ops->run(loop);
105104
}
106105

107106
int orb_loop_deinit(FAR struct orb_loop_s *loop)
108107
{
109-
eventfd_t exit = 1;
110108
int ret;
111109

112-
loop->running = false;
113-
write(loop->exit_handle.fd, &exit, sizeof(exit));
114-
115-
if (gettid() != loop->self)
116-
{
117-
ret = waitpid(loop->self, &ret, 0);
118-
if (ret < 0)
119-
{
120-
uorberr("loop deinit waitpid failed! ret:%d", -errno);
121-
}
122-
}
123-
110+
orb_handle_stop(loop, &loop->exit_handle);
124111
close(loop->exit_handle.fd);
125112
ret = loop->ops->uninit(loop);
126113
if (ret >= 0)
@@ -131,6 +118,25 @@ int orb_loop_deinit(FAR struct orb_loop_s *loop)
131118
return ret;
132119
}
133120

121+
int orb_loop_exit_async(FAR struct orb_loop_s *loop)
122+
{
123+
eventfd_t exit = 1;
124+
ssize_t n;
125+
126+
if (!loop)
127+
{
128+
return -EINVAL;
129+
}
130+
131+
n = write(loop->exit_handle.fd, &exit, sizeof(exit));
132+
if (n < 0)
133+
{
134+
return -errno;
135+
}
136+
137+
return n == sizeof(exit) ? OK : ERROR;
138+
}
139+
134140
int orb_handle_init(FAR struct orb_handle_s *handle, int fd, int events,
135141
FAR void *arg, orb_datain_cb_t datain_cb,
136142
orb_dataout_cb_t dataout_cb, orb_eventpri_cb_t pri_cb,

system/uorb/uORB/uORB.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ struct orb_loop_ops_s;
118118
struct orb_loop_s
119119
{
120120
FAR const struct orb_loop_ops_s *ops; /* Loop handle ops. */
121-
bool running; /* uORB loop is running flag. */
122121
int fd; /* Loop fd. */
123122
struct orb_handle_s exit_handle; /* The exit handle */
124-
pid_t self; /* The pid of the loop */
125123
};
126124
#endif
127125

@@ -959,6 +957,21 @@ int orb_loop_run(FAR struct orb_loop_s *loop);
959957

960958
int orb_loop_deinit(FAR struct orb_loop_s *loop);
961959

960+
/****************************************************************************
961+
* Name: orb_loop_exit_async
962+
*
963+
* Description:
964+
* Send exit event to the current loop(not wait).
965+
*
966+
* Input Parameters:
967+
* loop orb loop contains multiple handles.
968+
*
969+
* Returned Value:
970+
* Zero (OK) on success; a -1 (ERROR) or negated errno value on failure.
971+
****************************************************************************/
972+
973+
int orb_loop_exit_async(FAR struct orb_loop_s *loop);
974+
962975
/****************************************************************************
963976
* Name: orb_handle_init
964977
*

0 commit comments

Comments
 (0)