Logo AND Algorithmique Numérique Distribuée

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