On Tue, Jan 31, 2017 at 11:36 PM, Justin Cinkelj <[email protected]>
wrote:

> Fixes #845.
>
> Signed-off-by: Justin Cinkelj <[email protected]>
> ---
>  modules/httpserver/api-doc/listings/app.json | 26
> ++++++++++++++++++++++++++
>  modules/httpserver/api/app.cc                | 18 ++++++++++++++++++
>  2 files changed, 44 insertions(+)
>
> diff --git a/modules/httpserver/api-doc/listings/app.json
> b/modules/httpserver/api-doc/listings/app.json
> index 7306e58..43ff1da 100644
> --- a/modules/httpserver/api-doc/listings/app.json
> +++ b/modules/httpserver/api-doc/listings/app.json
> @@ -34,5 +34,31 @@
>                  }
>              ]
>          }
> +        ,{
> +            "path": "/app/finished",
> +            "operations": [
> +                {
> +                    "method": "GET",
> +                    "summary": "Check if application terminated",
> +                    "notes": "Check if application specified by thread ID
> tid finished. Invalid tid is reported as finished.",
> +                    "type": "string",
> +                    "nickname": "finished_app",
> +                    "produces": [
> +                        "application/json",
> +                        "application/xml"
> +                    ],
> +                    "parameters": [
> +                       {
> +                                     "name": "tid",
> +                                     "description": "application main
> thread ID",
> +                                     "required": true,
> +                                     "allowMultiple":false,
> +                                     "type":"string",
> +                                     "paramType":"query"
> +                               }
> +                    ]
> +                }
> +            ]
> +        }
>      ]
>  }
> diff --git a/modules/httpserver/api/app.cc b/modules/httpserver/api/app.cc
> index 694b00e..80882a5 100644
> --- a/modules/httpserver/api/app.cc
> +++ b/modules/httpserver/api/app.cc
> @@ -11,6 +11,7 @@
>  #include <boost/format.hpp>
>  #include <osv/commands.hh>
>  #include <osv/app.hh>
> +#include <osv/sched.hh>
>
>  namespace httpserver {
>
> @@ -51,12 +52,29 @@ static std::string exec_app(const std::string&
> cmnd_line) {
>  void init(routes& routes)
>  {
>      app_json_init_path("app API");
> +
>      run_app.set_handler([](const_req req) {
>          string command = req.get_query_param("command");
>          static std::string app_ids; // static, so that c_str data is not
> invalidated after return.
>          app_ids = exec_app(command);
>          return app_ids.c_str();
>      });
> +
> +    finished_app.set_handler([](const_req req) {
> +        std::string tid_str = req.get_query_param("tid");
> +        // TODO accept as input a space separated list too ?
> +        pid_t tid = std::stol(tid_str);
> +        // If the app did finish and was joined (or started detached),
> thread no longer exists.
> +        // Or such thread never existed. In both cases, report thread as
> finished.
> +        const char* th_finished = "1";
> +        sched::with_all_threads([&](sched::thread &t) {
> +            if (t.id() == tid && t.get_status() !=
> sched::thread::status::terminated) {
> +                    th_finished = "0";
> +            }
> +        });
>

Thanks. This would work but is inefficient (O(N) in the number of threads
in the system), which is worrying when you consider that you'll probably
poll this function many times to figure out when the thread exited (and
usually get a response that it didn't).

We have the sched::thread::find_by_id(tid) function which is much more
efficient (O(1)) in finding the thread with the given id.
But a drawback of that function is that theoretically it can return a
thread which will exit before you get around to testing it.

I think the solution is to create a function
sched::thread::with_thread_by_id(tid, func) which, like with_all_threads
holds the thread_map_mutex while running the given function, and use that.



> +        return th_finished;
> +    });
> +
>  }
>  }
>  }
> --
> 2.9.3
>
> --
> You received this message because you are subscribed to the Google Groups
> "OSv Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to