Skip to content

Commit

Permalink
Change __sync_* to std::atomic and remove unneeded code (#266)
Browse files Browse the repository at this point in the history
Update __sync_* to std::atomic.
  • Loading branch information
rakshasa authored Dec 2, 2024
1 parent e7199d5 commit 1f350a3
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 435 deletions.
38 changes: 1 addition & 37 deletions src/thread_disk.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <rak/timer.h>
Expand Down Expand Up @@ -67,7 +31,7 @@ thread_disk::call_events() {
if ((m_flags & flag_did_shutdown))
throw internal_error("Already trigged shutdown.");

__sync_or_and_fetch(&m_flags, flag_did_shutdown);
m_flags |= flag_did_shutdown;
throw shutdown_exception();
}

Expand Down
36 changes: 0 additions & 36 deletions src/thread_main.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <rak/timer.h>
Expand Down
36 changes: 0 additions & 36 deletions src/thread_main.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#ifndef LIBTORRENT_THREAD_MAIN_H
#define LIBTORRENT_THREAD_MAIN_H

Expand Down
38 changes: 0 additions & 38 deletions src/torrent/poll_epoll.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <cerrno>
Expand Down Expand Up @@ -203,13 +167,11 @@ PollEPoll::do_poll(int64_t timeout_usec, int flags) {

if (!(flags & poll_worker_thread)) {
thread_base::release_global_lock();
thread_base::entering_main_polling();
}

int status = poll((timeout.usec() + 999) / 1000);

if (!(flags & poll_worker_thread)) {
thread_base::leaving_main_polling();
thread_base::acquire_global_lock();
}

Expand Down
38 changes: 0 additions & 38 deletions src/torrent/poll_kqueue.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.213
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <cerrno>
Expand Down Expand Up @@ -262,13 +226,11 @@ PollKQueue::do_poll(int64_t timeout_usec, int flags) {

if (!(flags & poll_worker_thread)) {
thread_base::release_global_lock();
thread_base::entering_main_polling();
}

int status = poll((timeout.usec() + 999) / 1000);

if (!(flags & poll_worker_thread)) {
thread_base::leaving_main_polling();
thread_base::acquire_global_lock();
}

Expand Down
40 changes: 1 addition & 39 deletions src/torrent/poll_select.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <algorithm>
Expand Down Expand Up @@ -243,14 +207,12 @@ PollSelect::do_poll(int64_t timeout_usec, int flags) {
timeval t = timeout.tval();

if (!(flags & poll_worker_thread)) {
thread_base::entering_main_polling();
thread_base::release_global_lock();
}

int status = select(maxFd + 1, read_set, write_set, error_set, &t);

if (!(flags & poll_worker_thread)) {
thread_base::leaving_main_polling();
thread_base::acquire_global_lock();
}

Expand All @@ -273,7 +235,7 @@ log_poll_open(Event* event) {

if (log_fd == -1) {
snprintf(buffer, 256, LT_LOG_POLL_OPEN, getpid());

if ((log_fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC)) == -1)
throw internal_error("Could not open poll open log file.");
}
Expand Down
25 changes: 13 additions & 12 deletions src/torrent/utils/signal_bitfield.cc
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
#include "config.h"

#include "torrent/exceptions.h"

#include "signal_bitfield.h"

#include <cassert>
#include "torrent/exceptions.h"

namespace torrent {

const unsigned int signal_bitfield::max_size;

// Only the thread owning this signal bitfield should add signals.
unsigned int
signal_bitfield::add_signal(slot_type slot) {
if (m_thread_id != std::this_thread::get_id())
throw internal_error("signal_bitfield::add_signal(...): Only the owning thread can add signals.");

if (m_size >= max_size)
throw internal_error("signal_bitfield::add_signal(...): No more available slots.");

if (!slot)
throw internal_error("signal_bitfield::add_signal(...): Cannot add empty slot.");

unsigned int index = m_size;
__sync_add_and_fetch(&m_size, 1);

unsigned int index = m_size++;
m_slots[index] = slot;

return index;
}

void
signal_bitfield::work() {
bitfield_type bitfield;
// static_assert(std::atomic<bitfield_type>()::is_always_lock_free(), "signal_bitfield::work(...): Bitfield type is not lockfree.");

while (!__sync_bool_compare_and_swap(&m_bitfield, (bitfield = m_bitfield), 0))
; // Do nothing.
if (m_thread_id != std::this_thread::get_id())
throw internal_error("signal_bitfield::work(...): Only the owning thread can do work for signal bitfields.");

unsigned int i = 0;
auto bitfield = m_bitfield.exchange(0);

while (bitfield) {
for (unsigned int i = 0; bitfield != 0 && i < m_size; i++) {
if ((bitfield & (1 << i))) {
m_slots[i]();
bitfield = bitfield & ~(1 << i);
}

i++;
}
}

Expand Down
Loading

0 comments on commit 1f350a3

Please sign in to comment.