Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Encapsulate main function, argc and argv in a closure
[simgrid.git] / src / bindings / java / JavaContext.hpp
1 /* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_JAVA_JAVA_CONTEXT_HPP
8 #define SIMGRID_JAVA_JAVA_CONTEXT_HPP
9
10 #include <jni.h>
11
12 #include <xbt/misc.h>
13 #include <simgrid/simix.h>
14 #include <xbt/xbt_os_thread.h>
15
16 #include "src/simix/smx_private.hpp"
17
18 #include "jmsg.h"
19 #include "jmsg_process.h"
20
21 namespace simgrid {
22 namespace java {
23
24 class JavaContext;
25 class JavacontextFactory;
26
27 class JavaContext : public simgrid::simix::Context {
28 public:
29   // The java process instance bound with the msg process structure:
30   jobject jprocess = nullptr;
31   // JNI interface pointer associated to this thread:
32   JNIEnv *jenv = nullptr;
33   xbt_os_thread_t thread = nullptr;
34   // Sempahore used to schedule/yield the process:
35   xbt_os_sem_t begin = nullptr;
36   // Semaphore used to schedule/unschedule the process:
37   xbt_os_sem_t end = nullptr;
38 public:
39   friend class JavaContextFactory;
40   JavaContext(std::function<void()> code,
41           void_pfn_smxprocess_t cleanup_func,
42           smx_process_t process);
43   ~JavaContext() override;
44   void stop() override;
45   void suspend() override;
46   void resume();
47 private:
48   static void* wrapper(void *data);
49 };
50
51 class JavaContextFactory : public simgrid::simix::ContextFactory {
52 public:
53   JavaContextFactory();
54   ~JavaContextFactory() override;
55   JavaContext* self() override;
56   JavaContext* create_context(std::function<void()> code,
57     void_pfn_smxprocess_t, smx_process_t process) override;
58   void run_all() override;
59 };
60
61 XBT_PRIVATE simgrid::simix::ContextFactory* java_factory();
62 XBT_PRIVATE void java_main_jprocess(jobject process);
63
64 }
65 }
66
67 #endif                          /* !_XBT_CONTEXT_JAVA_H */