Skip to content

Commit 4eb5855

Browse files
committed
added gthreads.h for mt examples and to fix linux errors
1 parent 1738ea0 commit 4eb5855

9 files changed

Lines changed: 58 additions & 95 deletions

File tree

.github/workflows/build_and_test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
# Per-arch build jobs
5353
# ===========================
5454
build_amd64:
55-
name: Build amd64 from ${{ matrix.image }}
55+
name: Build amd64 from ${{ matrix.osname }}
5656
needs: [ overview, discover ]
5757
runs-on: ubuntu-latest
5858
permissions:
@@ -100,6 +100,8 @@ jobs:
100100
- name: Build & Push (amd64 only)
101101
uses: docker/build-push-action@v5
102102
with:
103+
pull: true
104+
no-cache: true
103105
build-args: |
104106
UPSTREAM_REV=${{ env.UPSTREAM_REV }}
105107
context: .

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
actions: read
2929
contents: read
3030

31-
container: jeffersonlab/geant4:g4v11.3.1-almalinux94
31+
container: ghcr.io/gemc/g4install:11.3.2-ubuntu-24.04
3232

3333
strategy:
3434
fail-fast: false

ci/distros_tags.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ build_matrix() {
1616
json=$(
1717
cat <<EOF
1818
{"include":[
19-
{"osnmae":"ubuntu", "image":"$(get_geant4_tag)-ubuntu-$(get_ubuntu_lts)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
20-
{"osnmae":"fedora", "image":"$(get_geant4_tag)-fedora-$(get_fedora_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
21-
{"osnmae":"almalinux", "image":"$(get_geant4_tag)-almalinux-$(get_almalinux_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
22-
{"osnmae":"debian", "image":"$(get_geant4_tag)-debian-$(get_debian_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"}
19+
{"osname":"ubuntu", "image":"$(get_geant4_tag)-ubuntu-$(get_ubuntu_lts)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
20+
{"osname":"fedora", "image":"$(get_geant4_tag)-fedora-$(get_fedora_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
21+
{"osname":"almalinux", "image":"$(get_geant4_tag)-almalinux-$(get_almalinux_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"},
22+
{"osname":"debian", "image":"$(get_geant4_tag)-debian-$(get_debian_latest)", "gemc_tag":"$(get_gemc_tag)", "geant4_tag":"$(get_geant4_tag)"}
2323
]}
2424
EOF
2525
)

gdata/examples/gdata_event_example.cc

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,7 @@
2727

2828
// gemc
2929
#include "glogger.h"
30-
31-
32-
// TODO: remove when C++20 is widely available
33-
// ===== portable jthread-like wrapper =========================================
34-
// If real std::jthread is present, use it. Otherwise, define a minimal shim
35-
// that joins in the destructor (no stop_token support, but good enough here).
36-
#if defined(__cpp_lib_jthread) // header exists
37-
#include <jthread>
38-
using jthread_alias = std::jthread;
39-
#else
40-
// join: pause right here until that thread is finished.
41-
class jthread_alias : public std::thread {
42-
public:
43-
using std::thread::thread; // inherit all ctors
44-
~jthread_alias() { if (joinable()) join(); }
45-
jthread_alias(jthread_alias&&) noexcept = default;
46-
jthread_alias& operator=(jthread_alias&&) noexcept = default;
47-
// no copy
48-
jthread_alias(const jthread_alias&) = delete;
49-
jthread_alias& operator=(const jthread_alias&) = delete;
50-
};
51-
#endif
30+
#include "gthreads.h"
5231

5332
auto run_simulation_in_threads(int nevents,
5433
int nthreads,

gdetector/examples/gdetector_example.cc

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,17 @@
66
#include "glogger.h"
77
#include "event/gEventDataCollection.h"
88
#include "gdynamicdigitizationConventions.h"
9+
#include "gthreads.h"
910

1011
// geant4
1112
#include "G4RunManagerFactory.hh"
1213
#include "QBBC.hh"
1314

1415
// c++
1516
#include <atomic> // std::atomic<T>: lock-free, thread-safe integers, flags…
16-
//#include <jthread> // C++20 std::jthread: joins automatically in its dtor (destructor)
17-
#include <thread> // replaces <jthread> until C++20 is widely available. remove this line when <jthread> is available.
1817
#include <vector>
1918
#include <memory> // smart pointers
2019

21-
// TODO: remove when C++20 is widely available
22-
// ===== portable jthread-like wrapper =========================================
23-
// If real std::jthread is present, use it. Otherwise, define a minimal shim
24-
// that joins in the destructor (no stop_token support, but good enough here).
25-
#if defined(__cpp_lib_jthread) // header exists
26-
#include <jthread>
27-
using jthread_alias = std::jthread;
28-
#else
29-
// join: pause right here until that thread is finished.
30-
class jthread_alias : public std::thread {
31-
public:
32-
using std::thread::thread; // inherit all ctors
33-
~jthread_alias() { if (joinable()) join(); }
34-
jthread_alias(jthread_alias&&) noexcept = default;
35-
jthread_alias& operator=(jthread_alias&&) noexcept = default;
36-
// no copy
37-
jthread_alias(const jthread_alias&) = delete;
38-
jthread_alias& operator=(const jthread_alias&) = delete;
39-
};
40-
#endif
41-
42-
const std::string plugin_name = "test_gdynamic_plugin";
4320

4421
auto run_simulation_in_threads(int nevents,
4522
int nthreads,

gdynamicDigitization/examples/plugin_load_example.cc

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,7 @@
99
// gemc
1010
#include "gfactory.h"
1111
#include "event/gEventDataCollection.h"
12-
13-
// TODO: remove when C++20 is widely available
14-
// ===== portable jthread-like wrapper =========================================
15-
// If real std::jthread is present, use it. Otherwise, define a minimal shim
16-
// that joins in the destructor (no stop_token support, but good enough here).
17-
#if defined(__cpp_lib_jthread) // header exists
18-
#include <jthread>
19-
using jthread_alias = std::jthread;
20-
#else
21-
// join: pause right here until that thread is finished.
22-
class jthread_alias : public std::thread {
23-
public:
24-
using std::thread::thread; // inherit all ctors
25-
~jthread_alias() { if (joinable()) join(); }
26-
jthread_alias(jthread_alias&&) noexcept = default;
27-
jthread_alias& operator=(jthread_alias&&) noexcept = default;
28-
// no copy
29-
jthread_alias(const jthread_alias&) = delete;
30-
jthread_alias& operator=(const jthread_alias&) = delete;
31-
};
32-
#endif
12+
#include "gthreads.h"
3313

3414
const std::string plugin_name = "test_gdynamic_plugin";
3515

gstreamer/examples/gstreamer_example.cc

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,16 @@
55
#include "glogger.h"
66
#include "gdynamicdigitization.h"
77
#include "gutilities.h"
8+
#include "gthreads.h"
89

910
// c++
1011
#include <atomic> // std::atomic<T>: lock-free, thread-safe integers, flags…
11-
//#include <jthread> // C++20 std::jthread: joins automatically in its dtor (destructor)
12-
#include <thread> // replaces <jthread> until C++20 is widely available. remove this line when <jthread> is available.
1312
#include <ranges> // std::views::iota – range of integers 0,1,…,n-1
1413
#include <vector>
1514
#include <memory> // smart pointers
1615
#include <unordered_map>
1716

18-
// TODO: remove when C++20 is widely available
19-
// ===== portable jthread-like wrapper =========================================
20-
// If real std::jthread is present, use it. Otherwise, define a minimal shim
21-
// that joins in the destructor (no stop_token support, but good enough here).
22-
#if defined(__cpp_lib_jthread) // header exists
23-
#include <jthread>
24-
using jthread_alias = std::jthread;
25-
#else
26-
// join: pause right here until that thread is finished.
27-
class jthread_alias : public std::thread {
28-
public:
29-
using std::thread::thread; // inherit all ctors
30-
~jthread_alias() { if (joinable()) join(); }
31-
jthread_alias(jthread_alias&&) noexcept = default;
32-
jthread_alias& operator=(jthread_alias&&) noexcept = default;
33-
// no copy
34-
jthread_alias(const jthread_alias&) = delete;
35-
jthread_alias& operator=(const jthread_alias&) = delete;
36-
};
37-
#endif
17+
3818

3919
const std::string plugin_name = "test_gdynamic_plugin";
4020

guts/gthreads.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
// 1) Prefer to see feature macros if available
4+
#if defined(__has_include)
5+
# if __has_include(<version>)
6+
# include <version>
7+
# endif
8+
#endif
9+
10+
#include <thread>
11+
12+
// 2) Use std::jthread when the library actually ships it
13+
#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
14+
using jthread_alias = std::jthread;
15+
16+
#else
17+
// Fallback: RAII "join-on-destruction" wrapper.
18+
// Use composition (safer than inheriting from std::thread).
19+
class jthread_alias {
20+
std::thread t_;
21+
public:
22+
jthread_alias() noexcept = default;
23+
24+
template<class F, class... Args>
25+
explicit jthread_alias(F&& f, Args&&... args)
26+
: t_(std::forward<F>(f), std::forward<Args>(args)...) {}
27+
28+
jthread_alias(jthread_alias&&) noexcept = default;
29+
jthread_alias& operator=(jthread_alias&&) noexcept = default;
30+
31+
jthread_alias(const jthread_alias&) = delete;
32+
jthread_alias& operator=(const jthread_alias&) = delete;
33+
34+
~jthread_alias() { if (t_.joinable()) t_.join(); }
35+
36+
// minimal forwarding API
37+
bool joinable() const noexcept { return t_.joinable(); }
38+
void join() { t_.join(); }
39+
void detach() { t_.detach(); }
40+
std::thread::id get_id() const noexcept { return t_.get_id(); }
41+
auto native_handle() { return t_.native_handle(); }
42+
void swap(jthread_alias& other) noexcept { t_.swap(other.t_); }
43+
};
44+
#endif

guts/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LD += {
55
'gutilities.cc'
66
),
77
'headers' : files(
8+
'gthreads.h',
89
'gutilities.h',
910
'gutsConventions.h'
1011
),

0 commit comments

Comments
 (0)