Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
191094aeb990471f601a88699807bd9d45ab9aaf
[simgrid.git] / src / s4u / s4u_Semaphore.cpp
1 /* Copyright (c) 2006-201. 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/msg/msg_private.hpp"
7 #include "src/simix/smx_synchro_private.hpp"
8 #include "xbt/log.h"
9
10 #include "simgrid/s4u/Semaphore.hpp"
11
12 namespace simgrid {
13 namespace s4u {
14
15 Semaphore::Semaphore(unsigned int initial_capacity)
16 {
17     sem_ = simgrid::simix::simcall([initial_capacity] { return SIMIX_sem_init(initial_capacity); });
18 }
19
20 Semaphore::~Semaphore()
21 {
22     SIMIX_sem_destroy(sem_);
23 }
24
25 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
26 {
27     return SemaphorePtr(new Semaphore(initial_capacity));
28 }
29
30 void Semaphore::acquire()
31 {
32     simcall_sem_acquire(sem_);
33 }
34
35 void Semaphore::release()
36 {
37     simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); });
38 }
39
40 void intrusive_ptr_add_ref(Semaphore* sem)
41 {
42   xbt_assert(sem);
43   sem->refcount_.fetch_add(1, std::memory_order_relaxed);
44 }
45
46 void intrusive_ptr_release(Semaphore* sem)
47 {
48   xbt_assert(sem);
49   if (sem->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
50     std::atomic_thread_fence(std::memory_order_acquire);
51     delete sem;
52   }
53 }
54
55 }
56 }