libcwdversion 2.0.0
The C++ Debugging Support Library
Loading...
Searching...
No Matches
Channel.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2000-2005, 2013, 2018, 2020, 2023, 2026 Carlo Wood
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
9
10#ifndef LIBCWD_CLASS_CHANNEL_H
11#define LIBCWD_CLASS_CHANNEL_H
12
13#include "control_flag.h"
14#include "max_label_len.h"
16#include "libcwd/config.h"
17
18#include <atomic>
19
20namespace libcwd {
21
22namespace _private_ {
23struct DebugChannels;
24struct ChannelSetsWat;
25} // namespace _private_
26
72
74{
75 private:
76 int index_;
77 // Assigned during initialization before this channel is made visible to other threads.
78 // A unique id that is used as index into the TSD array `off_cnt_array`.
79
80 char label_[max_label_len + 1]; // +1 for zero termination.
81 // Initialized before this channel is made visible to other threads and read-only
82 // afterward. A reference name for the represented debug channel This label will be
83 // printed in front of each output written to this debug channel.
84
85 bool initialized_;
86 // Written during initialization before this channel is made visible to other threads.
87 // Set to true when initialized.
88
89 static Channel const off_channel;
90 // Channel that is always off.
91
92 public:
93 //---------------------------------------------------------------------------
94 // Constructor
95 //
96
97 // MT: All channel objects must be global so that `initialized_` is false
98 // at the start of the program and initialization occurs before other threads
99 // share the object.
100 explicit Channel(char const* label, bool add_to_channel_list = true);
101
102 // MT: May only be called from the constructors of global objects (or single threaded functions).
103 void NS_initialize(char const* label, LIBCWD_TSD_PARAM, bool add_to_channel_list);
104 // Force initialization in case the constructor of this global object
105 // wasn't called yet. Does nothing when the object was already initialized.
106
107 private:
108 // MT: Take advantage of the public debug-channel registry write lock to prevent
109 // simultaneous access to ChannelSets::next_index_ in the case of simultaneously
110 // dlopen-loaded libraries and to create a happens-before edge between the write-once
111 // and potential reads of this class members by other threads.
112 friend struct _private_::DebugChannels;
113 void initialize(_private_::ChannelSetsWat wat, char const* label, size_t label_len);
114
115 public:
116 //---------------------------------------------------------------------------
117 // Manipulators
118 //
119
120 void off();
121 void on();
122
123 struct OnOffState
124 {
125 int off_cnt;
126 };
127
128 void force_on(OnOffState& state, char const* label);
129 void restore(OnOffState const& state);
130
131 Channel const& operator()(bool cond) const { return cond ? *this : off_channel; }
132
133 public:
134 //---------------------------------------------------------------------------
135 // Accessors
136 //
137
138 int index() const { return index_; }
139 char const* get_label() const;
140 bool is_on() const;
141 bool is_on(LIBCWD_TSD_PARAM) const;
142};
143
144} // namespace libcwd
145
146#endif // LIBCWD_CLASS_CHANNEL_H
Channel(char const *label, bool add_to_channel_list=true)
Construct a new debug channel with name label.
Definition Channel.inl.h:21
void off()
Turn this channel off.
Definition debug.cxx:1428
void on()
Cancel one call to off().
Definition debug.cxx:1439
Channel(char const *label, bool add_to_channel_list=true)
Construct a new debug channel with name label.
Definition Channel.inl.h:21
char const * get_label() const
Pointer to the label of the debug channel.
Definition Channel.inl.h:44
bool is_on() const
Returns true if the channel is active.
Definition Channel.inl.h:35
namespace for libcwd.
void initialize()
Initialize libcwd global state before normal dynamic initialization reaches libcw_do.
Definition debug.cxx:401
constexpr unsigned short max_label_len
The maximum number of characters that are allowed in a debug channel label.
Definition max_label_len.h:16