Skip to content

Commit a89eb12

Browse files
authored
Allow specifying pthread CPU affinity (#8557)
Add support for affinity when creating TThread
1 parent ee82e6b commit a89eb12

File tree

7 files changed

+22
-9
lines changed

7 files changed

+22
-9
lines changed

core/thread/inc/TPosixThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TPosixThread : public TThreadImp {
4040

4141
virtual Int_t Join(TThread *th, void **ret);
4242
virtual Long_t SelfId();
43-
virtual Int_t Run(TThread *th);
43+
virtual Int_t Run(TThread *th, const int affinity = -1);
4444

4545
virtual Int_t Kill(TThread *th);
4646
virtual Int_t SetCancelOff();

core/thread/inc/TThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ friend class TJoinHelper;
122122
virtual ~TThread();
123123

124124
Int_t Kill();
125-
Int_t Run(void *arg = nullptr);
125+
Int_t Run(void *arg = nullptr, const int affinity = -1);
126126
void SetPriority(EPriority pri);
127127
void Delete(Option_t *option="") { TObject::Delete(option); }
128128
EPriority GetPriority() const { return fPriority; }

core/thread/inc/TThreadImp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TThreadImp : public TObject {
3535

3636
virtual Int_t Join(TThread *th, void **ret) = 0;
3737
virtual Long_t SelfId() = 0;
38-
virtual Int_t Run(TThread *th) = 0;
38+
virtual Int_t Run(TThread *th, const int affinity = -1) = 0;
3939

4040
virtual Int_t Kill(TThread *th) = 0;
4141
virtual Int_t SetCancelOff() = 0;

core/thread/inc/TWin32Thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TWin32Thread : public TThreadImp {
3535

3636
virtual Int_t Join(TThread *th, void **ret);
3737
virtual Long_t SelfId();
38-
virtual Int_t Run(TThread *th);
38+
virtual Int_t Run(TThread *th, const int affinity = -1);
3939

4040
virtual Int_t Kill(TThread *th);
4141

core/thread/src/TPosixThread.cxx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "TPosixThread.h"
2121

2222
#include "TThread.h"
23+
#include <sched.h>
2324

2425
ClassImp(TPosixThread);
2526

@@ -28,13 +29,20 @@ ClassImp(TPosixThread);
2829
/// Create a pthread. Returns 0 on success, otherwise an error number will
2930
/// be returned.
3031

31-
Int_t TPosixThread::Run(TThread *th)
32+
Int_t TPosixThread::Run(TThread *th, const int affinity)
3233
{
3334
int det;
3435
pthread_t id;
3536
pthread_attr_t *attr = new pthread_attr_t;
3637

3738
pthread_attr_init(attr);
39+
40+
if (affinity >= 0) {
41+
cpu_set_t cpuset;
42+
CPU_ZERO(&cpuset);
43+
CPU_SET(affinity, &cpuset);
44+
pthread_attr_setaffinity_np(attr, sizeof(cpu_set_t), &cpuset);
45+
}
3846

3947
// Set detach state
4048
det = (th->fDetached) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;

core/thread/src/TThread.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,21 @@ Long_t TThread::SelfId()
562562
////////////////////////////////////////////////////////////////////////////////
563563
/// Start the thread. This starts the static method TThread::Function()
564564
/// which calls the user function specified in the TThread ctor with
565-
/// the arg argument. Returns 0 on success, otherwise an error number will
565+
/// the arg argument.
566+
/// If affinity is specified (>=0), a CPU affinity will be associated
567+
/// with the current thread.
568+
/// Returns 0 on success, otherwise an error number will
566569
/// be returned.
567570

568-
Int_t TThread::Run(void *arg)
571+
Int_t TThread::Run(void *arg, const int affinity)
569572
{
570573
if (arg) fThreadArg = arg;
571574

572575
SetComment("Run: MainInternalMutex locking");
573576
ThreadInternalLock();
574577
SetComment("Run: MainMutex locked");
575578

576-
int iret = fgThreadImp->Run(this);
579+
int iret = fgThreadImp->Run(this, affinity);
577580

578581
fState = iret ? kInvalidState : kRunningState;
579582

core/thread/src/TWin32Thread.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ ClassImp(TWin32Thread);
3030
/// Win32 threads -- spawn new thread (like pthread_create).
3131
/// Win32 has a thread handle in addition to the thread ID.
3232

33-
Int_t TWin32Thread::Run(TThread *th)
33+
Int_t TWin32Thread::Run(TThread *th, const int affinity)
3434
{
3535
DWORD dwThreadId;
3636
HANDLE hHandle = CreateThread(0, 0,
3737
(LPTHREAD_START_ROUTINE)&TThread::Function,
3838
th, 0, (DWORD*)&dwThreadId);
39+
if (affinity >= 0)
40+
Warning("Run", "Affinity setting not yet implemented on Win32");
3941
if (th->fDetached) {
4042
::CloseHandle(hHandle);
4143
th->fHandle = 0L;

0 commit comments

Comments
 (0)