Skip to content

Commit

Permalink
change lifecycle of handle, not auto detach the handdle.
Browse files Browse the repository at this point in the history
  • Loading branch information
levalup committed Jul 23, 2024
1 parent 33cc07a commit 015e982
Show file tree
Hide file tree
Showing 29 changed files with 288 additions and 199 deletions.
25 changes: 14 additions & 11 deletions docs/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ stateDiagram-v2
direction LR
[*] --> Suspend
Suspend --> Running : run
Suspend --> [*] : close
Suspend --> Running: run
Suspend --> [*]: close
Running --> Suspend
Running --> [*] : close
Running --> [*]: close
classDef attached color:#67C23A;
classDef detached stroke:red,color:#F56C6C;
Expand Down Expand Up @@ -47,9 +47,9 @@ stateDiagram-v2
direction LR
[*] --> Hanging
Hanging --> Waiting : request
Waiting --> Dealing : response
Dealing --> Waiting : request
Hanging --> Waiting: request
Waiting --> Dealing: response
Dealing --> Waiting: request
Dealing --> Hanging
classDef attached color:#67C23A;
Expand All @@ -73,18 +73,21 @@ However, it should be noted that `request` operations can only be performed in t

## Handle

> In the new version, the strategy for the lifecycle has been modified, and it no longer automatically "detaches".
> An explicit "detach" interface is provided, allowing the status to be explicitly adjusted to "detach" when needed.
Following is the life states of `handle`.

