Logo AND Algorithmique Numérique Distribuée

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