Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b7d89726ffc083d9183434a92cd710699fb9bb43
[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 = NULL;
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_INFO("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("sthread main() is launching the simulation");
45   sg4::Engine::get_instance()->run();
46
47   return 0;
48 }
49
50 struct sthread_mutex {
51   s4u_Mutex* mutex;
52 };
53
54 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
55 {
56 #if HAVE_SMPI
57   if (SMPI_is_inited())
58     SMPI_thread_create();
59 #endif
60   sthread_enable();
61   user_function(param);
62   sthread_disable();
63 }
64
65 int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
66                    void* arg)
67 {
68   static int TID = 0;
69   TID++;
70   XBT_INFO("Create thread %d", TID);
71   int rank = 0;
72 #if HAVE_SMPI
73   if (SMPI_is_inited())
74     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
75 #endif
76   std::string name    = simgrid::xbt::string_printf("%d:%d", rank, TID);
77   sg4::ActorPtr actor = sg4::Actor::init(name.c_str(), lilibeth);
78   actor->start(thread_create_wrapper, start_routine, arg);
79
80   intrusive_ptr_add_ref(actor.get());
81   *thread = reinterpret_cast<unsigned long>(actor.get());
82   return 0;
83 }
84 int sthread_join(sthread_t thread, void** retval)
85 {
86   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
87   actor->join();
88   intrusive_ptr_release(actor.get());
89
90   return 0;
91 }
92
93 int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
94 {
95   auto m = sg4::Mutex::create();
96   intrusive_ptr_add_ref(m.get());
97
98   mutex->mutex = m.get();
99   return 0;
100 }
101
102 int sthread_mutex_lock(sthread_mutex_t* mutex)
103 {
104   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
105   return 0;
106 }
107
108 int sthread_mutex_trylock(sthread_mutex_t* mutex)
109 {
110   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
111 }
112
113 int sthread_mutex_unlock(sthread_mutex_t* mutex)
114 {
115   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
116   return 0;
117 }
118 int sthread_mutex_destroy(sthread_mutex_t* mutex)
119 {
120   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
121   return 0;
122 }
123
124 #if 0
125 int sem_init(sem_t *sem, int pshared, unsigned int value) {
126         int res;
127
128         res=raw_sem_init(sem,pshared,value);
129         return res;
130 }
131
132 int sem_wait(sem_t *sem) {
133         int res;
134
135         res = raw_sem_wait(sem);
136         return res;
137 }
138
139 int sem_post(sem_t *sem) {
140         return raw_sem_post(sem);
141 }
142
143 int pthread_join(pthread_t thread, void **retval) {
144         sg_actor_join(thread, -1);
145     return 0;
146 }
147
148 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
149     *cond = sg_cond_init();
150     return 0;
151 }
152
153 int pthread_cond_signal(pthread_cond_t *cond) {
154         sg_cond_notify_one(*cond);
155     return 0;
156 }
157
158 int pthread_cond_broadcast(pthread_cond_t *cond) {
159         sg_cond_notify_all(*cond);
160     return 0;
161 }
162
163 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
164         sg_cond_wait(*cond, *mutex);
165     return 0;
166 }
167
168 int pthread_cond_destroy(pthread_cond_t *cond) {
169         sg_cond_destroy(*cond);
170     return 0;
171 }
172 #endif