Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/Onesphore/simgrid into Onesphore-master
[simgrid.git] / src / kernel / activity / MutexImpl.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/activity/MutexImpl.hpp"
7 #include "src/kernel/activity/SynchroRaw.hpp"
8 #include "src/simix/smx_synchro_private.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_mutex, simix_synchro, "Mutex kernel-space implementation");
11
12 namespace simgrid {
13 namespace kernel {
14 namespace activity {
15
16 MutexImpl::MutexImpl() : piface_(this)
17 {
18   XBT_IN("(%p)", this);
19   XBT_OUT();
20 }
21
22 MutexImpl::~MutexImpl()
23 {
24   XBT_IN("(%p)", this);
25   XBT_OUT();
26 }
27
28 void MutexImpl::lock(smx_actor_t issuer)
29 {
30   XBT_IN("(%p; %p)", this, issuer);
31   /* FIXME: check where to validate the arguments */
32   smx_activity_t synchro = nullptr;
33
34   if (this->locked) {
35     /* FIXME: check if the host is active ? */
36     /* Somebody using the mutex, use a synchronization to get host failures */
37     synchro = SIMIX_synchro_wait(issuer->host_, -1);
38     synchro->simcalls_.push_back(&issuer->simcall);
39     issuer->waiting_synchro = synchro;
40     this->sleeping.push_back(*issuer);
41   } else {
42     /* mutex free */
43     this->locked = true;
44     this->owner  = issuer;
45     SIMIX_simcall_answer(&issuer->simcall);
46   }
47   XBT_OUT();
48 }
49
50 /** Tries to lock the mutex for a process
51  *
52  * \param  issuer  the process that tries to acquire the mutex
53  * \return whether we managed to lock the mutex
54  */
55 bool MutexImpl::try_lock(smx_actor_t issuer)
56 {
57   XBT_IN("(%p, %p)", this, issuer);
58   if (this->locked) {
59     XBT_OUT();
60     return false;
61   }
62
63   this->locked = true;
64   this->owner  = issuer;
65   XBT_OUT();
66   return true;
67 }
68
69 /** Unlock a mutex for a process
70  *
71  * Unlocks the mutex and gives it to a process waiting for it.
72  * If the unlocker is not the owner of the mutex nothing happens.
73  * If there are no process waiting, it sets the mutex as free.
74  */
75 void MutexImpl::unlock(smx_actor_t issuer)
76 {
77   XBT_IN("(%p, %p)", this, issuer);
78   if (not this->locked)
79     THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
80
81   /* If the mutex is not owned by the issuer, that's not good */
82   if (issuer != this->owner)
83     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
84            this->owner->get_cname(), this->owner->pid_);
85
86   if (not this->sleeping.empty()) {
87     /*process to wake up */
88     smx_actor_t p = &this->sleeping.front();
89     this->sleeping.pop_front();
90     p->waiting_synchro = nullptr;
91     this->owner        = p;
92     SIMIX_simcall_answer(&p->simcall);
93   } else {
94     /* nobody to wake up */
95     this->locked = false;
96     this->owner  = nullptr;
97   }
98   XBT_OUT();
99 }
100 }
101 }
102 }
103
104 /** Increase the refcount for this mutex */
105 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
106 {
107   if (mutex != nullptr)
108     intrusive_ptr_add_ref(mutex);
109   return mutex;
110 }
111
112 /** Decrease the refcount for this mutex */
113 void SIMIX_mutex_unref(smx_mutex_t mutex)
114 {
115   if (mutex != nullptr)
116     intrusive_ptr_release(mutex);
117 }
118
119 // Simcall handlers:
120
121 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
122 {
123   mutex->lock(simcall->issuer);
124 }
125
126 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
127 {
128   return mutex->try_lock(simcall->issuer);
129 }
130
131 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
132 {
133   mutex->unlock(simcall->issuer);
134 }