Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d3e41ae346be16737152f8071c79c8db255b397e
[simgrid.git] / src / sthread / sthread_impl.cpp
1 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
2
3 #include "smpi/smpi.h"
4 #include <simgrid/actor.h>
5 #include <simgrid/s4u/Actor.hpp>
6 #include <simgrid/s4u/Engine.hpp>
7 #include <simgrid/s4u/Mutex.hpp>
8 #include <simgrid/s4u/NetZone.hpp>
9 #include <xbt/base.h>
10 #include <xbt/sysdep.h>
11
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h"
14
15 #include <dlfcn.h>
16 #include <pthread.h>
17 #include <semaphore.h>
18 #include <sstream>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <thread>
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
24 namespace sg4 = simgrid::s4u;
25
26 static sg4::Host* lilibeth = nullptr;
27
28 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
29 {
30   std::ostringstream id;
31   id << std::this_thread::get_id();
32
33   XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
34
35   sg4::Engine e(&argc, argv);
36   auto* zone = sg4::create_full_zone("world");
37   lilibeth   = zone->create_host("Lilibeth", 1e15);
38   zone->seal();
39
40   /* Launch the user's main() on an actor */
41   sthread_enable();
42   sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
43
44   XBT_INFO("Starting the simulation.");
45   sg4::Engine::get_instance()->run();
46   XBT_INFO("All threads exited. Terminating the simulation.");
47
48   return 0;
49 }
50
51 struct sthread_mutex {
52   s4u_Mutex* mutex;
53 };
54
55 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
56 {
57 #if HAVE_SMPI
58   if (SMPI_is_inited())
59     SMPI_thread_create();
60 #endif
61   sthread_enable();
62   user_function(param);
63   sthread_disable();
64 }
65
66 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
67                    void* arg)
68 {
69   static int TID = 0;
70   TID++;
71   XBT_VERB("Create thread %d", TID);
72   int rank = 0;
73 #if HAVE_SMPI
74   if (SMPI_is_inited())
75     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
76 #endif
77   std::string name    = simgrid::xbt::string_printf("%d:%d", rank, TID);
78   sg4::ActorPtr actor = sg4::Actor::init(name.c_str(), lilibeth);
79   actor->start(thread_create_wrapper, start_routine, arg);
80
81   intrusive_ptr_add_ref(actor.get());
82   *thread = reinterpret_cast<unsigned long>(actor.get());
83   return 0;
84 }
85 int sthread_join(sthread_t thread, void** /*retval*/)
86 {
87   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
88   actor->join();
89   intrusive_ptr_release(actor.get());
90
91   return 0;
92 }
93
94 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
95 {
96   auto m = sg4::Mutex::create();
97   intrusive_ptr_add_ref(m.get());
98
99   mutex->mutex = m.get();
100   return 0;
101 }
102
103 int sthread_mutex_lock(sthread_mutex_t* mutex)
104 {
105   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
106   return 0;
107 }
108
109 int sthread_mutex_trylock(sthread_mutex_t* mutex)
110 {
111   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
112 }
113
114 int sthread_mutex_unlock(sthread_mutex_t* mutex)
115 {
116   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
117   return 0;
118 }
119 int sthread_mutex_destroy(sthread_mutex_t* mutex)
120 {
121   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
122   return 0;
123 }
124
125 #if 0
126 int sem_init(sem_t *sem, int pshared, unsigned int value) {
127         int res;
128
129         res=raw_sem_init(sem,pshared,value);
130         return res;
131 }
132
133 int sem_wait(sem_t *sem) {
134         int res;
135
136         res = raw_sem_wait(sem);
137         return res;
138 }
139
140 int sem_post(sem_t *sem) {
141         return raw_sem_post(sem);
142 }
143
144 int pthread_join(pthread_t thread, void **retval) {
145         sg_actor_join(thread, -1);
146     return 0;
147 }
148
149 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
150     *cond = sg_cond_init();
151     return 0;
152 }
153
154 int pthread_cond_signal(pthread_cond_t *cond) {
155         sg_cond_notify_one(*cond);
156     return 0;
157 }
158
159 int pthread_cond_broadcast(pthread_cond_t *cond) {
160         sg_cond_notify_all(*cond);
161     return 0;
162 }
163
164 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
165         sg_cond_wait(*cond, *mutex);
166     return 0;
167 }
168
169 int pthread_cond_destroy(pthread_cond_t *cond) {
170         sg_cond_destroy(*cond);
171     return 0;
172 }
173 #endif