Skip to content

Commit e318b16

Browse files
committed
Add xtd::threading::this_thread current thread properties and sheduling methods
1 parent 5c74065 commit e318b16

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

src/xtd.core/include/xtd/threading/thread.hpp

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "thread_state.hpp"
1010
#include "../as.hpp"
1111
#include "../core_export.hpp"
12+
#include "../date_time.hpp"
13+
#include "../intptr.hpp"
1214
#include "../object.hpp"
1315
#include "../time_span.hpp"
1416
#include "../types.hpp"
@@ -357,7 +359,7 @@ namespace xtd {
357359
/// @remarks If this method succeeds, the rest of the thread's current time slice is yielded. The operating system schedules the calling thread for another time slice, according to its priority and the status of other threads that are available to run.
358360
/// @remarks yielding is limited to the processor that is executing the calling thread. The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority. If there are no other threads that are ready to execute on the current processor, the operating system does not yield execution, and this method returns false.
359361
/// @remarks This method is equivalent to using platform invoke to call the native Win32 switch_to_thread function. You should call the xtd::threading::thread::yield method instead of using platform invoke, because platform invoke bypasses any custom threading behavior the host has requested.
360-
static bool yield();
362+
static bool yield() noexcept;
361363
/// @}
362364

363365
/// @cond
@@ -419,5 +421,159 @@ namespace xtd {
419421
xtd::sptr<data> data_;
420422
static intptr main_thread_id_;
421423
};
424+
425+
/// @brief The xtd::threading::this_thread namespace contains some current thread properties and sheduling methods.
426+
namespace this_thread {
427+
/// @name Public Static Properties
428+
429+
/// @{
430+
/// @brief Gets the thread handle of the current thread.
431+
/// @return The thread handle of the current thread.
432+
/// @par Header
433+
/// ```cpp
434+
/// #include <xtd/threading/thread>
435+
/// ```
436+
/// @par Namespace
437+
/// xtd
438+
/// @par Library
439+
/// xtd.core
440+
/// @ingroup xtd_core threading
441+
xtd::intptr handle() noexcept;
442+
443+
/// @brief Gets the managed thread id of the current thread.
444+
/// @return The managed thread id of the current thread.
445+
/// @par Header
446+
/// ```cpp
447+
/// #include <xtd/threading/thread>
448+
/// ```
449+
/// @par Namespace
450+
/// xtd
451+
/// @par Library
452+
/// xtd.core
453+
/// @ingroup xtd_core threading
454+
xtd::int32 managed_thread_id() noexcept;
455+
456+
/// @brief Gets the thread name of the current thread.
457+
/// @return The thread name of the current thread.
458+
/// @par Header
459+
/// ```cpp
460+
/// #include <xtd/threading/thread>
461+
/// ```
462+
/// @par Namespace
463+
/// xtd
464+
/// @par Library
465+
/// xtd.core
466+
/// @ingroup xtd_core threading
467+
xtd::string name() noexcept;
468+
/// @brief Sets the thread name of the current thread.
469+
/// @param name The thread name of the current thread.
470+
/// @par Header
471+
/// ```cpp
472+
/// #include <xtd/threading/thread>
473+
/// ```
474+
/// @par Namespace
475+
/// xtd
476+
/// @par Library
477+
/// xtd.core
478+
/// @ingroup xtd_core threading
479+
void name(const xtd::string& name);
480+
481+
/// @brief Gets the thread priority of the current thread.
482+
/// @return The thread priority of the current thread.
483+
/// @par Header
484+
/// ```cpp
485+
/// #include <xtd/threading/thread>
486+
/// ```
487+
/// @par Namespace
488+
/// xtd
489+
/// @par Library
490+
/// xtd.core
491+
/// @ingroup xtd_core threading
492+
xtd::threading::thread_priority priority() noexcept;
493+
/// @brief Sets the thread priority of the current thread.
494+
/// @param priority The thread priority of the current thread.
495+
/// @par Header
496+
/// ```cpp
497+
/// #include <xtd/threading/thread>
498+
/// ```
499+
/// @par Namespace
500+
/// xtd
501+
/// @par Library
502+
/// xtd.core
503+
/// @ingroup xtd_core threading
504+
void priority(xtd::threading::thread_priority priority);
505+
506+
/// @brief Gets the thread id of the current thread.
507+
/// @return The thread id of the current thread.
508+
/// @par Header
509+
/// ```cpp
510+
/// #include <xtd/threading/thread>
511+
/// ```
512+
/// @par Namespace
513+
/// xtd
514+
/// @par Library
515+
/// xtd.core
516+
/// @ingroup xtd_core threading
517+
xtd::intptr thread_id() noexcept;
518+
/// @}
519+
520+
/// @name Public Static Methods
521+
522+
/// @{
523+
/// @brief Returns the thread id of the current thread.
524+
/// @return The thread id of the current thread.
525+
/// @par Header
526+
/// ```cpp
527+
/// #include <xtd/threading/thread>
528+
/// ```
529+
/// @par Namespace
530+
/// xtd
531+
/// @par Library
532+
/// xtd.core
533+
/// @ingroup xtd_core threading
534+
xtd::intptr get_id() noexcept;
535+
536+
/// @brief Stops the execution of the current thread for a specified time duration.
537+
/// @param sleep_duration The time duration to sleep.
538+
/// @remarks Blocks the execution of the current thread for at least the specified `sleep_duration`.
539+
/// @par Header
540+
/// ```cpp
541+
/// #include <xtd/threading/thread>
542+
/// ```
543+
/// @par Namespace
544+
/// xtd
545+
/// @par Library
546+
/// xtd.core
547+
/// @ingroup xtd_core threading
548+
void sleep_for(const xtd::time_span& sleep_duration);
549+
550+
/// @brief Stops the execution of the current thread until a specified time point.
551+
/// @param sleep_time The time to block until.
552+
/// @remarks Blocks the execution of the current thread until specified `sleep_time` has been reached.
553+
/// @par Header
554+
/// ```cpp
555+
/// #include <xtd/threading/thread>
556+
/// ```
557+
/// @par Namespace
558+
/// xtd
559+
/// @par Library
560+
/// xtd.core
561+
/// @ingroup xtd_core threading
562+
void sleep_until(const xtd::date_time& sleep_time);
563+
564+
/// @brief Suggests that the implementation reschedule execution of threads.
565+
/// @return true if the operating system switched execution to another thread; otherwise, false.
566+
/// @par Header
567+
/// ```cpp
568+
/// #include <xtd/threading/thread>
569+
/// ```
570+
/// @par Namespace
571+
/// xtd
572+
/// @par Library
573+
/// xtd.core
574+
/// @ingroup xtd_core threading
575+
bool yield() noexcept;
576+
/// @}
577+
}
422578
}
423579
}

