Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into CRTP
[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(actor::ActorImpl* issuer)
28 {
29   XBT_IN("(%p; %p)", this, issuer);
30   /* FIXME: check where to validate the arguments */
31   RawImplPtr synchro = nullptr;
32
33   if (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());
37     (*synchro).set_host(issuer->get_host()).start();
38     synchro->simcalls_.push_back(&issuer->simcall);
39     issuer->waiting_synchro = synchro;
40     sleeping_.push_back(*issuer);
41   } else {
42     /* mutex free */
43     locked_ = true;
44     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(actor::ActorImpl* issuer)
56 {
57   XBT_IN("(%p, %p)", this, issuer);
58   if (locked_) {
59     XBT_OUT();
60     return false;
61   }
62
63   locked_ = true;
64   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(actor::ActorImpl* issuer)
76 {
77   XBT_IN("(%p, %p)", this, issuer);
78   if (not 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 != owner_)
83     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
84            owner_->get_cname(), owner_->get_pid());
85
86   if (not sleeping_.empty()) {
87     /*process to wake up */
88     actor::ActorImpl* p = &sleeping_.front();
89     sleeping_.pop_front();
90     p->waiting_synchro = nullptr;
91     owner_             = p;
92     SIMIX_simcall_answer(&p->simcall);
93   } else {
94     /* nobody to wake up */
95     locked_ = false;
96     owner_  = nullptr;
97   }
98   XBT_OUT();
99 }
100 /** Increase the refcount for this mutex */
101 MutexImpl* MutexImpl::ref()
102 {
103   intrusive_ptr_add_ref(this);
104   return this;
105 }
106
107 /** Decrease the refcount for this mutex */
108 void MutexImpl::unref()
109 {
110   intrusive_ptr_release(this);
111 }
112
113 } // namespace activity
114 } // namespace kernel
115 } // namespace simgrid
116
117 // Simcall handlers:
118
119 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
120 {
121   mutex->lock(simcall->issuer);
122 }
123
124 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
125 {
126   return mutex->try_lock(simcall->issuer);
127 }
128
129 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
130 {
131   mutex->unlock(simcall->issuer);
132 }