BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_threadattributes.h
Go to the documentation of this file.
1/// @file bslmt_threadattributes.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_threadattributes.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_THREADATTRIBUTES
9#define INCLUDED_BSLMT_THREADATTRIBUTES
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_threadattributes bslmt_threadattributes
15/// @brief Provide a description of the attributes of a thread.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_threadattributes
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_threadattributes-purpose"> Purpose</a>
25/// * <a href="#bslmt_threadattributes-classes"> Classes </a>
26/// * <a href="#bslmt_threadattributes-description"> Description </a>
27/// * <a href="#bslmt_threadattributes-attributes"> Attributes </a>
28/// * <a href="#bslmt_threadattributes-detachedstate-attribute"> detachedState Attribute </a>
29/// * <a href="#bslmt_threadattributes-stacksize-attribute"> stackSize Attribute </a>
30/// * <a href="#bslmt_threadattributes-guardsize-attribute"> guardSize Attribute </a>
31/// * <a href="#bslmt_threadattributes-inheritschedule-attribute"> inheritSchedule Attribute </a>
32/// * <a href="#bslmt_threadattributes-schedulingpolicy-attribute"> schedulingPolicy Attribute </a>
33/// * <a href="#bslmt_threadattributes-schedulingpriority-attribute"> schedulingPriority Attribute </a>
34/// * <a href="#bslmt_threadattributes-threadname-attribute"> threadName Attribute </a>
35/// * <a href="#bslmt_threadattributes-fluent-interface"> Fluent Interface </a>
36/// * <a href="#bslmt_threadattributes-usage"> Usage </a>
37/// * <a href="#bslmt_threadattributes-example-1-creating-and-modifying-thread-attributes-objects"> Example 1: Creating and Modifying Thread Attributes Objects </a>
38///
39/// # Purpose {#bslmt_threadattributes-purpose}
40/// Provide a description of the attributes of a thread.
41///
42/// # Classes {#bslmt_threadattributes-classes}
43///
44/// - bslmt::ThreadAttributes: description of the attributes of a thread
45///
46/// @see bslmt_threadutil, bslmt_configuration
47///
48/// # Description {#bslmt_threadattributes-description}
49/// This component provides a simply constrained (value-semantic)
50/// attribute class, `bslmt::ThreadAttributes`, for describing attributes of a
51/// thread in a platform-independent way.
52///
53/// ## Attributes {#bslmt_threadattributes-attributes}
54///
55///
56/// The default values and constraints for the attributes provided by
57/// `bslmt::ThreadAttributes` are listed in the following two tables:
58/// @code
59/// Name Type Default
60/// ------------------ --------------------- ----------------------
61/// detachedState enum DetachedState e_CREATE_JOINABLE
62/// stackSize int e_UNSET_STACK_SIZE
63/// guardSize int e_UNSET_GUARD_SIZE
64/// inheritSchedule bool 'true'
65/// schedulingPolicy enum SchedulingPolicy e_SCHED_DEFAULT
66/// schedulingPriority int e_UNSET_PRIORITY
67/// threadName bsl::string ""
68///
69/// Name Constraint
70/// --------- ---------------------------------------------------
71/// stackSize 'e_UNSET_STACK_SIZE == stackSize || 0 <= stackSize'
72/// guardSize 'e_UNSET_GUARD_SIZE == guardSize || 0 <= guardSize'
73/// @endcode
74///
75/// ### detachedState Attribute {#bslmt_threadattributes-detachedstate-attribute}
76///
77///
78/// The `detachedState` attribute indicates whether an associated thread should
79/// be created in a joinable or detached state, through the enum values
80/// `e_CREATE_JOINABLE` and `e_CREATE_DETACHED`, respectively. A thread in the
81/// joinable state will have its exit status maintained after thread
82/// termination. Any thread can join with a joinable thread (see
83/// @ref bslmt_threadutil ), in which case the joining thread will block, waiting
84/// for the joined thread's execution to complete, after which the joined
85/// thread's termination status will be reported back to the joining thread, and
86/// its resources reclaimed. A thread in a detached state will have its
87/// resources claimed at thread termination, and cannot be joined. Note that a
88/// joinable thread can be made detached after it is created, but not vice
89/// versa.
90///
91/// ### stackSize Attribute {#bslmt_threadattributes-stacksize-attribute}
92///
93///
94/// The `stackSize` attribute indicates the size, in bytes, of the stack that
95/// should be provided to a newly created thread. If the stack size is
96/// `e_UNSET_STACK_SIZE` then a created thread will be provided a default stack
97/// size (see @ref bslmt_configuration ). The `stackSize` attribute should be
98/// interpreted to mean that a created thread can safely define an automatic
99/// variable of the configured `stackSize` bytes in its thread-entry function.
100/// Note that, on some platforms, an adjusted value derived from the `stackSize`
101/// attribute may be supplied to the underlying representation by the thread
102/// creation function.
103///
104/// ### guardSize Attribute {#bslmt_threadattributes-guardsize-attribute}
105///
106///
107/// The `guardSize` attribute indicates the size of the memory region to provide
108/// past the end of a created thread's stack to protect against stack overflows.
109/// If a thread's stack pointer overflows into a guard area, the task will
110/// receive an error (e.g., a signal). If `guardSize` is `e_UNSET_GUARD_SIZE`,
111/// then a created thread will be provided with a default native guard size (see
112/// @ref bslmt_configuration ). Note that the interpretation of `guardSize` may
113/// vary among platforms, and the value may be adjusted up (e.g., by rounding up
114/// to a multiple of page size) or ignored entirely (e.g., the Windows platform
115/// does not support this attribute).
116///
117/// ### inheritSchedule Attribute {#bslmt_threadattributes-inheritschedule-attribute}
118///
119///
120/// The `inheritSchedule` attribute, if `true`, indicates that a created
121/// thread's `schedulingPolicy` and `schedulingPriority` attributes should be
122/// taken from its parent thread and the configured values of those thread
123/// attributes should be ignored. If `inheritSchedule` is `false`, then the
124/// `schedulingPolicy` and `schedulingPriority` attribute values should be used
125/// to configure a thread. See @ref bslmt_threadutil for information about support
126/// for this attribute.
127///
128/// ### schedulingPolicy Attribute {#bslmt_threadattributes-schedulingpolicy-attribute}
129///
130///
131/// The `schedulingPolicy` attribute indicates the policy that should be used to
132/// schedule the created thread for execution. Typically clients should use the
133/// default platform supplied scheduling policy, which is indicated by the
134/// `e_SCHED_DEFAULT` value. The alternative scheduling policies,
135/// `e_THREAD_FIFO` and `e_SCHED_RR`, are "real-time" scheduling policies, and
136/// may not be available unless the task is run with the appropriate privileges.
137/// `e_SCHED_FIFO` indicates a thread should run until it either yields or is
138/// interrupted by a thread of higher priority. `e_SCHED_RR` is the same as
139/// `e_SCHED_FIFO`, except that the created thread may be interrupted by a ready
140/// thread of equal priority after a finite time-slice. This attribute is
141/// ignored unless `inheritSchedule` is `false`. See @ref bslmt_threadutil for
142/// information about support for this attribute.
143///
144/// ### schedulingPriority Attribute {#bslmt_threadattributes-schedulingpriority-attribute}
145///
146///
147/// The `schedulingPriority` attribute is a platform specific value whose valid
148/// values range from the minimum to the maximum value for the associated
149/// `schedulingPolicy`, with higher numbers indicating a more urgent priority.
150/// Functions to obtain the minimum and maximum values are in this component and
151/// @ref bslmt_threadutil . This attribute is ignored unless `inheritSchedule` is
152/// `false`. See @ref bslmt_threadutil for information about support for this
153/// attribute.
154///
155/// ### threadName Attribute {#bslmt_threadattributes-threadname-attribute}
156///
157///
158/// The `threadName` attribute indicates the name the thread is to have. Thread
159/// names show up in debuggers on some platforms, and are unsupported on others.
160/// Thread names have unlimited lengths in a thread attributes object, but the
161/// thread names actually supported by specific platforms may have limited
162/// length, depending upon the platform, so thread names may be truncated when
163/// assigned to the actual thread. At this time, only Linux, Solaris, Darwin,
164/// and Windows support thread names. Unix platforms have a maximum thread name
165/// length of 15, while on Windows, the limit is 32767, or `(1 << 15) - 1`
166/// characters.
167///
168/// ## Fluent Interface {#bslmt_threadattributes-fluent-interface}
169///
170///
171/// `bslmt::ThreadAttributes` provides manipulators that return a non-`const`
172/// reference to the object so that setting individual attributes can be
173/// "chained" into a single expression statement, or that attributes can be
174/// "built" in place as a function argument. For example:
175/// @code
176/// bslmt::ThreadUtil::Handle handle;
177///
178/// int status = bslmt::ThreadUtil::create(
179/// &handle,
180/// bslmt::ThreadAttributes().setThreadName("myName")
181/// .setInheritSchedule(true),
182/// myThreadFunction,
183/// &myThreadArgument);
184/// @endcode
185///
186/// ## Usage {#bslmt_threadattributes-usage}
187///
188///
189/// This section illustrates intended use of this component.
190///
191/// ### Example 1: Creating and Modifying Thread Attributes Objects {#bslmt_threadattributes-example-1-creating-and-modifying-thread-attributes-objects}
192///
193///
194/// In this example we will demonstrate creating and configuring a
195/// `bslmt::ThreadAttributes` object, then using it with a hypothetical
196/// thread-creation function. Finally we show how a thread creation function
197/// might interpret those attributes for the underlying operating system.
198///
199/// First we forward declare a routine that we will use to create a thread:
200/// @code
201/// // Spawn a thread having properties described by the specified
202/// // `attributes` and that runs the specified `function`, and assign a
203/// // handle referring to the spawned thread to the specified
204/// // `*threadHandle`.
205/// void myThreadCreate(int *threadHandle,
206/// const bslmt::ThreadAttributes& attributes,
207/// void (*function)());
208/// @endcode
209/// Then, we declare two routines that will return the minimum and maximum
210/// thread priority given a scheduling policy. Note that similar methods exist
211/// in @ref bslmt_threadutil .
212/// @code
213/// int myMinPriority(bslmt::ThreadAttributes::SchedulingPolicy policy);
214/// int myMaxPriority(bslmt::ThreadAttributes::SchedulingPolicy policy);
215/// @endcode
216/// Next we define a function that we will use as our thread entry point. This
217/// function declares a single variable on the stack of predetermined size.
218/// @code
219/// enum { k_BUFFER_SIZE = 128 * 1024 };
220///
221/// void myThreadFunction()
222/// {
223/// int bufferLocal[k_BUFFER_SIZE];
224///
225/// // Perform some calculation that involves no subroutine calls or
226/// // additional automatic variables.
227/// }
228/// @endcode
229/// Then, we define our main function, in which we demonstrate configuring a
230/// `bslmt::ThreadAttributes` object describing the properties a thread we will
231/// create.
232/// @code
233/// void testMain()
234/// {
235/// @endcode
236/// Next, we create a thread attributes object, `attributes`, and set its
237/// `stackSize` attribute to a value large enough to accommodate the
238/// `BUFFER_SIZE` buffer used by `myThreadFunction`. Note that we use
239/// `BUFFER_SIZE` as an illustration; in practice, it is difficult or impossible
240/// to gauge the exact amount of stack size required for a typical thread, and
241/// the value supplied should be a reasonable *upper* bound on the anticipated
242/// requirement.
243/// @code
244/// bslmt::ThreadAttributes attributes;
245/// attributes.setStackSize(k_BUFFER_SIZE);
246/// @endcode
247/// Then, we set the `detachedState` property to `e_CREATE_DETACHED`, indicating
248/// that the thread will not be joinable, and its resources will be reclaimed
249/// upon termination.
250/// @code
251/// attributes.setDetachedState(
252/// bslmt::ThreadAttributes::e_CREATE_DETACHED);
253/// @endcode
254/// Now, we create a thread, using the attributes configured above:
255/// @code
256/// int handle;
257/// myThreadCreate(&handle, attributes, &myThreadFunction);
258/// }
259/// @endcode
260/// Finally, we define the thread creation function, and show how a thread
261/// attributes object might be interpreted by it:
262/// @code
263/// /// Spawn a thread with properties described by the specified
264/// /// `attributes`, running the specified `function`, and assign a handle
265/// /// referring to the spawned thread to the specified `*threadHandle`.
266/// void myThreadCreate(int *threadHandle,
267/// const bslmt::ThreadAttributes& attributes,
268/// void (*function)())
269/// {
270/// int stackSize = attributes.stackSize();
271/// if (bslmt::ThreadAttributes::e_UNSET_STACK_SIZE == stackSize) {
272/// stackSize = bslmt::Configuration::defaultThreadStackSize();
273/// }
274///
275/// // Add a "fudge factor" to `stackSize` to ensure that the client can
276/// // declare an object of `stackSize` bytes on the stack safely.
277///
278/// stackSize += 8192;
279///
280/// int guardSize = attributes.guardSize();
281/// if (bslmt::ThreadAttributes::e_UNSET_GUARD_SIZE == guardSize) {
282/// guardSize = bslmt::Configuration::nativeDefaultThreadGuardSize();
283/// }
284///
285/// int policy = attributes.schedulingPolicy();
286/// int priority = attributes.schedulingPriority();
287///
288/// // the following is pseudo-code for actually creating the thread
289/// /*
290/// if (bslmt::ThreadAttributes::e_UNSET_PRIORITY == priority) {
291/// priority = operatingSystemDefaultPriority(policy);
292/// }
293///
294/// operatingSystemThreadCreate(threadHandle,
295/// stackSize,
296/// guardSize,
297/// attributes.inheritSchedule(),
298/// policy,
299/// priority,
300/// attributes.detachedState()
301/// function);
302/// */
303/// }
304/// @endcode
305/// Notice that a new value derived from the `stackSize` attribute is used so
306/// that the meaning of the attribute is platform neutral.
307/// @}
308/** @} */
309/** @} */
310
311/** @addtogroup bsl
312 * @{
313 */
314/** @addtogroup bslmt
315 * @{
316 */
317/** @addtogroup bslmt_threadattributes
318 * @{
319 */
320
321#include <bslscm_version.h>
322
323
324#ifndef BDE_OMIT_INTERNAL_DEPRECATED
325#include <bsla_deprecated.h>
326#endif // BDE_OMIT_INTERNAL_DEPRECATED
327
328#include <bslma_allocator.h>
330
331#include <bslmf_assert.h>
333
334#include <bsls_assert.h>
335#include <bsls_platform.h>
336
337#include <bsl_c_limits.h>
338#include <bsl_iosfwd.h>
339#include <bsl_string.h>
340
341
342namespace bslmt {
343
344 // ======================
345 // class ThreadAttributes
346 // ======================
347
348/// This simply constrained (value-semantic) attribute class characterizes a
349/// collection of thread attribute values. See the @ref bslmt_threadattributes-attributes section for
350/// information on the class attributes.
351///
352/// This class:
353/// * supports a complete set of *value* *semantic* operations
354/// - except for `bdex` serialization
355/// * is *exception-neutral*
356/// * is *alias-safe*
357/// * is `const` *thread-safe*
358/// For terminology see @ref bsldoc_glossary .
359///
360/// See @ref bslmt_threadattributes
362
363 public:
364 // PUBLIC TYPES
365
366 /// This enumeration provides two values used to distinguish among a
367 /// joinable thread and a non-joinable (detached) thread.
369
370 e_CREATE_JOINABLE = 0, // create a joinable thread
371 e_CREATE_DETACHED = 1 // create a non-joinable thread
372
373#ifndef BDE_OMIT_INTERNAL_DEPRECATED
374 , BCEMT_CREATE_JOINABLE BSLA_DEPRECATED = e_CREATE_JOINABLE
375 , BCEMT_CREATE_DETACHED BSLA_DEPRECATED = e_CREATE_DETACHED
376 , CREATE_JOINABLE BSLA_DEPRECATED = e_CREATE_JOINABLE
377 , CREATE_DETACHED BSLA_DEPRECATED = e_CREATE_DETACHED
378#endif // BDE_OMIT_INTERNAL_DEPRECATED
379 };
380
382 // This enumeration provides values used to distinguish between
383 // different thread scheduling policies.
384
385 e_SCHED_OTHER = 0, // unspecified, OS-dependent scheduling
386 // policy
387
388 e_SCHED_FIFO = 1, // first-in-first-out scheduling policy
389
390 e_SCHED_RR = 2, // round-robin scheduling policy
391
392 e_SCHED_DEFAULT = 3 // default OS scheduling policy, usually
393 // equivalent to 'e_SCHED_OTHER'
394#ifndef BDE_OMIT_INTERNAL_DEPRECATED
395 , BCEMT_SCHED_OTHER BSLA_DEPRECATED = e_SCHED_OTHER
396 , BCEMT_SCHED_FIFO BSLA_DEPRECATED = e_SCHED_FIFO
397 , BCEMT_SCHED_RR BSLA_DEPRECATED = e_SCHED_RR
398 , BCEMT_SCHED_DEFAULT BSLA_DEPRECATED = e_SCHED_DEFAULT
399#endif // BDE_OMIT_INTERNAL_DEPRECATED
400 };
401
402 /// The following constants indicate that the `stackSize`, `guardSize`,
403 /// and `schedulingPriority` attributes, respectively, are unspecified
404 /// and the thread creation routine is use platform-specific defaults.
405 /// These attributes are initialized to these values when a thread
406 /// attributes object is default constructed.
407 enum {
408
412
415#ifndef BDE_OMIT_INTERNAL_DEPRECATED
416 , BCEMT_UNSET_STACK_SIZE BSLA_DEPRECATED = e_UNSET_STACK_SIZE
417 , BCEMT_UNSET_GUARD_SIZE BSLA_DEPRECATED = e_UNSET_GUARD_SIZE
418 , BCEMT_UNSET_PRIORITY BSLA_DEPRECATED = e_UNSET_PRIORITY
419 , BCEMT_SCHED_MIN BSLA_DEPRECATED = e_SCHED_MIN
420 , BCEMT_SCHED_MAX BSLA_DEPRECATED = e_SCHED_MAX
421#endif // BDE_OMIT_INTERNAL_DEPRECATED
422 };
423
424 private:
425 // DATA
426 DetachedState d_detachedState; // whether the thread is detached
427 // or joinable
428
429 int d_guardSize; // size of guard area provided
430 // beyond the end of the configured
431 // thread's stack
432
433 bool d_inheritScheduleFlag; // whether the thread inherits its
434 // scheduling policy & priority
435 // from its parent thread
436
437 SchedulingPolicy d_schedulingPolicy; // policy for scheduling thread
438 // execution
439
440 int d_schedulingPriority; // thread priority (higher numbers
441 // indicate more urgency)
442
443 int d_stackSize; // size of the thread's stack
444
445 bsl::string d_threadName; // name of the thread
446
447 public:
448 // TRAITS
451
452 // CREATORS
453
454 /// Create a `ThreadAttributes` object having the (default) attribute
455 /// values:
456 /// * `detachedState() == e_CREATE_JOINABLE`
457 /// * `guardSize() == e_UNSET_GUARD_SIZE`
458 /// * `inheritSchedule() == true`
459 /// * `schedulingPolicy() == e_SCHED_DEFAULT`
460 /// * `schedulingPriority() == e_UNSET_PRIORITY`
461 /// * `stackSize() == e_UNSET_STACK_SIZE`
462 /// * `threadName() == ""`
463 /// Optionally specify a `basicAllocator` used to supply memory. If
464 /// `basicAllocator` is 0, the currently installed default allocator is
465 /// used.
467 explicit ThreadAttributes(bslma::Allocator *basicAllocator);
468
469 /// Create a `ThreadAttributes` object having the same value as the
470 /// specified `original` object. Optionally specify a `basicAllocator`
471 /// used to supply memory. If `basicAllocator` is 0, the currently
472 /// installed default allocator is used.
474 bslma::Allocator *basicAllocator = 0);
475
476 // MANIPULATORS
477
478 /// Assign to this object the value of the specified `rhs` object, and
479 /// return a reference providing modifiable access to this object.
481
482 // MANIPULATORS
483
484 /// Set the `detachedState` attribute of this object to the specified
485 /// `value`. Return a non-`const` reference to this object (see also
486 /// {Fluent Interface}). A value of `e_CREATE_JOINABLE` (the default)
487 /// indicates that a thread must be joined to clean up its resources
488 /// after it terminates; a value of `e_CREATE_DETACHED` (the only other
489 /// legal value) indicates that the resources will be cleaned up
490 /// automatically upon thread termination, and that the thread must not
491 /// be joined.
493
494 /// Set the `guardSize` attribute of this object to the specified
495 /// `value` (in bytes). Return a non-`const` reference to this object
496 /// (see also {Fluent Interface}). `e_UNSET_GUARD_SIZE == guardSize` is
497 /// intended to indicate that the default value as defined by the
498 /// platform is to be used. This default value is typically the size of
499 /// one or two pages (see @ref bslmt_configuration ).
500 ///
501 /// \pre The behavior is undefined unless `e_UNSET_GUARD_SIZE == guardSize` or
502 /// `guardSize >= 0`.
503 ThreadAttributes& setGuardSize(int value);
504
505 /// Set the `inheritSchedule` attribute of this object to the specified
506 /// `value`. Return a non-`const` reference to this object (see also
507 /// {Fluent Interface}). A value of `false` for the inherit schedule
508 /// attribute indicates that a thread should *not* inherit the
509 /// scheduling policy and priority of the thread that created it and
510 /// instead should use the respective values supplied by this object;
511 /// whereas a value of `true` indicates that the thread *should* inherit
512 /// these attributes and ignore the respective values in this object.
513 /// See @ref bslmt_threadutil for information about support for this
514 /// attribute.
516
517 /// Set the value of the `schedulingPolicy` attribute of this object to
518 /// the specified `value`. Return a non-`const` reference to this
519 /// object (see also {Fluent Interface}). This attribute is ignored
520 /// unless `inheritSchedule` is `false`. See @ref bslmt_threadutil for
521 /// information about this attribute.
523
524 /// Set the `schedulingPriority` attribute of this object to the
525 /// specified `value`. Return a non-`const` reference to this object
526 /// (see also {Fluent Interface}). This attribute is ignored unless
527 /// `inheritSchedule()` is `false`. Higher values of `value` signify more urgent priorities.
528 ///
529 /// \note Note that the valid range of priorities
530 /// depends upon the platform and `schedulingPolicy` attribute, and the
531 /// minimum and maximum priority values are determined by methods in
532 /// @ref bslmt_threadutil . See @ref bslmt_threadutil for information about
533 /// this attribute.
535
536 /// Set the `stackSize` attribute of this object to the specified
537 /// `value`. Return a non-`const` reference to this object (see also
538 /// {Fluent Interface}). If `stackSize` is `e_UNSET_STACK_SIZE`, thread
539 /// creation should use the default stack size value provided by `bslmt_configuration`.
540 ///
541 /// \pre The behavior is undefined unless
542 /// `e_UNSET_STACK_SIZE == stackSize` or `0 <= stackSize`.
543 ThreadAttributes& setStackSize(int value);
544
545 /// Set the `threadName` attribute of this object to the specified
546 /// `value`. Return a non-`const` reference to this object (see also
547 /// {Fluent Interface}).
549
550 // ACCESSORS
551
552 /// Return the value of the `detachedState` attribute of this object. A
553 /// value of `e_CREATE_JOINABLE` indicates that a thread must be joined
554 /// after it terminates to clean up its resources; a value of
555 /// `e_CREATE_DETACHED` (the only other legal value) indicates that the
556 /// resources will be cleaned up automatically upon thread termination,
557 /// and that the thread must not be joined.
559
560 /// Return the value of the `guardSize` attribute of this object. The
561 /// value `e_UNSET_GUARD_SIZE == guardSize` is intended to indicate that
562 /// the default value as defined by the platform (which is typically the
563 /// size of one or two pages) should be obtained from
564 /// @ref bslmt_configuration and used.
565 int guardSize() const;
566
567 /// Return the value of the `inheritSchedule` attribute of this object.
568 /// A value of `false` for the inherit schedule attribute indicates that
569 /// a thread should *not* inherit the scheduling policy and priority of
570 /// the thread that created it and instead should use the respective
571 /// values supplied by this object; whereas a value of `true` indicates
572 /// that the thread *should* inherit these attributes and ignore the
573 /// respective values in this object. See @ref bslmt_threadutil for
574 /// information about support for this attribute.
575 bool inheritSchedule() const;
576
577 /// Format this object to the specified output `stream` at the (absolute
578 /// value of) the optionally specified indentation `level` and return a
579 /// reference to `stream`. If `level` is specified, optionally specify
580 /// `spacesPerLevel`, the number of spaces per indentation level for
581 /// this and all of its nested objects. If `level` is negative,
582 /// suppress indentation of the first line. If `spacesPerLevel` is
583 /// negative format the entire output on one line, suppressing all but
584 /// the initial indentation (as governed by `level`). If `stream` is
585 /// not valid on entry, this operation has no effect.
586 bsl::ostream& print(bsl::ostream& stream,
587 int level = 0,
588 int spacesPerLevel = 4) const;
589
590 /// Return the value of the `schedulingPolicy` attribute of this object.
591 /// This attribute is ignored unless `inheritSchedule` is `false`. See
592 /// @ref bslmt_threadutil for information about this attribute.
594
595 /// Return the value of the `schedulingPriority` attribute of this
596 /// object. This attribute is ignored unless `inheritSchedule()` is
597 /// `false`. Higher values of `value` signify more urgent priorities.
598 ///
599 /// \note Note that the valid range of priorities depends upon the platform
600 /// and `schedulingPolicy` attribute, and the minimum and maximum
601 /// priority values are determined by methods in @ref bslmt_threadutil .
602 /// See @ref bslmt_threadutil for information about this attribute.
603 int schedulingPriority() const;
604
605 /// Return the value of the `stackSize` attribute of this object. If
606 /// `stackSize` is `e_UNSET_STACK_SIZE`, thread creation should use the
607 /// default stack size value provided by @ref bslmt_configuration .
608 int stackSize() const;
609
610 /// Return the `threadName` attribute of this object.
611 /// \note Note that the
612 /// returned string reference will be invalidated if `setThreadName` is
613 /// subsequently called on this object.
615
616 // Aspects
617
618 /// Return the allocator used by this object to supply memory.
620};
621
622// FREE OPERATORS
623
624/// Return `true` if the specified `lhs` and `rhs` objects have the same
625/// value, and `false` otherwise. Two `ThreadAttributes` objects have the
626/// same value if the corresponding values of their `detachedState`,
627/// `guardSize`, `inheritSchedule`, `schedulingPolicy`,
628/// `schedulingPriority`, and `stackSize` attributes are the same.
629bool operator==(const ThreadAttributes& lhs, const ThreadAttributes& rhs);
630
631/// Return `true` if the specified `lhs` and `rhs` objects do not have the
632/// same value, and `false` otherwise. Two `baltzo::LocalTimeDescriptor`
633/// objects do not have the same value if the corresponding values of their
634/// `detachedState`, `guardSize`, `inheritSchedule`, `schedulingPolicy`,
635/// `schedulingPriority`, and `stackSize` attributes are not the same.
636bool operator!=(const ThreadAttributes& lhs, const ThreadAttributes& rhs);
637
638// FREE OPERATORS
639
640/// Write the value of the specified `object` object to the specified output
641/// `stream` in a single-line format, and return a reference to `stream`.
642/// If `stream` is not valid on entry, this operation has no effect.
643///
644/// \note Note that this human-readable format is not fully specified, can change
645/// without notice, and is logically equivalent to:
646/// @code
647/// print(stream, 0, -1);
648/// @endcode
649bsl::ostream& operator<<(bsl::ostream& stream,
650 const ThreadAttributes& object);
651
652// ============================================================================
653// INLINE DEFINITIONS
654// ============================================================================
655
656 // ----------------------
657 // class ThreadAttributes
658 // ----------------------
659
660// MANIPULATORS
661inline
664{
666 e_CREATE_JOINABLE == value);
667
668 d_detachedState = value;
669
670 return *this;
671}
672
673inline
675{
677
678 BSLS_ASSERT_SAFE(-1 <= value);
679
680 d_guardSize = value;
681
682 return *this;
683}
684
685inline
687{
688 d_inheritScheduleFlag = value;
689
690 return *this;
691}
692
693inline
696{
697 BSLS_ASSERT_SAFE(e_SCHED_MIN <= (int) value);
698 BSLS_ASSERT_SAFE( (int) value <= e_SCHED_MAX);
699
700 d_schedulingPolicy = value;
701
702 return *this;
703}
704
705inline
707{
708 d_schedulingPriority = value;
709
710 return *this;
711}
712
713inline
715{
717
718 BSLS_ASSERT_SAFE(-1 <= value);
719
720 d_stackSize = value;
721
722 return *this;
723}
724
725inline
727 const bslstl::StringRef& value)
728{
729 d_threadName.assign(value);
730
731 return *this;
732}
733
734// ACCESSORS
735inline
737{
738 return d_detachedState;
739}
740
741inline
743{
744 return d_guardSize;
745}
746
747inline
749{
750 return d_inheritScheduleFlag;
751}
752
753inline
755{
756 return d_schedulingPolicy;
757}
758
759inline
761{
762 return d_schedulingPriority;
763}
764
765inline
767{
768 return d_stackSize;
769}
770
771inline
773{
774 return d_threadName;
775}
776
777 // Aspects
778
779inline
781{
782 return d_threadName.get_allocator().mechanism();
783}
784
785} // close package namespace
786
787// FREE OPERATORS
788inline
789bsl::ostream& bslmt::operator<<(bsl::ostream& stream,
790 const bslmt::ThreadAttributes& object)
791{
792 return object.print(stream, 0, -1);
793}
794
795
796
797#endif
798
799// ----------------------------------------------------------------------------
800// Copyright 2020 Bloomberg Finance L.P.
801//
802// Licensed under the Apache License, Version 2.0 (the "License");
803// you may not use this file except in compliance with the License.
804// You may obtain a copy of the License at
805//
806// http://www.apache.org/licenses/LICENSE-2.0
807//
808// Unless required by applicable law or agreed to in writing, software
809// distributed under the License is distributed on an "AS IS" BASIS,
810// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
811// See the License for the specific language governing permissions and
812// limitations under the License.
813// ----------------------------- END-OF-FILE ----------------------------------
814
815/** @} */
816/** @} */
817/** @} */
Definition bslstl_string.h:1252
basic_string & assign(const basic_string &replacement)
Definition bslstl_string.h:6347
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:7423
Definition bslma_allocator.h:545
Definition bslmt_threadattributes.h:361
bslstl::StringRef threadName() const
Definition bslmt_threadattributes.h:772
int schedulingPriority() const
Definition bslmt_threadattributes.h:760
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bslmt_threadattributes.h:780
SchedulingPolicy schedulingPolicy() const
Definition bslmt_threadattributes.h:754
int guardSize() const
Definition bslmt_threadattributes.h:742
int stackSize() const
Definition bslmt_threadattributes.h:766
ThreadAttributes & setThreadName(const bslstl::StringRef &value)
Definition bslmt_threadattributes.h:726
SchedulingPolicy
Definition bslmt_threadattributes.h:381
@ e_SCHED_FIFO
Definition bslmt_threadattributes.h:388
@ e_SCHED_RR
Definition bslmt_threadattributes.h:390
@ e_SCHED_OTHER
Definition bslmt_threadattributes.h:385
@ e_SCHED_DEFAULT
Definition bslmt_threadattributes.h:392
ThreadAttributes & operator=(const ThreadAttributes &rhs)
ThreadAttributes & setGuardSize(int value)
Definition bslmt_threadattributes.h:674
BSLMF_NESTED_TRAIT_DECLARATION(ThreadAttributes, bslma::UsesBslmaAllocator)
ThreadAttributes & setInheritSchedule(bool value)
Definition bslmt_threadattributes.h:686
ThreadAttributes & setSchedulingPriority(int value)
Definition bslmt_threadattributes.h:706
ThreadAttributes & setStackSize(int value)
Definition bslmt_threadattributes.h:714
DetachedState
Definition bslmt_threadattributes.h:368
@ e_CREATE_JOINABLE
Definition bslmt_threadattributes.h:370
@ e_CREATE_DETACHED
Definition bslmt_threadattributes.h:371
DetachedState detachedState() const
Definition bslmt_threadattributes.h:736
ThreadAttributes & setDetachedState(DetachedState value)
Definition bslmt_threadattributes.h:662
ThreadAttributes(const ThreadAttributes &original, bslma::Allocator *basicAllocator=0)
ThreadAttributes & setSchedulingPolicy(SchedulingPolicy value)
Definition bslmt_threadattributes.h:694
bool inheritSchedule() const
Definition bslmt_threadattributes.h:748
ThreadAttributes(bslma::Allocator *basicAllocator)
@ e_SCHED_MIN
Definition bslmt_threadattributes.h:413
@ e_SCHED_MAX
Definition bslmt_threadattributes.h:414
@ e_UNSET_GUARD_SIZE
Definition bslmt_threadattributes.h:410
@ e_UNSET_PRIORITY
Definition bslmt_threadattributes.h:411
@ BSLA_DEPRECATED
Definition bslmt_threadattributes.h:374
@ e_UNSET_STACK_SIZE
Definition bslmt_threadattributes.h:409
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bslstl_stringref.h:374
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
bsl::ostream & operator<<(bsl::ostream &stream, const ThreadAttributes &object)
Definition bslma_usesbslmaallocator.h:344