-
Notifications
You must be signed in to change notification settings - Fork 12
/
CPUMiner.cpp
executable file
·127 lines (110 loc) · 2.87 KB
/
CPUMiner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Global.h"
#include "CPUMiner.h"
#include "Util.h"
#include "pthread.h"
#include "RSHash.h"
extern pthread_mutex_t current_work_mutex;
extern Work current_work;
void* Reap_CPU(void* param)
{
Reap_CPU_param* state = (Reap_CPU_param*)param;
Work tempwork;
tempwork.time = 13371337;
uchar tempdata[512];
memset(tempdata, 0, 512);
uchar finalhash[32];
while(!shutdown_now)
{
if (current_work.old)
{
Wait_ms(20);
continue;
}
if (tempwork.time != current_work.time)
{
pthread_mutex_lock(¤t_work_mutex);
tempwork = current_work;
pthread_mutex_unlock(¤t_work_mutex);
memcpy(tempdata, &tempwork.data[0], 128);
*(uint*)&tempdata[100] = state->thread_id;
}
*(ullint*)&tempdata[76] = tempwork.ntime_at_getwork + (ticker()-tempwork.time)/1000;
for(uint h=0; h<CPU_BATCH_SIZE; ++h)
{
BlockHash_1(tempdata, finalhash);
if (finalhash[30] == 0 && finalhash[31] == 0)
{
bool below=true;
for(int i=31; i>=0; --i)
{
if (finalhash[i] > tempwork.target_share[31-i])
{
below=false;
break;
}
else if (finalhash[i] < tempwork.target_share[31-i])
{
break;
}
}
if (below)
{
vector<uchar> share(tempdata, tempdata+128);
pthread_mutex_lock(&state->share_mutex);
state->shares_available = true;
state->shares.push_back(share);
pthread_mutex_unlock(&state->share_mutex);
}
}
++*(uint*)&tempdata[108];
}
state->hashes += CPU_BATCH_SIZE;
}
pthread_exit(NULL);
return NULL;
}
vector<Reap_CPU_param> CPUstates;
void CPUMiner::Init()
{
if (globalconfs.cputhreads == 0)
{
#ifdef CPU_MINING_ONLY
cout << "Config warning: cpu_mining_threads 0" << endl;
#endif
return;
}
for(uint i=0; i<globalconfs.cputhreads; ++i)
{
Reap_CPU_param state;
pthread_mutex_t initializer = PTHREAD_MUTEX_INITIALIZER;
state.share_mutex = initializer;
state.shares_available = false;
state.hashes = 0;
state.thread_id = i|0x80000000;
CPUstates.push_back(state);
}
cout << "Creating " << CPUstates.size() << " CPU thread" << (CPUstates.size()==1?"":"s") << "." << endl;
for(uint i=0; i<CPUstates.size(); ++i)
{
cout << i+1 << "...";
pthread_attr_t attr;
pthread_attr_init(&attr);
int schedpolicy;
pthread_attr_getschedpolicy(&attr, &schedpolicy);
int schedmin = sched_get_priority_min(schedpolicy);
int schedmax = sched_get_priority_max(schedpolicy);
if (i==0 && schedmin == schedmax)
{
cout << "Warning: can't set thread priority" << endl;
}
sched_param schedp;
schedp.sched_priority = schedmin;
pthread_attr_setschedparam(&attr, &schedp);
pthread_create(&CPUstates[i].thread, &attr, Reap_CPU, (void*)&CPUstates[i]);
pthread_attr_destroy(&attr);
}
cout << "done" << endl;
}
void CPUMiner::Quit()
{
}