```mermaid
stateDiagram-v2
direction LR
[*] --> Allocated : set_data
Allocated --> Suspend : init
Suspend --> Running : start
Suspend --> [*] : close
[*] --> Allocated: set_data
Allocated --> Suspend: init
Suspend --> Running: start
Suspend --> [*]: close
Running --> Suspend: stop
Running --> [*] : close
Running --> [*]: close
classDef attached color:#67C23A;
classDef detached stroke:red,color:#F56C6C;
Expand Down
2 changes: 1 addition & 1 deletion examples/fs_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main() {
});

// close fs_event
uv::timer_t().start(2 * 1000, 1).call([&]() {
uv::timer_t().start(2 * 1000, 1).detach().call([&]() {
fs_event.close(nullptr);
throw uvcxx::close_handle();
});
Expand Down
4 changes: 2 additions & 2 deletions examples/idle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
int main() {
uv::loop_t loop;
int64_t counter = 0;
uv::idle_t(loop).start().call([&]() {
uv::idle_t(loop).start().detach().call([&]() {
counter++;
if (counter >= 1000) {
throw uvcxx::close_handle();
Expand All @@ -19,4 +19,4 @@ int main() {
loop.run();

return 0;
}
}
8 changes: 4 additions & 4 deletions examples/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ int main() {
uv::pipe_t server(server_loop, false);
server.bind(pipe_name);

server.listen(128).call([=]() mutable {
server.listen(128).detach().call([=]() mutable {
auto conn = server.accept(false);

uv::buf_t server_buf;
conn.alloc().call([=](size_t size, uv_buf_t *buf) mutable {
server_buf.resize(size);
*buf = server_buf;
});
conn.read_start().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
conn.read_start().detach().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
std::cout << "server read: " << std::string(buf->base, nread) << std::endl;
}).except<uvcxx::E_AGAIN>([]() {
}).except<uvcxx::E_EOF>([=]() mutable {
Expand All @@ -49,7 +49,7 @@ int main() {
});

std::cout << "[INFO] stop server after " << server_time_ms << "ms" << std::endl;
uv::timer_t(server_loop).start(server_time_ms, 1).call([=]() mutable {
uv::timer_t(server_loop).start(server_time_ms, 1).detach().call([=]() mutable {
server.close(nullptr);
throw uvcxx::close_handle(); // close timer
});
Expand All @@ -60,7 +60,7 @@ int main() {
auto msg = uvcxx::catstr("hello~", i);

uv::pipe_t client(client_loop, false);
client.connect(pipe_name).then([=]() mutable {
client.connect(pipe_name).detach().then([=]() mutable {
client.write(msg).then([=]() {
std::cout << "client write: " << msg << std::endl;
});
Expand Down
7 changes: 5 additions & 2 deletions examples/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, const char *argv[]) {
{
// start backend
uv::signal_t signal(loop);
signal.start(sig).call([key, &count](int) mutable {
signal.start(sig).detach().call([key, &count](int) mutable {
++count;
std::cout << "Received " << key << " " << count << std::endl;
throw uvcxx::close_handle();
Expand All @@ -61,9 +61,12 @@ int main(int argc, const char *argv[]) {
uvcxx_assert(loop_handle_count(loop) == 0, "count = ", loop_handle_count(loop));
// start oneshot
{
uv::signal_t(loop).start_oneshot(sig).then([key, &count](int) mutable {
uv::signal_t signal(loop);
signal.start_oneshot(sig).detach().then([key, &count](int) mutable {
++count;
std::cout << "Received " << key << " " << count << std::endl;
}).finally([signal]() mutable {
signal.close(nullptr);
});
}
loop.run();
Expand Down
8 changes: 4 additions & 4 deletions examples/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main() {

std::cout << "[INFO] server start listen: " << server_sock << std::endl;

server.listen(128).call([=]() mutable {
server.listen(128).detach().call([=]() mutable {
auto conn = server.accept(false);

std::cout << "[INFO] received [" << getsockname(conn) << " <- " << getpeername(conn) << "]" << std::endl;
Expand All @@ -59,7 +59,7 @@ int main() {
server_buf.resize(size);
*buf = server_buf;
});
conn.read_start().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
conn.read_start().detach().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
std::cout << "server read: " << std::string(buf->base, nread) << std::endl;
}).except<uvcxx::E_AGAIN>([]() {
}).except<uvcxx::E_EOF>([=]() mutable {
Expand All @@ -73,7 +73,7 @@ int main() {
});

std::cout << "[INFO] stop server after " << server_time_ms << "ms" << std::endl;
uv::timer_t(server_loop).start(server_time_ms, 1).call([=]() mutable {
uv::timer_t(server_loop).start(server_time_ms, 1).detach().call([=]() mutable {
server.close(nullptr);
throw uvcxx::close_handle(); // close timer
});
Expand All @@ -86,7 +86,7 @@ int main() {
auto msg = uvcxx::catstr("hello~", i);

uv::tcp_t client(client_loop, false);
client.connect(addr).then([=]() mutable {
client.connect(addr).detach().then([=]() mutable {
client.write(msg).then([=]() {
std::cout << "client write: " << msg << std::endl;
});
Expand Down
2 changes: 1 addition & 1 deletion examples/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <uvcxx.h>

int main() {
uv::timer_t().start(1000, 1000).call([]() {
uv::timer_t().start(1000, 1000).detach().call([]() {
std::cout << "Hello~~~" << std::endl;
throw uvcxx::close_handle();
});
Expand Down
6 changes: 3 additions & 3 deletions examples/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main() {
server_buf.resize(size);
*buf = server_buf;
});
server.recv_start().call(
server.recv_start().detach().call(
[=](ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, uv_udp_flags flags) mutable {
std::cout << "server read: " << std::string(buf->base, nread) << " with "
<< uvcxx::to_string(uv_udp_flags(flags)) << " from " << uvcxx::any_address_t(addr)
Expand All @@ -60,7 +60,7 @@ int main() {
});

std::cout << "[INFO] stop server after " << server_time_ms << "ms" << std::endl;
uv::timer_t(server_loop).start(server_time_ms, 1).call([=]() mutable {
uv::timer_t(server_loop).start(server_time_ms, 1).detach().call([=]() mutable {
server.close(nullptr);
throw uvcxx::close_handle(); // close timer
});
Expand All @@ -73,7 +73,7 @@ int main() {
auto msg = uvcxx::catstr("hello~", i);

uv::udp_t client(client_loop, false);
client.send(msg, addr).then([=]() {
client.send(msg, addr).detach().then([=]() {
std::cout << "client write: " << msg << std::endl;
}).finally([=]() mutable {
client.close(nullptr);
Expand Down
2 changes: 2 additions & 0 deletions include/uvcxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "uvcxx/utils/apply.h"
#include "uvcxx/utils/assert.h"
#include "uvcxx/utils/attached_callback.h"
#include "uvcxx/utils/attached_promise.h"
#include "uvcxx/utils/callback.h"
#include "uvcxx/utils/defer.h"
#include "uvcxx/utils/detach.h"
Expand Down
14 changes: 9 additions & 5 deletions include/uvcxx/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ namespace uv {

async_t() {
set_data(new data_t(*this)); //< data will be deleted in close action
_attach_data_();
}

self &detach() {
_detach_();
return *this;
}

UVCXX_NODISCARD
Expand All @@ -25,14 +29,14 @@ namespace uv {
}

UVCXX_NODISCARD
uvcxx::callback<> init(const loop_t &loop) {
uvcxx::attached_callback<> init(const loop_t &loop) {
UVCXX_APPLY(uv_async_init(loop, *this, raw_callback), nullptr);
_detach_();
return callback();
_initialized_();
return {*this, callback()};
}

UVCXX_NODISCARD
uvcxx::callback<> init() {
uvcxx::attached_callback<> init() {
return init(default_loop());
}

Expand Down
13 changes: 8 additions & 5 deletions include/uvcxx/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ namespace uv {
explicit check_t(const loop_t &loop) {
set_data(new data_t(*this)); //< data will be deleted in close action
(void) uv_check_init(loop, *this);
_attach_close_();
_initialized_();
}

self &detach() {
_detach_();
return *this;
}

UVCXX_NODISCARD
Expand All @@ -28,15 +33,13 @@ namespace uv {
}

UVCXX_NODISCARD
uvcxx::callback<> start() {
uvcxx::attached_callback<> start() {
UVCXX_APPLY(uv_check_start(*this, raw_callback), nullptr);
_detach_();
return callback();
return {*this, callback()};
}

void stop() {
(void) uv_check_stop(*this);
_attach_close_();
}

private:
Expand Down
13 changes: 8 additions & 5 deletions include/uvcxx/fs_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ namespace uv {
#endif
set_data(new data_t(*this)); //< data will be deleted in close action
(void) uv_fs_event_init(loop, *this);
_attach_close_();
_initialized_();
}

self &detach() {
_detach_();
return *this;
}

UVCXX_NODISCARD
Expand All @@ -34,15 +39,13 @@ namespace uv {
}

UVCXX_NODISCARD
uvcxx::callback<const char *, uv_fs_event> start(uvcxx::string path, int flags) {
uvcxx::attached_callback<const char *, uv_fs_event> start(uvcxx::string path, int flags) {
UVCXX_APPLY(uv_fs_event_start(*this, raw_callback, path, flags), nullptr);
_detach_();
return callback();
return {*this, callback()};
}

void stop() {
(void) uv_fs_event_stop(*this);
_attach_close_();
}

int getpath(char *buffer, size_t *size) {
Expand Down
13 changes: 8 additions & 5 deletions include/uvcxx/fs_poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ namespace uv {
explicit fs_poll_t(const loop_t &loop) {
set_data(new data_t(*this)); //< data will be deleted in close action
(void) uv_fs_poll_init(loop, *this);
_attach_close_();
_initialized_();
}

self &detach() {
_detach_();
return *this;
}

UVCXX_NODISCARD
Expand All @@ -31,15 +36,13 @@ namespace uv {
}

UVCXX_NODISCARD
uvcxx::callback<const uv_stat_t *, const uv_stat_t *> start(uvcxx::string path, unsigned int interval) {
uvcxx::attached_callback<const uv_stat_t *, const uv_stat_t *> start(uvcxx::string path, unsigned int interval) {
UVCXX_APPLY(uv_fs_poll_start(*this, raw_callback, path, interval), nullptr);
_detach_();
return callback();
return {*this, callback()};
}

void stop() {
(void) uv_fs_poll_stop(*this);
_attach_close_();
}

int getpath(char *buffer, size_t *size) const {
Expand Down
Loading

0 comments on commit 015e982

Please sign in to comment.