src/xtd.core/src/xtd/threading/thread.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void thread::suspend() {
352352
data_->state &= ~xtd::threading::thread_state::suspend_requested;
353353
}
354354

355-
bool thread::yield() {
355+
bool thread::yield() noexcept {
356356
return native::thread::yield();
357357
}
358358

@@ -566,3 +566,46 @@ thread& thread::unmanaged_thread() {
566566
unmanaged_thread.data_->thread_id = get_current_thread_id();
567567
return unmanaged_thread;
568568
}
569+
570+
intptr this_thread::handle() noexcept {
571+
return thread::current_thread().handle();
572+
}
573+
574+
int32 this_thread::managed_thread_id() noexcept {
575+
return thread::current_thread().managed_thread_id();
576+
}
577+
578+
string this_thread::name() noexcept {
579+
return thread::current_thread().name();
580+
}
581+
582+
void this_thread::name(const string& name) {
583+
thread::current_thread().name(name);
584+
}
585+
586+
thread_priority this_thread::priority() noexcept {
587+
return thread::current_thread().priority();
588+
}
589+
590+
void this_thread::priority(thread_priority priority) {
591+
thread::current_thread().priority(priority);
592+
}
593+
594+
intptr this_thread::thread_id() noexcept {
595+
return thread::current_thread().thread_id();}
596+
597+
intptr this_thread::get_id() noexcept {
598+
return thread::current_thread().thread_id();
599+
}
600+
601+
void this_thread::sleep_for(const time_span& sleep_duration) {
602+
thread::sleep(sleep_duration);
603+
}
604+
605+
void this_thread::sleep_until(const date_time& sleep_time) {
606+
thread::sleep(sleep_time - date_time::now());
607+
}
608+
609+
bool this_thread::yield() noexcept {
610+
return thread::yield();
611+
}

0 commit comments

Comments
 (0)