1 /* Copyright (c) 2006-201. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/msg/msg_private.hpp"
7 #include "src/simix/smx_synchro_private.hpp"
10 #include "simgrid/s4u/Semaphore.hpp"
15 Semaphore::Semaphore(unsigned int initial_capacity)
17 sem_ = simgrid::simix::simcall([initial_capacity] { return SIMIX_sem_init(initial_capacity); });
20 Semaphore::~Semaphore()
22 SIMIX_sem_destroy(sem_);
25 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
27 return SemaphorePtr(new Semaphore(initial_capacity));
30 void Semaphore::acquire()
32 simcall_sem_acquire(sem_);
35 void Semaphore::release()
37 simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); });
40 void intrusive_ptr_add_ref(Semaphore* sem)
43 sem->refcount_.fetch_add(1, std::memory_order_relaxed);
46 void intrusive_ptr_release(Semaphore* sem)
49 if (sem->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
50 std::atomic_thread_fence(std::memory_order_acquire);